Advertisement
Aslai

Untitled

Oct 25th, 2012
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.15 KB | None | 0 0
  1. GLuint glLoadImageFile( const char* fname, bool ulctrans = false, bool correct = true ){
  2.     #if SDL_BYTEORDER == SDL_LIL_ENDIAN
  3.        static const Uint32 rmask = 0x000000FF;
  4.        static const Uint32 bmask = 0x0000FF00;
  5.        static const Uint32 gmask = 0x00FF0000;
  6.        static const Uint32 amask = 0xFF000000;
  7.     #else
  8.        static const Uint32 rmask = 0xFF000000;
  9.        static const Uint32 bmask = 0x00FF0000;
  10.        static const Uint32 gmask = 0x0000FF00;
  11.        static const Uint32 amask = 0x000000FF;
  12.     #endif
  13.     SDL_Surface* surface = IMG_Load( fname );
  14.     SDL_DisplayFormat( surface );
  15.     SDL_Surface* newSurface = surface;
  16.     if( correct ){
  17.         SDL_PixelFormat *format = surface->format;
  18.         Uint32 width = surface->w;
  19.         Uint32 height = surface->h;
  20.         Uint32 widthPow = (unsigned) pow( 2, ceil( log( width ) / log( 2 ) ) );
  21.         Uint32 heightPow = (unsigned) pow( 2, ceil( log( height ) / log( 2 ) ) );
  22.         // Create new empty surface.
  23.         newSurface = SDL_CreateRGBSurface( SDL_SRCALPHA, widthPow, heightPow, 32, rmask, bmask, gmask, amask );
  24.         SDL_DisplayFormat( newSurface );
  25.         // Fill sprite with alpha.
  26.         Uint32 alpha = 0;
  27.         alpha = SDL_MapRGBA( format, 0, 0, 0, amask );
  28.         SDL_Rect rect;
  29.         rect.x = 0;
  30.         rect.y = 0;
  31.         rect.h = heightPow;
  32.         rect.w = widthPow;
  33.         int ret = SDL_FillRect( newSurface, &rect, alpha);
  34.         surface->flags &= !SDL_SRCALPHA;
  35.         SDL_SetAlpha( newSurface, SDL_SRCALPHA, SDL_ALPHA_TRANSPARENT );
  36.         // Copy image data to our new surface.
  37.         if( ulctrans ){
  38.         SDL_SetColorKey( surface, SDL_SRCCOLORKEY, SDL_MapRGB(surface->format, 255, 0, 255) /* ((uint32_t*)(surface->pixels))[1]*/ );
  39.         }
  40.         ret = SDL_BlitSurface( surface, 0, newSurface, 0 );
  41.     }
  42.     GLuint texture;
  43.     glGenTextures( 1, &texture );
  44.     glBindTexture( GL_TEXTURE_2D, texture );
  45.     gluBuild2DMipmaps( GL_TEXTURE_2D, 4, newSurface->w, newSurface->h, GL_RGBA, GL_UNSIGNED_BYTE, newSurface->pixels );
  46.     SDL_FreeSurface( surface );
  47.     if( correct )
  48.         SDL_FreeSurface( newSurface );
  49.     return texture;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement