Loading PNG with SDL_Image

Started by
1 comment, last by gharen2 17 years, 1 month ago
I am trying to load and draw an image using SDL_Image to OpenGL, but its just drawing a big blue box, not even where the coords are. My image is 256x16. It has worked before and the only thing I've changes is the way it loads the images. Main code:

Image *img = graphics->loadImage("vital_bar.png");

graphics->beginScene();
	graphics->drawImage( img, Rect( 0, 0, 256, 16 ), Rect( 10, 10, 0, 0 ) );
graphics->endScene();
SDL_Delay(3000);

Load:

Image *VideoDriver::loadImage( std::string filename )
{
	SDL_Surface *tmp = IMG_Load( filename.c_str() );
	GLuint texture;
	
	if(tmp == NULL)
	{
		logger->write( "Faild to load image: %s", filename.c_str() );
		return false;
	}
	else
	{
		SDL_SetColorKey( tmp, SDL_SRCCOLORKEY, SDL_MapRGB( tmp->format, 255, 128, 0 ));

		if( ( tmp->w & (tmp->w - 1)) != 0 )
		{
			logger->write( "warning: %s's width is not a power of 2.", filename.c_str() );
		}

		if( (tmp->h & (tmp->h -1)) != 0 )
		{
			logger->write( "warning: %s's height is not a power of 2.", filename.c_str() );
		}

		glGenTextures( 1, &texture );
		glBindTexture( GL_TEXTURE_2D, texture );
		glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
		glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );

		glTexImage2D( GL_TEXTURE_2D, 0, 3, tmp->w, tmp->h, 0, GL_BGR, GL_UNSIGNED_BYTE, tmp->pixels );

		mHeight = tmp->h;
		mWidth = tmp->w;

		Image *img = new Image( texture );

		SDL_FreeSurface( tmp );

		return img;
	}
}
Image.h

#include "log.h"
#include <SDL_opengl.h>

extern Logger *logger;

class Image
{
public:
	Image( GLuint texture );
	GLuint getTexture();
	int getWidth();
	int getHeight();
private:
	GLuint mTexture;
	int mWidth, mHeight;
};


Drawing Code:

void VideoDriver::drawImage( Image *image, Rect &src, Rect &dest )
{
	if(image == NULL)
		return;

	float texX1	= src.x / (float)image->getWidth();
	float texY1 = src.y / (float)image->getHeight();
	float texX2 = (src.x + src.width) / (float)image->getWidth();
	float texY2 = (src.y + src.height) / (float)image->getHeight();

	glBindTexture( GL_TEXTURE_2D, image->getTexture() );
	
	glEnable( GL_TEXTURE_2D );

	glBegin( GL_QUADS );
		glTexCoord2f( texX1, texY1 );
        glVertex3f( (float)dest.x , (float)dest.y, 0 );

		glTexCoord2f( texX2, texY1 );
        glVertex3f( (float)dest.x + (float)src.width, (float)dest.y, 0 );

		glTexCoord2f( texX2, texY2 );
        glVertex3f( (float)dest.x + (float)src.width, (float)dest.y + (float)src.height, 0 );

		glTexCoord2f( texX1, texY2 );
        glVertex3f( (float)dest.x, (float)dest.y + (float)src.height, 0 );
    glEnd();

	glDisable( GL_TEXTURE_2D );

}
[/source]
Advertisement
Shameless bump. 18 views over 2 days and no replies. I feel special!
Check the values you're using as texture coordinates. A solid color quad suggests your texture coordinates are all (0,0), so you're only seeing the color of upper left-most pixel. (though I can't say this for certain, as I don't know what the texture looks like).

You say you've "only changed the way images are loaded", but that can have a lot of effects. For example, is your Image class returning the correct width and height? If not, this will mess up the texture coordinates you're calculating.

This topic is closed to new replies.

Advertisement