Jump to content
  • Advertisement
Sign in to follow this  
  • entries
  • comments
  • views

Sono Pazzi Questi Romani

Sign in to follow this  


I really wish I had something to say here, but I don't. We mostly made our milestone last week and so I slept for a large portion of the weekend. But it looks like we're going to continue in lesser crunch mode for the rest of the year. So now I'm looking forward to Christmas for more than just the prezzies.

So, one thing that I've been "working" on for the last week is figuring out how I'm going to work animations into the game. I've been using FreeImage to load up images, and that works really well (I happily recommend it to anyone). But now I want to load up animations strips that contain metadata. I could do this with FreeImage tags. However, since I'm mostly using PNG images anyway I ought to be able to put the metadata into custom chunks in the PNGs. At least, that's what the specs say. So I thought I'd have a look at using libpng to load and save PNGs again.

My last effort with libpng was a failure, and this time didn't fare any better. Why, oh why, can I not load a PNG using libpng!!? This has nothing to do with anything fancy, all I want to do is load a simple freaking texture. I've gone through the documentation and I'm pretty sure that I know roughly what is going on, but apparently not.

I've gotten to the point where I figure it must be something like a conflict between the libpng and zlib libraries and the way I've set up my projects in visual studio, but I'll be damned if I can figure out what. So this is a plea to the GDNet community -- PUHLEEEEASE HELP MEEEE!!! Have you also suffered defeat at the hands of libpng? Or, were you able bend it to your will? Vent your spleen or share you wisdom!

Here's a code snippet to show what I got. It's pretty much what you find in the library docs, but obviously there's something amiss.

static const int PNG_HEADER_LENGTH = 8;

void LoadPNG( const char* filename )
unsigned char header[PNG_HEADER_LENGTH];
FILE *fp;

// Open the file and check that it has the expected header
if ( ( fp = fopen( filename, "rb" ) ) == NULL )
throw new std::exception("Unable to open file");

fread( header, 1, PNG_HEADER_LENGTH, fp );

if (png_sig_cmp(header, 0, PNG_HEADER_LENGTH))
fclose( fp );
throw new std::exception("This is not a PNG file");

// Create and initialize png_struct
png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL );
if ( !png_ptr )
fclose( fp );
throw new std::exception("Unable to create png_ptr");

// Create and initialize png_info. This contains the image information.
png_infop info_ptr = png_create_info_struct(png_ptr);
if ( !info_ptr )
png_destroy_read_struct(&png_ptr, png_infopp_NULL, png_infopp_NULL);
throw new std::exception("Unable to create info_ptr");

// Something to do with error handling that I don't understand
if ( setjmp( png_jmpbuf( png_ptr ) ) )
png_destroy_read_struct(&png_ptr, png_infopp_NULL, png_infopp_NULL);
fclose( fp );
throw new std::exception("Problem occured when calling setjmp()");

// Initialize IO
png_init_io(png_ptr, fp);

// Tell the library that we've already read the header
png_set_sig_bytes(png_ptr, PNG_HEADER_LENGTH);

// Read in the whole image. This is where I get an unhandled exception.
png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, png_voidp_NULL);

// We're done, so close the file.
fclose( fp );


Yay! It looks like I might have it working! At least, it isn't crashing at the moment. I don't have time to test it because I have to go to work now, but it seems that the problem was related to using different runtime libraries. Compiling zlib and libpng in the same solution as the project I'm working on seems to be the answer.
Sign in to follow this  


Recommended Comments

There are no comments to display.

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
  • 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!