| 1 |
#include "oodle.h" |
| 2 |
|
| 3 |
CSurface::CSurface(Uint32 flags, int width, int height, int depth, |
| 4 |
Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask) |
| 5 |
: width(width), height(height), depth(depth), creationFlags(flags), |
| 6 |
Rmask(Rmask), Gmask(Gmask), Bmask(Bmask), Amask(Amask), |
| 7 |
locked(0) |
| 8 |
{ |
| 9 |
/*LOG("Creating surface {" << this << "}", 6, LOG_INFO);*/ |
| 10 |
s = createSurface(flags, width, height, depth, |
| 11 |
Rmask, Gmask, Bmask, Amask); |
| 12 |
if (!s) |
| 13 |
{ |
| 14 |
// surface creation failed, throw exception |
| 15 |
_THROWEX(ex_sdlSurfaceCreationFailed, "SDL Surface Creation Failed", "CSurface", "CSurface", |
| 16 |
"flags = " << flags << ", width = " << width << ", height = " << height << ", depth = " |
| 17 |
<< depth << ", Rmask = " << Rmask << ", Gmask = " << Gmask << ", Bmask = " << Bmask |
| 18 |
<< ", Amask = " << Amask); |
| 19 |
} |
| 20 |
|
| 21 |
// displayFormatAlpha(); |
| 22 |
} |
| 23 |
|
| 24 |
CSurface::CSurface(SDL_Surface* s) |
| 25 |
: s(s), locked(0) |
| 26 |
{ |
| 27 |
if (!s) |
| 28 |
{ |
| 29 |
_THROWEX(ex_illegalArguments, "Illegal argument 's'", "CSurface", "CSurface", "s = " << s); |
| 30 |
} |
| 31 |
width = s->w; |
| 32 |
height = s->h; |
| 33 |
depth = s->format->BitsPerPixel; |
| 34 |
creationFlags = s->flags; |
| 35 |
Rmask = s->format->Rmask; |
| 36 |
Gmask = s->format->Gmask; |
| 37 |
Bmask = s->format->Bmask; |
| 38 |
Amask = s->format->Amask; |
| 39 |
|
| 40 |
// displayFormatAlpha(); |
| 41 |
} |
| 42 |
|
| 43 |
CSurface::CSurface(const CSurface &surf) |
| 44 |
: width(surf.width), height(surf.height), depth(surf.depth), creationFlags(surf.creationFlags), |
| 45 |
Rmask(surf.Rmask), Gmask(surf.Gmask), Bmask(surf.Bmask), Amask(surf.Amask), |
| 46 |
locked(0) |
| 47 |
{ |
| 48 |
s = SDL_ConvertSurface(surf.s, surf.s->format, surf.s->flags); |
| 49 |
|
| 50 |
if (!s) |
| 51 |
{ |
| 52 |
// surface creation failed, throw exception |
| 53 |
_THROWEX(ex_sdlSurfaceCreationFailed, "SDL Surface Creation Failed", "CSurface", "CSurface", "surf = " << surf); |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
CSurface::~CSurface() |
| 58 |
{ |
| 59 |
//LOG("Freeing surface {" << this << "}", 6, LOG_INFO); |
| 60 |
SDL_FreeSurface(s); |
| 61 |
} |
| 62 |
|
| 63 |
CSurface* CSurface::getSection(int x, int y, int width, int height) const |
| 64 |
{ |
| 65 |
CSurface* tmp = new CSurface(creationFlags, width, height, depth, Rmask, Gmask, Bmask, Amask); |
| 66 |
|
| 67 |
tmp->blitSrcRect(*this, CRect(x, y, width, height)); |
| 68 |
return tmp; |
| 69 |
} |
| 70 |
|
| 71 |
void CSurface::blitSrcRect(const CSurface& src, CRect r) |
| 72 |
{ |
| 73 |
SDL_Rect sr; |
| 74 |
sr.x = (Sint16)r.getX(); |
| 75 |
sr.y = (Sint16)r.getY(); |
| 76 |
sr.w = (Sint16)r.getWidth(); |
| 77 |
sr.h = (Sint16)r.getHeight(); |
| 78 |
|
| 79 |
int ret = SDL_BlitSurface(src.s, &sr, s, NULL); |
| 80 |
} |
| 81 |
|
| 82 |
void CSurface::blitScale(const CSurface& src, CRect r, double scale) |
| 83 |
{ |
| 84 |
Uint8 alpha = src.getSurface()->format->alpha; |
| 85 |
SDL_Rect sr; |
| 86 |
sr.x = (Sint16)r.getX(); |
| 87 |
sr.y = (Sint16)r.getY(); |
| 88 |
|
| 89 |
SDL_Surface* z = zoomSurface(src.s, scale, scale, 1); |
| 90 |
if (z) |
| 91 |
{ |
| 92 |
if (alpha != SDL_ALPHA_OPAQUE) |
| 93 |
{ |
| 94 |
SDL_SetAlpha(z, SDL_SRCALPHA, alpha); |
| 95 |
} |
| 96 |
SDL_BlitSurface(z, NULL, s, &sr); |
| 97 |
SDL_FreeSurface(z); |
| 98 |
} |
| 99 |
else |
| 100 |
{ |
| 101 |
_THROWEX(ex_sdlSurfaceCreationFailed, "Failed to rotozoom surface", "CSurface", "blitScale", "src = {" << &src << "}, r = {" << &r << "}, scale = " << scale); |
| 102 |
} |
| 103 |
// dirty.push_back(CRect(sr.x,sr.y,z->w, z->h)); |
| 104 |
} |
| 105 |
|
| 106 |
void CSurface::blitScaleEx(const CSurface& src, CRect r, double scaleX, double scaleY) |
| 107 |
{ |
| 108 |
Uint8 alpha = s->format->alpha; |
| 109 |
SDL_Rect sr; |
| 110 |
sr.x = (Sint16)r.getX(); |
| 111 |
sr.y = (Sint16)r.getY(); |
| 112 |
|
| 113 |
SDL_Surface* z = zoomSurface(src.s, scaleX, scaleY, 1); |
| 114 |
if (z) |
| 115 |
{ |
| 116 |
if (alpha != SDL_ALPHA_OPAQUE) SDL_SetAlpha(z, SDL_SRCALPHA, alpha); |
| 117 |
SDL_BlitSurface(z, NULL, s, &sr); |
| 118 |
SDL_FreeSurface(z); |
| 119 |
} |
| 120 |
else |
| 121 |
{ |
| 122 |
_THROWEX(ex_sdlSurfaceCreationFailed, "Failed to rotozoom surface", "CSurface", "blitScale", "src = {" << &src << "}, r = {" << &r << "}, scaleX = " << scaleX << ", scaleY = " << scaleY); |
| 123 |
} |
| 124 |
// dirty.push_back(CRect(sr.x, sr.y, z->w, z->h)); |
| 125 |
} |
| 126 |
|
| 127 |
void CSurface::rotateAndScale(double scale, double rad) |
| 128 |
{ |
| 129 |
Uint8 alpha = s->format->alpha; |
| 130 |
SDL_Surface* tmp = rotozoomSurface (s, rad * (180/PI), scale, 1); |
| 131 |
if (alpha != SDL_ALPHA_OPAQUE) SDL_SetAlpha(tmp, SDL_SRCALPHA, alpha); |
| 132 |
|
| 133 |
if (!tmp) |
| 134 |
_THROWEX( ex_sdlSurfaceCreationFailed, "Failed to rotozoom surface", "CSurface", "rotateAndScale", "scale=" << scale << ", rad=" << rad); |
| 135 |
|
| 136 |
SDL_FreeSurface(s); |
| 137 |
s = tmp; |
| 138 |
} |
| 139 |
|
| 140 |
void CSurface::blitRotScale(const CSurface& src, CRect r, double scale, double rad) |
| 141 |
{ |
| 142 |
Uint8 alpha = s->format->alpha; |
| 143 |
SDL_Rect sr; |
| 144 |
sr.x = (Sint16)r.getX(); |
| 145 |
sr.y = (Sint16)r.getY(); |
| 146 |
|
| 147 |
SDL_Surface* z = rotozoomSurface (src.s, rad * (180/PI), scale, 1); |
| 148 |
if (z) |
| 149 |
{ |
| 150 |
if (alpha != SDL_ALPHA_OPAQUE) SDL_SetAlpha(z, SDL_SRCALPHA, alpha); |
| 151 |
SDL_BlitSurface(z, NULL, s, &sr); |
| 152 |
SDL_FreeSurface(z); |
| 153 |
} |
| 154 |
else |
| 155 |
{ |
| 156 |
_THROWEX(ex_sdlSurfaceCreationFailed, "Failed to rotozoom surface", "CSurface", "blitScale", "src = {" << &src << "}, r = {" << &r << "}, scale = " << scale << ", rad = " << rad); |
| 157 |
} |
| 158 |
// dirty.push_back(CRect(sr.x, sr.y, z->w, z->h)); |
| 159 |
} |
| 160 |
|
| 161 |
void CSurface::blitBothRect(const CSurface& src, CRect rsrc, CRect rdest) |
| 162 |
{ |
| 163 |
SDL_Rect sr1; |
| 164 |
sr1.x = (Sint16)rsrc.getX(); |
| 165 |
sr1.y = (Sint16)rsrc.getY(); |
| 166 |
sr1.w = (Sint16)rsrc.getWidth(); |
| 167 |
sr1.h = (Sint16)rsrc.getHeight(); |
| 168 |
|
| 169 |
SDL_Rect sr2; |
| 170 |
sr2.x = (Sint16)rdest.getX(); |
| 171 |
sr2.y = (Sint16)rdest.getY(); |
| 172 |
sr2.w = (Sint16)rdest.getWidth(); |
| 173 |
sr2.h = (Sint16)rdest.getHeight(); |
| 174 |
|
| 175 |
int ret = SDL_BlitSurface(src.s, &sr1, s, &sr2); |
| 176 |
// dirty.push_back(rdest); |
| 177 |
} |
| 178 |
|
| 179 |
void CSurface::blit(const CSurface &src, int x, int y) |
| 180 |
{ |
| 181 |
SDL_Rect r; |
| 182 |
r.x = x; |
| 183 |
r.y = y; |
| 184 |
int b = SDL_BlitSurface(src.s, NULL, s, &r); |
| 185 |
/* if (src.width != width && src.height != height) |
| 186 |
dirty.push_back(CRect(x,y,src.width,src.height));*/ |
| 187 |
} |
| 188 |
|
| 189 |
void CSurface::fill(CColor color) |
| 190 |
{ |
| 191 |
//Uint32 surfcolor = SDL_MapRGB(s->format, color.getR(), color.getG(), color.getB()); |
| 192 |
SDL_FillRect(s, NULL, color.getDispFormatColor()); |
| 193 |
//dirty.clear(); |
| 194 |
//dirty.push_back(CRect(0,0,width,height)); |
| 195 |
} |
| 196 |
|
| 197 |
void CSurface::doneDrawing() |
| 198 |
{ |
| 199 |
if ((s->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF) // if surface is dbl buffered, we have to flip it |
| 200 |
{ |
| 201 |
SDL_Flip(s); |
| 202 |
} |
| 203 |
else |
| 204 |
{ |
| 205 |
SDL_UpdateRect(s, 0, 0, 0, 0); // invalidate/redraw whole screen |
| 206 |
} |
| 207 |
/*for (list<CRect>::iterator i = dirty.begin(); i != dirty.end(); ++i) |
| 208 |
{ |
| 209 |
CRect r = (*i); |
| 210 |
SDL_Rect r1 = {r.getX(), r.getY(), r.getWidth(), r.getHeight()}; |
| 211 |
//SDL_FillRect(s, &r1, COLOR_BLACK.getDispFormatColor()); |
| 212 |
SDL_UpdateRect(s, r.getX(), r.getY(), r.getWidth(), r.getHeight()); |
| 213 |
}*/ |
| 214 |
} |
| 215 |
|
| 216 |
void CSurface::setAlpha(Uint32 flags, Uint8 alpha) |
| 217 |
{ |
| 218 |
SDL_SetAlpha(s, flags, alpha); |
| 219 |
} |
| 220 |
|
| 221 |
const CSurface& CSurface::operator=(const CSurface &s) |
| 222 |
{ |
| 223 |
if (&s != this) // don't allow assignment to self |
| 224 |
{ |
| 225 |
SDL_FreeSurface(CSurface::s); |
| 226 |
|
| 227 |
width = s.width; |
| 228 |
height = s.height; |
| 229 |
depth = s.depth; |
| 230 |
creationFlags = s.creationFlags; |
| 231 |
Rmask = s.Rmask; |
| 232 |
Gmask = s.Gmask; |
| 233 |
Bmask = s.Bmask; |
| 234 |
Amask = s.Amask; |
| 235 |
CSurface::s = SDL_ConvertSurface(s.s, s.s->format, s.s->flags); |
| 236 |
|
| 237 |
if (!CSurface::s) |
| 238 |
{ |
| 239 |
_THROWEX(ex_sdlSurfaceCreationFailed, "SDL Surface Creation Failed", "CSurface", "operator=", "s = " << s); |
| 240 |
} |
| 241 |
} |
| 242 |
return *this; |
| 243 |
} |
| 244 |
|
| 245 |
SDL_Surface* CSurface::getSurface() const |
| 246 |
{ |
| 247 |
return s; |
| 248 |
} |
| 249 |
|
| 250 |
void CSurface::displayFormat() |
| 251 |
{ |
| 252 |
SDL_Surface* tmp; |
| 253 |
if (SDL_GetVideoSurface() != NULL && s != SDL_GetVideoSurface()) |
| 254 |
{ |
| 255 |
tmp = SDL_DisplayFormat(s); |
| 256 |
if (!tmp) |
| 257 |
{ |
| 258 |
// throw exception |
| 259 |
_THROWEX(ex_sdlSurfaceConversionFailed, "SDL Surface Creation Failed", "CSurface", "displayFormat", ""); |
| 260 |
} |
| 261 |
SDL_FreeSurface(s); |
| 262 |
s = tmp; |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
void CSurface::displayFormatAlpha() |
| 267 |
{ |
| 268 |
SDL_Surface* tmp; |
| 269 |
if (SDL_GetVideoSurface() != NULL && s != SDL_GetVideoSurface()) |
| 270 |
{ |
| 271 |
tmp = SDL_DisplayFormatAlpha(s); |
| 272 |
if (!tmp) |
| 273 |
{ |
| 274 |
// throw exception |
| 275 |
_THROWEX(ex_sdlSurfaceConversionFailed, "SDL Surface Creation Failed", "CSurface", "displayFormatAlpha", ""); |
| 276 |
} |
| 277 |
SDL_FreeSurface(s); |
| 278 |
s = tmp; |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
int CSurface::getWidth() const |
| 283 |
{ |
| 284 |
return width; |
| 285 |
} |
| 286 |
|
| 287 |
int CSurface::getHeight() const |
| 288 |
{ |
| 289 |
return height; |
| 290 |
} |
| 291 |
|
| 292 |
bool CSurface::onSurface(const CVector &pos) const |
| 293 |
{ |
| 294 |
return onSurface((int)pos.getX(), (int)pos.getY()); |
| 295 |
} |
| 296 |
|
| 297 |
bool CSurface::onSurface(int x, int y) const |
| 298 |
{ |
| 299 |
if (x < 0 || x >= width || y < 0 || y >= height) |
| 300 |
{ |
| 301 |
return false; |
| 302 |
} |
| 303 |
else |
| 304 |
{ |
| 305 |
return true; |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
void CSurface::lock() |
| 310 |
{ |
| 311 |
// attempt to lock surface if necessary |
| 312 |
if (SDL_MUSTLOCK(s) && locked == 0) |
| 313 |
{ |
| 314 |
//cout << "Locking {" << s << "}" << endl; |
| 315 |
if (SDL_LockSurface(s) < 0) |
| 316 |
{ |
| 317 |
// FIXME: throw an exception |
| 318 |
return; |
| 319 |
} |
| 320 |
locked++; |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
void CSurface::unlock() |
| 325 |
{ |
| 326 |
if (locked <= 0) |
| 327 |
{ |
| 328 |
// FIXME: throw an exception |
| 329 |
return; |
| 330 |
} |
| 331 |
|
| 332 |
//cout << "Unlocking {" << s << "}" << endl; |
| 333 |
// unlock surface if necessary |
| 334 |
if (locked > 0) |
| 335 |
{ |
| 336 |
SDL_UnlockSurface(s); |
| 337 |
|
| 338 |
locked--; |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
void CSurface::getPixel(int x, int y, CColor &color) |
| 343 |
{ |
| 344 |
Uint8 red, green, blue; |
| 345 |
|
| 346 |
try |
| 347 |
{ |
| 348 |
SDL_GetRGB(getPixel(x, y), s->format, &red, &green, &blue); |
| 349 |
|
| 350 |
color.setR(red); |
| 351 |
color.setG(green); |
| 352 |
color.setB(blue); |
| 353 |
} |
| 354 |
catch (ex_Generic e) |
| 355 |
{ |
| 356 |
_ADDTRACEINFO(e, "CSurface", "getPixel", |
| 357 |
"x = " << x << ", y = " << y << ", color = " << (void*) &color); |
| 358 |
throw e; |
| 359 |
} |
| 360 |
|
| 361 |
} |
| 362 |
|
| 363 |
void CSurface::setPixel(int x, int y, CColor &color) |
| 364 |
{ |
| 365 |
try |
| 366 |
{ |
| 367 |
setPixel(x, y, color.getDispFormatColor()); |
| 368 |
} |
| 369 |
catch (ex_Generic e) |
| 370 |
{ |
| 371 |
_ADDTRACEINFO(e, "CSurface", "setPixel", |
| 372 |
"x = " << x << ", y = " << y << ", color = " << (void*) &color); |
| 373 |
throw e; |
| 374 |
} |
| 375 |
} |
| 376 |
|
| 377 |
SDL_Surface* CSurface::createSurface(Uint32 flags, int width, int height, int depth, |
| 378 |
Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask) |
| 379 |
{ |
| 380 |
return SDL_CreateRGBSurface(flags, width, height, depth, |
| 381 |
Rmask, Gmask, Bmask, Amask); |
| 382 |
} |
| 383 |
|
| 384 |
Uint32 CSurface::getPixel(int x, int y) |
| 385 |
{ |
| 386 |
lock(); |
| 387 |
|
| 388 |
if (! onSurface(x, y)) |
| 389 |
{ |
| 390 |
// the x coord is out of range |
| 391 |
_THROWEX(ex_illegalArguments, |
| 392 |
"(x, y) does not lie on surface", |
| 393 |
"CSurface", "getPixel", |
| 394 |
"x = " << x << ", y = " << y); |
| 395 |
} |
| 396 |
|
| 397 |
Uint8 *ptr = (Uint8*) s->pixels + y * s->pitch + x * s->format->BytesPerPixel; |
| 398 |
|
| 399 |
switch (s->format->BytesPerPixel) |
| 400 |
{ |
| 401 |
case 1: |
| 402 |
return (Uint32) *ptr; |
| 403 |
case 2: |
| 404 |
return (Uint32) *((Uint16*) ptr); |
| 405 |
case 3: |
| 406 |
if (SDL_BYTEORDER == SDL_BIG_ENDIAN) |
| 407 |
return (Uint32) ptr[0] << 16 | (Uint32) ptr[1] << 8 | (Uint32) ptr[2]; |
| 408 |
else |
| 409 |
return (Uint32) ptr[0] | (Uint32) ptr[1] << 8 | (Uint32) ptr[2] << 16; |
| 410 |
case 4: |
| 411 |
return *((Uint32*) ptr); |
| 412 |
default: |
| 413 |
// throw an exception? this shouldn't EVER happen. |
| 414 |
return 0; |
| 415 |
} |
| 416 |
unlock(); |
| 417 |
} |
| 418 |
|
| 419 |
void CSurface::setPixel(int x, int y, Uint32 val) |
| 420 |
{ |
| 421 |
lock(); |
| 422 |
|
| 423 |
if (! onSurface(x, y)) |
| 424 |
{ |
| 425 |
// the x coord is out of range |
| 426 |
_THROWEX(ex_illegalArguments, |
| 427 |
"(x, y) does not lie on surface", |
| 428 |
"CSurface", "setPixel", |
| 429 |
"x = " << x << ", y = " << y << ", val = " << val); |
| 430 |
} |
| 431 |
|
| 432 |
Uint8 *ptr = (Uint8*) s->pixels + y * s->pitch + x * s->format->BytesPerPixel; |
| 433 |
|
| 434 |
switch(s->format->BytesPerPixel) |
| 435 |
{ |
| 436 |
case 1: |
| 437 |
*ptr = (Uint8) val; |
| 438 |
break; |
| 439 |
case 2: |
| 440 |
*((Uint16*) ptr) = (Uint16) val; |
| 441 |
break; |
| 442 |
case 3: |
| 443 |
if (SDL_BYTEORDER == SDL_BIG_ENDIAN) |
| 444 |
{ |
| 445 |
ptr[0] = (Uint8) (val >> 16) & 0xFF; |
| 446 |
ptr[1] = (Uint8) (val >> 8) & 0xFF; |
| 447 |
ptr[2] = (Uint8) val & 0xFF; |
| 448 |
} |
| 449 |
else |
| 450 |
{ |
| 451 |
ptr[0] = (Uint8) val & 0xFF; |
| 452 |
ptr[1] = (Uint8) (val >> 8) & 0xFF; |
| 453 |
ptr[2] = (Uint8) (val >> 16) & 0xFF; |
| 454 |
} |
| 455 |
break; |
| 456 |
case 4: |
| 457 |
*((Uint32*) ptr) = val; |
| 458 |
break; |
| 459 |
default: |
| 460 |
break; |
| 461 |
} |
| 462 |
|
| 463 |
unlock(); |
| 464 |
} |
| 465 |
|
| 466 |
void CSurface::setClippingRect(CRect r) |
| 467 |
{ |
| 468 |
SDL_Rect rect = {(Sint16)r.getX(), (Sint16)r.getY(), (Sint16)r.getWidth(), (Sint16)r.getHeight()}; |
| 469 |
SDL_SetClipRect(s, &rect); |
| 470 |
} |
| 471 |
|
| 472 |
void CSurface::resetClippingRect() |
| 473 |
{ |
| 474 |
SDL_SetClipRect(s, NULL); |
| 475 |
} |
| 476 |
|
| 477 |
ostream& operator<<(ostream& os, const CSurface& v) |
| 478 |
{ |
| 479 |
|
| 480 |
return (os << "CSurface @ {" << &v << "}: [width = " << v.width << ", height = " << v.height << ", depth = " << v.depth << |
| 481 |
", Rmask = " << v.Rmask << ", v.Gmask = " << v.Gmask << ", Bmask = " << v.Bmask << |
| 482 |
", Amask = " << v.Amask << ", creationFlags = " << v.creationFlags << |
| 483 |
", locked = " << v.locked << "]"); |
| 484 |
} |