Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- GLuint glLoadImageFile( const char* fname, bool ulctrans = false, bool correct = true ){
- #if SDL_BYTEORDER == SDL_LIL_ENDIAN
- static const Uint32 rmask = 0x000000FF;
- static const Uint32 bmask = 0x0000FF00;
- static const Uint32 gmask = 0x00FF0000;
- static const Uint32 amask = 0xFF000000;
- #else
- static const Uint32 rmask = 0xFF000000;
- static const Uint32 bmask = 0x00FF0000;
- static const Uint32 gmask = 0x0000FF00;
- static const Uint32 amask = 0x000000FF;
- #endif
- SDL_Surface* surface = IMG_Load( fname );
- SDL_DisplayFormat( surface );
- SDL_Surface* newSurface = surface;
- if( correct ){
- SDL_PixelFormat *format = surface->format;
- Uint32 width = surface->w;
- Uint32 height = surface->h;
- Uint32 widthPow = (unsigned) pow( 2, ceil( log( width ) / log( 2 ) ) );
- Uint32 heightPow = (unsigned) pow( 2, ceil( log( height ) / log( 2 ) ) );
- // Create new empty surface.
- newSurface = SDL_CreateRGBSurface( SDL_SRCALPHA, widthPow, heightPow, 32, rmask, bmask, gmask, amask );
- SDL_DisplayFormat( newSurface );
- // Fill sprite with alpha.
- Uint32 alpha = 0;
- alpha = SDL_MapRGBA( format, 0, 0, 0, amask );
- SDL_Rect rect;
- rect.x = 0;
- rect.y = 0;
- rect.h = heightPow;
- rect.w = widthPow;
- int ret = SDL_FillRect( newSurface, &rect, alpha);
- surface->flags &= !SDL_SRCALPHA;
- SDL_SetAlpha( newSurface, SDL_SRCALPHA, SDL_ALPHA_TRANSPARENT );
- // Copy image data to our new surface.
- if( ulctrans ){
- SDL_SetColorKey( surface, SDL_SRCCOLORKEY, SDL_MapRGB(surface->format, 255, 0, 255) /* ((uint32_t*)(surface->pixels))[1]*/ );
- }
- ret = SDL_BlitSurface( surface, 0, newSurface, 0 );
- }
- GLuint texture;
- glGenTextures( 1, &texture );
- glBindTexture( GL_TEXTURE_2D, texture );
- gluBuild2DMipmaps( GL_TEXTURE_2D, 4, newSurface->w, newSurface->h, GL_RGBA, GL_UNSIGNED_BYTE, newSurface->pixels );
- SDL_FreeSurface( surface );
- if( correct )
- SDL_FreeSurface( newSurface );
- return texture;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement