• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
rakoon2

loading a png with sdl + sdl_image.

25 posts in this topic

Hello guys! How can I load a png file using SDL_Image? My problem is that IMG_LoadPNG_RW( rwop ); returns a null pointer! Here is the code:
 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;
    }

Thank you! :/
0

Share this post


Link to post
Share on other sites
If your loading from an actual PNG file and not from memory then you can simoly use IMG_Load("Blah.png"). SDL_image will do the rest in determining which function to use to load the image based on it's extension.
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
Here's what I did to load a png (this is in C++):

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
0

Share this post


Link to post
Share on other sites
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
0

Share this post


Link to post
Share on other sites
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
}
0

Share this post


Link to post
Share on other sites
I just copied over the meat of the function, there is a lot of other stuff that goes on there. When my program gets to that code I already know that the image is valid (I store it in a cache before I come to this part of the code, and if it can't be loaded into the cache I return an error). So I've got the IMG_Load failure check covered.

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
0

Share this post


Link to post
Share on other sites
Thank you! But I still get

[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



:(?
0

Share this post


Link to post
Share on other sites
This is likely to be an issue with your compiler and setup rather than the code. Try experimenting with the order of the libraries as gcc requires them to be in a certain order (which I forget).
0

Share this post


Link to post
Share on other sites
Hmm. I tried 10 different orders.. still the same linker error.
Maybe my lib is broken? Where can I download it again? *forgot it*
Thank you..!
0

Share this post


Link to post
Share on other sites
Visit http://www.devpaks.org for all your devpak needs.

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.
0

Share this post


Link to post
Share on other sites
It works now! The lib was broken~! =3

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
0

Share this post


Link to post
Share on other sites
Your power of 2 checking is wrong.

I also don't believe you need the GL_UNPACK_ROW_LENGTH with a software surface that you've just created, but I don't see it doing any harm.
0

Share this post


Link to post
Share on other sites
PNG files have several advantages over BMPs when it comes to games - file size perhaps being the most important.
0

Share this post


Link to post
Share on other sites
Quote:
Original post by Spudder
PNG files have several advantages over BMPs when it comes to games - file size perhaps being the most important.

... and an alpha channel, required for transparency in sprites.
0

Share this post


Link to post
Share on other sites
Well, I am using OpenGl. I still have problems with those damn linker errors!

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!

0

Share this post


Link to post
Share on other sites
If the linker errors are bothering you that much then jut add the source for SDL_image into your project, you are going to need to link to a few other libs if you do though - zlib, libpng and jpeblib but if you are only using PNG images you could just rip the PNG loading code.

0

Share this post


Link to post
Share on other sites
Then there's something seriously wrong with either your linker or your setup.

You can get a copy of VS.NET academic for around $100 or so, or you might even be able to find a copy on eBay or something similiar. It's definitely worth the investment but your situation may not allow it.
0

Share this post


Link to post
Share on other sites
Quote:
[Linker error] undefined reference to `IMG_Load'

You probably have sdl_image downloaded from http://devpaks.org/. right ? that pack was fscked last time i checked. download its source code and build the lib yourself or PM me for my libSDL_image.a and SDL_image.dll files.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0