Disabled some more code, game now starts up and plays music, no graphics yet, just...
[supertux.git] / src / video / sdl / sdl_texture.cpp
1 //  SuperTux
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3 //
4 //  This program is free software: you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation, either version 3 of the License, or
7 //  (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 #include <config.h>
18
19 #include "supertux/gameconfig.hpp"
20 #include "supertux/globals.hpp"
21 #include "video/color.hpp"
22 #include "video/sdl/sdl_texture.hpp"
23 #include "video/sdl/sdl_renderer.hpp"
24 #include "util/log.hpp"
25 #include "math/random_generator.hpp"
26
27 #include <assert.h>
28
29 #include <SDL.h>
30
31 namespace {
32 #define BILINEAR
33 #ifdef OLD_SDL1
34 static Uint32 get_pixel_mapping (SDL_Surface *src, void *pixel)
35 {
36   Uint32 mapped = 0;
37
38   switch (src->format->BytesPerPixel)
39   {
40     case 1:
41       mapped = *((Uint8 *) pixel);
42       break;
43     case 2:
44       mapped = *((Uint16 *) pixel);
45       break;
46     case 3:
47     {
48       Uint8 *tmp = (Uint8 *) pixel;
49 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
50       mapped |= tmp[0] << 16;
51       mapped |= tmp[1] << 8;
52       mapped |= tmp[2] << 0;
53 #else
54       mapped |= tmp[0] << 0;
55       mapped |= tmp[1] << 8;
56       mapped |= tmp[2] << 16;
57 #endif
58       break;
59     }
60     case 4:
61       mapped = *((Uint32 *) pixel);
62       break;
63
64     default:
65       log_warning << "Unknown BytesPerPixel value: "
66         << src->format->BytesPerPixel << std::endl;
67       mapped = 0;
68   } /* switch (bpp) */
69
70   return (mapped);
71 } /* Uint32 get_pixel_mapping */
72
73 static Uint32 get_random_color (SDL_Surface *src)
74 {
75   Uint32 r;
76
77   r = (Uint32) graphicsRandom.rand ();
78   /* rand() returns 31bit random numbers. So call it twice to get full 32 bit. */
79   r <<= 1;
80   r |= (Uint32) graphicsRandom.rand ();
81
82   switch (src->format->BytesPerPixel)
83   {
84     case 1:
85       r &= 0x000000ff;
86       break;
87
88     case 2:
89       r &= 0x0000ffff;
90       break;
91
92     case 3:
93       r &= 0x0000ffff;
94       break;
95   }
96
97   return (r);
98 } /* Uint32 get_random_color */
99
100 static bool color_is_used (SDL_Surface *src, Uint32 color)
101 {
102   if(SDL_MUSTLOCK(src))
103     SDL_LockSurface(src);
104
105   for(int y = 0; y < src->h; y++) {
106     for(int x = 0; x < src->w; x++) {
107       Uint8 *pixel = (Uint8 *) src->pixels
108         + (y * src->pitch) + (x * src->format->BytesPerPixel);
109       Uint32 mapped = get_pixel_mapping (src, pixel);
110
111       if (color == mapped)
112         return (true);
113     }
114   }
115
116   return (false);
117 } /* bool color_is_used */
118
119 static Uint32 get_unused_color (SDL_Surface *src)
120 {
121   Uint32 random_color;
122
123   do
124   {
125     random_color = get_random_color (src);
126   } while (color_is_used (src, random_color));
127
128   return (random_color);
129 } /* Uint32 get_unused_color */
130 #endif
131
132 #ifdef NAIVE
133 SDL_Surface *scale(SDL_Surface *src, int numerator, int denominator)
134 {
135   if(numerator == denominator)
136   {
137     src->refcount++;
138     return src;
139   }
140   else
141   {
142     SDL_Surface *dst = SDL_CreateRGBSurface(src->flags, src->w * numerator / denominator, src->h * numerator / denominator, src->format->BitsPerPixel, src->format->Rmask,  src->format->Gmask, src->format->Bmask, src->format->Amask);
143     int bpp = dst->format->BytesPerPixel;
144     if(SDL_MUSTLOCK(src))
145     {
146       SDL_LockSurface(src);
147     }
148     if(SDL_MUSTLOCK(dst))
149     {
150       SDL_LockSurface(dst);
151     }
152     for(int y = 0;y < dst->h;y++) {
153       for(int x = 0;x < dst->w;x++) {
154         Uint8 *srcpixel = (Uint8 *) src->pixels + (y * denominator / numerator) * src->pitch + (x * denominator / numerator) * bpp;
155         Uint8 *dstpixel = (Uint8 *) dst->pixels + y * dst->pitch + x * bpp;
156         switch(bpp) {
157           case 4:
158             dstpixel[3] = srcpixel[3];
159           case 3:
160             dstpixel[2] = srcpixel[2];
161           case 2:
162             dstpixel[1] = srcpixel[1];
163           case 1:
164             dstpixel[0] = srcpixel[0];
165         }
166       }
167     }
168     if(SDL_MUSTLOCK(dst))
169     {
170       SDL_UnlockSurface(dst);
171     }
172     if(SDL_MUSTLOCK(src))
173     {
174       SDL_UnlockSurface(src);
175     }
176     if(!src->format->Amask)
177     {
178       if(src->flags & SDL_SRCALPHA)
179       {
180         SDL_SetAlpha(dst, SDL_SRCALPHA | SDL_RLEACCEL, src->format->alpha);
181       }
182       if(src->flags & SDL_SRCCOLORKEY)
183       {
184         SDL_SetColorKey(dst, SDL_SRCCOLORKEY | SDL_RLEACCEL, src->format->colorkey);
185       }
186     }
187     return dst;
188   }
189 } // namespace
190 #endif
191
192 #ifdef BILINEAR
193 #ifdef OLD_SDL1
194 void getpixel(SDL_Surface *src, int srcx, int srcy, Uint8 color[4])
195 {
196   int bpp = src->format->BytesPerPixel;
197   if(srcx == src->w)
198   {
199     srcx--;
200   }
201   if(srcy == src->h)
202   {
203     srcy--;
204   }
205   Uint8 *srcpixel = (Uint8 *) src->pixels + srcy * src->pitch + srcx * bpp;
206   Uint32 mapped = get_pixel_mapping (src, srcpixel);
207   SDL_GetRGBA(mapped, src->format, &color[0], &color[1], &color[2], &color[3]);
208 }
209
210 void merge(Uint8 color[4], Uint8 color0[4], Uint8 color1[4], int rem, int total)
211 {
212   color[0] = (color0[0] * (total - rem) + color1[0] * rem) / total;
213   color[1] = (color0[1] * (total - rem) + color1[1] * rem) / total;
214   color[2] = (color0[2] * (total - rem) + color1[2] * rem) / total;
215   color[3] = (color0[3] * (total - rem) + color1[3] * rem) / total;
216 }
217
218 SDL_Surface *scale(SDL_Surface *src, int numerator, int denominator)
219 {
220   if(numerator == denominator)
221   {
222     src->refcount++;
223     return src;
224   }
225   else
226   {
227     SDL_Surface *dst = SDL_CreateRGBSurface(src->flags, src->w * numerator / denominator, src->h * numerator / denominator, src->format->BitsPerPixel, src->format->Rmask,  src->format->Gmask, src->format->Bmask, src->format->Amask);
228     int bpp = dst->format->BytesPerPixel;
229     if(SDL_MUSTLOCK(src))
230     {
231       SDL_LockSurface(src);
232     }
233     if(SDL_MUSTLOCK(dst))
234     {
235       SDL_LockSurface(dst);
236     }
237     for(int y = 0;y < dst->h;y++) {
238       for(int x = 0;x < dst->w;x++) {
239         int srcx = x * denominator / numerator;
240         int srcy = y * denominator / numerator;
241         Uint8 color00[4], color01[4], color10[4], color11[4];
242         getpixel(src, srcx, srcy, color00);
243         getpixel(src, srcx + 1, srcy, color01);
244         getpixel(src, srcx, srcy + 1, color10);
245         getpixel(src, srcx + 1, srcy + 1, color11);
246         Uint8 color0[4], color1[4], color[4];
247         int remx = x * denominator % numerator;
248         merge(color0, color00, color01, remx, numerator);
249         merge(color1, color10, color11, remx, numerator);
250         int remy = y * denominator % numerator;
251         merge(color, color0, color1, remy, numerator);
252         Uint8 *dstpixel = (Uint8 *) dst->pixels + y * dst->pitch + x * bpp;
253         Uint32 mapped = SDL_MapRGBA(dst->format, color[0], color[1], color[2], color[3]);
254         switch(bpp) {
255           case 1:
256             *dstpixel = mapped;
257             break;
258           case 2:
259             *(Uint16 *)dstpixel = mapped;
260             break;
261           case 3:
262 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
263             dstpixel[0] = (mapped >> 16) & 0xff;
264             dstpixel[1] = (mapped >> 8) & 0xff;
265             dstpixel[2] = (mapped >> 0) & 0xff;
266 #else
267             dstpixel[0] = (mapped >> 0) & 0xff;
268             dstpixel[1] = (mapped >> 8) & 0xff;
269             dstpixel[2] = (mapped >> 16) & 0xff;
270 #endif
271             break;
272           case 4:
273             *(Uint32 *)dstpixel = mapped;
274             break;
275         }
276       }
277     }
278     if(SDL_MUSTLOCK(dst))
279     {
280       SDL_UnlockSurface(dst);
281     }
282     if(SDL_MUSTLOCK(src))
283     {
284       SDL_UnlockSurface(src);
285     }
286     if(!src->format->Amask)
287     {
288 #ifdef OLD_SDL1
289       if(src->flags & SDL_SRCALPHA)
290       {
291         SDL_SetAlpha(dst, SDL_SRCALPHA | SDL_RLEACCEL, src->format->alpha);
292       }
293       if(src->flags & SDL_SRCCOLORKEY)
294       {
295         SDL_SetColorKey(dst, SDL_SRCCOLORKEY | SDL_RLEACCEL, src->format->colorkey);
296       }
297 #endif
298     }
299     return dst;
300   }
301 }
302 #endif
303
304 SDL_Surface *horz_flip(SDL_Surface *src)
305 {
306   SDL_Surface *dst = SDL_CreateRGBSurface(src->flags, src->w, src->h, src->format->BitsPerPixel, src->format->Rmask,  src->format->Gmask, src->format->Bmask, src->format->Amask);
307   int bpp = dst->format->BytesPerPixel;
308   if(SDL_MUSTLOCK(src))
309   {
310     SDL_LockSurface(src);
311   }
312   if(SDL_MUSTLOCK(dst))
313   {
314     SDL_LockSurface(dst);
315   }
316   for(int y = 0;y < dst->h;y++) {
317     for(int x = 0;x < dst->w;x++) {
318       Uint8 *srcpixel = (Uint8 *) src->pixels + y * src->pitch + x * bpp;
319       Uint8 *dstpixel = (Uint8 *) dst->pixels + y * dst->pitch + (dst->w - x - 1) * bpp;
320       switch(bpp) {
321         case 4:
322           dstpixel[3] = srcpixel[3];
323         case 3:
324           dstpixel[2] = srcpixel[2];
325         case 2:
326           dstpixel[1] = srcpixel[1];
327         case 1:
328           dstpixel[0] = srcpixel[0];
329       }
330     }
331   }
332   if(SDL_MUSTLOCK(dst))
333   {
334     SDL_UnlockSurface(dst);
335   }
336   if(SDL_MUSTLOCK(src))
337   {
338     SDL_UnlockSurface(src);
339   }
340   if(!src->format->Amask)
341   {
342 #ifdef OLD_SDL1
343     if(src->flags & SDL_SRCALPHA)
344     {
345       SDL_SetAlpha(dst, SDL_SRCALPHA | SDL_RLEACCEL, src->format->alpha);
346     }
347     if(src->flags & SDL_SRCCOLORKEY)
348     {
349       SDL_SetColorKey(dst, SDL_SRCCOLORKEY | SDL_RLEACCEL, src->format->colorkey);
350     }
351 #endif
352   }
353   return dst;
354 }
355
356 SDL_Surface *vert_flip(SDL_Surface *src)
357 {
358   SDL_Surface *dst = SDL_CreateRGBSurface(src->flags, src->w, src->h, src->format->BitsPerPixel, src->format->Rmask,  src->format->Gmask, src->format->Bmask, src->format->Amask);
359   int bpp = dst->format->BytesPerPixel;
360   if(SDL_MUSTLOCK(src))
361   {
362     SDL_LockSurface(src);
363   }
364   if(SDL_MUSTLOCK(dst))
365   {
366     SDL_LockSurface(dst);
367   }
368   for(int y = 0;y < dst->h;y++) {
369     for(int x = 0;x < dst->w;x++) {
370       Uint8 *srcpixel = (Uint8 *) src->pixels + y * src->pitch + x * bpp;
371       Uint8 *dstpixel = (Uint8 *) dst->pixels + (dst->h - y - 1) * dst->pitch + x * bpp;
372       switch(bpp) {
373         case 4:
374           dstpixel[3] = srcpixel[3];
375         case 3:
376           dstpixel[2] = srcpixel[2];
377         case 2:
378           dstpixel[1] = srcpixel[1];
379         case 1:
380           dstpixel[0] = srcpixel[0];
381       }
382     }
383   }
384   if(SDL_MUSTLOCK(dst))
385   {
386     SDL_UnlockSurface(dst);
387   }
388   if(SDL_MUSTLOCK(src))
389   {
390     SDL_UnlockSurface(src);
391   }
392 #ifdef OLD_SDL1
393   if(!src->format->Amask)
394   {
395     if(src->flags & SDL_SRCALPHA)
396     {
397       SDL_SetAlpha(dst, SDL_SRCALPHA | SDL_RLEACCEL, src->format->alpha);
398     }
399     if(src->flags & SDL_SRCCOLORKEY)
400     {
401       SDL_SetColorKey(dst, SDL_SRCCOLORKEY | SDL_RLEACCEL, src->format->colorkey);
402     }
403   }
404 #endif
405   return dst;
406 }
407
408 SDL_Surface *colorize(SDL_Surface *src, const Color &color)
409 {
410   // FIXME: This is really slow
411   SDL_Surface *dst = SDL_CreateRGBSurface(src->flags, src->w, src->h, src->format->BitsPerPixel, src->format->Rmask,  src->format->Gmask, src->format->Bmask, src->format->Amask);
412   int bpp = dst->format->BytesPerPixel;
413   if(SDL_MUSTLOCK(src))
414   {
415     SDL_LockSurface(src);
416   }
417   if(SDL_MUSTLOCK(dst))
418   {
419     SDL_LockSurface(dst);
420   }
421   for(int y = 0;y < dst->h;y++) {
422     for(int x = 0;x < dst->w;x++) {
423       Uint8 *srcpixel = (Uint8 *) src->pixels + y * src->pitch + x * bpp;
424       Uint8 *dstpixel = (Uint8 *) dst->pixels + y * dst->pitch + x * bpp;
425       Uint32 mapped = 0;
426       switch(bpp) {
427         case 1:
428           mapped = *srcpixel;
429           break;
430         case 2:
431           mapped = *(Uint16 *)srcpixel;
432           break;
433         case 3:
434 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
435           mapped |= srcpixel[0] << 16;
436           mapped |= srcpixel[1] << 8;
437           mapped |= srcpixel[2] << 0;
438 #else
439           mapped |= srcpixel[0] << 0;
440           mapped |= srcpixel[1] << 8;
441           mapped |= srcpixel[2] << 16;
442 #endif
443           break;
444         case 4:
445           mapped = *(Uint32 *)srcpixel;
446           break;
447       }
448 #ifdef OLD_SDL1
449       if(src->format->Amask || !(src->flags & SDL_SRCCOLORKEY) || mapped != src->format->colorkey)
450       {
451         assert(color.red != 1.0 || color.green != 1.0 || color.blue != 1.0);
452         int red = (int) (color.red * 256);
453         int green = (int) (color.green * 256);
454         int blue = (int) (color.blue * 256);
455
456         Uint8 r, g, b, a;
457         SDL_GetRGBA(mapped, src->format, &r, &g, &b, &a);
458         mapped = SDL_MapRGBA(dst->format, (r * red) >> 8, (g * green) >> 8, (b * blue) >> 8, a);
459       }
460 #endif
461       switch(bpp) {
462         case 1:
463           *dstpixel = mapped;
464           break;
465         case 2:
466           *(Uint16 *)dstpixel = mapped;
467           break;
468         case 3:
469 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
470           dstpixel[0] = (mapped >> 16) & 0xff;
471           dstpixel[1] = (mapped >> 8) & 0xff;
472           dstpixel[2] = (mapped >> 0) & 0xff;
473 #else
474           dstpixel[0] = (mapped >> 0) & 0xff;
475           dstpixel[1] = (mapped >> 8) & 0xff;
476           dstpixel[2] = (mapped >> 16) & 0xff;
477 #endif
478           break;
479         case 4:
480           *(Uint32 *)dstpixel = mapped;
481           break;
482       }
483     }
484   }
485   if(SDL_MUSTLOCK(dst))
486   {
487     SDL_UnlockSurface(dst);
488   }
489   if(SDL_MUSTLOCK(src))
490   {
491     SDL_UnlockSurface(src);
492   }
493   if(!src->format->Amask)
494   {
495 #ifdef OLD_SDL1
496     if(src->flags & SDL_SRCALPHA)
497     {
498       SDL_SetAlpha(dst, SDL_SRCALPHA | SDL_RLEACCEL, src->format->alpha);
499     }
500     if(src->flags & SDL_SRCCOLORKEY)
501     {
502       SDL_SetColorKey(dst, SDL_SRCCOLORKEY | SDL_RLEACCEL, src->format->colorkey);
503     }
504 #endif
505   }
506   return dst;
507 }
508 #endif
509
510 /** Optimizes a SDL_Surface surface and returns it in the "display format". If
511  *  the surface does not have an alpha channel, simply calls
512  *  "SDL_DisplayFormat". If the surface has an alpha channel, examines all the
513  *  pixels. If in fact semi-transparent pixels are found, calls
514  *  "SDL_DisplayFormatAlpha". If only fully transparent and fully opaque pixels
515  *  are found, converts the surface to a 1-bit alpha surface with
516  *  "SDL_SetColorKey". */
517 SDL_Surface *optimize(SDL_Surface *src)
518 {
519 #ifdef OLD_SDL1
520   bool have_transparent = false;
521   bool have_semi_trans = false;
522   bool have_opaque = false;
523
524   if(!src->format->Amask)
525     return SDL_DisplayFormat(src);
526
527   if(SDL_MUSTLOCK(src))
528     SDL_LockSurface(src);
529
530   /* Iterate over all the pixels and record which ones we found. */
531   for(int y = 0; y < src->h; y++) {
532     for(int x = 0; x < src->w; x++) {
533       Uint8 *pixel = (Uint8 *) src->pixels
534         + (y * src->pitch) + (x * src->format->BytesPerPixel);
535       Uint32 mapped = get_pixel_mapping (src, pixel);
536       Uint8 red, green, blue, alpha;
537       SDL_GetRGBA(mapped, src->format, &red, &green, &blue, &alpha);
538
539       if (alpha < 16)
540         have_transparent = true;
541       else if (alpha > 240)
542         have_opaque = true;
543       else
544         have_semi_trans = true;
545     } /* for (x) */
546   } /* for (y) */
547
548   if(SDL_MUSTLOCK(src))
549     SDL_UnlockSurface(src);
550
551   if (have_semi_trans)
552     return SDL_DisplayFormatAlpha(src);
553
554   if (!have_transparent /* && !have_semi_trans */)
555     return SDL_DisplayFormat(src);
556
557   /* The surface is totally transparent. We shouldn't return a surface at all,
558    * but since the calling code can't cope with that, use the alpha channel in
559    * this case. */
560   if (!have_opaque /* && !have_semi_trans */)
561     return SDL_DisplayFormatAlpha(src);
562
563   /* If we get here, the surface has fully transparent pixels and fully opaque
564    * pixels, but no semi-transparent pixels. We can therefore use a one bit
565    * transparency, which is pretty fast to draw. This code path is a bit bulky
566    * and rarely used (~25 surfaces when starting the game and entering a
567    * level), so it could be removed for readabilities sake. -octo */
568
569   /* Create a new surface without alpha channel */
570   SDL_Surface *dst = SDL_CreateRGBSurface(src->flags & ~(SDL_SRCALPHA),
571       src->w, src->h, src->format->BitsPerPixel,
572       src->format->Rmask,  src->format->Gmask, src->format->Bmask, /* Amask = */ 0);
573   /* Get a color that's not in the source surface. It is used to mark
574    * transparent pixels. There's a possible race condition: Maybe we should
575    * lock the surface before calling this function and add a "bool have_lock"
576    * argument to "get_unused_color"? -octo */
577   Uint32 color_key = get_unused_color (src);
578
579   if(SDL_MUSTLOCK(src))
580     SDL_LockSurface(src);
581   if(SDL_MUSTLOCK(dst))
582     SDL_LockSurface(dst);
583
584   /* Copy all the pixels to the new surface */
585   for(int y = 0; y < src->h; y++) {
586     for(int x = 0; x < src->w; x++) {
587       Uint8 *src_pixel = (Uint8 *) src->pixels
588         + (y * src->pitch) + (x * src->format->BytesPerPixel);
589       Uint8 *dst_pixel = (Uint8 *) dst->pixels
590         + (y * dst->pitch) + (x * dst->format->BytesPerPixel);
591       Uint32 mapped = get_pixel_mapping (src, src_pixel);
592       Uint8 red, green, blue, alpha;
593       SDL_GetRGBA(mapped, src->format, &red, &green, &blue, &alpha);
594
595       /* "alpha" should either be smaller than 16 or greater than 240. We
596        * unlocked the surface in between though, so use 128 to play it save,
597        * i.e. don't leave any unspecified code paths. */
598       if (alpha < 128)
599         mapped = color_key;
600
601       switch (dst->format->BytesPerPixel)
602       {
603         case 1:
604           *dst_pixel = (Uint8) mapped;
605           break;
606
607         case 2:
608           *((Uint16 *) dst_pixel) = (Uint16) mapped;
609           break;
610
611         case 3:
612 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
613             dst_pixel[0] = (mapped >> 16) & 0xff;
614             dst_pixel[1] = (mapped >> 8) & 0xff;
615             dst_pixel[2] = (mapped >> 0) & 0xff;
616 #else
617             dst_pixel[0] = (mapped >> 0) & 0xff;
618             dst_pixel[1] = (mapped >> 8) & 0xff;
619             dst_pixel[2] = (mapped >> 16) & 0xff;
620 #endif
621             break;
622
623         case 4:
624             *((Uint32 *) dst_pixel) = mapped;
625       } /* switch (dst->format->BytesPerPixel) */
626     } /* for (x) */
627   } /* for (y) */
628
629   if(SDL_MUSTLOCK(src))
630     SDL_UnlockSurface(src);
631   if(SDL_MUSTLOCK(dst))
632     SDL_UnlockSurface(dst);
633
634   /* Tell SDL that the "color_key" color is supposed to be transparent. */
635   SDL_SetColorKey (dst, SDL_SRCCOLORKEY | SDL_RLEACCEL, color_key);
636   SDL_Surface *convert = SDL_DisplayFormat(dst);
637   SDL_FreeSurface(dst);
638   return convert;
639 #else
640   return 0;
641 #endif
642 } /* SDL_Surface *optimize */
643
644 } /* namespace */
645
646 SDLTexture::SDLTexture(SDL_Surface* image) :
647   texture()
648 {
649   texture = SDL_CreateTextureFromSurface(static_cast<SDLRenderer*>(Renderer::instance())->get_sdl_renderer(),
650                                          image);
651   if (!texture)
652   {
653     std::ostringstream msg;
654     msg << "couldn't create texture: " << SDL_GetError();
655     throw std::runtime_error(msg.str());
656   }
657
658   width = image->w;
659   height = image->h;
660
661 #ifdef OLD_SDL1
662   int numerator   = 1;
663   int denominator = 1;
664   //FIXME: float xfactor = (float) config->screenwidth / SCREEN_WIDTH;
665   //FIXME: float yfactor = (float) config->screenheight / SCREEN_HEIGHT;
666   /* FIXME: 
667      if(xfactor < yfactor)
668      {
669      numerator = config->screenwidth;
670      denominator = SCREEN_WIDTH;
671      }
672      else
673      {
674      numerator = config->screenheight;
675      denominator = SCREEN_HEIGHT;
676      }
677   */
678   cache[NO_EFFECT][Color::WHITE] = scale(texture, numerator, denominator);
679 #endif
680 }
681
682 SDLTexture::~SDLTexture()
683 {
684   SDL_DestroyTexture(texture);
685 }
686
687 #ifdef OLD_SDL1
688 SDL_Surface*
689 SDLTexture::get_transform(const Color &color, DrawingEffect effect)
690 {
691   if(cache[NO_EFFECT][color] == 0) {
692     assert(cache[NO_EFFECT][Color::WHITE]);
693     cache[NO_EFFECT][color] = colorize(cache[NO_EFFECT][Color::WHITE], color);
694   }
695   if(cache[effect][color] == 0) {
696     assert(cache[NO_EFFECT][color]);
697     switch(effect) {
698       case NO_EFFECT:
699         break;
700       case HORIZONTAL_FLIP:
701         cache[HORIZONTAL_FLIP][color] = horz_flip(cache[NO_EFFECT][color]);
702         break;
703       case VERTICAL_FLIP:
704         cache[VERTICAL_FLIP][color] = vert_flip(cache[NO_EFFECT][color]);
705         break;
706       default:
707         return 0;
708     }
709   }
710   return cache[effect][color];
711 }
712 #endif
713
714 /* vim: set sw=2 sts=2 et : */
715 /* EOF */