PNG loading with libpng

Started by
2 comments, last by Omerta7486 5 years, 7 months ago

Hey, everyone,

I've been teaching myself C++ for the past week, although I've been coding and tinkering with games for almost 16 years. I'm comfortable with the C++ syntax already and have been chugging along fine, but I've hit a bit of a snag. I'm using CodeBlocks 17.12, SDL 2.0.8, and SDL Image 2.0.3, and I am trying to load a PNG file in for use as a sprite. I have SDL linked perfectly fine, and have been messing around with it's commands for a couple of days with no problems. Loading BMPs, editing pixel matrices, getting user input, basic loop timing and flow, etc. Yesterday, I tried to install SDL Image, and I have the library installed to the point that CodeBlocks can pick up it's syntax and I can use auto finish on it's commands, and there are no compilation errors. The problem comes when I run my code. Through process of elimination(thanks to my in-built error checking), I have narrowed it down to this code:


	/***   -INITIALIZE PNG LOADING-   ***/
	int flags=IMG_INIT_PNG;
	int initted=IMG_Init(flags);
	if((initted&&flags) != flags)
	{
    	SDL_Log("Unable to initialize image load: %s", IMG_GetError());
		return EXIT_FAILURE;
	}

	/***   -LOAD IMAGES-   ***/
	SDL_Surface *image;
	SDL_RWops *rwop;
	rwop=SDL_RWFromFile("\media\die.png", "rb");
	image=IMG_LoadPNG_RW(rwop);
	if(!image)
	{
    	SDL_Log("Unable to initialize load image: %s", IMG_GetError());
		return EXIT_FAILURE;
	}

If I comment block all the Load Images part it runs(even with the PNG Loading part still in), but if I dont, it exits due to the error.

After digging around to see what could be wrong I went to the SDL Image website to read that PNG files may need libPNG and zlib. I tried for hours to install those two libraries, but it looks like no matter which archive I download it's just full of UNIX garbage that I can't use on Windows. This, however, doesn't seem to be the path I needed to take, as through testing BMPs do the same thing(Which SDL Image says should load natively). I don't know if there is an error in my code, or if I need libPNG and zlib. I'm lost here.

Help would be greatly appreciated, thanks,

-Omerta

√?²

The knowledge that's seeking the favor of another means the murder of self.

Advertisement

I've never used SDL image. My experience with the SDL extensions is that there are typically better options out there and the extensions aren't all that great. For example, I bet you stb_image.h would be a lot easier to use: https://aras-p.info/blog/2007/05/28/now-thats-what-i-call-a-good-api-stb_image/

You can load and then delete an image with 3 lines of code.

Lots of libraries that load PNGs use zlib -- zlib is an implementation of DEFLATE, the compression PNG files use. zlib is, in my opinion, very *old* code with a poor API. It's main purpose is largely educaitonal and for posterity. There's no reason you have to use it for a game. The same exact story goes for libpng (which depends on zlib). You do not need to install zlib, or libpng, or any other dependencies, if you pick a good implementation (like the stb_image.h file I linked you by Sean).

@Randy Gaul Thank you. I'm going to begin testing stb_image. it seems very promising.

 

@fleabay It's definitely not the image files. I used two different pngs, plus I tried it with a tif and bmp. I created all files myself, and as a studied graphic artist I highly doubt I did anything wrong with them. 8bpc/32bpp/standard compression for the pngs. Standard 24bpp true color format for the bmp. The tif I created just to round my bases. I've never used tif, but I used default settings on save, so... One question: I see you used '/' instead of '\\', are you on Unix or Linux? That may be the difference. I'm on Windows 10.

 

As a side note, I did notice that I hadn't escaped my backslashes, and I also didn't use the origin identifier in my path. But, even after fixing my blunder, it still crashes upon loading ANY image.

 

[UPDATE] I have gotten SDL_Image to work. There were 2 problems, I noticed I didn't have all of the dlls in the right places, and I also hadn't input the path correctly. Here it is, working:

Untitled.png.b77871b44c50217ae86a81328784725b.png

Thank you, both. You were both very helpful!

√?²

The knowledge that's seeking the favor of another means the murder of self.

This topic is closed to new replies.

Advertisement