source: abuse/trunk/src/imlib/image.h @ 549

Last change on this file since 549 was 526, checked in by Sam Hocevar, 12 years ago

imlib: get rid of temporary memory allocations in image::FlipX()
and image::FlipY().

  • Property svn:keywords set to Id
File size: 5.7 KB
RevLine 
[56]1/*
2 *  Abuse - dark 2D side-scrolling platform game
3 *  Copyright (c) 1995 Crack dot Com
[494]4 *  Copyright (c) 2005-2011 Sam Hocevar <sam@hocevar.net>
[56]5 *
6 *  This software was released into the Public Domain. As with most public
7 *  domain software, no warranty is made or implied by Crack dot Com or
8 *  Jonathan Clark.
9 */
10
[115]11#ifndef _IMAGE_HPP_
12#define _IMAGE_HPP_
13
[2]14#include <stdlib.h>
[481]15#include "linked.h"
16#include "palette.h"
17#include "specs.h"
[2]18#define MAX_DIRTY 200
19
20void image_init();
21void image_uninit();
22extern linked_list image_list;
23
24class dirty_rect : public linked_node
25{
26public :
[17]27  int16_t dx1,dy1,dx2,dy2;
28  dirty_rect(int16_t x1, int16_t y1, int16_t x2, int16_t y2)
[115]29  { dx1=x1; dy1=y1; dx2=x2; dy2=y2;
30    if(x2<x1 || y2<y1)
[515]31      printf("add incorrect dirty\n");
[2]32  }
[17]33  virtual int16_t compare(void *n1, int16_t field)
[115]34  { return((dirty_rect *)n1)->dy1>dy1; }
[2]35} ;
36
37class image_descriptor
38{
[115]39private:
[518]40    int m_l, m_h;
41    int m_clipx1, m_clipy1, m_clipx2, m_clipy2;
[2]42
[115]43public:
44    uint8_t keep_dirt,
45            static_mem; // if set, don't free memory on exit
46
47    linked_list dirties;
48    void *extended_descriptor;
49
[513]50    image_descriptor(vec2i size, int keep_dirties = 1, int static_memory = 0);
[518]51    int bound_x1(int x1) { return x1 < m_clipx1 ? m_clipx1 : x1; }
52    int bound_y1(int y1) { return y1 < m_clipy1 ? m_clipy1 : y1; }
53    int bound_x2(int x2) { return x2 > m_clipx2 ? m_clipx2 : x2; }
54    int bound_y2(int y2) { return y2 > m_clipy2 ? m_clipy2 : y2; }
55    inline int x1_clip() { return m_clipx1; }
56    inline int y1_clip() { return m_clipy1; }
57    inline int x2_clip() { return m_clipx2; }
58    inline int y2_clip() { return m_clipy2; }
59    void ClearDirties();
60    void GetClip(int &x1, int &y1, int &x2, int &y2)
[115]61    {
[518]62        x1 = m_clipx1; y1 = m_clipy1; x2 = m_clipx2; y2 = m_clipy2;
[2]63    }
[518]64    void SetClip(int x1, int y1, int x2, int y2)
[115]65    {
[518]66        if(x2 < x1 + 1) x2 = x1 + 1;
67        if(y2 < y1 + 1) y2 = y1 + 1;
68        m_clipx1 = Max(x1, 0); m_clipy1 = Max(y1, 0);
69        m_clipx2 = Min(x2, m_l); m_clipy2 = Min(y2, m_h);
[115]70    }
[518]71    void ReduceDirties();
72    void AddDirty(int x1, int y1, int x2, int y2);
[115]73    void delete_dirty(int x1, int y1, int x2, int y2);
[518]74    void Resize(vec2i size)
[115]75    {
[518]76        m_l = size.x; m_h = size.y;
77        m_clipx1 = 0; m_clipy1 = 0; m_clipx2 = m_l; m_clipy2 = m_h;
[115]78    }
79};
[2]80
81class image : public linked_node
82{
[115]83private:
[513]84    uint8_t *m_data;
85    vec2i m_size;
86    bool m_locked;
[2]87
[515]88    void MakePage(vec2i size, uint8_t *page_buffer);
89    void DeletePage();
90
[115]91public:
[515]92    image_descriptor *m_special;
[2]93
[523]94    image(bFILE *fp, spec_entry *e = NULL);
[513]95    image(vec2i size, uint8_t *page_buffer = NULL, int create_descriptor = 0);
[115]96    ~image();
[2]97
[515]98    void Lock();
99    void Unlock();
[2]100
[515]101    uint8_t Pixel(vec2i pos);
102    void PutPixel(vec2i pos, uint8_t color);
103
104    inline uint8_t *scan_line(int16_t y)
[115]105    {
[513]106        return m_data + y * m_size.x;
[115]107    }
[515]108    inline uint8_t *next_line(int16_t lasty, uint8_t *last_scan)
[115]109    {
[513]110        return last_scan + m_size.x;
[115]111    }
112    image *copy(); // makes a copy of an image
113    void clear(int16_t color = -1); // -1 is background color
[512]114
[513]115    vec2i Size() const { return m_size; }
[512]116
[115]117    void scroll(int16_t x1, int16_t y1, int16_t x2, int16_t y2,
118                int16_t xd, int16_t yd);
119    void fill_image(image *screen, int16_t x1, int16_t y1,
120                    int16_t x2, int16_t y2, int16_t align = 1);
121    void put_image(image *screen, int16_t x, int16_t y, char transparent = 0);
122    void put_part(image *screen, int16_t x, int16_t y, int16_t x1, int16_t y1,
123                  int16_t x2, int16_t y2, char transparent = 0);
124    void put_part_xrev(image *screen, int16_t x, int16_t y,
125                       int16_t x1, int16_t y1, int16_t x2, int16_t y2,
126                       char transparent = 0);
127    void put_part_masked(image *screen, image *mask, int16_t x, int16_t y,
128                         int16_t maskx, int16_t masky, int16_t x1, int16_t y1,
129                         int16_t x2, int16_t y2);
130    image *copy_part_dithered(int16_t x1, int16_t y1, int16_t x2, int16_t y2);
131    void bar(int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint8_t color);
132    void xor_bar(int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint8_t color);
133    void widget_bar(int16_t x1, int16_t y1, int16_t x2, int16_t y2,
134                    uint8_t light, uint8_t med, uint8_t dark);
135    void line(int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint8_t color);
136    void rectangle(int16_t x1, int16_t y1, int16_t x2, int16_t y2,
137                   uint8_t color);
138    void burn_led(int16_t x, int16_t y, int32_t num, int16_t color,
139                  int16_t scale = 1);
[518]140    void SetClip(int x1, int y1, int x2, int y2);
141    void GetClip(int &x1, int &y1, int &x2, int &y2);
142    void InClip(int x1, int y1, int x2, int y2);
[2]143
[115]144    void dirt_off()
145    {
[515]146        if(m_special && m_special->keep_dirt) m_special->keep_dirt = 0;
[115]147    }
148    void dirt_on()
149    {
[515]150        if(m_special) m_special->keep_dirt = 1;
[115]151    }
[2]152
[518]153    void AddDirty(int x1, int y1, int x2, int y2)
[115]154    {
[518]155        if (m_special) m_special->AddDirty(x1, y1, x2, y2);
[115]156    }
157    void delete_dirty(int x1, int y1, int x2, int y2)
158    {
[515]159        if(m_special) m_special->delete_dirty(x1, y1, x2, y2);
[115]160    }
[518]161    void ClearDirties()
[115]162    {
[518]163        if (m_special) m_special->ClearDirties();
[115]164    }
165    void dither(palette *pal); // use a b&w palette!
[513]166    void Scale(vec2i size);
167    void SetSize(vec2i size, uint8_t *page = NULL);
[115]168    void flood_fill(int16_t x, int16_t y, uint8_t color);
169    image *create_smooth(int16_t smoothness = 1); // 0 no smoothness
170    void unpack_scanline(int16_t line, char bitsperpixel = 1);
[526]171    void FlipX();
172    void FlipY();
[115]173};
[2]174
[115]175class image_controller
176{
177public:
178    image_controller()
179    {
180        image_init();
181    }
182    ~image_controller()
183    {
184        image_uninit();
185    }
186};
[2]187
[115]188#endif /* _IMAGE_HPP_ */
[2]189
Note: See TracBrowser for help on using the repository browser.