- moved lots of code around, made gameloop even more into a class
[supertux.git] / src / texture.cpp
index b13b463..ef22a3e 100644 (file)
 #include "setup.h"
 #include "texture.h"
 
-void (*texture_load) (texture_type* ptexture,const  char * file, int use_alpha);
-void (*texture_load_part) (texture_type* ptexture,const  char * file, int x, int y, int w, int h, int use_alpha);
-void (*texture_free) (texture_type* ptexture);  
-void (*texture_draw) (texture_type* ptexture, float x, float y, int update);  
-void (*texture_draw_bg) (texture_type* ptexture, int update);  
-void (*texture_draw_part) (texture_type* ptexture, float sx, float sy, float x, float y, float w, float h, int update);
+void (*texture_load)     (texture_type* ptexture, const std::string& file, int use_alpha);
+void (*texture_load_part)(texture_type* ptexture, const std::string& file, int x, int y, int w, int h, int use_alpha);
+void (*texture_free)     (texture_type* ptexture);  
+void (*texture_draw)     (texture_type* ptexture, float x, float y, Uint8 alpha, bool update);  
+void (*texture_draw_bg)  (texture_type* ptexture, Uint8 alpha,  bool update);  
+void (*texture_draw_part)(texture_type* ptexture, float sx, float sy, float x, float y, float w, float h, Uint8 alpha, bool update);
 
 
 void texture_setup(void)
@@ -58,13 +58,13 @@ void texture_setup(void)
 }
 
 #ifndef NOOPENGL
-void texture_load_gl(texture_type* ptexture,const  char * file, int use_alpha)
+void texture_load_gl(texture_type* ptexture, const std::string& file, int use_alpha)
 {
   texture_load_sdl(ptexture,file,use_alpha);
   texture_create_gl(ptexture->sdl_surface,&ptexture->gl_texture);
 }
 
