if( sprite != NULL ) { SDL_FreeSurface( sprite ); sprite = NULL; } SDL_RWops* rwop(NULL); rwop = SDL_RWFromFile( filename, "rb"); if( rwop == NULL ) { LOG << "SDL_RWFromFile error " << filename << " " << ENDL; } sprite = IMG_LoadPNG_RW( rwop ); delete rwop; if ( sprite == NULL ) { LOG << "Couldn't load " << filename << " -- " << ENDL; return false; }
loading a png with sdl + sdl_image.
#1 Members - Reputation: 140
Posted 03 September 2004 - 10:44 PM
#6 Members - Reputation: 661
Posted 04 September 2004 - 09:08 AM
string filename = "my_image.png";
SDL_Surface *new_surf; // this points to the image data once it is loaded
new_surf = IMG_Load(filename.c_str()); // We need to convert the string to a C-type string
new_surf = SDL_DisplayFormat(new_surf); // convert the image format to fit the screen for fast blitting
if (new_surf == NULL) { // typo in filename arg or missing/corrupt file are the most likely errors to cause this
cout << "ERROR: Could not load image " << filename << endl;
exit(1);
}
To compile it (in Linux):
$g++ myfile.cpp -o test `sdl-config --cflags --libs` -lSDL_image
A free, open-source 2D RPG in development.
Latest release Oct. 10th, 2010.
#7 Members - Reputation: 794
Posted 04 September 2004 - 10:17 AM
Quote:
Original post by Spudder
You need to link to SDL_image, add -lSDL_image to your linker options and make sure you have the SDL import library where the linker can find it.
Hey thanks Spudder! I've been trying to compile my game for the last two hours and I couldn't figure out what I was doing wrong. I _was_ using
gcc -Wall asteroids.c -o asteroids `sdl-config --libs --cflags`
Which was giving me undefined references errors. I'm now compiling like this:
gcc -Wall asteroids.c -lSDL_gfx -o asteroids `sdl-config --libs --cflags`
and it is working perfectly! Thanks for the tip.
- Stephen
#8 Members - Reputation: 276
Posted 04 September 2004 - 05:19 PM
Quote:
Original post by Roots
SDL_Surface *new_surf; // this points to the image data once it is loaded
new_surf = IMG_Load(filename.c_str()); // We need to convert the string to a C-type string
new_surf = SDL_DisplayFormat(new_surf); // convert the image format to fit the screen for fast blitting
Careful.. that's a memory leak (and a segfault waiting to happen)
SDL_DisplayFormat creates a NEW surface. The old one is still there.
You need to store the DisplayFormat pointer somewhere else, and SDL_FreeSurface() the new_surf (which is now the old surf)
Also if IMG_Load returns NULL, you'll segfault in SDL_DisplayFormat. Instead you should do something like:
new_surf = IMG_Load(filename.c_str());
if (!new_surf){
printf("Couldn't load %s:%s\n",filename.c_str(),IMG_GetError());
//Exit, return error code, or throw an exception
}
#9 Members - Reputation: 661
Posted 04 September 2004 - 07:04 PM
Thanks for the tip on the memory leak though, I totally spaced that out. Yeah I just looked it up in my book and you are absolutely correct. My code has been running without seg faulting though. Hmm. Oh well, its an easy fix :D
A free, open-source 2D RPG in development.
Latest release Oct. 10th, 2010.
#10 Members - Reputation: 140
Posted 20 September 2004 - 10:31 PM
[Linker error] undefined reference to `IMG_Load'
../../../Programme/dev-c++/sdl_ming/libSDL.dll.a
../../../Programme/dev-c++/sdl_ming/libSDL_image.a
../../../Programme/dev-c++/sdl_ming/libSDLmain.a
../../../Programme/dev-c++/sdl_ming/libSDL.a
E:/Programme/dev-c++/lib/libopengl32.a
:(?
#13 Members - Reputation: 463
Posted 22 September 2004 - 10:10 AM
GCC, in its usual sane fashion, requires libraries in reverse order. ie, if libSDL_net requires a function from libwsock32, then they must be ordered g++ ... -lSDL_net -lwsock32 .... (That one drove me bonkers for a while).
The gcc manual seems to agree with me, although the wording is a little confusing.
#14 Members - Reputation: 140
Posted 23 September 2004 - 08:40 AM
Thank you! :)
Is this correct? ( texture loading ) XD :) ;)
bool Texture::loadImage( const char* filename )
{
SDL_Surface* sprite = IMG_Load( filename );
if( !sprite )
{
LOG << "Could not load " << filename
<< " bool Texture::loadImage( const char* filename ) "
<< ENDL;
return false;
}
else if( sprite->w != sprite->h )
{
LOG << "Texture w and h are not the same! Filename: " << filename
<< " W: " << sprite->w << " H " << sprite->h << ENDL;
return false;
}
else if( sprite->w % 2 != 0 )
{
LOG << "Texture w/h are not a power of 2! w/h: " << sprite->w << ENDL;
}
texture_wh = sprite->w;
SDL_Surface* conv;
// convert to 32-bit RGBA format
conv = SDL_CreateRGBSurface( SDL_SWSURFACE, sprite->w, sprite->h, 32,
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
#else
0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
#endif
SDL_BlitSurface( sprite, 0, conv, 0);
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 );
glPixelStorei( GL_UNPACK_ROW_LENGTH, conv->pitch / conv->format->BytesPerPixel );
glTexImage2D( GL_TEXTURE_2D, 0, 3, conv->w, conv->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, conv->pixels);
glPixelStorei( GL_UNPACK_ROW_LENGTH, 0 );
SDL_FreeSurface(conv);
SDL_FreeSurface(sprite);
return true;
}
Thanks ;d
#20 Members - Reputation: 140
Posted 03 October 2004 - 03:40 AM
My engine lib (.a) has a texture class.
And as you can see this line in it:
SDL_Surface* sprite = IMG_Load( filename );
It compiles fine. But I get
[Linker error] undefined reference to `IMG_Load'
if I want to use the the Texture class in one of my other projects(editor) that use the lib!
Here the linker from my editor:
../sdl_ogl_engine_lib/ogl_sdl_2d_engine_lib.a
E:/Programme/dev-c++/sdl_ming/libSDL.a
E:/Programme/dev-c++/sdl_ming/libSDL.dll.a
E:/Programme/dev-c++/sdl_ming/libSDLmain.a
E:/Programme/dev-c++/sdl_ming/SDL_image.a
E:/Programme/dev-c++/open_gl/libopengl32.a
E:/Programme/dev-c++/f_mod/libfmod.a
I am using Dev-C++.
Help please! :((( (i hate link errors)
Thank you!






