1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
|
/*
* Copyright (C) 2002,2003 Daniel Heck
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#ifndef ECL_VIDEO_HH_INCLUDED
#define ECL_VIDEO_HH_INCLUDED
#include "ecl_fwd.hh"
#include "ecl_geom.hh"
#include "SDL.h"
namespace ecl
{
/* -------------------- Colors -------------------- */
struct RGBA_Mask {
RGBA_Mask(Uint32 rr=0, Uint32 gg=0, Uint32 bb=0, Uint32 aa=0)
: r(rr), g(gg), b(bb), a(aa)
{}
Uint32 r,g,b,a;
};
struct RGB {
RGB(char rr=0, char gg=0, char bb=0)
: r(rr), g(gg), b(bb)
{}
char r,g,b;
};
struct RGBA {
RGBA(char rr=0, char gg=0, char bb=0, char aa=0)
: r(rr), g(gg), b(bb), a(aa)
{}
char r,g,b,a;
};
typedef Uint32 PackedColor;
/* -------------------- Graphics State (GS) -------------------- */
enum GS_Flags {
GS_DEFAULT = 0,
GS_ANTIALIAS = 1,
GS_NOCLIP = 2
};
struct GraphicsState {
// Constructors.
GraphicsState (const Rect &clipr = Rect())
: cliprect(clipr), pcolor(0), flags(GS_DEFAULT)
{}
// Variables.
Rect cliprect; // current clipping rectangle
PackedColor pcolor; // current color
unsigned flags;
};
typedef GraphicsState GS;
/* -------------------- Drawable -------------------- */
class Drawable {
public:
virtual ~Drawable() {}
/* ---------- Drawable interface ---------- */
virtual PackedColor map_color(int r, int g, int b) = 0;
virtual PackedColor map_color(int r, int g, int b, int a) = 0;
virtual void blit (const GS &gs, int x, int y, const Surface* s) = 0;
virtual void blit (const GS &gs, int x, int y, const Surface* s, const Rect& r) = 0;
virtual Uint32 get_pixel (int x, int y) = 0;
//! Set a single pixel
virtual void set_pixel (const GS &gs, int x, int y) = 0;
//! Set multiple pixels at once
virtual void set_pixels (const GS &gs, int n, const int *x, const int *y);
//! Draw a horizontal line
virtual void hline (const GS &gs, int x, int y, int w);
//! Draw a vertical line
virtual void vline (const GS &gs, int x, int y, int h);
//! Draw an arbitrary line
virtual void line (const GS &gs, int x1, int y1, int x2, int y2);
//! Draw a filled box.
virtual void box (const GS &gs, int x, int y, int w, int h);
//! Return size of drawable: Rect (0,0,width, height)
virtual Rect size() const = 0;
};
/* -------------------- Graphics Context (GC) -------------------- */
struct GC : public GraphicsState {
GC(Drawable* d) :GraphicsState (d->size()) {
drawable = d;
}
Drawable *drawable;
};
/* -------------------- Surface -------------------- */
class Surface : public Drawable {
public:
~Surface();
Surface *zoom(int w, int h);
void set_color_key (int r, int g, int b);
void set_alpha (int a);
void lock();
void unlock();
int bypp() const { return m_surface->format->BytesPerPixel; }
int bipp() const { return m_surface->format->BitsPerPixel; }
Uint32 pitch() const { return m_surface->pitch; }
inline void* scanline_pointer(int y) {
return (Uint8*)m_surface->pixels + y*pitch();
}
inline void* pixel_pointer(int x, int y) {
return static_cast<Uint8*>(scanline_pointer(y)) + x*bypp();
}
int height() const { return m_surface->h; }
int width() const { return m_surface->w; }
SDL_Surface *get_surface() const { return m_surface; }
/* ---------- Drawable interface ---------- */
PackedColor map_color(int r, int g, int b);
PackedColor map_color(int r, int g, int b, int a);
Rect size() const { return Rect(0,0,m_surface->w, m_surface->h); }
void box (const GS &gs, int x, int y, int w, int h);
void line (const GS &gs, int x1, int y1, int x2, int y2);
void blit (const GS &gs, int x, int y, const Surface* s, const Rect &r);
void blit (const GS &gs, int x, int y, const Surface* src);
/* ---------- Static methods ---------- */
//! Create a new surface
static Surface *make_surface (SDL_Surface *s);
protected:
// Constructor.
Surface (SDL_Surface* sfc);
// Variables
SDL_Surface *m_surface;
private:
};
class SurfaceLock {
Surface *s;
public:
SurfaceLock (Surface *s_) : s(s_) { s->lock(); }
~SurfaceLock() { s->unlock(); }
};
/* -------------------- Screen -------------------- */
class Screen {
public:
Screen (Surface *s);
Screen (SDL_Surface *s);
~Screen();
void update_all();
void update_rect(const Rect& r);
void flush_updates();
void set_caption(const char* str);
/* ---------- Accessors ---------- */
Surface *get_surface() const { return m_surface; }
Rect size() const;
int width() const;
int height() const;
/* ---------- Static methods ---------- */
static Screen *get_instance();
private:
// Variables.
static Screen *m_instance;
Surface *m_surface;
SDL_Surface *m_sdlsurface;
RectList m_dirtyrects;
bool update_all_p;
Screen(const Screen&);
Screen& operator=(const Screen&);
};
/* -------------------- Graphics primitives -------------------- */
inline void set_color (GC &gc, int r, int g, int b, int a) {
gc.pcolor = gc.drawable->map_color(r, g, b, a);
}
inline void set_color (GC &gc, int r, int g, int b) {
gc.pcolor = gc.drawable->map_color(r, g, b);
}
inline void set_color (GC &gc, const RGB &c) {
set_color (gc, c.r, c.g, c.b);
}
inline void set_color (GS &gs, PackedColor c) {
gs.pcolor=c;
}
inline void enable_clipping(GS &gs) {
clear_flags (gs.flags, GS_NOCLIP);
}
inline void disable_clipping (GS &gs) {
set_flags (gs.flags, GS_NOCLIP);
}
inline void clip (GS &gs, const Rect& r) {
gs.cliprect = r;
enable_clipping(gs);
}
inline void clip (GC &gc, const Rect& r) {
gc.cliprect = intersect (r, gc.drawable->size());
enable_clipping(gc);
}
inline void clip (GC &gc) {
clip (gc, gc.drawable->size());
}
inline void blit (const GC &gc, int x, int y, const Surface *s) {
gc.drawable->blit (gc, x, y, s);
}
inline void blit(const GC &gc, int x, int y, const Surface *s, const Rect &r) {
gc.drawable->blit (gc, x, y, s, r);
}
inline void set_pixel(const GC &gc, int x, int y) {
gc.drawable->set_pixel (gc, x, y);
}
inline void hline (const GC & gc, int x, int y, int w) {
gc.drawable->hline (gc, x, y, w);
}
inline void vline (const GC & gc, int x, int y, int h) {
gc.drawable->vline (gc, x, y, h);
}
inline void box (const GC &gc, const Rect& r) {
gc.drawable->box(gc, r.x, r.y, r.w, r.h);
}
inline void box (const GC &gc, int x, int y, int w, int h) {
gc.drawable->box (gc, x, y, w, h);
}
void line (const GC &gc, int x1, int y1, int x2, int y2);
void frame (const GC & gc, int x, int y, int w, int h);
inline void frame (const GC &gc, const Rect& r) {
frame (gc, r.x, r.y, r.w, r.h);
}
/* -------------------- Functions -------------------- */
Screen *OpenScreen (int w, int h, int bipp);
Screen* DisplayFormat(Screen* s);
/*! Create a new surface. */
Surface *MakeSurface(int w, int h, int bipp,
const RGBA_Mask &mask = RGBA_Mask());
/*! Create a surface from image data that is already somewhere in
memory. */
Surface * MakeSurface(void* data, int w, int h, int bipp, int pitch,
const RGBA_Mask &mask = RGBA_Mask());
/*! Create a new surface with the same image format as
`surface'. */
Surface *MakeSurfaceLike (int w, int h, Surface *surface);
/*! Create a copy of a surface. */
Surface *Duplicate(const Surface *s);
/*! Save a surface to a PNG file. */
void SavePNG (const Surface *s, const std::string& filename);
/*! Create a new surface from a selection of an old one. Performs
proper clipping and returns a surface of the appropriate size
(this may be smaller than the original size given in `r'.) The
function stores the real region in `r'. */
Surface *Grab(const Surface *s, Rect &r);
/*! Convert a surface to the current screen's native format. */
Surface* DisplayFormat(Surface* s);
/*! Load an image using SDL_image and convert it to an optimized
format. */
Surface* LoadImage(const char* filename);
Surface* LoadImage(SDL_RWops *src, int freesrc);
Surface* LoadImage(SDL_Surface *tmpImage);
/*! Overlay a rectangle `rect' in `s' with a transparent colored
box. */
void TintRect(Surface *s, Rect rect, Uint8 r, Uint8 g, Uint8 b, Uint8 a);
enum ResampleFilter {
FILTER_BILINEAR,
FILTER_LANCZOS
};
/*! Resample a region inside a surface to a new size. Returns a
new 32 bit RGBA image containing the scaled image.
(Only partially implemented so far.)
*/
Surface *Resample (Surface *s, Rect rect, int neww, int newh,
ResampleFilter filter = FILTER_BILINEAR);
}
#endif /* !ECL_VIDEO_HH_INCLUDED */
|