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 | 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 | |
---|
37 | class image_descriptor |
---|
38 | { |
---|
39 | private: |
---|
40 | int m_l, m_h; |
---|
41 | int m_clipx1, m_clipy1, m_clipx2, m_clipy2; |
---|
42 | |
---|
43 | public: |
---|
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 | |
---|
81 | class image : public linked_node |
---|
82 | { |
---|
83 | private: |
---|
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 | |
---|
91 | public: |
---|
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 | image *copy(); // makes a copy of an image |
---|
109 | void clear(int16_t color = -1); // -1 is background color |
---|
110 | |
---|
111 | vec2i Size() const { return m_size; } |
---|
112 | |
---|
113 | void scroll(int16_t x1, int16_t y1, int16_t x2, int16_t y2, |
---|
114 | int16_t xd, int16_t yd); |
---|
115 | void fill_image(image *screen, int16_t x1, int16_t y1, |
---|
116 | int16_t x2, int16_t y2, int16_t align = 1); |
---|
117 | void PutImage(image *screen, vec2i pos, int transparent = 0); |
---|
118 | void PutPart(image *screen, int16_t x, int16_t y, int16_t x1, int16_t y1, |
---|
119 | int16_t x2, int16_t y2, char transparent = 0); |
---|
120 | void PutPartXrev(image *screen, int16_t x, int16_t y, |
---|
121 | int16_t x1, int16_t y1, int16_t x2, int16_t y2, |
---|
122 | char transparent = 0); |
---|
123 | void PutPartMasked(image *screen, image *mask, int16_t x, int16_t y, |
---|
124 | int16_t maskx, int16_t masky, int16_t x1, int16_t y1, |
---|
125 | int16_t x2, int16_t y2); |
---|
126 | image *copy_part_dithered(int16_t x1, int16_t y1, int16_t x2, int16_t y2); |
---|
127 | void bar(int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint8_t color); |
---|
128 | void xor_bar(int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint8_t color); |
---|
129 | void widget_bar(int16_t x1, int16_t y1, int16_t x2, int16_t y2, |
---|
130 | uint8_t light, uint8_t med, uint8_t dark); |
---|
131 | void line(int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint8_t color); |
---|
132 | void rectangle(int16_t x1, int16_t y1, int16_t x2, int16_t y2, |
---|
133 | uint8_t color); |
---|
134 | void burn_led(int16_t x, int16_t y, int32_t num, int16_t color, |
---|
135 | int16_t scale = 1); |
---|
136 | void SetClip(int x1, int y1, int x2, int y2); |
---|
137 | void GetClip(int &x1, int &y1, int &x2, int &y2); |
---|
138 | void InClip(int x1, int y1, int x2, int y2); |
---|
139 | |
---|
140 | void dirt_off() |
---|
141 | { |
---|
142 | if(m_special && m_special->keep_dirt) m_special->keep_dirt = 0; |
---|
143 | } |
---|
144 | void dirt_on() |
---|
145 | { |
---|
146 | if(m_special) m_special->keep_dirt = 1; |
---|
147 | } |
---|
148 | |
---|
149 | void AddDirty(int x1, int y1, int x2, int y2) |
---|
150 | { |
---|
151 | if (m_special) m_special->AddDirty(x1, y1, x2, y2); |
---|
152 | } |
---|
153 | void delete_dirty(int x1, int y1, int x2, int y2) |
---|
154 | { |
---|
155 | if(m_special) m_special->delete_dirty(x1, y1, x2, y2); |
---|
156 | } |
---|
157 | void ClearDirties() |
---|
158 | { |
---|
159 | if (m_special) m_special->ClearDirties(); |
---|
160 | } |
---|
161 | void dither(palette *pal); // use a b&w palette! |
---|
162 | void Scale(vec2i size); |
---|
163 | void SetSize(vec2i size, uint8_t *page = NULL); |
---|
164 | void flood_fill(int16_t x, int16_t y, uint8_t color); |
---|
165 | image *create_smooth(int16_t smoothness = 1); // 0 no smoothness |
---|
166 | void unpack_scanline(int16_t line, char bitsperpixel = 1); |
---|
167 | void FlipX(); |
---|
168 | void FlipY(); |
---|
169 | }; |
---|
170 | |
---|
171 | class image_controller |
---|
172 | { |
---|
173 | public: |
---|
174 | image_controller() |
---|
175 | { |
---|
176 | image_init(); |
---|
177 | } |
---|
178 | ~image_controller() |
---|
179 | { |
---|
180 | image_uninit(); |
---|
181 | } |
---|
182 | }; |
---|
183 | |
---|
184 | #endif /* _IMAGE_HPP_ */ |
---|
185 | |
---|