home *** CD-ROM | disk | FTP | other *** search
/ Launch & Play / spustahrej2.iso / Egoboo / code / gltexture.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-12-03  |  7.0 KB  |  208 lines

  1. // GLTexture.c
  2.  
  3. // Egoboo, Copyright (C) 2000 Aaron Bishop
  4.  
  5. #include "egoboo.h" // GAC - Needed for Win32 stuff
  6. #include "gltexture.h"
  7.  
  8.  
  9. /********************> GLTexture_Load() <*****/
  10. void        GLTexture_Load( GLTexture *texture, const char *filename )
  11. {
  12.     
  13.     SDL_Surface    *tempSurface, *imageSurface;
  14.     
  15.     /* Load the bitmap into an SDL_Surface */
  16.     imageSurface = SDL_LoadBMP( FILENAME(filename) );
  17.     
  18.     /* Make sure a valid SDL_Surface was returned */
  19.     if ( imageSurface != NULL )
  20.     {
  21.         /* Generate an OpenGL texture */
  22.         glGenTextures( 1, &texture->textureID );
  23.         
  24.         /* Set up some parameters for the format of the OpenGL texture */
  25.         glBindTexture( GL_TEXTURE_2D, texture->textureID );                    /* Bind Our Texture */
  26.         glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );    /* Linear Filtered */
  27.         glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );    /* Linear Filtered */
  28.         
  29.         /* Set the original image's size (incase it's not an exact square of a power of two) */
  30.         texture->imgHeight = imageSurface->h;
  31.         texture->imgWidth = imageSurface->w;
  32.         
  33.         /* Determine the correct power of two greater than or equal to the original image's size */
  34.         texture->txDimensions = 2;
  35.         while ( ( texture->txDimensions < texture->imgHeight ) && ( texture->txDimensions < texture->imgWidth ) )
  36.             texture->txDimensions *= 2;
  37.         
  38.         /* Set the texture's alpha */
  39.         texture->alpha = 1;
  40.         
  41.         /* Create a blank SDL_Surface (of the smallest size to fit the image) & copy imageSurface into it*/
  42.         if(imageSurface->format->Gmask)
  43.         {
  44.             tempSurface = SDL_CreateRGBSurface( SDL_SWSURFACE, texture->txDimensions, texture->txDimensions, 24, imageSurface->format->Rmask, imageSurface->format->Gmask, imageSurface->format->Bmask, imageSurface->format->Amask );
  45.         }
  46.         else
  47.         {
  48. #ifdef _LITTLE_ENDIAN
  49.             tempSurface = SDL_CreateRGBSurface( SDL_SWSURFACE, texture->txDimensions, texture->txDimensions, 24, 0x0000FF, 0x00FF00, 0xFF0000, 0x000000);
  50. #else
  51.             tempSurface = SDL_CreateRGBSurface( SDL_SWSURFACE, texture->txDimensions, texture->txDimensions, 24, 0xFF0000, 0x00FF00, 0x0000FF, 0x000000);
  52. #endif
  53.         }
  54.         SDL_BlitSurface( imageSurface, &imageSurface->clip_rect, tempSurface, &imageSurface->clip_rect );
  55.         
  56.         /* actually create the OpenGL textures */
  57.         glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, tempSurface->w, tempSurface->h, 0, GL_RGB, GL_UNSIGNED_BYTE, tempSurface->pixels );
  58.         
  59.         /* get rid of our SDL_Surfaces now that we're done with them */
  60.         SDL_FreeSurface( tempSurface );
  61.         SDL_FreeSurface( imageSurface );
  62.     }
  63.     
  64. }
  65.  
  66. /********************> GLTexture_LoadA() <*****/
  67. void        GLTexture_LoadA( GLTexture *texture, const char *filename, Uint32 key )
  68. {
  69.     
  70.     /* The key param indicates which color to set alpha to 0.  All other values are 0xFF. */
  71.     SDL_Surface    *tempSurface, *imageSurface;
  72.     Sint16  x,y;
  73.     Uint32  *p;
  74.  
  75.     /* Load the bitmap into an SDL_Surface */
  76.     imageSurface = SDL_LoadBMP( FILENAME(filename) );
  77.     
  78.     /* Make sure a valid SDL_Surface was returned */
  79.     if ( imageSurface != NULL )
  80.     {
  81.         /* Generate an OpenGL texture */
  82.         glGenTextures( 1, &texture->textureID );
  83.         
  84.         /* Set up some parameters for the format of the OpenGL texture */
  85.         glBindTexture( GL_TEXTURE_2D, texture->textureID );                    /* Bind Our Texture */
  86.         glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );    /* Linear Filtered */
  87.         glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );    /* Linear Filtered */
  88.         
  89.         /* Set the original image's size (incase it's not an exact square of a power of two) */
  90.         texture->imgHeight = imageSurface->h;
  91.         texture->imgWidth = imageSurface->w;
  92.         
  93.         /* Determine the correct power of two greater than or equal to the original image's size */
  94.         texture->txDimensions = 2;
  95.         while ( ( texture->txDimensions < texture->imgHeight ) && ( texture->txDimensions < texture->imgWidth ) )
  96.             texture->txDimensions *= 2;
  97.         
  98.         /* Set the texture's alpha */
  99.         texture->alpha = 1;
  100.         
  101.         /* Create a blank SDL_Surface (of the smallest size to fit the image) & copy imageSurface into it*/
  102.               //SDL_SetColorKey(imageSurface, SDL_SRCCOLORKEY,0);
  103.               //cvtSurface = SDL_ConvertSurface(imageSurface, &fmt, SDL_SWSURFACE);
  104.         
  105.         if(imageSurface->format->Gmask)
  106.         {
  107.             tempSurface = SDL_CreateRGBSurface( SDL_SWSURFACE, texture->txDimensions, texture->txDimensions, 32, imageSurface->format->Rmask, imageSurface->format->Gmask, imageSurface->format->Bmask, imageSurface->format->Amask );
  108.         }
  109.         else
  110.         {
  111.             #ifdef _LITTLE_ENDIAN
  112.             tempSurface = SDL_CreateRGBSurface( SDL_SWSURFACE, texture->txDimensions, texture->txDimensions, 32, 0x0000FF, 0x00FF00, 0xFF0000, 0x000000);
  113.             #else
  114.             tempSurface = SDL_CreateRGBSurface( SDL_SWSURFACE, texture->txDimensions, texture->txDimensions, 32, 0xFF000000, 0x00FF0000, 0x0000FF00, 0x00000000);
  115.             #endif
  116.         }
  117.         //SDL_BlitSurface( cvtSurface, &cvtSurface->clip_rect, tempSurface, &cvtSurface->clip_rect );
  118.         SDL_BlitSurface( imageSurface, &imageSurface->clip_rect, tempSurface, &imageSurface->clip_rect );
  119.               
  120.         /* Fix the alpha values */
  121.         SDL_LockSurface(tempSurface);
  122.         p = tempSurface->pixels;
  123.         for( y = (texture->txDimensions - 1) ;y >= 0; y-- )
  124.         {
  125.             for( x = (texture->txDimensions - 1); x >= 0; x-- )
  126.             {
  127.                 if( p[x+y*texture->txDimensions] != key )
  128.                 {
  129.                     #ifdef _LITTLE_ENDIAN
  130.                     p[x+y*texture->txDimensions] = p[x+y*texture->txDimensions] | 0xFF000000;
  131.                     #else
  132.                     p[x+y*texture->txDimensions] = p[x+y*texture->txDimensions] | 0x000000FF;
  133.                     #endif
  134.                 }
  135.             }
  136.         }
  137.         SDL_UnlockSurface(tempSurface);
  138.         
  139.         /* actually create the OpenGL textures */
  140.         glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, tempSurface->w, tempSurface->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, tempSurface->pixels );
  141.         
  142.         /* get rid of our SDL_Surfaces now that we're done with them */
  143.         SDL_FreeSurface( tempSurface );
  144.         SDL_FreeSurface( imageSurface );
  145.     }
  146.  
  147. }
  148.  
  149. /********************> GLTexture_GetTextureID() <*****/
  150. GLuint        GLTexture_GetTextureID( GLTexture *texture )
  151. {
  152.     
  153.     return texture->textureID;
  154.     
  155. }
  156.  
  157. /********************> GLTexture_GetImageHeight() <*****/
  158. GLsizei        GLTexture_GetImageHeight( GLTexture *texture )
  159. {
  160.     
  161.     return texture->imgHeight;
  162.     
  163. }
  164.  
  165. /********************> GLTexture_GetImageWidth() <*****/
  166. GLsizei        GLTexture_GetImageWidth( GLTexture *texture )
  167. {
  168.     
  169.     return texture->imgWidth;
  170.     
  171. }
  172.  
  173. /********************> GLTexture_GetDimensions() <*****/
  174. GLsizei        GLTexture_GetDimensions( GLTexture *texture )
  175. {
  176.     
  177.     return texture->txDimensions;
  178.     
  179. }
  180.  
  181. /********************> GLTexture_SetAlpha() <*****/
  182. void        GLTexture_SetAlpha( GLTexture *texture, GLfloat alpha )
  183. {
  184.     
  185.     texture->alpha = alpha;
  186.     
  187. }
  188.  
  189. /********************> GLTexture_GetAlpha() <*****/
  190. GLfloat        GLTexture_GetAlpha( GLTexture *texture )
  191. {
  192.     
  193.     return texture->alpha;
  194.     
  195. }
  196.  
  197. /********************> GLTexture_Release() <*****/
  198. void        GLTexture_Release( GLTexture *texture )
  199. {
  200.     
  201.     /* Delete the OpenGL texture */
  202.     glDeleteTextures( 1, &texture->textureID );
  203.     
  204.     /* Reset the other data */
  205.     texture->imgHeight = texture->imgWidth = texture->txDimensions = 0;
  206.     
  207. }
  208.