Huge code merge. This reflects the current status of my rewrite/restructuring. A...
[supertux.git] / src / type.c
1 //
2 // C Implementation: type
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_image.h>
14 #include "setup.h"
15 #include "globals.h"
16 #include "screen.h"
17 #include "defines.h"
18 #include "type.h"
19 #include "scene.h"
20
21 double get_frame_ratio(itop_type* pit)
22 {
23   unsigned int cur_time = SDL_GetTicks();
24   double frame_ratio = (float)(cur_time-*pit->updated)/(float)(FRAME_RATE);
25   *pit->updated = cur_time;
26   return frame_ratio;
27 }
28
29 void timer_init(timer_type* ptimer)
30 {
31   ptimer->period = 0;
32   ptimer->time = 0;
33 }
34
35 void timer_start(timer_type* ptimer, unsigned int period)
36 {
37   ptimer->time = SDL_GetTicks();
38   ptimer->period = period;
39 }
40
41 void timer_stop(timer_type* ptimer)
42 {
43  timer_init(ptimer);
44 }
45
46 int timer_check(timer_type* ptimer)
47 {
48   if(ptimer->time != 0 && ptimer->time + ptimer->period > SDL_GetTicks())
49     return YES;
50   else
51     {
52       ptimer->time = 0;
53       return NO;
54     }
55 }
56
57 int timer_started(timer_type* ptimer)
58 {
59   if(ptimer->time != 0)
60     return YES;
61   else
62     return NO;
63 }
64
65 int timer_get_left(timer_type* ptimer)
66 {
67   return (ptimer->period - (SDL_GetTicks() - ptimer->time));
68 }
69
70 int timer_get_gone(timer_type* ptimer)
71 {
72   return (SDL_GetTicks() - ptimer->time);
73 }
74
75 void texture_load(texture_type* ptexture, char * file, int use_alpha)
76 {
77   SDL_Surface * temp;
78
79   temp = IMG_Load(file);
80
81   if (temp == NULL)
82     st_abort("Can't load", file);
83
84   ptexture->sdl_surface = SDL_DisplayFormatAlpha(temp);
85
86   if (ptexture->sdl_surface == NULL)
87     st_abort("Can't covert to display format", file);
88
89   if (use_alpha == IGNORE_ALPHA)
90     SDL_SetAlpha(ptexture->sdl_surface, 0, 0);
91
92   SDL_FreeSurface(temp);
93
94   ptexture->w = ptexture->sdl_surface->w;
95   ptexture->h = ptexture->sdl_surface->h;
96
97   if(use_gl)
98     {
99       create_gl_texture(ptexture->sdl_surface,&ptexture->gl_texture);
100     }
101 }
102
103 void texture_from_sdl_surface(texture_type* ptexture, SDL_Surface* sdl_surf, int use_alpha)
104 {
105  /* SDL_Surface * temp;
106
107   temp = IMG_Load(file);
108
109   if (temp == NULL)
110     st_abort("Can't load", file);*/
111
112   ptexture->sdl_surface = SDL_DisplayFormatAlpha(sdl_surf);
113
114   if (ptexture->sdl_surface == NULL)
115     st_abort("Can't covert to display format", "SURFACE");
116
117   if (use_alpha == IGNORE_ALPHA)
118     SDL_SetAlpha(ptexture->sdl_surface, 0, 0);
119
120   ptexture->w = ptexture->sdl_surface->w;
121   ptexture->h = ptexture->sdl_surface->h;
122
123   if(use_gl)
124     {
125       create_gl_texture(ptexture->sdl_surface,&ptexture->gl_texture);
126     }
127 }
128
129 void texture_draw(texture_type* ptexture, float x, float y, int update)
130 {
131   if(use_gl)
132     {
133       glColor4ub(255, 255, 255,255);
134       glBlendFunc (GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
135       glEnable (GL_BLEND);
136       glBindTexture(GL_TEXTURE_RECTANGLE_NV, ptexture->gl_texture);
137
138       glBegin(GL_QUADS);
139       glTexCoord2f(0, 0);
140       glVertex2f(x, y);
141       glTexCoord2f((float)ptexture->w, 0);
142       glVertex2f((float)ptexture->w+x, y);
143       glTexCoord2f((float)ptexture->w, (float)ptexture->h);
144       glVertex2f((float)ptexture->w+x, (float)ptexture->h+y);
145       glTexCoord2f(0, (float)ptexture->h);
146       glVertex2f(x, (float)ptexture->h+y);
147       glEnd();
148     }
149   else
150     {
151       SDL_Rect dest;
152
153       dest.x = x;
154       dest.y = y;
155       dest.w = ptexture->w;
156       dest.h = ptexture->h;
157
158       SDL_BlitSurface(ptexture->sdl_surface, NULL, screen, &dest);
159
160       if (update == UPDATE)
161         SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h);
162     }
163 }
164
165 void texture_draw_bg(texture_type* ptexture, int update)
166 {
167 if(use_gl)
168 {
169     //glColor3ub(255, 255, 255);
170
171     glEnable(GL_TEXTURE_RECTANGLE_NV);
172     glBindTexture(GL_TEXTURE_RECTANGLE_NV, ptexture->gl_texture);
173
174     glBegin(GL_QUADS);
175         glTexCoord2f(0, 0);    glVertex2f(0, 0);
176         glTexCoord2f((float)ptexture->w, 0);    glVertex2f(screen->w, 0);
177         glTexCoord2f((float)ptexture->w, (float)ptexture->h);    glVertex2f(screen->w, screen->h);
178         glTexCoord2f(0, (float)ptexture->h); glVertex2f(0, screen->h);
179     glEnd();
180  
181 }
182 else
183 {
184   SDL_Rect dest;
185   
186   dest.x = 0;
187   dest.y = 0;
188   dest.w = screen->w;
189   dest.h = screen->h;
190   
191   SDL_BlitSurface(ptexture->sdl_surface, NULL, screen, &dest);
192   
193   if (update == UPDATE)
194     SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h);
195 }
196 }
197
198 void texture_draw_part(texture_type* ptexture, float x, float y, float w, float h, int update)
199 {
200   if(use_gl)
201     {
202       glColor3ub(255, 255, 255);
203
204       glEnable(GL_TEXTURE_RECTANGLE_NV);
205       glBindTexture(GL_TEXTURE_RECTANGLE_NV, ptexture->gl_texture);
206
207       glBegin(GL_QUADS);
208       glTexCoord2f(x, y);
209       glVertex2f(x, y);
210       glTexCoord2f(x+w, y);
211       glVertex2f(w+x, y);
212       glTexCoord2f(x+w, y+h);
213       glVertex2f(w+x, h+y);
214       glTexCoord2f(x, y+h);
215       glVertex2f(x, h+y);
216       glEnd();
217     }
218   else
219     {
220       SDL_Rect src, dest;
221
222       src.x = x;
223       src.y = y;
224       src.w = w;
225       src.h = h;
226
227       dest.x = x;
228       dest.y = y;
229       dest.w = w;
230       dest.h = h;
231
232
233       SDL_BlitSurface(ptexture->sdl_surface, &src, screen, &dest);
234
235       if (update == UPDATE)
236         update_rect(screen, dest.x, dest.y, dest.w, dest.h);
237     }
238 }
239
240 void texture_free(texture_type* ptexture)
241 {
242   SDL_FreeSurface(ptexture->sdl_surface);
243   if(use_gl)
244     glDeleteTextures(1, &ptexture->gl_texture);
245 }
246