many code-cleanups. merged leveleditor patch from Ricardo Cruz. Fixed bugs. many...
[supertux.git] / src / texture.c
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/SDL.h>
14 #include <SDL/SDL_image.h>
15 #include "globals.h"
16 #include "screen.h"
17 #include "setup.h"
18 #include "texture.h"
19
20 void (*texture_load) (texture_type* ptexture, char * file, int use_alpha);
21 void (*texture_load_part) (texture_type* ptexture, char * file, int x, int y, int w, int h, int use_alpha);
22 void (*texture_free) (texture_type* ptexture);  
23 void (*texture_draw) (texture_type* ptexture, float x, float y, int update);  
24 void (*texture_draw_bg) (texture_type* ptexture, int update);  
25 void (*texture_draw_part) (texture_type* ptexture, float sx, float sy, float x, float y, float w, float h, int update);
26
27
28 void texture_setup(void)
29 {
30 #ifdef NOOPENGL
31   texture_load = texture_load_sdl;
32   texture_load_part = texture_load_part_sdl;
33   texture_free = texture_free_sdl;
34   texture_draw = texture_draw_sdl;
35   texture_draw_bg = texture_draw_bg_sdl;
36   texture_draw_part = texture_draw_part_sdl;
37 #else
38
39   if(use_gl)
40     {
41       texture_load = texture_load_gl;
42       texture_load_part = texture_load_part_gl;
43       texture_free = texture_free_gl;
44       texture_draw = texture_draw_gl;
45       texture_draw_bg = texture_draw_bg_gl;
46       texture_draw_part = texture_draw_part_gl;
47     }
48   else
49     {
50       texture_load = texture_load_sdl;
51       texture_load_part = texture_load_part_sdl;
52       texture_free = texture_free_sdl;
53       texture_draw = texture_draw_sdl;
54       texture_draw_bg = texture_draw_bg_sdl;
55       texture_draw_part = texture_draw_part_sdl;
56     }
57 #endif
58 }
59
60 #ifndef NOOPENGL
61 void texture_load_gl(texture_type* ptexture, char * file, int use_alpha)
62 {
63   texture_load_sdl(ptexture,file,use_alpha);
64   texture_create_gl(ptexture->sdl_surface,&ptexture->gl_texture);
65 }
66
67 void texture_load_part_gl(texture_type* ptexture, char * file, int x, int y, int w, int h, int use_alpha)
68 {
69   texture_load_part_sdl(ptexture,file,x,y,w,h,use_alpha);
70   texture_create_gl(ptexture->sdl_surface,&ptexture->gl_texture);
71 }
72
73 /* Quick utility function for texture creation */
74 static int power_of_two(int input)
75 {
76         int value = 1;
77
78         while ( value < input ) {
79                 value <<= 1;
80         }
81         return value;
82 }
83
84 void texture_create_gl(SDL_Surface * surf, GLuint * tex)
85 {
86   Uint32 saved_flags;
87   Uint8  saved_alpha;
88   int w, h;
89   SDL_Surface *conv;
90  
91   w = power_of_two(surf->w);
92   h = power_of_two(surf->h),
93   conv = SDL_CreateRGBSurface(surf->flags, w, h, surf->format->BitsPerPixel,
94 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
95                               0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
96 #else
97
98                               0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
99 #endif
100
101   /* Save the alpha blending attributes */
102   saved_flags = surf->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
103   saved_alpha = surf->format->alpha;
104   if ( (saved_flags & SDL_SRCALPHA)
105        == SDL_SRCALPHA )
106     {
107       SDL_SetAlpha(surf, 0, 0);
108     }
109
110   SDL_BlitSurface(surf, 0, conv, 0);
111
112   /* Restore the alpha blending attributes */
113   if ( (saved_flags & SDL_SRCALPHA)
114        == SDL_SRCALPHA )
115     {
116       SDL_SetAlpha(surf, saved_flags, saved_alpha);
117     }
118
119
120   glGenTextures(1, &*tex);
121
122   glBindTexture(GL_TEXTURE_2D , *tex);
123   glEnable(GL_TEXTURE_2D);
124   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
125   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
126   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
127   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
128   glPixelStorei(GL_UNPACK_ROW_LENGTH, conv->pitch / conv->format->BytesPerPixel);
129   glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, conv->pixels);
130   glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
131   glDisable(GL_BLEND);
132   SDL_FreeSurface(conv);
133 }
134
135 void texture_free_gl(texture_type* ptexture)
136 {
137   SDL_FreeSurface(ptexture->sdl_surface);
138   glDeleteTextures(1, &ptexture->gl_texture);
139 }
140
141 void texture_draw_gl(texture_type* ptexture, float x, float y, int update)
142 {
143 float pw = power_of_two(ptexture->w);
144 float ph = power_of_two(ptexture->h);
145
146   glEnable(GL_TEXTURE_2D);
147   glEnable(GL_BLEND);
148   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
149
150   glColor4ub(255, 255, 255,255);
151
152   glBindTexture(GL_TEXTURE_2D, ptexture->gl_texture);
153
154   glBegin(GL_QUADS);
155   glTexCoord2f(0, 0);
156   glVertex2f(x, y);
157   glTexCoord2f((float)ptexture->w / pw, 0);
158   glVertex2f((float)ptexture->w+x, y);
159   glTexCoord2f((float)ptexture->w / pw, (float)ptexture->h / ph);  glVertex2f((float)ptexture->w+x, (float)ptexture->h+y);
160   glTexCoord2f(0, (float)ptexture->h / ph);
161   glVertex2f(x, (float)ptexture->h+y);
162   glEnd();
163   
164   glDisable(GL_TEXTURE_2D);
165   glDisable(GL_BLEND);
166 }
167
168 void texture_draw_bg_gl(texture_type* ptexture, int update)
169 {
170 float pw = power_of_two(ptexture->w);
171 float ph = power_of_two(ptexture->h);
172
173   glColor3ub(255, 255, 255);
174
175   glEnable(GL_TEXTURE_2D);
176   glBindTexture(GL_TEXTURE_2D, ptexture->gl_texture);
177
178   glBegin(GL_QUADS);
179   glTexCoord2f(0, 0);
180   glVertex2f(0, 0);
181   glTexCoord2f((float)ptexture->w / pw, 0);
182   glVertex2f(screen->w, 0);
183   glTexCoord2f((float)ptexture->w / pw, (float)ptexture->h / ph);
184   glVertex2f(screen->w, screen->h);
185   glTexCoord2f(0, (float)ptexture->h / ph);
186   glVertex2f(0, screen->h);
187   glEnd();
188   
189   glDisable(GL_TEXTURE_2D);
190 }
191
192 void texture_draw_part_gl(texture_type* ptexture,float sx, float sy, float x, float y, float w, float h, int update)
193 {
194 float pw = power_of_two(ptexture->w);
195 float ph = power_of_two(ptexture->h);
196
197   glBindTexture(GL_TEXTURE_2D, ptexture->gl_texture);
198
199   glEnable(GL_BLEND);
200   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
201
202   glColor4ub(255, 255, 255,255);
203
204   glEnable(GL_TEXTURE_2D);
205
206
207   glBegin(GL_QUADS);
208   glTexCoord2f(sx / pw, sy / ph);
209   glVertex2f(x, y);
210   glTexCoord2f((float)(sx + w) / pw, sy / ph);
211   glVertex2f(w+x, y);
212   glTexCoord2f((sx+w) / pw, (sy+h) / ph);
213   glVertex2f(w +x, h+y);
214   glTexCoord2f(sx / pw, (float)(sy+h) / ph);
215   glVertex2f(x, h+y);
216   glEnd();
217
218   glDisable(GL_TEXTURE_2D);
219   glDisable(GL_BLEND);
220
221 }
222 #endif
223
224 void texture_load_sdl(texture_type* ptexture, char * file, int use_alpha)
225 {
226   SDL_Surface * temp;
227
228   temp = IMG_Load(file);
229
230   if (temp == NULL)
231     st_abort("Can't load", file);
232
233   if(use_alpha == IGNORE_ALPHA)
234   ptexture->sdl_surface = SDL_DisplayFormat(temp);
235   else
236   ptexture->sdl_surface = SDL_DisplayFormatAlpha(temp);
237
238   if (ptexture->sdl_surface == NULL)
239     st_abort("Can't covert to display format", file);
240
241   if (use_alpha == IGNORE_ALPHA)
242     SDL_SetAlpha(ptexture->sdl_surface, 0, 0);
243
244   SDL_FreeSurface(temp);
245
246   ptexture->w = ptexture->sdl_surface->w;
247   ptexture->h = ptexture->sdl_surface->h;
248
249 }
250
251 void texture_load_part_sdl(texture_type* ptexture, char * file, int x, int y, int w, int h,  int use_alpha)
252 {
253
254   SDL_Rect src;
255   SDL_Surface * temp;
256   SDL_Surface * conv;
257
258   temp = IMG_Load(file);
259
260   if (temp == NULL)
261     st_abort("Can't load", file);
262
263   /* Set source rectangle for conv: */
264
265   src.x = x;
266   src.y = y;
267   src.w = w;
268   src.h = h;
269
270   conv = SDL_CreateRGBSurface(temp->flags, w, h, temp->format->BitsPerPixel,
271                               temp->format->Rmask,
272                               temp->format->Gmask,
273                               temp->format->Bmask,
274                               temp->format->Amask);
275
276   /* #if SDL_BYTEORDER == SDL_BIG_ENDIAN
277                                0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
278   #else
279
280                                0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
281   #endif*/
282
283   SDL_SetAlpha(temp,0,0);
284
285   SDL_BlitSurface(temp, &src, conv, NULL);
286   if(use_alpha == IGNORE_ALPHA)
287   ptexture->sdl_surface = SDL_DisplayFormat(conv);
288   else
289   ptexture->sdl_surface = SDL_DisplayFormatAlpha(conv);
290
291   if (ptexture->sdl_surface == NULL)
292     st_abort("Can't covert to display format", file);
293
294   if (use_alpha == IGNORE_ALPHA)
295     SDL_SetAlpha(ptexture->sdl_surface, 0, 0);
296
297   SDL_FreeSurface(temp);
298   SDL_FreeSurface(conv);
299
300   ptexture->w = ptexture->sdl_surface->w;
301   ptexture->h = ptexture->sdl_surface->h;
302 }
303
304 void texture_from_sdl_surface(texture_type* ptexture, SDL_Surface* sdl_surf, int use_alpha)
305 {
306   Uint32 saved_flags;
307   Uint8  saved_alpha;
308   
309   /* Save the alpha blending attributes */
310   saved_flags = sdl_surf->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
311   saved_alpha = sdl_surf->format->alpha;
312   if ( (saved_flags & SDL_SRCALPHA)
313        == SDL_SRCALPHA )
314     {
315       SDL_SetAlpha(sdl_surf, 0, 0);
316     }
317    
318   if(use_alpha == IGNORE_ALPHA)
319   ptexture->sdl_surface = SDL_DisplayFormat(sdl_surf);
320   else
321   ptexture->sdl_surface = SDL_DisplayFormatAlpha(sdl_surf);
322
323   /* Restore the alpha blending attributes */
324   if ( (saved_flags & SDL_SRCALPHA)
325        == SDL_SRCALPHA )
326     {
327       SDL_SetAlpha(ptexture->sdl_surface, saved_flags, saved_alpha);
328     }
329   
330   if (ptexture->sdl_surface == NULL)
331     st_abort("Can't covert to display format", "SURFACE");
332
333   if (use_alpha == IGNORE_ALPHA)
334     SDL_SetAlpha(ptexture->sdl_surface, 0, 0);
335
336   ptexture->w = ptexture->sdl_surface->w;
337   ptexture->h = ptexture->sdl_surface->h;
338
339 #ifndef NOOPENGL
340
341   if(use_gl)
342     {
343       texture_create_gl(ptexture->sdl_surface,&ptexture->gl_texture);
344     }
345 #endif
346 }
347
348 void texture_draw_sdl(texture_type* ptexture, float x, float y, int update)
349 {
350
351   SDL_Rect dest;
352
353   dest.x = (int)x;
354   dest.y = (int)y;
355   dest.w = ptexture->w;
356   dest.h = ptexture->h;
357   SDL_BlitSurface(ptexture->sdl_surface, NULL, screen, &dest);
358   
359   if (update == UPDATE)
360     SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h);
361 }
362
363
364 void texture_draw_bg_sdl(texture_type* ptexture, int update)
365 {
366   SDL_Rect dest;
367   
368   dest.x = 0;
369   dest.y = 0;
370   dest.w = screen->w;
371   dest.h = screen->h;
372   
373   SDL_SoftStretch(ptexture->sdl_surface, NULL, screen, &dest);
374   
375   if (update == UPDATE)
376     SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h);
377 }
378
379 void texture_draw_part_sdl(texture_type* ptexture, float sx, float sy, float x, float y, float w, float h, int update)
380 {
381   SDL_Rect src, dest;
382
383   src.x = (int)sx;
384   src.y = (int)sy;
385   src.w = (int)w;
386   src.h = (int)h;
387
388   dest.x = (int)x;
389   dest.y = (int)y;
390   dest.w = (int)w;
391   dest.h = (int)h;
392
393
394   SDL_BlitSurface(ptexture->sdl_surface, &src, screen, &dest);
395
396   if (update == UPDATE)
397     update_rect(screen, dest.x, dest.y, dest.w, dest.h);
398 }
399
400 void texture_free_sdl(texture_type* ptexture)
401 {
402   SDL_FreeSurface(ptexture->sdl_surface);
403 }
404