Skip to main content
gamedev.net
🔒 Locked

SDL PixelFormat question

Started by Lazy Foo Oct 11, 2005 at 9:19 PM 2 replies 1.2k views
Original Post
Lazy Foo
Lazy Foo
What exactly is [RGBA]shift and [RGBA]loss? I have this texture loading function here:

/////////////////////////////////////////////////
void TextQuad::load_image( std::string filename )
/////////////////////////////////////////////////
{    
    //The loaded surface
    SDL_Surface *LoadedImage = NULL; 
    
    //The optimized surface
    SDL_Surface *OptimizedImage = NULL; 

    //The texture surface
    SDL_Surface *TextureImage = NULL; 
    
    //load image
    LoadedImage = IMG_Load( filename.c_str() );
    
    //If the image loaded fine
    if( LoadedImage != NULL )
    {
        //The format
        SDL_PixelFormat format;
        
        //Set format
        format.palette = NULL;
        format.BitsPerPixel = 32;
        format.BytesPerPixel = 4;
        
        //Set mask
        #if SDL_BYTEORDER == SDL_BIG_ENDIAN
        format.Rmask = 0xFF000000;
        format.Gmask = 0x00FF0000;
        format.Bmask = 0x0000FF00;
        format.Amask = 0x000000FF;
        #else
        format.Rmask = 0x000000FF;
        format.Gmask = 0x0000FF00;
        format.Bmask = 0x00FF0000;
        format.Amask = 0xFF000000;
        #endif
        
        //Set shift
        format.Rshift;
        format.Gshift;
        format.Bshift;
        format.Ashift;
        
        //Set loss
        format.Rloss;
        format.Gloss;
        format.Bloss;
        format.Aloss;
        
        //Optimize image
        TextureImage = SDL_ConvertSurface( LoadedImage, &format, SDL_SWSURFACE );
        
        //Get rid of temp surface
        SDL_FreeSurface( LoadedImage );
    }
    
    //Convert to 2^n surface
    
    
    //If the image loaded fine
    if( TextureImage != NULL )
    {  
        //Get the dimensions of surface
        w = TextureImage->w;
        h = TextureImage->h;
            
        //Generate texture
        glGenTextures( 1, &texture );

        //Use newly made texture
        glBindTexture( GL_TEXTURE_2D, texture );
        
        //Create texture from loaded surface
        glTexImage2D( GL_TEXTURE_2D, 0, 4, TextureImage->w, TextureImage->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, TextureImage->pixels );
	    
        //Set filtering
        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
	    
        //Get rid of temp surface
        SDL_FreeSurface( TextureImage );
    }
}

It works, its just that I don't like those stray values there. I want to set those to the proper value but I don't even know what they do.
Learn to make games with my SDL 2 Tutorials
evillive2
evillive2
First of all I don't know what either of them do exactly but I don't think it matters. You can use CreateRGBSurface instead and by setting the bit depth all those things get set for you. You can then Blit one surface to the next or manually copy the pixels over. I think you can still blit from surface to surface with SDL_OPENGL set as long as you don't try to blit to the screen but I am probably wrong.

I use something similar in my 2d OpenGL apps where I load the image using SDL_Image, create a new 32 bit surface of the correct power of 2 size then copy the pixels to the new surface. I copy the pixels manually since depending on the system I may need to load in RGBA or BGRA where I have to switch the R & B pixel values for the image to show up right.

Take a look at PnP Bios's hxRender library too. There is some good stuff in there. I know it helped me a ton.

edit: BTW, nice tutorials.

[edit]
The ConvertSurface function is used internally in the SDL_DisplayFormat function where they do most of this for you. However to answer your question the [rgba]shift is the binary left shift of each color component. This is figured out by the mask of the color component. I hope that makes sense. Then I don't know how the loss is determined or even what it's use is but according to the docs it is the precision loss of each color component. Perhaps meant for 16 bit surfaces where the components might be 555x or 565x for some internal use by the blitters.
[/edit]


[Edited by - evillive2 on October 11, 2005 11:28:27 PM]
Evillive2
BleedingBlue
BleedingBlue
The thing is, is those "hanging values" dont really need to be edited by you, if you load the surface from an image, then they will be set automatically to whatever is needed, and if you create an RGB surf, again filled in for you, even palette you dont have to set to null, since unless you use it, it will be null, or undefined, either way, doesnt matter, only <=8bit colors will use the palette. I've always just left parameters in SDL I dont need alone, and I have never had an issue.
--Edit--
http://docs.mandragor.org/files/Common_libs_documentation/SDL/SDL_Documentation_project_en/sdlpixelformat.html
KindFred
KindFred
You're handling the error all wrong. If something goes wrong, you should print a message to stdout or stderr and exit gracefully. You shouldn't be editing any of the pixel format's values on your own.

edit: My bad... I was reading your code all wrong. You still shouldn't be messing with those values on your own. What is it that you're trying to accomplish with this?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.