22 |
|
|
23 |
#include "pxfwd.hh" |
#include "pxfwd.hh" |
24 |
#include "geom.hh" |
#include "geom.hh" |
25 |
|
#include "tools.hh" |
26 |
#include "SDL.h" |
#include "SDL.h" |
27 |
|
|
28 |
//---------------------------------------- |
//---------------------------------------- |
30 |
//---------------------------------------- |
//---------------------------------------- |
31 |
namespace px |
namespace px |
32 |
{ |
{ |
33 |
struct RGBA_Mask { |
struct RGBA_Mask |
34 |
|
{ |
35 |
RGBA_Mask(Uint32 rr=0, Uint32 gg=0, Uint32 bb=0, Uint32 aa=0) |
RGBA_Mask(Uint32 rr=0, Uint32 gg=0, Uint32 bb=0, Uint32 aa=0) |
36 |
: r(rr), g(gg), b(bb), a(aa) |
: r(rr), g(gg), b(bb), a(aa) |
37 |
{} |
{} |
38 |
Uint32 r,g,b,a; |
Uint32 r,g,b,a; |
39 |
}; |
}; |
40 |
struct RGB { |
|
41 |
|
struct RGB |
42 |
|
{ |
43 |
RGB(char rr=0, char gg=0, char bb=0) |
RGB(char rr=0, char gg=0, char bb=0) |
44 |
: r(rr), g(gg), b(bb) |
: r(rr), g(gg), b(bb) |
45 |
{} |
{} |
46 |
char r,g,b; |
char r,g,b; |
47 |
}; |
}; |
48 |
struct RGBA { |
|
49 |
|
struct RGBA |
50 |
|
{ |
51 |
RGBA(char rr=0, char gg=0, char bb=0, char aa=0) |
RGBA(char rr=0, char gg=0, char bb=0, char aa=0) |
52 |
: r(rr), g(gg), b(bb), a(aa) |
: r(rr), g(gg), b(bb), a(aa) |
53 |
{} |
{} |
54 |
char r,g,b,a; |
char r,g,b,a; |
55 |
}; |
}; |
56 |
|
|
57 |
|
typedef Uint32 PackedColor; |
58 |
|
} |
59 |
|
|
60 |
|
|
61 |
|
namespace px |
62 |
|
{ |
63 |
|
enum GS_Flags { |
64 |
|
GS_DEFAULT = 0, |
65 |
|
GS_ANTIALIAS = 1, |
66 |
|
GS_NOCLIP = 2 |
67 |
|
}; |
68 |
|
|
69 |
|
struct GraphicsState { |
70 |
|
// Constructors. |
71 |
|
GraphicsState (const Rect &clipr = Rect()) |
72 |
|
: cliprect(clipr), pcolor(0), flags(GS_DEFAULT) |
73 |
|
{} |
74 |
|
|
75 |
|
// Variables. |
76 |
|
Rect cliprect; // current clipping rectangle |
77 |
|
PackedColor pcolor; // current color |
78 |
|
unsigned flags; |
79 |
|
}; |
80 |
|
|
81 |
|
typedef GraphicsState GS; |
82 |
} |
} |
83 |
|
|
84 |
|
//---------------------------------------- |
85 |
|
// Graphics contexts / drawables |
86 |
|
//---------------------------------------- |
87 |
namespace px |
namespace px |
88 |
{ |
{ |
89 |
class FrameBuffer { |
class Drawable { |
90 |
public: |
public: |
91 |
FrameBuffer (SDL_Surface *s) |
virtual ~Drawable() {} |
|
: m_surface(s), m_pitch(m_surface->pitch) |
|
|
{} |
|
|
virtual ~FrameBuffer() { SDL_FreeSurface(m_surface); } |
|
92 |
|
|
93 |
void* scanline_pointer(int y); |
/* |
94 |
void* pixel_pointer(int x, int y); |
** Drawable interface. |
95 |
void lock(); |
*/ |
96 |
void unlock(); |
virtual PackedColor map_color(int r, int g, int b) = 0; |
97 |
|
virtual PackedColor map_color(int r, int g, int b, int a) = 0; |
98 |
|
|
99 |
Uint32 map_color(int r, int g, int b, int a); |
virtual void blit (const GS &gs, int x, int y, Surface* s) = 0; |
100 |
Uint32 map_color(int r, int g, int b); |
virtual void blit (const GS &gs, int x, int y, Surface* s, const Rect& r) = 0; |
101 |
|
|
102 |
void blit(int x, int y, Surface* s); |
virtual Uint32 get_pixel (int x, int y) = 0; |
|
void blit(int x, int y, Surface* s, const Rect& r); |
|
103 |
|
|
104 |
Uint32 pitch() const { return m_pitch; } |
//! Set a single pixel |
105 |
int bypp() const { return m_surface->format->BytesPerPixel; } |
virtual void set_pixel (const GS &gs, int x, int y) = 0; |
|
int bipp() const { return m_surface->format->BitsPerPixel; } |
|
106 |
|
|
107 |
SDL_Surface* get_surface() const { return m_surface; } |
//! Set multiple pixels at once |
108 |
|
virtual void set_pixels (const GS &gs, int n, const int *x, const int *y); |
109 |
|
|
110 |
int width() const { return m_surface->w; } |
//! Draw a horizontal line |
111 |
int height() const { return m_surface->h; } |
virtual void hline (const GS &gs, int x, int y, int w); |
|
Rect size() const { return Rect(0,0,m_surface->w, m_surface->h); } |
|
112 |
|
|
113 |
private: |
//! Draw a vertical line |
114 |
SDL_Surface *m_surface; |
virtual void vline (const GS &gs, int x, int y, int h); |
|
Uint32 m_pitch; |
|
115 |
|
|
116 |
FrameBuffer(const FrameBuffer &); |
//! Draw an arbitrary line |
117 |
FrameBuffer& operator=(const FrameBuffer &); |
virtual void line (const GS &gs, int x1, int y1, int x2, int y2); |
|
}; |
|
118 |
|
|
119 |
inline void* FrameBuffer::scanline_pointer(int y) |
//! Draw a filled box. |
120 |
{ |
virtual void box (const GS &gs, int x, int y, int w, int h); |
121 |
return (Uint8*)m_surface->pixels + y*pitch(); |
|
122 |
} |
//! Return size of drawable: Rect (0,0,width, height) |
123 |
|
virtual Rect size() const = 0; |
124 |
inline void* FrameBuffer::pixel_pointer(int x, int y) |
}; |
|
{ |
|
|
return static_cast<Uint8*>(scanline_pointer(y)) + x*bypp(); |
|
|
} |
|
125 |
} |
} |
126 |
|
|
127 |
//---------------------------------------- |
//---------------------------------------- |
129 |
//---------------------------------------- |
//---------------------------------------- |
130 |
namespace px |
namespace px |
131 |
{ |
{ |
132 |
class Surface : public FrameBuffer { |
class Surface : public Drawable { |
133 |
public: |
public: |
134 |
// Constructor. |
~Surface(); |
|
Surface (SDL_Surface* sfc); |
|
|
virtual ~Surface(); |
|
135 |
|
|
136 |
Surface *zoom(int w, int h); |
Surface *zoom(int w, int h); |
137 |
|
|
138 |
void set_color_key (int r, int g, int b); |
void set_color_key (int r, int g, int b); |
139 |
void set_alpha (int a); |
void set_alpha (int a); |
140 |
|
|
141 |
px::Drawable *get_drawable() { return drawable; } |
void lock(); |
142 |
|
void unlock(); |
143 |
|
|
144 |
|
int bypp() const { return m_surface->format->BytesPerPixel; } |
145 |
|
int bipp() const { return m_surface->format->BitsPerPixel; } |
146 |
|
Uint32 pitch() const { return m_surface->pitch; } |
147 |
|
|
148 |
|
inline void* scanline_pointer(int y) |
149 |
|
{ |
150 |
|
return (Uint8*)m_surface->pixels + y*pitch(); |
151 |
|
} |
152 |
|
|
153 |
|
inline void* pixel_pointer(int x, int y) |
154 |
|
{ |
155 |
|
return static_cast<Uint8*>(scanline_pointer(y)) + x*bypp(); |
156 |
|
} |
157 |
|
|
158 |
|
int height() const { return m_surface->h; } |
159 |
|
int width() const { return m_surface->w; } |
160 |
|
|
161 |
|
SDL_Surface *get_surface() const { return m_surface; } |
162 |
|
|
163 |
|
/* |
164 |
|
** Drawable interface |
165 |
|
*/ |
166 |
|
|
167 |
|
PackedColor map_color(int r, int g, int b); |
168 |
|
PackedColor map_color(int r, int g, int b, int a); |
169 |
|
|
170 |
|
Rect size() const { return Rect(0,0,m_surface->w, m_surface->h); } |
171 |
|
void box (const GS &gs, int x, int y, int w, int h); |
172 |
|
void line (const GS &gs, int x1, int y2, int x2, int y2); |
173 |
|
void blit (const GS &gs, int x, int y, Surface* s, const Rect &r); |
174 |
|
void blit (const GS &gs, int x, int y, Surface* src); |
175 |
|
|
176 |
|
/* |
177 |
|
** Creation of new surfaces |
178 |
|
*/ |
179 |
|
|
180 |
|
static Surface *make_surface (SDL_Surface *s); |
181 |
|
|
182 |
|
protected: |
183 |
|
// Constructor. |
184 |
|
Surface (SDL_Surface* sfc); |
185 |
|
|
186 |
|
// Variables |
187 |
|
SDL_Surface *m_surface; |
188 |
|
|
189 |
private: |
private: |
190 |
px::Drawable *drawable; |
}; |
191 |
|
|
192 |
|
class SurfaceLock { |
193 |
|
Surface *s; |
194 |
|
public: |
195 |
|
SurfaceLock (Surface *s_) : s(s_) { s->lock(); } |
196 |
|
~SurfaceLock() { s->unlock(); } |
197 |
}; |
}; |
198 |
} |
} |
199 |
|
|
202 |
//---------------------------------------- |
//---------------------------------------- |
203 |
namespace px |
namespace px |
204 |
{ |
{ |
205 |
class Screen : public Surface { |
class Screen { |
206 |
public: |
public: |
207 |
Screen(SDL_Surface *s); |
Screen (Surface *s); |
208 |
|
Screen (SDL_Surface *s); |
209 |
|
|
210 |
void update_all(); |
void update_all(); |
211 |
void update_rect(const Rect& r); |
void update_rect(const Rect& r); |
212 |
void flush_updates(); |
void flush_updates(); |
213 |
void set_caption(const char* str); |
void set_caption(const char* str); |
214 |
|
|
215 |
|
Surface *get_surface() const { return m_surface; } |
216 |
|
|
217 |
|
Rect size() const; |
218 |
|
int width() const; |
219 |
|
int height() const; |
220 |
private: |
private: |
221 |
RectList m_dirtyrects; |
// Variables. |
222 |
bool update_all_p; |
Surface *m_surface; |
223 |
|
SDL_Surface *m_sdlsurface; |
224 |
|
RectList m_dirtyrects; |
225 |
|
bool update_all_p; |
226 |
|
|
227 |
Screen(const Screen&); |
Screen(const Screen&); |
228 |
Screen& operator=(const Screen&); |
Screen& operator=(const Screen&); |
229 |
}; |
}; |
230 |
} |
} |
231 |
|
|
232 |
//---------------------------------------- |
|
|
// Graphics contexts / drawables |
|
|
//---------------------------------------- |
|
233 |
namespace px |
namespace px |
234 |
{ |
{ |
235 |
class Drawable { |
struct GC : public GraphicsState { |
236 |
public: |
GC(Drawable* d) :GraphicsState (d->size()) { drawable = d; } |
|
virtual ~Drawable() {} |
|
|
|
|
|
virtual Uint32 map_color(int r, int g, int b) = 0; |
|
|
virtual Uint32 map_color(int r, int g, int b, int a) = 0; |
|
237 |
|
|
238 |
// Drawable interface. |
Drawable *drawable; |
|
virtual void blit(int x, int y, Surface* s) = 0; |
|
|
virtual void blit(int x, int y, Surface* s, const Rect& r) = 0; |
|
|
|
|
|
virtual Uint32 get_pixel(int x, int y) = 0; |
|
|
virtual void set_pixel(int x, int y, Uint32 color) = 0; |
|
|
|
|
|
virtual void set_pixels(int n, const int* x, const int* y, Uint32 color); |
|
|
virtual void hline(int x, int y, int w, Uint32 color); |
|
|
virtual void vline(int x, int y, int h, Uint32 color); |
|
|
virtual void box(int x, int y, int w, int h, Uint32 color); |
|
|
virtual void line(int x1, int y1, int x2, int y2, Uint32 color); |
|
|
|
|
|
virtual Rect size() const = 0; |
|
239 |
}; |
}; |
240 |
} |
} |
241 |
|
|
242 |
|
//---------------------------------------- |
243 |
|
// Graphics primitives |
244 |
|
//---------------------------------------- |
245 |
|
|
246 |
namespace px |
namespace px |
247 |
{ |
{ |
248 |
class GC { |
inline void set_color (GC &gc, int r, int g, int b, int a) |
249 |
public: |
{ |
250 |
GC(Drawable* d); |
gc.pcolor = gc.drawable->map_color(r, g, b, a); |
251 |
GC(Surface *s); |
} |
252 |
|
|
253 |
|
inline void set_color (GC &gc, int r, int g, int b) |
254 |
|
{ |
255 |
|
gc.pcolor = gc.drawable->map_color(r, g, b); |
256 |
|
} |
257 |
|
|
258 |
void clip(const Rect& r) { cliprect.assign(r.x+xoff, r.y+yoff, r.w, r.h); } |
inline void set_color (GC &gc, const RGB &c) |
259 |
Rect get_cliprect() const { return cliprect; } |
{ |
260 |
void noclip() { cliprect = drawable->size(); } |
set_color (gc, c.r, c.g, c.b); |
261 |
|
} |
262 |
void set_color(int r, int g, int b, int a) |
|
263 |
{color = drawable->map_color(r, g, b, a);} |
inline void set_color (GS &gs, PackedColor c) |
264 |
void set_color(int r, int g, int b) |
{ |
265 |
{color = drawable->map_color(r, g, b);} |
gs.pcolor=c; |
266 |
void set_color(Uint32 c) { color=c; } |
} |
|
|
|
|
void set_pixel(int x, int y) { |
|
|
x+=xoff; y+=yoff; |
|
|
if (cliprect.contains(x, y)) |
|
|
drawable->set_pixel(x, y, color); |
|
|
} |
|
|
void set_offset(int xo, int yo) { xoff=xo; yoff=yo; } |
|
267 |
|
|
268 |
void blit(int x, int y, Surface* s) const; |
inline void enable_clipping(GS &gs) |
269 |
void blit(int x, int y, Surface* s, const Rect &r) const; |
{ |
270 |
void box(const Rect& r); |
clear_flags (gs.flags, GS_NOCLIP); |
271 |
void hline(int x, int y, int w); |
} |
272 |
void vline(int x, int y, int h); |
|
273 |
void line(int x1, int y1, int x2, int y2); |
inline void disable_clipping (GS &gs) |
274 |
private: |
{ |
275 |
Drawable* drawable; |
set_flags (gs.flags, GS_NOCLIP); |
276 |
Rect cliprect; // current clipping rectangle |
} |
|
Uint32 color; // current color |
|
|
int xoff, yoff; |
|
|
}; |
|
277 |
|
|
278 |
inline void clip(GC &gc, const Rect& r) { gc.clip(r); } |
inline void clip (GS &gs, const Rect& r) |
279 |
|
{ |
280 |
|
gs.cliprect = r; |
281 |
|
enable_clipping(gs); |
282 |
|
} |
283 |
|
|
284 |
inline void set_color(GC & gc, int r, int g, int b) |
inline void clip (GC &gc, const Rect& r) |
285 |
{ gc.set_color(r, g, b); } |
{ |
286 |
|
gc.cliprect = intersect (r, gc.drawable->size()); |
287 |
|
enable_clipping(gc); |
288 |
|
} |
289 |
|
|
290 |
inline void set_color(GC & gc, int r, int g, int b, int a) |
inline void clip (GC &gc) |
291 |
{ gc.set_color(r, g, b, a); } |
{ |
292 |
|
clip (gc, gc.drawable->size()); |
293 |
|
} |
294 |
|
|
295 |
inline void set_color (GC &gc, const RGB &c) |
inline void blit (const GC &gc, int x, int y, Surface *s) |
296 |
{ gc.set_color (c.r, c.g, c.b); } |
{ |
297 |
|
gc.drawable->blit (gc, x, y, s); |
298 |
|
} |
299 |
|
|
300 |
inline void blit (const GC & gc, int x, int y, Surface *s) |
inline void blit(const GC &gc, int x, int y, Surface *s, const Rect &r) |
301 |
{ gc.blit(x, y, s); } |
{ |
302 |
inline void blit(const GC & gc, int x, int y, Surface *s, const Rect &r) |
gc.drawable->blit (gc, x, y, s, r); |
303 |
{ gc.blit(x,y,s,r); } |
} |
304 |
|
|
305 |
inline void set_pixel(GC & gc, int x, int y) { gc.set_pixel(x, y); } |
inline void set_pixel(const GC &gc, int x, int y) |
306 |
inline void hline(GC & gc, int x, int y, int w) { gc.hline(x, y, w); } |
{ |
307 |
inline void vline(GC & gc, int x, int y, int h) { gc.vline(x, y, h); } |
gc.drawable->set_pixel (gc, x, y); |
308 |
|
} |
309 |
|
|
310 |
void frame(GC & gc, int x, int y, int w, int h); |
inline void hline (const GC & gc, int x, int y, int w) |
311 |
inline void frame(GC & gc, const Rect& r) { frame(gc, r.x, r.y, r.w, r.h);} |
{ |
312 |
|
gc.drawable->hline (gc, x, y, w); |
313 |
|
} |
314 |
|
|
315 |
|
inline void vline (const GC & gc, int x, int y, int h) |
316 |
|
{ |
317 |
|
gc.drawable->vline (gc, x, y, h); |
318 |
|
} |
319 |
|
|
320 |
inline void box(GC & gc, const Rect& r) { gc.box(r); } |
inline void box (const GC &gc, const Rect& r) |
321 |
inline void box(GC & gc, int x, int y, int w, int h) |
{ |
322 |
{ gc.box(Rect(x, y, w, h)); } |
gc.drawable->box(gc, r.x, r.y, r.w, r.h); |
323 |
|
} |
324 |
|
|
325 |
|
inline void box (const GC &gc, int x, int y, int w, int h) |
326 |
|
{ |
327 |
|
gc.drawable->box (gc, x, y, w, h); |
328 |
|
} |
329 |
|
|
330 |
|
void line (const GC &gc, int x1, int y1, int x2, int y2); |
331 |
|
void frame (const GC & gc, int x, int y, int w, int h); |
332 |
|
|
333 |
|
inline void frame (const GC &gc, const Rect& r) |
334 |
|
{ |
335 |
|
frame (gc, r.x, r.y, r.w, r.h); |
336 |
|
} |
337 |
} |
} |
338 |
|
|
339 |
|
|