5b6de050a2600347b9272ade4dc5f336654b9535
[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 <SDL/SDL_opengl.h>
16 #include "globals.h"
17 #include "screen.h"
18 #include "setup.h"
19 #include "texture.h"
20
21 void texture_load(texture_type* ptexture, char * file, int use_alpha)
22 {
23   SDL_Surface * temp;
24
25   temp = IMG_Load(file);
26
27   if (temp == NULL)
28     st_abort("Can't load", file);
29
30   ptexture->sdl_surface = SDL_DisplayFormatAlpha(temp);
31
32   if (ptexture->sdl_surface == NULL)
33     st_abort("Can't covert to display format", file);
34
35   if (use_alpha == IGNORE_ALPHA)
36     SDL_SetAlpha(ptexture->sdl_surface, 0, 0);
37
38   SDL_FreeSurface(temp);
39
40   ptexture->w = ptexture->sdl_surface->w;
41   ptexture->h = ptexture->sdl_surface->h;
42
43   if(use_gl)
44     {
45       create_gl_texture(ptexture->sdl_surface,&ptexture->gl_texture);
46     }
47 }
48
49 void texture_from_sdl_surface(texture_type* ptexture, SDL_Surface* sdl_surf, int use_alpha)
50 {
51  /* SDL_Surface * temp;
52
53   temp = IMG_Load(file);
54
55   if (temp == NULL)
56     st_abort("Can't load", file);*/
57
58   ptexture->sdl_surface = SDL_DisplayFormatAlpha(sdl_surf);
59
60   if (ptexture->sdl_surface == NULL)
61     st_abort("Can't covert to display format", "SURFACE");
62
63   if (use_alpha == IGNORE_ALPHA)
64     SDL_SetAlpha(ptexture->sdl_surface, 0, 0);
65
66   ptexture->w = ptexture->sdl_surface->w;
67   ptexture->h = ptexture->sdl_surface->h;
68
69   if(use_gl)
70     {
71       create_gl_texture(ptexture->sdl_surface,&ptexture->gl_texture);
72     }
73 }
74
75 void texture_draw(texture_type* ptexture, float x, float y, int update)
76 {
77   if(use_gl)
78     {
79       glColor4ub(255, 255, 255,255);
80       glBlendFunc (GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
81       glEnable (GL_BLEND);
82       glBindTexture(GL_TEXTURE_RECTANGLE_NV, ptexture->gl_texture);
83
84       glBegin(GL_QUADS);
85       glTexCoord2f(0, 0);
86       glVertex2f(x, y);
87       glTexCoord2f((float)ptexture->w, 0);
88       glVertex2f((float)ptexture->w+x, y);
89       glTexCoord2f((float)ptexture->w, (float)ptexture->h);
90       glVertex2f((float)ptexture->w+x, (float)ptexture->h+y);
91       glTexCoord2f(0, (float)ptexture->h);
92       glVertex2f(x, (float)ptexture->h+y);
93       glEnd();
94     }
95   else
96     {
97       SDL_Rect dest;
98
99       dest.x = x;
100       dest.y = y;
101       dest.w = ptexture->w;
102       dest.h = ptexture->h;
103
104       SDL_BlitSurface(ptexture->sdl_surface, NULL, screen, &dest);
105
106       if (update == UPDATE)
107         SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h);
108     }
109 }
110
111 void texture_draw_bg(texture_type* ptexture, int update)
112 {
113 if(use_gl)
114 {
115     //glColor3ub(255, 255, 255);
116
117     glEnable(GL_TEXTURE_RECTANGLE_NV);
118     glBindTexture(GL_TEXTURE_RECTANGLE_NV, ptexture->gl_texture);
119
120     glBegin(GL_QUADS);
121         glTexCoord2f(0, 0);    glVertex2f(0, 0);
122         glTexCoord2f((float)ptexture->w, 0);    glVertex2f(screen->w, 0);
123         glTexCoord2f((float)ptexture->w, (float)ptexture->h);    glVertex2f(screen->w, screen->h);
124         glTexCoord2f(0, (float)ptexture->h); glVertex2f(0, screen->h);
125     glEnd();
126  
127 }
128 else
129 {
130   SDL_Rect dest;
131   
132   dest.x = 0;
133   dest.y = 0;
134   dest.w = screen->w;
135   dest.h = screen->h;
136   
137   SDL_BlitSurface(ptexture->sdl_surface, NULL, screen, &dest);
138   
139   if (update == UPDATE)
140     SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h);
141 }
142 }
143
144 void texture_draw_part(texture_type* ptexture, float x, float y, float w, float h, int update)
145 {
146   if(use_gl)
147     {
148       glColor3ub(255, 255, 255);
149
150       glEnable(GL_TEXTURE_RECTANGLE_NV);
151       glBindTexture(GL_TEXTURE_RECTANGLE_NV, ptexture->gl_texture);
152
153       glBegin(GL_QUADS);
154       glTexCoord2f(x, y);
155       glVertex2f(x, y);
156       glTexCoord2f(x+w, y);
157       glVertex2f(w+x, y);
158       glTexCoord2f(x+w, y+h);
159       glVertex2f(w+x, h+y);
160       glTexCoord2f(x, y+h);
161       glVertex2f(x, h+y);
162       glEnd();
163     }
164   else
165     {
166       SDL_Rect src, dest;
167
168       src.x = x;
169       src.y = y;
170       src.w = w;
171       src.h = h;
172
173       dest.x = x;
174       dest.y = y;
175       dest.w = w;
176       dest.h = h;
177
178
179       SDL_BlitSurface(ptexture->sdl_surface, &src, screen, &dest);
180
181       if (update == UPDATE)
182         update_rect(screen, dest.x, dest.y, dest.w, dest.h);
183     }
184 }
185
186 void texture_free(texture_type* ptexture)
187 {
188   SDL_FreeSurface(ptexture->sdl_surface);
189   if(use_gl)
190     glDeleteTextures(1, &ptexture->gl_texture);
191 }
192