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
| //!@file interface.cpp
#include "intervideo.hpp"
#include "exception.hpp"
#include <string>
#include <SDL/SDL_image.h>
using std::string;
namespace interface
{
Rect::Rect(Sint16 x, Sint16 y, Uint16 w, Uint16 h)
{
this->x = x, this->y = y, this->w = w, this->h = h;
}
bool Rect::isNull() const
{
return x==0 && y==0 && w==0 && h==0;
}
Color::Color(Uint8 base)
{
r=base, g=base, b=base;
}
Color::Color(Uint8 r, Uint8 g, Uint8 b)
{
this->r = r, this->g = g, this->b = b;
}
Uint32 Color::map(const Surface &s) const
{
return SDL_MapRGB(s.s->format, r,g,b);
}
void Surface::create (SDL_Surface *ext)
{
free();
s = ext;
}
Surface::Surface(SDL_Surface *ext)
{
s = ext;
if (s == NULL)
{
throw InterfaceException("Surface::Surface(SDL_Surface*) : surface NULL passée en paramètre!");
}
}
Surface::Surface() : s(NULL)
{
;
}
Surface::Surface(const Surface &s) : s(NULL)
{
shareFrom(s);
}
Surface::Surface(const char *filename, bool raw) : s(NULL)
{
if (raw)
raw_load(filename);
else
load(filename);
}
Surface::Surface(const char *filename, const ColorKey &colorkey, bool raw) : s(NULL)
{
if (raw)
raw_load(filename, colorkey);
else
load(filename, colorkey);
}
Surface::Surface(SDL_RWops *rw, bool close_the_RW) : s(NULL)
{
load(rw, close_the_RW);
}
Surface::Surface(Uint16 bpp, Uint16 w, Uint16 h, Sint32 flags) : s(NULL)
{
create(bpp,w,h,flags);
}
Surface::~Surface()
{
free();
}
Surface& Surface::operator = (const Surface &s)
{
shareFrom(s);
return *this;
}
//charger
void Surface::raw_load(const char *filename)
{
free();
s = IMG_Load(filename);
if (!s)
throw InterfaceException(string() + "Surface::raw_load() -- loading failed: " + filename);
}
void Surface::raw_load(const char *filename, const Color &colorkeys)
{
raw_load(filename);
colorkey(colorkeys);
}
void Surface::load(const char * filename)
{
raw_load(filename);
adapt();
}
void Surface::load(SDL_RWops *ops, bool close_the_RW)
{
free();
SDL_Surface *temp = IMG_Load_RW(ops, close_the_RW);
if (temp == NULL)
{
throw InterfaceException(string() + "Surface::load() with RWops -- loading failed");
}
s = SDL_DisplayFormat(temp);
SDL_FreeSurface(temp);
if (s == NULL)
{
throw InterfaceException("Surface::load() -- SDL_DisplayFormat failed");
}
}
void Surface::load(const char * filename, const ColorKey &colorkey)
{
raw_load(filename, colorkey);
adapt();
}
//créer
void Surface::create(Uint16 bpp, Uint16 w, Uint16 h, Sint32 flags)
{
free();
s = SDL_CreateRGBSurface(flags,w,h,bpp,0,0,0,0);
if (s == NULL) throw InterfaceException("Surface::create(bpp,w,h,flags) -- error with SDL_CreateRGBSurface");
}
//s'adapter
void Surface::adapt()
{
if (s == NULL)
return;
SDL_Surface *s2 = SDL_DisplayFormat(s);
if (s2 == NULL)
throw InterfaceException("Surface::adapt -- error with SDL_DisplayFormat");
free();
s = s2;
}
void Surface::fill(const Rect &r, const Color &color)
{
Uint32 c = color.map(*this);
if (r.isNull())
{
if (SDL_FillRect(s, 0, c) < 0)
throw InterfaceException("Surface::fill -- error with SDL_FillRect");
} else
{
Rect r2(r);
if (SDL_FillRect(s, &r2, c) < 0)
throw InterfaceException("Surface::fill -- error with SDL_FillRect");
}
}
//partager à partir d'une autre surface
void Surface::shareFrom (const Surface &ext)
{
if (s == ext.s)
return;
free();
s = ext.s;
if (s != NULL)
{
//On utilise une fonctionnalité SDL qui permet d'avoir un compteur de références
s->refcount++;
}
}
//Se libérer
void Surface::free()
{
SDL_FreeSurface(s);
s = NULL;
}
//L'alpha
void Surface::alpha(Uint8 alpha)
{
if (SDL_SetAlpha(s,SDL_SRCALPHA|SDL_RLEACCEL,alpha) < 0)
throw InterfaceException("Surface::alpha -- error");
}
/* Change le colorkey de la surface avec la nouvelle couleur */
void Surface::colorkey(const Color &color)
{
if (SDL_SetColorKey(s,SDL_SRCCOLORKEY|SDL_RLEACCEL, color.map(*this)) < 0)
throw InterfaceException("Surface::colorkey -- error");
}
//accesseurs
Uint16 Surface::w()
{
return s->w;
}
Uint16 Surface::h()
{
return s->h;
}
//dessiner
void Surface::blitTo(Surface &dest_surf, const Rect &clip_src, const Rect &clip_dest)
{
/* Si le rectangle est de coordonnées nulles on fournit NULL, qui veut dire "toute la surface" */
Rect * clip_s = clip_src.isNull() ? NULL : const_cast<Rect *>(&clip_src);
Rect * clip_d = clip_dest.isNull() ? NULL : const_cast<Rect *>(&clip_dest);
if (SDL_BlitSurface(s, clip_s, dest_surf.s, clip_d) < 0)
throw InterfaceException("Surface::blitTo -- error with SDL_BlitSurface)");
}
//refcount
size_t Surface::refcount() const
{
if (s)
return s->refcount;
else
return 0;
}
Surface::operator bool ()
{
return (s);
}
Window::Window()
{
}
Window::Window(Uint16 bpp, Uint16 w, Uint16 h, Sint32 flags)
{
create(bpp,w,h,flags);
}
Window::~Window()
{
}
void Window::create(Uint16 bpp, Uint16 w, Uint16 h, Sint32 flags)
{
free();
s = SDL_SetVideoMode(w,h,bpp,flags);
if (s == NULL)
throw InterfaceException("Window::create() -- Error with SDL_SetVideoMode");
//little hack because actually its SDL that should free the surface
s->refcount++;
}
//Retourne l'écran ;)
void Window::flip()
{
SDL_Flip(s);
}
//Diverses fonctions pour updater l'écran qu'en partie
void Window::update_rect(const Rect &r)
{
SDL_UpdateRect(s,r.x,r.y,r.w,r.h);
}
template <class iterator>
void Window::update_rects(iterator debut, iterator fin)
{
SDL_UpdateRects(s, debut, fin-debut);
}
} //namespace interface |