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

Last change on this file since 665 was 665, checked in by Sam Hocevar, 11 years ago

imlib: refactor GetClip?, SetClip? etc. to use vec2i.

  • Property svn:keywords set to Id
File size: 5.3 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, by
8 *  Jonathan Clark, or by Sam Hocevar.
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    {
30        dx1 = x1; dy1 = y1; dx2 = x2; dy2 = y2;
31        if (x2 < x1 || y2 < y1)
32            printf("add incorrect dirty\n");
33    }
34    virtual int compare(void *n1) { return ((dirty_rect *)n1)->dy1 > dy1; }
35};
36
37class image_descriptor
38{
39private:
40    vec2i m_size, m_aa, m_bb;
41
42public:
43    uint8_t keep_dirt,
44            static_mem; // if set, don't free memory on exit
45
46    linked_list dirties;
47    void *extended_descriptor;
48
49    image_descriptor(vec2i size, int keep_dirties = 1, int static_memory = 0);
50    int bound_x1(int x1) { return Max(x1, m_aa.x); }
51    int bound_y1(int y1) { return Max(y1, m_aa.y); }
52    int bound_x2(int x2) { return Min(x2, m_bb.x); }
53    int bound_y2(int y2) { return Min(y2, m_bb.y); }
54    inline int x1_clip() { return m_aa.x; }
55    inline int y1_clip() { return m_aa.y; }
56    inline int x2_clip() { return m_bb.x; }
57    inline int y2_clip() { return m_bb.y; }
58    void ClearDirties();
59    void GetClip(vec2i &aa, vec2i &bb)
60    {
61        aa = m_aa; bb = m_bb;
62    }
63    void SetClip(vec2i aa, vec2i bb)
64    {
65        m_aa = Max(aa, vec2i(0));
66        m_bb = Min(Max(bb, m_aa + vec2i(1)), m_size);
67    }
68    void GetClip(int &x1, int &y1, int &x2, int &y2)
69    {
70        x1 = m_aa.x; y1 = m_aa.y; x2 = m_bb.x; y2 = m_bb.y;
71    }
72    void SetClip(int x1, int y1, int x2, int y2)
73    {
74        if(x2 < x1 + 1) x2 = x1 + 1;
75        if(y2 < y1 + 1) y2 = y1 + 1;
76        m_aa.x = Max(x1, 0); m_aa.y = Max(y1, 0);
77        m_bb.x = Min(x2, m_size.x); m_bb.y = Min(y2, m_size.y);
78    }
79    void ReduceDirties();
80    void AddDirty(int x1, int y1, int x2, int y2);
81    void delete_dirty(int x1, int y1, int x2, int y2);
82    void Resize(vec2i size)
83    {
84        m_size = size;
85        m_aa = vec2i(0);
86        m_bb = size;
87    }
88};
89
90class image : public linked_node
91{
92private:
93    uint8_t *m_data;
94    vec2i m_size;
95    bool m_locked;
96
97    void MakePage(vec2i size, uint8_t *page_buffer);
98    void DeletePage();
99
100public:
101    image_descriptor *m_special;
102
103    image(bFILE *fp, spec_entry *e = NULL);
104    image(vec2i size, uint8_t *page_buffer = NULL, int create_descriptor = 0);
105    ~image();
106
107    void Lock();
108    void Unlock();
109
110    uint8_t Pixel(vec2i pos);
111    void PutPixel(vec2i pos, uint8_t color);
112
113    inline uint8_t *scan_line(int16_t y)
114    {
115        return m_data + y * m_size.x;
116    }
117    image *copy(); // makes a copy of an image
118    void clear(int16_t color = -1); // -1 is background color
119
120    vec2i Size() const { return m_size; }
121
122    void scroll(int16_t x1, int16_t y1, int16_t x2, int16_t y2,
123                int16_t xd, int16_t yd);
124    void PutImage(image *screen, vec2i pos, int transparent = 0);
125    void PutPart(image *screen, vec2i pos, vec2i aa, vec2i bb,
126                 int transparent = 0);
127    void PutPartMasked(image *screen, vec2i pos, image *mask, vec2i mpos,
128                       vec2i aa, vec2i bb);
129    image *copy_part_dithered(int16_t x1, int16_t y1, int16_t x2, int16_t y2);
130    void Bar(vec2i p1, vec2i p2, uint8_t color);
131    void xor_bar(int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint8_t color);
132    void WidgetBar(vec2i p1, vec2i p2,
133                   uint8_t light, uint8_t med, uint8_t dark);
134    void Line(vec2i p1, vec2i p2, uint8_t color);
135    void Rectangle(vec2i p1, vec2i p2, uint8_t color);
136    void burn_led(int16_t x, int16_t y, int32_t num, int16_t color,
137                  int16_t scale = 1);
138    void SetClip(vec2i aa, vec2i bb);
139    void GetClip(vec2i &aa, vec2i &bb);
140    void InClip(vec2i aa, vec2i bb);
141    void SetClip(int x1, int y1, int x2, int y2);
142    void GetClip(int &x1, int &y1, int &x2, int &y2);
143    void InClip(int x1, int y1, int x2, int y2);
144
145    void dirt_off()
146    {
147        if(m_special && m_special->keep_dirt) m_special->keep_dirt = 0;
148    }
149    void dirt_on()
150    {
151        if(m_special) m_special->keep_dirt = 1;
152    }
153
154    void AddDirty(int x1, int y1, int x2, int y2)
155    {
156        if (m_special) m_special->AddDirty(x1, y1, x2, y2);
157    }
158    void delete_dirty(int x1, int y1, int x2, int y2)
159    {
160        if(m_special) m_special->delete_dirty(x1, y1, x2, y2);
161    }
162    void ClearDirties()
163    {
164        if (m_special) m_special->ClearDirties();
165    }
166    void dither(palette *pal); // use a b&w palette!
167    void Scale(vec2i size);
168    void SetSize(vec2i size, uint8_t *page = NULL);
169    void flood_fill(int16_t x, int16_t y, uint8_t color);
170    image *create_smooth(int16_t smoothness = 1); // 0 no smoothness
171    void unpack_scanline(int16_t line, char bitsperpixel = 1);
172    void FlipX();
173    void FlipY();
174};
175
176class image_controller
177{
178public:
179    image_controller()
180    {
181        image_init();
182    }
183    ~image_controller()
184    {
185        image_uninit();
186    }
187};
188
189#endif /* _IMAGE_HPP_ */
190
Note: See TracBrowser for help on using the repository browser.