2 // C Implementation: type
7 // Author: Tobias Glaesser <tobi.web@gmx.de>, (C) 2004
9 // Copyright: See COPYING file that comes with this distribution
13 #include <SDL/SDL_image.h>
21 double get_frame_ratio(itop_type* pit)
23 unsigned int cur_time = SDL_GetTicks();
24 double frame_ratio = (float)(cur_time-*pit->updated)/(float)(FRAME_RATE);
25 *pit->updated = cur_time;
29 void timer_init(timer_type* ptimer)
35 void timer_start(timer_type* ptimer, unsigned int period)
37 ptimer->time = SDL_GetTicks();
38 ptimer->period = period;
41 void timer_stop(timer_type* ptimer)
46 int timer_check(timer_type* ptimer)
48 if(ptimer->time != 0 && ptimer->time + ptimer->period > SDL_GetTicks())
57 int timer_started(timer_type* ptimer)
65 int timer_get_left(timer_type* ptimer)
67 return (ptimer->period - (SDL_GetTicks() - ptimer->time));
70 int timer_get_gone(timer_type* ptimer)
72 return (SDL_GetTicks() - ptimer->time);
75 void texture_load(texture_type* ptexture, char * file, int use_alpha)
79 temp = IMG_Load(file);
82 st_abort("Can't load", file);
84 ptexture->sdl_surface = SDL_DisplayFormatAlpha(temp);
86 if (ptexture->sdl_surface == NULL)
87 st_abort("Can't covert to display format", file);
89 if (use_alpha == IGNORE_ALPHA)
90 SDL_SetAlpha(ptexture->sdl_surface, 0, 0);
92 SDL_FreeSurface(temp);
94 ptexture->w = ptexture->sdl_surface->w;
95 ptexture->h = ptexture->sdl_surface->h;
99 create_gl_texture(ptexture->sdl_surface,&ptexture->gl_texture);
103 void texture_from_sdl_surface(texture_type* ptexture, SDL_Surface* sdl_surf, int use_alpha)
105 /* SDL_Surface * temp;
107 temp = IMG_Load(file);
110 st_abort("Can't load", file);*/
112 ptexture->sdl_surface = SDL_DisplayFormatAlpha(sdl_surf);
114 if (ptexture->sdl_surface == NULL)
115 st_abort("Can't covert to display format", "SURFACE");
117 if (use_alpha == IGNORE_ALPHA)
118 SDL_SetAlpha(ptexture->sdl_surface, 0, 0);
120 ptexture->w = ptexture->sdl_surface->w;
121 ptexture->h = ptexture->sdl_surface->h;
125 create_gl_texture(ptexture->sdl_surface,&ptexture->gl_texture);
129 void texture_draw(texture_type* ptexture, float x, float y, int update)
133 glColor4ub(255, 255, 255,255);
134 glBlendFunc (GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
136 glBindTexture(GL_TEXTURE_RECTANGLE_NV, ptexture->gl_texture);
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);
155 dest.w = ptexture->w;
156 dest.h = ptexture->h;
158 SDL_BlitSurface(ptexture->sdl_surface, NULL, screen, &dest);
160 if (update == UPDATE)
161 SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h);
165 void texture_draw_bg(texture_type* ptexture, int update)
169 //glColor3ub(255, 255, 255);
171 glEnable(GL_TEXTURE_RECTANGLE_NV);
172 glBindTexture(GL_TEXTURE_RECTANGLE_NV, ptexture->gl_texture);
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);
191 SDL_BlitSurface(ptexture->sdl_surface, NULL, screen, &dest);
193 if (update == UPDATE)
194 SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h);
198 void texture_draw_part(texture_type* ptexture, float x, float y, float w, float h, int update)
202 glColor3ub(255, 255, 255);
204 glEnable(GL_TEXTURE_RECTANGLE_NV);
205 glBindTexture(GL_TEXTURE_RECTANGLE_NV, ptexture->gl_texture);
210 glTexCoord2f(x+w, y);
212 glTexCoord2f(x+w, y+h);
213 glVertex2f(w+x, h+y);
214 glTexCoord2f(x, y+h);
233 SDL_BlitSurface(ptexture->sdl_surface, &src, screen, &dest);
235 if (update == UPDATE)
236 update_rect(screen, dest.x, dest.y, dest.w, dest.h);
240 void texture_free(texture_type* ptexture)
242 SDL_FreeSurface(ptexture->sdl_surface);
244 glDeleteTextures(1, &ptexture->gl_texture);