I’m using LodePNG (which has been proven to work for me) to load a 32x32 png image that I use to create an SDL_Texture, then render it to the screen.
I’m using the Direct3D render on Windows (it’s what SDL chooses for me, I only ask for hardware accelerated double buffered).
Code:
Graphic::Graphic(const char* fname, unsigned long format)
{
m_width = 0;
m_height = 0;
m_texture = NULL;
unsigned char* buffer;
unsigned char* image;
size_t buffersize, imagesize;
LodePNG_Decoder decoder;
LodePNG_loadFile(&buffer, &buffersize, fname); /*load the image file with given filename*/
LodePNG_Decoder_init(&decoder);
LodePNG_decode(&decoder, &image, &imagesize, buffer, buffersize); /*decode the png*/
/*load and decode*/
/*if there's an error, display it, otherwise display information about the image*/
if(decoder.error) printf("PNG Decoding error: %d\n", decoder.error);
else
{
m_width = decoder.infoPng.width;
m_height = decoder.infoPng.height;
m_texture = SDL_CreateTexture(format, SDL_TEXTUREACCESS_STATIC, m_width, m_height);
if(!m_width || !m_height || !m_texture){
SDL_DestroyTexture(m_texture);
printf("Error: SDL could not build texture: %s\n", SDL_GetError());
m_width = m_height = -1;
m_texture = NULL;
}
else {
SDL_UpdateTexture(m_texture, NULL, image, 0);
}
}
free(image);
free(buffer);
LodePNG_Decoder_cleanup(&decoder);
}
…
while(true){
SDL_Rect loc;
loc.x = loc.w = loc.y = loc.h = 32;
Graphic testSprite(“testimg.png”, info.texture_formats[0]);
SDL_RenderCopy(testSprite.getTexture(), &loc, NULL);
SDL_RenderPresent();
SDL_Delay(1);
}
The result is that the window contents get messed up, and nothing looking like the image is even rendered.
is there anything I’m doing wrong here?------------------------
EM3 Nathaniel Fries, U.S. Navy