Jump to content
  • Advertisement
Sign in to follow this  
Ruddie

[SDL] - Images not correctly loading

This topic is 3004 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi their! I'm following (lazyfoo's) tutorials on SDL right now and I'm stumbled onto a problem.

I found out that for some reason PNG's and JPEG's are not correctly loading (they stay null). When running to code it exits with an return 1 error. I found out the error is caused by the IMG_Load() function supported with SDL_Image.

It's quite strange, because when I load in a .bmp or .gif file with the same function, it does work.

Code:


#include "SDL.h"
#include "SDL_image.h"

#include <string>

const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;

//Images
SDL_Surface *foo = NULL;
SDL_Surface *background = NULL;
SDL_Surface *screen = NULL;

//event
SDL_Event event;

SDL_Surface *load_image( std::string filename )
{
// temp storage
SDL_Surface* loadedImage = NULL;

//optimized
SDL_Surface* optimizedImage = NULL;
loadedImage = IMG_Load( filename.c_str() );

if ( loadedImage != NULL )
{
//create optimized
optimizedImage = SDL_DisplayFormat( loadedImage );

//free old one
SDL_FreeSurface( loadedImage );
}

return optimizedImage;
}

void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination )
{
//temp rectangle
SDL_Rect offset;

offset.x = x;
offset.y = y;

//blit
SDL_BlitSurface ( source, NULL, destination, &offset );
}

bool init()
{
//init SDL
if ( SDL_Init( SDL_INIT_EVERYTHING ) == -1 ){
return false;
}

// set up screen
screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
if ( screen == NULL )
{
return false;
}
//caption
SDL_WM_SetCaption( "SDL Tutorial", NULL );
return true;
}

bool load_files()
{
background = load_image( "background.jpeg" );
foo = load_image( "foo.jpeg" );

if ( background == NULL || foo == NULL )
{
return false;
}
return true;
}

void clean_up()
{
SDL_FreeSurface( foo );
SDL_FreeSurface( background );

SDL_QUIT;
}

int main( int argc, char* args[] )
{
bool quit = false;

if ( init() == false )
{
return 1;
}
if ( load_files() == false )
{
return 1;
}

apply_surface( 0, 0, background, screen );
apply_surface( 240, 190, foo, screen );

//Update the screen
if( SDL_Flip( screen ) == -1 )
{
return 1;
}

while ( quit == false )
{
while( SDL_PollEvent( &event ) )
{
if ( event.type == SDL_QUIT )
{
quit = true;
}
}
}
clean_up();

return 0;
}




I'm using visual C++ 2008 express

Any suggestions?

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by Kambiz
According to http://www.libsdl.org/projects/SDL_image/, "JPEG, PNG, and TIFF image loading libraries are dynamically loaded, ...".
Thus, if you want to load jpeg or png, you need to compile and include required shared libraries (libpng, libz and libjpeg).


Thanks, I should get it working now ^^!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!