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
Line 
1/*
2 *  Abuse - dark 2D side-scrolling platform game
3 *  Copyright (c) 1995 Crack dot Com
4 *  Copyright (c) 2005-2011 Sam Hocevar <sam@hocevar.net>
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
11#ifndef _IMAGE_HPP_
12#define _IMAGE_HPP_
13
14#include <stdlib.h>
15#include "linked.h"
16#include "palette.h"
17#include "specs.h"
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 :
27  int16_t dx1,dy1,dx2,dy2;
28  dirty_rect(int16_t x1, int16_t y1, int16_t x2, int16_t y2)
29  { dx1=x1; dy1=y1; dx2=x2; dy2=y2;
30    if(x2<x1 || y2<y1)
31      printf("add incorrect dirty\n");
32  }
33  virtual int16_t compare(void *n1, int16_t field)
34  { return((dirty_rect *)n1)->dy1>dy1; }
35} ;
36
37class image_descriptor
38{
39private:
40    int m_l, m_h;
41    int m_clipx1, m_clipy1, m_clipx2, m_clipy2;
42
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
50    image_descriptor(vec2i size, int keep_dirties = 1, int static_memory = 0);
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)
61    {
62        x1 = m_clipx1; y1 = m_clipy1; x2 = m_clipx2; y2 = m_clipy2;
63    }
64    void SetClip(int x1, int y1, int x2, int y2)
65    {
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);
70    }
71    void ReduceDirties();
72    void AddDirty(int x1, int y1, int x2, int y2);
73    void delete_dirty(int x1, int y1, int x2, int y2);
74    void Resize(vec2i size)
75    {
76        m_l = size.x; m_h = size.y;
77        m_clipx1 = 0; m_clipy1 = 0; m_clipx2 = m_l; m_clipy2 = m_h;
78    }
79};
80
81class image : public linked_node
82{
83private:
84    uint8_t *m_data;
85    vec2i m_size;
86    bool m_locked;
87
88    void MakePage(vec2i size, uint8_t *page_buffer);
89    void DeletePage();
90
91public:
92    image_descriptor *m_special;
93
94    image(bFILE *fp, spec_entry *e = NULL);
95    image(vec2i size, uint8_t *page_buffer = NULL, int create_descriptor = 0);
96    ~image();
97
98    void Lock();
99    void Unlock();
100
101    uint8_t Pixel(vec2i pos);
102    void PutPixel(vec2i pos, uint8_t color);
103
104    inline uint8_t *scan_line(int16_t y)
105    {
106        return m_data + y * m_size.x;
107    }
108    inline uint8_t *next_line(int16_t lasty, uint8_t *last_scan)
109    {
110        return last_scan + m_size.x;
111    }
112    image *copy(); // makes a copy of an image
113    void clear(int16_t color = -1); // -1 is background color
114
115    vec2i Size() const { return m_size; }
116
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);
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);
143
144    void dirt_off()
145    {
146        if(m_special && m_special->keep_dirt) m_special->keep_dirt = 0;
147    }
148    void dirt_on()
149    {
150        if(m_special) m_special->keep_dirt = 1;
151    }
152
153    void AddDirty(int x1, int y1, int x2, int y2)
154    {
155        if (m_special) m_special->AddDirty(x1, y1, x2, y2);
156    }
157    void delete_dirty(int x1, int y1, int x2, int y2)
158    {
159        if(m_special) m_special->delete_dirty(x1, y1, x2, y2);
160    }
161    void ClearDirties()
162    {
163        if (m_special) m_special->ClearDirties();
164    }
165    void dither(palette *pal); // use a b&w palette!
166    void Scale(vec2i size);
167    void SetSize(vec2i size, uint8_t *page = NULL);
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);
171    void FlipX();
172    void FlipY();
173};
174
175class image_controller
176{
177public:
178    image_controller()
179    {
180        image_init();
181    }
182    ~image_controller()
183    {
184        image_uninit();
185    }
186};
187
188#endif /* _IMAGE_HPP_ */
189
Note: See TracBrowser for help on using the repository browser.