-void texture_load_part_gl(texture_type* ptexture,const  char * file, int x, int y, int w, int h, int use_alpha)
+void texture_load_part_gl(texture_type* ptexture, const std::string& file, int x, int y, int w, int h, int use_alpha)
 {
   texture_load_part_sdl(ptexture,file,x,y,w,h,use_alpha);
   texture_create_gl(ptexture->sdl_surface,&ptexture->gl_texture);
@@ -73,12 +73,12 @@ void texture_load_part_gl(texture_type* ptexture,const  char * file, int x, int
 /* Quick utility function for texture creation */
 static int power_of_two(int input)
 {
-       int value = 1;
+  int value = 1;
 
-       while ( value < input ) {
-               value <<= 1;
-       }
-       return value;
+  while ( value < input ) {
+    value <<= 1;
+  }
+  return value;
 }
 
 void texture_create_gl(SDL_Surface * surf, GLuint * tex)
@@ -135,7 +135,7 @@ void texture_free_gl(texture_type* ptexture)
   glDeleteTextures(1, &ptexture->gl_texture);
 }
 
-void texture_draw_gl(texture_type* ptexture, float x, float y, int update)
+void texture_draw_gl(texture_type* ptexture, float x, float y, Uint8 alpha, bool update)
 {
 float pw = power_of_two(ptexture->w);
 float ph = power_of_two(ptexture->h);
@@ -144,7 +144,7 @@ float ph = power_of_two(ptexture->h);
   glEnable(GL_BLEND);
   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
 
-  glColor4ub(255, 255, 255,255);
+  glColor4ub(alpha, alpha, alpha, alpha);
 
   glBindTexture(GL_TEXTURE_2D, ptexture->gl_texture);
 
@@ -160,14 +160,18 @@ float ph = power_of_two(ptexture->h);
   
   glDisable(GL_TEXTURE_2D);
   glDisable(GL_BLEND);
+  
+/* Avoid compiler warnings */
+if(update)
+{}
 }
 
-void texture_draw_bg_gl(texture_type* ptexture, int update)
+void texture_draw_bg_gl(texture_type* ptexture, Uint8 alpha, bool update)
 {
 float pw = power_of_two(ptexture->w);
 float ph = power_of_two(ptexture->h);
 
-  glColor3ub(255, 255, 255);
+  glColor3ub(alpha, alpha, alpha);
 
   glEnable(GL_TEXTURE_2D);
   glBindTexture(GL_TEXTURE_2D, ptexture->gl_texture);
@@ -184,9 +188,13 @@ float ph = power_of_two(ptexture->h);
   glEnd();
   
   glDisable(GL_TEXTURE_2D);
+
+/* Avoid compiler warnings */
+if(update)
+{}
 }
 
-void texture_draw_part_gl(texture_type* ptexture,float sx, float sy, float x, float y, float w, float h, int update)
+void texture_draw_part_gl(texture_type* ptexture,float sx, float sy, float x, float y, float w, float h, Uint8 alpha, bool update)
 {
 float pw = power_of_two(ptexture->w);
 float ph = power_of_two(ptexture->h);
@@ -196,7 +204,7 @@ float ph = power_of_two(ptexture->h);
   glEnable(GL_BLEND);
   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
 
-  glColor4ub(255, 255, 255,255);
+  glColor4ub(alpha, alpha, alpha, alpha);
 
   glEnable(GL_TEXTURE_2D);
 
@@ -215,14 +223,17 @@ float ph = power_of_two(ptexture->h);
   glDisable(GL_TEXTURE_2D);
   glDisable(GL_BLEND);
 
+/* Avoid compiler warnings */
+if(update)
+{}
 }
 #endif
 
-void texture_load_sdl(texture_type* ptexture,const  char * file, int use_alpha)
+void texture_load_sdl(texture_type* ptexture, const std::string& file, int use_alpha)
 {
   SDL_Surface * temp;
   
-  temp = IMG_Load(file);
+  temp = IMG_Load(file.c_str());
 
   if (temp == NULL)
     st_abort("Can't load", file);
@@ -245,14 +256,14 @@ void texture_load_sdl(texture_type* ptexture,const  char * file, int use_alpha)
   
 }
 
-void texture_load_part_sdl(texture_type* ptexture,const  char * file, int x, int y, int w, int h,  int use_alpha)
+void texture_load_part_sdl(texture_type* ptexture, const std::string& file, int x, int y, int w, int h,  int use_alpha)
 {
 
   SDL_Rect src;
   SDL_Surface * temp;
   SDL_Surface * conv;
 
-  temp = IMG_Load(file);
+  temp = IMG_Load(file.c_str());
 
   if (temp == NULL)
     st_abort("Can't load", file);
@@ -342,15 +353,19 @@ void texture_from_sdl_surface(texture_type* ptexture, SDL_Surface* sdl_surf, int
 #endif
 }
 
-void texture_draw_sdl(texture_type* ptexture, float x, float y, int update)
+void texture_draw_sdl(texture_type* ptexture, float x, float y, Uint8 alpha, bool update)
 {
-
   SDL_Rect dest;
 
   dest.x = (int)x;
   dest.y = (int)y;
   dest.w = ptexture->w;
   dest.h = ptexture->h;
+  
+  if(alpha != 255) /* SDL isn't capable of this kind of alpha :( therefore we'll leave now. */
+  return;
+  
+  SDL_SetAlpha(ptexture->sdl_surface ,SDL_SRCALPHA,alpha);
   SDL_BlitSurface(ptexture->sdl_surface, NULL, screen, &dest);
   
   if (update == UPDATE)
@@ -358,7 +373,7 @@ void texture_draw_sdl(texture_type* ptexture, float x, float y, int update)
 }
 
 
-void texture_draw_bg_sdl(texture_type* ptexture, int update)
+void texture_draw_bg_sdl(texture_type* ptexture, Uint8 alpha, bool update)
 {
   SDL_Rect dest;
   
@@ -366,14 +381,16 @@ void texture_draw_bg_sdl(texture_type* ptexture, int update)
   dest.y = 0;
   dest.w = screen->w;
   dest.h = screen->h;
-  
+
+  if(alpha != 255)
+  SDL_SetAlpha(ptexture->sdl_surface ,SDL_SRCALPHA,alpha);
   SDL_SoftStretch(ptexture->sdl_surface, NULL, screen, &dest);
   
   if (update == UPDATE)
     SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h);
 }
 
-void texture_draw_part_sdl(texture_type* ptexture, float sx, float sy, float x, float y, float w, float h, int update)
+void texture_draw_part_sdl(texture_type* ptexture, float sx, float sy, float x, float y, float w, float h, Uint8 alpha, bool update)
 {
   SDL_Rect src, dest;
 
@@ -387,7 +404,9 @@ void texture_draw_part_sdl(texture_type* ptexture, float sx, float sy, float x,
   dest.w = (int)w;
   dest.h = (int)h;
 
-
+  if(alpha != 255)
+  SDL_SetAlpha(ptexture->sdl_surface ,SDL_SRCALPHA,alpha);
+  
   SDL_BlitSurface(ptexture->sdl_surface, &src, screen, &dest);
 
   if (update == UPDATE)