ae7cec4b609a534b8606c7e343a68ef6eace1079
[supertux.git] / src / texture.cpp
1 //
2 // C Implementation: texture
3 //
4 // Description:
5 //
6 //
7 // Author: Tobias Glaesser <tobi.web@gmx.de>, (C) 2004
8 //
9 // Copyright: See COPYING file that comes with this distribution
10 //
11 //
12
13 #include "SDL.h"
14 #include "SDL_image.h"
15 #include "texture.h"
16 #include "globals.h"
17 #include "setup.h"
18
19 /* Quick utility function for texture creation */
20 static int power_of_two(int input)
21 {
22   int value = 1;
23
24   while ( value < input ) {
25     value <<= 1;
26   }
27   return value;
28 }
29
30 Surface::Surface(SDL_Surface* surf, int use_alpha)
31 {
32   if (use_gl)
33     impl = new SurfaceOpenGL(surf, use_alpha);
34   else
35     impl = new SurfaceSDL(surf, use_alpha);
36
37   w = impl->w;
38  h = impl->h;
39 }
40
41 Surface::Surface(const std::string& file, int use_alpha)
42 {
43   if (use_gl)
44     impl = new SurfaceOpenGL(file, use_alpha);
45   else
46     impl = new SurfaceSDL(file, use_alpha);
47
48   w = impl->w;
49   h = impl->h;
50 }
51
52 Surface::Surface(const std::string& file, int x, int y, int w, int h, int use_alpha)
53 {
54   if (use_gl)
55     impl = new SurfaceOpenGL(file, x, y, w, h, use_alpha);
56   else
57     impl = new SurfaceSDL(file, x, y, w, h, use_alpha);
58
59   w = impl->w;
60   h = impl->h;
61 }
62
63 Surface::~Surface()
64 {
65   delete impl;
66 }
67
68 void
69 Surface::draw(float x, float y, Uint8 alpha, bool update)
70 {
71   if (impl) impl->draw(x, y, alpha, update);
72 }
73
74 void
75 Surface::draw_bg(Uint8 alpha, bool update)
76 {
77   if (impl) impl->draw_bg(alpha, update);
78 }
79
80 void
81 Surface::draw_part(float sx, float sy, float x, float y, float w, float h,  Uint8 alpha, bool update)
82 {
83   if (impl) impl->draw_part(sx, sy, x, y, w, h, alpha, update);
84 }
85
86 SDL_Surface*
87 sdl_surface_part_from_file(const std::string& file, int x, int y, int w, int h,  int use_alpha)
88 {
89   SDL_Rect src;
90   SDL_Surface * sdl_surface;
91   SDL_Surface * temp;
92   SDL_Surface * conv;
93
94   temp = IMG_Load(file.c_str());
95
96   if (temp == NULL)
97     st_abort("Can't load", file);
98
99   /* Set source rectangle for conv: */
100
101   src.x = x;
102   src.y = y;
103   src.w = w;
104   src.h = h;
105
106   conv = SDL_CreateRGBSurface(temp->flags, w, h, temp->format->BitsPerPixel,
107                               temp->format->Rmask,
108                               temp->format->Gmask,
109                               temp->format->Bmask,
110                               temp->format->Amask);
111
112   /* #if SDL_BYTEORDER == SDL_BIG_ENDIAN
113      0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
114      #else
115
116      0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
117      #endif*/
118
119   SDL_SetAlpha(temp,0,0);
120
121   SDL_BlitSurface(temp, &src, conv, NULL);
122   if(use_alpha == IGNORE_ALPHA && !use_gl)
123     sdl_surface = SDL_DisplayFormat(conv);
124   else
125     sdl_surface = SDL_DisplayFormatAlpha(conv);
126
127   if (sdl_surface == NULL)
128     st_abort("Can't covert to display format", file);
129
130   if (use_alpha == IGNORE_ALPHA && !use_gl)
131     SDL_SetAlpha(sdl_surface, 0, 0);
132
133   SDL_FreeSurface(temp);
134   SDL_FreeSurface(conv);
135   
136   return sdl_surface;
137 }
138
139 SDL_Surface*
140 sdl_surface_from_file(const std::string& file, int use_alpha)
141 {
142   SDL_Surface* sdl_surface;
143   SDL_Surface* temp;
144   
145   temp = IMG_Load(file.c_str());
146
147   if (temp == NULL)
148     st_abort("Can't load", file);
149
150   if(use_alpha == IGNORE_ALPHA && !use_gl)
151     sdl_surface = SDL_DisplayFormat(temp);
152   else
153     sdl_surface = SDL_DisplayFormatAlpha(temp);
154   
155   if (sdl_surface == NULL)
156     st_abort("Can't covert to display format", file);
157
158   if (use_alpha == IGNORE_ALPHA && !use_gl)
159     SDL_SetAlpha(sdl_surface, 0, 0);
160
161   SDL_FreeSurface(temp);
162
163   return sdl_surface;
164 }
165
166 SDL_Surface* 
167 sdl_surface_from_sdl_surface(SDL_Surface* sdl_surf, int use_alpha)
168 {
169   SDL_Surface* sdl_surface;
170   Uint32 saved_flags;
171   Uint8  saved_alpha;
172   
173   /* Save the alpha blending attributes */
174   saved_flags = sdl_surf->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
175   saved_alpha = sdl_surf->format->alpha;
176   if ( (saved_flags & SDL_SRCALPHA)
177        == SDL_SRCALPHA )
178     {
179       SDL_SetAlpha(sdl_surf, 0, 0);
180     }
181    
182   if(use_alpha == IGNORE_ALPHA && !use_gl)
183     sdl_surface = SDL_DisplayFormat(sdl_surf);
184   else
185     sdl_surface = SDL_DisplayFormatAlpha(sdl_surf);
186
187   /* Restore the alpha blending attributes */
188   if ( (saved_flags & SDL_SRCALPHA)
189        == SDL_SRCALPHA )
190     {
191       SDL_SetAlpha(sdl_surface, saved_flags, saved_alpha);
192     }
193   
194   if (sdl_surface == NULL)
195     st_abort("Can't covert to display format", "SURFACE");
196
197   if (use_alpha == IGNORE_ALPHA && !use_gl)
198     SDL_SetAlpha(sdl_surface, 0, 0);
199
200   return sdl_surface;
201 }
202
203 #ifndef NOOPENGL
204 SurfaceOpenGL::SurfaceOpenGL(SDL_Surface* surf, int use_alpha)
205 {
206   sdl_surface = sdl_surface_from_sdl_surface(surf, use_alpha);
207   create_gl(sdl_surface,&gl_texture);
208
209   w = sdl_surface->w;
210   h = sdl_surface->h;
211 }
212
213 SurfaceOpenGL::SurfaceOpenGL(const std::string& file, int use_alpha) 
214 {
215   sdl_surface = sdl_surface_from_file(file, use_alpha);
216   create_gl(sdl_surface,&gl_texture);
217
218   w = sdl_surface->w;
219   h = sdl_surface->h;
220 }
221
222 SurfaceOpenGL::SurfaceOpenGL(const std::string& file, int x, int y, int w, int h, int use_alpha)
223 {
224   sdl_surface = sdl_surface_part_from_file(file,x,y,w,h,use_alpha);
225   create_gl(sdl_surface, &gl_texture);
226
227   w = sdl_surface->w;
228   h = sdl_surface->h;
229 }
230
231 SurfaceOpenGL::~SurfaceOpenGL()
232 {
233   SDL_FreeSurface(sdl_surface);
234   glDeleteTextures(1, &gl_texture);
235 }
236
237 void
238 SurfaceOpenGL::create_gl(SDL_Surface * surf, GLuint * tex)
239 {
240   Uint32 saved_flags;
241   Uint8  saved_alpha;
242   int w, h;
243   SDL_Surface *conv;
244
245   w = power_of_two(surf->w);
246   h = power_of_two(surf->h),
247
248 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
249     conv = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, surf->format->BitsPerPixel,
250                                 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
251 #else
252   conv = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, surf->format->BitsPerPixel,
253                               0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
254 #endif
255
256   /* Save the alpha blending attributes */
257   saved_flags = surf->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
258   saved_alpha = surf->format->alpha;
259   if ( (saved_flags & SDL_SRCALPHA)
260        == SDL_SRCALPHA )
261     {
262       SDL_SetAlpha(surf, 0, 0);
263     }
264
265   SDL_BlitSurface(surf, 0, conv, 0);
266
267   /* Restore the alpha blending attributes */
268   if ( (saved_flags & SDL_SRCALPHA)
269        == SDL_SRCALPHA )
270     {
271       SDL_SetAlpha(surf, saved_flags, saved_alpha);
272     }
273
274   glGenTextures(1, &*tex);
275   glBindTexture(GL_TEXTURE_2D , *tex);
276   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
277   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
278   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
279   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
280   glPixelStorei(GL_UNPACK_ROW_LENGTH, conv->pitch / conv->format->BytesPerPixel);
281   glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, conv->pixels);
282   glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
283       
284   SDL_FreeSurface(conv);
285 }
286
287 void
288 SurfaceOpenGL::draw(float x, float y, Uint8 alpha, bool update)
289 {
290   float pw = power_of_two(w);
291   float ph = power_of_two(h);
292
293   glEnable(GL_TEXTURE_2D);
294   glEnable(GL_BLEND);
295   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
296
297   glColor4ub(alpha, alpha, alpha, alpha);
298
299   glBindTexture(GL_TEXTURE_2D, gl_texture);
300
301   glBegin(GL_QUADS);
302   glTexCoord2f(0, 0);
303   glVertex2f(x, y);
304   glTexCoord2f((float)w / pw, 0);
305   glVertex2f((float)w+x, y);
306   glTexCoord2f((float)w / pw, (float)h / ph);  glVertex2f((float)w+x, (float)h+y);
307   glTexCoord2f(0, (float)h / ph);
308   glVertex2f(x, (float)h+y);
309   glEnd();
310   
311   glDisable(GL_TEXTURE_2D);
312   glDisable(GL_BLEND);
313   
314   /* Avoid compiler warnings */
315   if(update)
316     {}
317 }
318
319 void
320 SurfaceOpenGL::draw_bg(Uint8 alpha, bool update)
321 {
322   float pw = power_of_two(w);
323   float ph = power_of_two(h);
324
325   glColor3ub(alpha, alpha, alpha);
326
327   glEnable(GL_TEXTURE_2D);
328   glBindTexture(GL_TEXTURE_2D, gl_texture);
329
330   glBegin(GL_QUADS);
331   glTexCoord2f(0, 0);
332   glVertex2f(0, 0);
333   glTexCoord2f((float)w / pw, 0);
334   glVertex2f(screen->w, 0);
335   glTexCoord2f((float)w / pw, (float)h / ph);
336   glVertex2f(screen->w, screen->h);
337   glTexCoord2f(0, (float)h / ph);
338   glVertex2f(0, screen->h);
339   glEnd();
340   
341   glDisable(GL_TEXTURE_2D);
342
343   /* Avoid compiler warnings */
344   if(update)
345     {}
346 }
347
348 void
349 SurfaceOpenGL::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, bool update)
350 {
351   float pw = power_of_two(int(this->w));
352   float ph = power_of_two(int(this->h));
353
354   glBindTexture(GL_TEXTURE_2D, gl_texture);
355
356   glEnable(GL_BLEND);
357   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
358
359   glColor4ub(alpha, alpha, alpha, alpha);
360
361   glEnable(GL_TEXTURE_2D);
362
363
364   glBegin(GL_QUADS);
365   glTexCoord2f(sx / pw, sy / ph);
366   glVertex2f(x, y);
367   glTexCoord2f((float)(sx + w) / pw, sy / ph);
368   glVertex2f(w+x, y);
369   glTexCoord2f((sx+w) / pw, (sy+h) / ph);
370   glVertex2f(w +x, h+y);
371   glTexCoord2f(sx / pw, (float)(sy+h) / ph);
372   glVertex2f(x, h+y);
373   glEnd();
374
375   glDisable(GL_TEXTURE_2D);
376   glDisable(GL_BLEND);
377
378   /* Avoid compiler warnings */
379   if(update)
380     {}
381 }
382 #endif
383
384 SurfaceSDL::SurfaceSDL(SDL_Surface* surf, int use_alpha)
385 {
386   sdl_surface = sdl_surface_from_sdl_surface(surf, use_alpha);
387   w = sdl_surface->w;
388   h = sdl_surface->h;
389 }
390
391 SurfaceSDL::SurfaceSDL(const std::string& file, int use_alpha)
392 {
393   sdl_surface = sdl_surface_from_file(file, use_alpha);
394   w = sdl_surface->w;
395   h = sdl_surface->h;
396 }
397
398 SurfaceSDL::SurfaceSDL(const std::string& file, int x, int y, int w, int h,  int use_alpha)
399 {
400   sdl_surface = sdl_surface_part_from_file(file, x, y, w, h, use_alpha);
401   w = sdl_surface->w;
402   h = sdl_surface->h;
403 }
404
405 void
406 SurfaceSDL::draw(float x, float y, Uint8 alpha, bool update)
407 {
408   SDL_Rect dest;
409
410   dest.x = (int)x;
411   dest.y = (int)y;
412   dest.w = w;
413   dest.h = h;
414   
415   if(alpha != 255) /* SDL isn't capable of this kind of alpha :( therefore we'll leave now. */
416   return;
417   
418   SDL_SetAlpha(sdl_surface ,SDL_SRCALPHA,alpha);
419   SDL_BlitSurface(sdl_surface, NULL, screen, &dest);
420   
421   if (update == UPDATE)
422     SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h);
423 }
424
425 void
426 SurfaceSDL::draw_bg(Uint8 alpha, bool update)
427 {
428   SDL_Rect dest;
429   
430   dest.x = 0;
431   dest.y = 0;
432   dest.w = screen->w;
433   dest.h = screen->h;
434
435   if(alpha != 255)
436   SDL_SetAlpha(sdl_surface ,SDL_SRCALPHA,alpha);
437   SDL_SoftStretch(sdl_surface, NULL, screen, &dest);
438   
439   if (update == UPDATE)
440     SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h);
441 }
442
443 void
444 SurfaceSDL::draw_part(float sx, float sy, float x, float y, float w, float h, Uint8 alpha, bool update)
445 {
446   SDL_Rect src, dest;
447
448   src.x = (int)sx;
449   src.y = (int)sy;
450   src.w = (int)w;
451   src.h = (int)h;
452
453   dest.x = (int)x;
454   dest.y = (int)y;
455   dest.w = (int)w;
456   dest.h = (int)h;
457
458   if(alpha != 255)
459   SDL_SetAlpha(sdl_surface ,SDL_SRCALPHA,alpha);
460   
461   SDL_BlitSurface(sdl_surface, &src, screen, &dest);
462
463   if (update == UPDATE)
464     update_rect(screen, dest.x, dest.y, dest.w, dest.h);
465 }
466
467 SurfaceSDL::~SurfaceSDL()
468 {
469   SDL_FreeSurface(sdl_surface);
470 }
471
472 /* EOF */