1 // $Id: texture.cpp 1053 2004-05-09 18:08:02Z tobgle $
2 //
3 // SuperTux
4 // Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.de>
5 //
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 // 02111-1307, USA.
20
21 #include <assert.h>
22 #include <iostream>
23 #include <algorithm>
24 #include "SDL.h"
25 #include "SDL_image.h"
26 #include "texture.h"
27 #include "globals.h"
28 #include "setup.h"
29
30 Surface::Surfaces Surface::surfaces;
31
SurfaceData(SDL_Surface * temp,int use_alpha_)32 SurfaceData::SurfaceData(SDL_Surface* temp, int use_alpha_)
33 : type(SURFACE), surface(0), use_alpha(use_alpha_)
34 {
35 // Copy the given surface and make sure that it is not stored in
36 // video memory
37 surface = SDL_CreateRGBSurface(temp->flags & (~SDL_HWSURFACE),
38 temp->w, temp->h,
39 temp->format->BitsPerPixel,
40 temp->format->Rmask,
41 temp->format->Gmask,
42 temp->format->Bmask,
43 temp->format->Amask);
44 if(!surface)
45 st_abort("No memory left.", "");
46 SDL_SetAlpha(temp,0,0);
47 SDL_BlitSurface(temp, NULL, surface, NULL);
48 }
49
SurfaceData(const std::string & file_,int use_alpha_)50 SurfaceData::SurfaceData(const std::string& file_, int use_alpha_)
51 : type(LOAD), surface(0), file(file_), use_alpha(use_alpha_)
52 {}
53
SurfaceData(const std::string & file_,int x_,int y_,int w_,int h_,int use_alpha_)54 SurfaceData::SurfaceData(const std::string& file_, int x_, int y_, int w_, int h_, int use_alpha_)
55 : type(LOAD_PART), surface(0), file(file_), use_alpha(use_alpha_),
56 x(x_), y(y_), w(w_), h(h_)
57 {}
58
~SurfaceData()59 SurfaceData::~SurfaceData()
60 {
61 SDL_FreeSurface(surface);
62 }
63
64 SurfaceImpl*
create()65 SurfaceData::create()
66 {
67 #ifndef NOOPENGL
68 if (use_gl)
69 return create_SurfaceOpenGL();
70 else
71 return create_SurfaceSDL();
72 #else
73 return create_SurfaceSDL();
74 #endif
75 }
76
77 SurfaceSDL*
create_SurfaceSDL()78 SurfaceData::create_SurfaceSDL()
79 {
80 switch(type)
81 {
82 case LOAD:
83 return new SurfaceSDL(file, use_alpha);
84 case LOAD_PART:
85 return new SurfaceSDL(file, x, y, w, h, use_alpha);
86 case SURFACE:
87 return new SurfaceSDL(surface, use_alpha);
88 }
89 assert(0);
90 }
91
92 SurfaceOpenGL*
create_SurfaceOpenGL()93 SurfaceData::create_SurfaceOpenGL()
94 {
95 #ifndef NOOPENGL
96 switch(type)
97 {
98 case LOAD:
99 return new SurfaceOpenGL(file, use_alpha);
100 case LOAD_PART:
101 return new SurfaceOpenGL(file, x, y, w, h, use_alpha);
102 case SURFACE:
103 return new SurfaceOpenGL(surface, use_alpha);
104 }
105 #endif
106 assert(0);
107 }
108
109 #ifndef NOOPENGL
110 /* Quick utility function for texture creation */
power_of_two(int input)111 static int power_of_two(int input)
112 {
113 int value = 1;
114
115 while ( value < input )
116 {
117 value <<= 1;
118 }
119 return value;
120 }
121 #endif
122
Surface(SDL_Surface * surf,int use_alpha)123 Surface::Surface(SDL_Surface* surf, int use_alpha)
124 : data(surf, use_alpha), w(0), h(0)
125 {
126 impl = data.create();
127 if (impl)
128 {
129 w = impl->w;
130 h = impl->h;
131 }
132 surfaces.push_back(this);
133 }
134
Surface(const std::string & file,int use_alpha)135 Surface::Surface(const std::string& file, int use_alpha)
136 : data(file, use_alpha), w(0), h(0)
137 {
138 impl = data.create();
139 if (impl)
140 {
141 w = impl->w;
142 h = impl->h;
143 }
144 surfaces.push_back(this);
145 }
146
Surface(const std::string & file,int x,int y,int w,int h,int use_alpha)147 Surface::Surface(const std::string& file, int x, int y, int w, int h, int use_alpha)
148 : data(file, x, y, w, h, use_alpha), w(0), h(0)
149 {
150 impl = data.create();
151 if (impl)
152 {
153 w = impl->w;
154 h = impl->h;
155 }
156 surfaces.push_back(this);
157 }
158
159 void
reload()160 Surface::reload()
161 {
162 delete impl;
163 impl = data.create();
164 if (impl)
165 {
166 w = impl->w;
167 h = impl->h;
168 }
169 }
170
~Surface()171 Surface::~Surface()
172 {
173 #ifdef DEBUG
174 bool found = false;
175 for(std::list<Surface*>::iterator i = surfaces.begin(); i != surfaces.end();
176 ++i)
177 {
178 if(*i == this)
179 {
180 found = true; break;
181 }
182 }
183 if(!found)
184 printf("Error: Surface freed twice!!!\n");
185 #endif
186 surfaces.remove(this);
187 delete impl;
188 }
189
190 void
reload_all()191 Surface::reload_all()
192 {
193 for(Surfaces::iterator i = surfaces.begin(); i != surfaces.end(); ++i)
194 {
195 (*i)->reload();
196 }
197 }
198
199 void
debug_check()200 Surface::debug_check()
201 {
202 for(Surfaces::iterator i = surfaces.begin(); i != surfaces.end(); ++i)
203 {
204 printf("Surface not freed: T:%d F:%s.\n", (*i)->data.type,
205 (*i)->data.file.c_str());
206 }
207 }
208
209 void
draw(float x,float y,Uint8 alpha,bool update)210 Surface::draw(float x, float y, Uint8 alpha, bool update)
211 {
212 if (impl)
213 {
214 if (impl->draw(x, y, alpha, update) == -2)
215 reload();
216 }
217 }
218
219 void
draw_bg(Uint8 alpha,bool update)220 Surface::draw_bg(Uint8 alpha, bool update)
221 {
222 if (impl)
223 {
224 if (impl->draw_bg(alpha, update) == -2)
225 reload();
226 }
227 }
228
229 void
draw_part(float sx,float sy,float x,float y,float w,float h,Uint8 alpha,bool update)230 Surface::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, bool update)
231 {
232 if (impl)
233 {
234 if (impl->draw_part(sx, sy, x, y, w, h, alpha, update) == -2)
235 reload();
236 }
237 }
238
239 void
draw_stretched(float x,float y,int w,int h,Uint8 alpha,bool update)240 Surface::draw_stretched(float x, float y, int w, int h, Uint8 alpha, bool update)
241 {
242 if (impl)
243 {
244 if (impl->draw_stretched(x, y, w, h, alpha, update) == -2)
245 reload();
246 }
247 }
248
249 void
resize(int w_,int h_)250 Surface::resize(int w_, int h_)
251 {
252 if (impl)
253 {
254 w = w_;
255 h = h_;
256 if (impl->resize(w_,h_) == -2)
257 reload();
258 }
259 }
260
CaptureScreen()261 Surface* Surface::CaptureScreen()
262 {
263 Surface *cap_screen;
264
265 if (!(screen->flags & SDL_OPENGL))
266 {
267 cap_screen = new Surface(SDL_GetVideoSurface(),false);
268 }
269
270 #ifndef NOOPENGL
271 if (use_gl)
272 {
273 SDL_Surface *temp;
274 unsigned char *pixels;
275 int i;
276 temp = SDL_CreateRGBSurface(SDL_SWSURFACE, screen->w, screen->h, 24,
277 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
278 0x000000FF, 0x0000FF00, 0x00FF0000, 0
279 #else
280 0x00FF0000, 0x0000FF00, 0x000000FF, 0
281 #endif
282 );
283 if (temp == NULL)
284 st_abort("Error while trying to capture the screen in OpenGL mode","");
285
286 pixels = (unsigned char*) malloc(3 * screen->w * screen->h);
287 if (pixels == NULL)
288 {
289 SDL_FreeSurface(temp);
290 st_abort("Error while trying to capture the screen in OpenGL mode","");
291 }
292
293 glReadPixels(0, 0, screen->w, screen->h, GL_RGB, GL_UNSIGNED_BYTE, pixels);
294
295 for (i=0; i<screen->h; i++)
296 memcpy(((char *) temp->pixels) + temp->pitch * i, pixels + 3*screen->w * (screen->h-i-1), screen->w*3);
297 free(pixels);
298
299 cap_screen = new Surface(temp,false);
300 SDL_FreeSurface(temp);
301
302 }
303 #endif
304
305 return cap_screen;
306 }
307
308 SDL_Surface*
sdl_surface_part_from_file(const std::string & file,int x,int y,int w,int h,int use_alpha)309 sdl_surface_part_from_file(const std::string& file, int x, int y, int w, int h, int use_alpha)
310 {
311 SDL_Rect src;
312 SDL_Surface * sdl_surface;
313 SDL_Surface * temp;
314 SDL_Surface * conv;
315
316 temp = IMG_Load(file.c_str());
317
318 if (temp == NULL)
319 st_abort("Can't load", file);
320
321 /* Set source rectangle for conv: */
322
323 src.x = x;
324 src.y = y;
325 src.w = w;
326 src.h = h;
327
328 conv = SDL_CreateRGBSurface(temp->flags, w, h, temp->format->BitsPerPixel,
329 temp->format->Rmask,
330 temp->format->Gmask,
331 temp->format->Bmask,
332 temp->format->Amask);
333
334 /* #if SDL_BYTEORDER == SDL_BIG_ENDIAN
335 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
336 #else
337
338 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
339 #endif*/
340
341 SDL_SetAlpha(temp,0,0);
342
343 SDL_BlitSurface(temp, &src, conv, NULL);
344 if(use_alpha == IGNORE_ALPHA && !use_gl)
345 sdl_surface = SDL_DisplayFormat(conv);
346 else
347 sdl_surface = SDL_DisplayFormatAlpha(conv);
348
349 if (sdl_surface == NULL)
350 st_abort("Can't covert to display format", file);
351
352 if (use_alpha == IGNORE_ALPHA && !use_gl)
353 SDL_SetAlpha(sdl_surface, 0, 0);
354
355 SDL_FreeSurface(temp);
356 SDL_FreeSurface(conv);
357
358 return sdl_surface;
359 }
360
361 SDL_Surface*
sdl_surface_from_file(const std::string & file,int use_alpha)362 sdl_surface_from_file(const std::string& file, int use_alpha)
363 {
364 SDL_Surface* sdl_surface;
365 SDL_Surface* temp;
366
367 temp = IMG_Load(file.c_str());
368
369 if (temp == NULL)
370 st_abort("Can't load", file);
371
372 if(use_alpha == IGNORE_ALPHA && !use_gl)
373 sdl_surface = SDL_DisplayFormat(temp);
374 else
375 sdl_surface = SDL_DisplayFormatAlpha(temp);
376
377 if (sdl_surface == NULL)
378 st_abort("Can't covert to display format", file);
379
380 if (use_alpha == IGNORE_ALPHA && !use_gl)
381 SDL_SetAlpha(sdl_surface, 0, 0);
382
383 SDL_FreeSurface(temp);
384
385 return sdl_surface;
386 }
387
388 SDL_Surface*
sdl_surface_from_sdl_surface(SDL_Surface * sdl_surf,int use_alpha)389 sdl_surface_from_sdl_surface(SDL_Surface* sdl_surf, int use_alpha)
390 {
391 SDL_Surface* sdl_surface;
392 Uint32 saved_flags;
393 Uint8 saved_alpha;
394
395 /* Save the alpha blending attributes */
396 saved_flags = sdl_surf->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
397 saved_alpha = sdl_surf->format->alpha;
398 if ( (saved_flags & SDL_SRCALPHA)
399 == SDL_SRCALPHA )
400 {
401 SDL_SetAlpha(sdl_surf, 0, 0);
402 }
403
404 if(use_alpha == IGNORE_ALPHA && !use_gl)
405 sdl_surface = SDL_DisplayFormat(sdl_surf);
406 else
407 sdl_surface = SDL_DisplayFormatAlpha(sdl_surf);
408
409 /* Restore the alpha blending attributes */
410 if ( (saved_flags & SDL_SRCALPHA)
411 == SDL_SRCALPHA )
412 {
413 SDL_SetAlpha(sdl_surface, saved_flags, saved_alpha);
414 }
415
416 if (sdl_surface == NULL)
417 st_abort("Can't covert to display format", "SURFACE");
418
419 if (use_alpha == IGNORE_ALPHA && !use_gl)
420 SDL_SetAlpha(sdl_surface, 0, 0);
421
422 return sdl_surface;
423 }
424
425 //---------------------------------------------------------------------------
426
SurfaceImpl()427 SurfaceImpl::SurfaceImpl()
428 {}
429
~SurfaceImpl()430 SurfaceImpl::~SurfaceImpl()
431 {
432 SDL_FreeSurface(sdl_surface);
433 }
434
get_sdl_surface() const435 SDL_Surface* SurfaceImpl::get_sdl_surface() const
436 {
437 return sdl_surface;
438 }
439
resize(int w_,int h_)440 int SurfaceImpl::resize(int w_, int h_)
441 {
442 w = w_;
443 h = h_;
444 SDL_Rect dest;
445 dest.x = 0;
446 dest.y = 0;
447 dest.w = w;
448 dest.h = h;
449 int ret = SDL_SoftStretch(sdl_surface, NULL,
450 sdl_surface, &dest);
451 return ret;
452 }
453
454 #ifndef NOOPENGL
SurfaceOpenGL(SDL_Surface * surf,int use_alpha)455 SurfaceOpenGL::SurfaceOpenGL(SDL_Surface* surf, int use_alpha)
456 {
457 sdl_surface = sdl_surface_from_sdl_surface(surf, use_alpha);
458 create_gl(sdl_surface,&gl_texture);
459
460 w = sdl_surface->w;
461 h = sdl_surface->h;
462 }
463
SurfaceOpenGL(const std::string & file,int use_alpha)464 SurfaceOpenGL::SurfaceOpenGL(const std::string& file, int use_alpha)
465 {
466 sdl_surface = sdl_surface_from_file(file, use_alpha);
467 create_gl(sdl_surface,&gl_texture);
468
469 w = sdl_surface->w;
470 h = sdl_surface->h;
471 }
472
SurfaceOpenGL(const std::string & file,int x,int y,int w,int h,int use_alpha)473 SurfaceOpenGL::SurfaceOpenGL(const std::string& file, int x, int y, int w, int h, int use_alpha)
474 {
475 sdl_surface = sdl_surface_part_from_file(file,x,y,w,h,use_alpha);
476 create_gl(sdl_surface, &gl_texture);
477
478 w = sdl_surface->w;
479 h = sdl_surface->h;
480 }
481
~SurfaceOpenGL()482 SurfaceOpenGL::~SurfaceOpenGL()
483 {
484 glDeleteTextures(1, &gl_texture);
485 }
486
487 void
create_gl(SDL_Surface * surf,GLuint * tex)488 SurfaceOpenGL::create_gl(SDL_Surface * surf, GLuint * tex)
489 {
490 Uint32 saved_flags;
491 Uint8 saved_alpha;
492 int w, h;
493 SDL_Surface *conv;
494
495 w = power_of_two(surf->w);
496 h = power_of_two(surf->h),
497
498 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
499 conv = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, surf->format->BitsPerPixel,
500 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
501 #else
502 conv = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, surf->format->BitsPerPixel,
503 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
504 #endif
505
506 /* Save the alpha blending attributes */
507 saved_flags = surf->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
508 saved_alpha = surf->format->alpha;
509 if ( (saved_flags & SDL_SRCALPHA)
510 == SDL_SRCALPHA )
511 {
512 SDL_SetAlpha(surf, 0, 0);
513 }
514
515 SDL_BlitSurface(surf, 0, conv, 0);
516
517 /* Restore the alpha blending attributes */
518 if ( (saved_flags & SDL_SRCALPHA)
519 == SDL_SRCALPHA )
520 {
521 SDL_SetAlpha(surf, saved_flags, saved_alpha);
522 }
523
524 glGenTextures(1, &*tex);
525 glBindTexture(GL_TEXTURE_2D , *tex);
526 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
527 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
528 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
529 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
530 glPixelStorei(GL_UNPACK_ROW_LENGTH, conv->pitch / conv->format->BytesPerPixel);
531 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, conv->pixels);
532 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
533
534 SDL_FreeSurface(conv);
535 }
536
537 int
draw(float x,float y,Uint8 alpha,bool update)538 SurfaceOpenGL::draw(float x, float y, Uint8 alpha, bool update)
539 {
540 float pw = power_of_two(w);
541 float ph = power_of_two(h);
542
543 glEnable(GL_TEXTURE_2D);
544 glEnable(GL_BLEND);
545 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
546
547 glColor4ub(alpha, alpha, alpha, alpha);
548
549 glBindTexture(GL_TEXTURE_2D, gl_texture);
550
551 glBegin(GL_QUADS);
552 glTexCoord2f(0, 0);
553 glVertex2f(x, y);
554 glTexCoord2f((float)w / pw, 0);
555 glVertex2f((float)w+x, y);
556 glTexCoord2f((float)w / pw, (float)h / ph); glVertex2f((float)w+x, (float)h+y);
557 glTexCoord2f(0, (float)h / ph);
558 glVertex2f(x, (float)h+y);
559 glEnd();
560
561 glDisable(GL_TEXTURE_2D);
562 glDisable(GL_BLEND);
563
564 (void) update; // avoid compiler warning
565
566 return 0;
567 }
568
569 int
draw_bg(Uint8 alpha,bool update)570 SurfaceOpenGL::draw_bg(Uint8 alpha, bool update)
571 {
572 float pw = power_of_two(w);
573 float ph = power_of_two(h);
574
575 glColor3ub(alpha, alpha, alpha);
576
577 glEnable(GL_TEXTURE_2D);
578 glBindTexture(GL_TEXTURE_2D, gl_texture);
579
580 glBegin(GL_QUADS);
581 glTexCoord2f(0, 0);
582 glVertex2f(0, 0);
583 glTexCoord2f((float)w / pw, 0);
584 glVertex2f(screen->w, 0);
585 glTexCoord2f((float)w / pw, (float)h / ph);
586 glVertex2f(screen->w, screen->h);
587 glTexCoord2f(0, (float)h / ph);
588 glVertex2f(0, screen->h);
589 glEnd();
590
591 glDisable(GL_TEXTURE_2D);
592
593 (void) update; // avoid compiler warning
594
595 return 0;
596 }
597
598 int
draw_part(float sx,float sy,float x,float y,float w,float h,Uint8 alpha,bool update)599 SurfaceOpenGL::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, bool update)
600 {
601 float pw = power_of_two(int(this->w));
602 float ph = power_of_two(int(this->h));
603
604 glBindTexture(GL_TEXTURE_2D, gl_texture);
605
606 glEnable(GL_BLEND);
607 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
608
609 glColor4ub(alpha, alpha, alpha, alpha);
610
611 glEnable(GL_TEXTURE_2D);
612
613
614 glBegin(GL_QUADS);
615 glTexCoord2f(sx / pw, sy / ph);
616 glVertex2f(x, y);
617 glTexCoord2f((float)(sx + w) / pw, sy / ph);
618 glVertex2f(w+x, y);
619 glTexCoord2f((sx+w) / pw, (sy+h) / ph);
620 glVertex2f(w +x, h+y);
621 glTexCoord2f(sx / pw, (float)(sy+h) / ph);
622 glVertex2f(x, h+y);
623 glEnd();
624
625 glDisable(GL_TEXTURE_2D);
626 glDisable(GL_BLEND);
627
628 (void) update; // avoid warnings
629 return 0;
630 }
631
632 int
draw_stretched(float x,float y,int sw,int sh,Uint8 alpha,bool update)633 SurfaceOpenGL::draw_stretched(float x, float y, int sw, int sh, Uint8 alpha, bool update)
634 {
635 float pw = power_of_two(int(this->w));
636 float ph = power_of_two(int(this->h));
637
638 glBindTexture(GL_TEXTURE_2D, gl_texture);
639
640 glEnable(GL_BLEND);
641 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
642
643 glColor4ub(alpha, alpha, alpha, alpha);
644
645 glEnable(GL_TEXTURE_2D);
646
647
648 glBegin(GL_QUADS);
649 glTexCoord2f(0, 0);
650 glVertex2f(x, y);
651 glTexCoord2f((float)w / pw, 0);
652 glVertex2f(sw+x, y);
653 glTexCoord2f((float)w / pw, (float)h / ph); glVertex2f((float)sw+x, (float)sh+y);
654 glVertex2f(sw +x, sh+y);
655 glTexCoord2f(0, (float)h / ph);
656 glVertex2f(x, sh+y);
657 glEnd();
658
659 glDisable(GL_TEXTURE_2D);
660 glDisable(GL_BLEND);
661
662 (void) update; // avoid warnings
663 return 0;
664 }
665
666 #endif
667
SurfaceSDL(SDL_Surface * surf,int use_alpha)668 SurfaceSDL::SurfaceSDL(SDL_Surface* surf, int use_alpha)
669 {
670 sdl_surface = sdl_surface_from_sdl_surface(surf, use_alpha);
671 w = sdl_surface->w;
672 h = sdl_surface->h;
673 }
674
SurfaceSDL(const std::string & file,int use_alpha)675 SurfaceSDL::SurfaceSDL(const std::string& file, int use_alpha)
676 {
677 sdl_surface = sdl_surface_from_file(file, use_alpha);
678 w = sdl_surface->w;
679 h = sdl_surface->h;
680 }
681
SurfaceSDL(const std::string & file,int x,int y,int w,int h,int use_alpha)682 SurfaceSDL::SurfaceSDL(const std::string& file, int x, int y, int w, int h, int use_alpha)
683 {
684 sdl_surface = sdl_surface_part_from_file(file, x, y, w, h, use_alpha);
685 w = sdl_surface->w;
686 h = sdl_surface->h;
687 }
688
689 int
draw(float x,float y,Uint8 alpha,bool update)690 SurfaceSDL::draw(float x, float y, Uint8 alpha, bool update)
691 {
692 SDL_Rect dest;
693
694 dest.x = (int)x;
695 dest.y = (int)y;
696 dest.w = w;
697 dest.h = h;
698
699 if(alpha != 255)
700 {
701 /* Create a Surface, make it using colorkey, blit surface into temp, apply alpha
702 to temp sur, blit the temp into the screen */
703 /* Note: this has to be done, since SDL doesn't allow to set alpha to surfaces that
704 already have an alpha mask yet... */
705
706 SDL_Surface* sdl_surface_copy = SDL_CreateRGBSurface (sdl_surface->flags,
707 sdl_surface->w, sdl_surface->h, sdl_surface->format->BitsPerPixel,
708 sdl_surface->format->Rmask, sdl_surface->format->Gmask,
709 sdl_surface->format->Bmask,
710 0);
711 int colorkey = SDL_MapRGB(sdl_surface_copy->format, 255, 0, 255);
712 SDL_FillRect(sdl_surface_copy, NULL, colorkey);
713 SDL_SetColorKey(sdl_surface_copy, SDL_SRCCOLORKEY, colorkey);
714
715
716 SDL_BlitSurface(sdl_surface, NULL, sdl_surface_copy, NULL);
717 SDL_SetAlpha(sdl_surface_copy ,SDL_SRCALPHA,alpha);
718
719 int ret = SDL_BlitSurface(sdl_surface_copy, NULL, screen, &dest);
720
721 if (update == UPDATE)
722 SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h);
723
724 SDL_FreeSurface (sdl_surface_copy);
725 return ret;
726 }
727
728 int ret = SDL_BlitSurface(sdl_surface, NULL, screen, &dest);
729
730 if (update == UPDATE)
731 SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h);
732
733 return ret;
734 }
735
736 int
draw_bg(Uint8 alpha,bool update)737 SurfaceSDL::draw_bg(Uint8 alpha, bool update)
738 {
739 SDL_Rect dest;
740
741 dest.x = 0;
742 dest.y = 0;
743 dest.w = screen->w;
744 dest.h = screen->h;
745
746 if(alpha != 255)
747 {
748 /* Create a Surface, make it using colorkey, blit surface into temp, apply alpha
749 to temp sur, blit the temp into the screen */
750 /* Note: this has to be done, since SDL doesn't allow to set alpha to surfaces that
751 already have an alpha mask yet... */
752
753 SDL_Surface* sdl_surface_copy = SDL_CreateRGBSurface (sdl_surface->flags,
754 sdl_surface->w, sdl_surface->h, sdl_surface->format->BitsPerPixel,
755 sdl_surface->format->Rmask, sdl_surface->format->Gmask,
756 sdl_surface->format->Bmask,
757 0);
758 int colorkey = SDL_MapRGB(sdl_surface_copy->format, 255, 0, 255);
759 SDL_FillRect(sdl_surface_copy, NULL, colorkey);
760 SDL_SetColorKey(sdl_surface_copy, SDL_SRCCOLORKEY, colorkey);
761
762
763 SDL_BlitSurface(sdl_surface, NULL, sdl_surface_copy, NULL);
764 SDL_SetAlpha(sdl_surface_copy ,SDL_SRCALPHA,alpha);
765
766 int ret = SDL_BlitSurface(sdl_surface_copy, NULL, screen, &dest);
767
768 if (update == UPDATE)
769 SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h);
770
771 SDL_FreeSurface (sdl_surface_copy);
772 return ret;
773 }
774
775 int ret = SDL_SoftStretch(sdl_surface, NULL, screen, &dest);
776
777 if (update == UPDATE)
778 SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h);
779
780 return ret;
781 }
782
783 int
draw_part(float sx,float sy,float x,float y,float w,float h,Uint8 alpha,bool update)784 SurfaceSDL::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, bool update)
785 {
786 SDL_Rect src, dest;
787
788 src.x = (int)sx;
789 src.y = (int)sy;
790 src.w = (int)w;
791 src.h = (int)h;
792
793 dest.x = (int)x;
794 dest.y = (int)y;
795 dest.w = (int)w;
796 dest.h = (int)h;
797
798 if(alpha != 255)
799 {
800 /* Create a Surface, make it using colorkey, blit surface into temp, apply alpha
801 to temp sur, blit the temp into the screen */
802 /* Note: this has to be done, since SDL doesn't allow to set alpha to surfaces that
803 already have an alpha mask yet... */
804
805 SDL_Surface* sdl_surface_copy = SDL_CreateRGBSurface (sdl_surface->flags,
806 sdl_surface->w, sdl_surface->h, sdl_surface->format->BitsPerPixel,
807 sdl_surface->format->Rmask, sdl_surface->format->Gmask,
808 sdl_surface->format->Bmask,
809 0);
810 int colorkey = SDL_MapRGB(sdl_surface_copy->format, 255, 0, 255);
811 SDL_FillRect(sdl_surface_copy, NULL, colorkey);
812 SDL_SetColorKey(sdl_surface_copy, SDL_SRCCOLORKEY, colorkey);
813
814
815 SDL_BlitSurface(sdl_surface, NULL, sdl_surface_copy, NULL);
816 SDL_SetAlpha(sdl_surface_copy ,SDL_SRCALPHA,alpha);
817
818 int ret = SDL_BlitSurface(sdl_surface_copy, NULL, screen, &dest);
819
820 if (update == UPDATE)
821 SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h);
822
823 SDL_FreeSurface (sdl_surface_copy);
824 return ret;
825 }
826
827 int ret = SDL_BlitSurface(sdl_surface, &src, screen, &dest);
828
829 if (update == UPDATE)
830 update_rect(screen, dest.x, dest.y, dest.w, dest.h);
831
832 return ret;
833 }
834
835 int
draw_stretched(float x,float y,int sw,int sh,Uint8 alpha,bool update)836 SurfaceSDL::draw_stretched(float x, float y, int sw, int sh, Uint8 alpha, bool update)
837 {
838 SDL_Rect dest;
839
840 dest.x = (int)x;
841 dest.y = (int)y;
842 dest.w = (int)sw;
843 dest.h = (int)sh;
844
845 if(alpha != 255)
846 SDL_SetAlpha(sdl_surface ,SDL_SRCALPHA,alpha);
847
848
849 SDL_Surface* sdl_surface_copy = SDL_CreateRGBSurface (sdl_surface->flags,
850 sw, sh, sdl_surface->format->BitsPerPixel,
851 sdl_surface->format->Rmask, sdl_surface->format->Gmask,
852 sdl_surface->format->Bmask,
853 0);
854
855 SDL_BlitSurface(sdl_surface, NULL, sdl_surface_copy, NULL);
856 SDL_SoftStretch(sdl_surface_copy, NULL, sdl_surface_copy, &dest);
857
858 int ret = SDL_BlitSurface(sdl_surface_copy,NULL,screen,&dest);
859 SDL_FreeSurface(sdl_surface_copy);
860
861 if (update == UPDATE)
862 update_rect(screen, dest.x, dest.y, dest.w, dest.h);
863
864 return ret;
865 }
866
~SurfaceSDL()867 SurfaceSDL::~SurfaceSDL()
868 {}
869
870 /* EOF */
871