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