Jump to content

  • Log In with Google      Sign In   
  • Create Account

multiple png textures in 2d game


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 QW3RTY ROCKS   Members   -  Reputation: 100

Like
0Likes
Like

Posted 08 April 2012 - 04:43 AM

So I was trying to make a very basic 2d game and I looked at some code for loading png files from this blog post: http://blog.nobel-jo...l-using-libpng/
The problem I face is that whenever I try to use more than one image I am not sure how to exactly select what file to reference.

Here is the main file:

//=================================================================//
//=================================================================//
//========================== Includes =============================//
//=================================================================//
//=================================================================//
#include <GL/glut.h>
#include <png.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <string>
#include "vars.h"
using namespace std;
//=================================================================//
//=================================================================//
//========================= Global Vars ===========================//
//=================================================================//
//=================================================================//
GLubyte *textureImage;
GLubyte *playerTexture;
bool loadPngImage(char *name, int &outWidth, int &outHeight, bool &outHasAlpha, GLubyte **outData);
int init_Game_Res();
int setImage(string Image);
GLuint tex_obj[2] = {0};
//=================================================================//
//=================================================================//
//========================== Functions ============================//
//=================================================================//
//=================================================================//
void init() {			  //========= Init()
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_BLEND);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_TEXTURE_2D);
	glShadeModel(GL_FLAT);
	init_Game_Res();
}
void handleResize(int w, int h) {		 //========= handleResize()
//Tell OpenGL how to convert from coordinates to pixel values
glViewport(0, 0, w, h);

glMatrixMode(GL_PROJECTION); //Switch to setting the camera perspective

//Set the camera perspective
glLoadIdentity(); //Reset the camera
gluPerspective(45.0,				  //The camera angle
	   (double)w / (double)h, //The width-to-height ratio
	   1.0,				   //The near z clipping coordinate
	   500.0);				//The far z clipping coordinate
}
void drawScene() {			 //========= drawScene()
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glBegin(GL_QUADS);
setImage("test");
glBindTexture(GL_TEXTURE_2D,tex_obj[0]);
glTexCoord2f(0.0, 0.0);
glVertex3f(1.0f, 1.0f, ZOOM);
glTexCoord2f(0.0, 1.0);
glVertex3f(1.0f, 2.0f, ZOOM);
glTexCoord2f(1.0, 1.0);
glVertex3f(2.0f, 2.0f, ZOOM);
glTexCoord2f(1.0, 0.0);
glVertex3f(2.0f, 1.0f, ZOOM);
setImage("player");
glBindTexture(GL_TEXTURE_2D,tex_obj[1]);
glTexCoord2f(0.0, 0.0);
glVertex3f(-1.0f, 1.0f, ZOOM);
glTexCoord2f(0.0, 1.0);
glVertex3f(-1.0f, 2.0f, ZOOM);
glTexCoord2f(1.0, 1.0);
glVertex3f(0.0f, 2.0f, ZOOM);
glTexCoord2f(1.0, 0.0);
glVertex3f(0.0f, 1.0f, ZOOM);
glEnd();
glutSwapBuffers();
}
int main(int argc, char** argv){		 //========= main()
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(320, 240);
glutCreateWindow(APP_TITLE);
init();
glutDisplayFunc(drawScene);
glutReshapeFunc(handleResize);
glutMainLoop();
return 0;
}

And then the file which deals with png files:

#include <GL/glut.h>
#include <png.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <string>
#include "vars.h"
using namespace std;
int init_Game_Res(){
	glGenTextures(2,tex_obj);
	glBindTexture(GL_TEXTURE_2D, tex_obj[0]); //bind the first texture
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glBindTexture(GL_TEXTURE_2D, tex_obj[1]); //bind the second texture
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	int width, height;
	bool hasAlpha;
	char filename[] = "assets/test.png";
	bool success = loadPngImage(filename, width, height, hasAlpha, &textureImage);
	if (!success) {
		std::cout << "Unable to load png file: " << filename << std::endl;
		return 0;
	}
	std::cout << "Image: " << filename << " loaded " << width << " " << height << " alpha " << hasAlpha << std::endl;
	glBindTexture(GL_TEXTURE_2D,tex_obj[0]);
	//glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
	glTexImage2D(GL_TEXTURE_2D, 0, hasAlpha ? 4 : 3, width,
			height, 0, hasAlpha ? GL_RGBA : GL_RGB, GL_UNSIGNED_BYTE,
			textureImage);
	char filename2[] = "assets/player.png";
	success = loadPngImage(filename2, width, height, hasAlpha, &playerTexture);
	if (!success) {
		std::cout << "Unable to load png file: " << filename2 << std::endl;
		return 0;
	}
	std::cout << "Image: " << filename2 << " loaded " << width << " " << height << " alpha " << hasAlpha << std::endl;
	glBindTexture(GL_TEXTURE_2D,tex_obj[1]);
	//glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
	glTexImage2D(GL_TEXTURE_2D, 0, hasAlpha ? 4 : 3, width,
			height, 0, hasAlpha ? GL_RGBA : GL_RGB, GL_UNSIGNED_BYTE,
			playerTexture);
	cout << tex_obj[0] << "\n";
	cout << tex_obj[1] << "\n";
return 0;
}
int setImage(string Image){
	if(Image == "test"){
		glBindTexture(GL_TEXTURE_2D,tex_obj[0]);
		return 0;
	}
	if(Image == "player"){
		glBindTexture(GL_TEXTURE_2D,tex_obj[1]);
		return 0;
	}
}
bool loadPngImage(char *name, int &outWidth, int &outHeight, bool &outHasAlpha, GLubyte **outData) {
	png_structp png_ptr;
	png_infop info_ptr;
	unsigned int sig_read = 0;
	int color_type, interlace_type;
	FILE *fp;
	if ((fp = fopen(name, "rb")) == NULL)
		return false;
	/* Create and initialize the png_struct
	 * with the desired error handler
	 * functions.  If you want to use the
	 * default stderr and longjump method,
	 * you can supply NULL for the last
	 * three parameters.  We also supply the
	 * the compiler header file version, so
	 * that we know if the application
	 * was compiled with a compatible version
	 * of the library.  REQUIRED
	 */
	png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
			NULL, NULL, NULL);
	if (png_ptr == NULL) {
		fclose(fp);
		return false;
	}
	/* Allocate/initialize the memory
	 * for image information.  REQUIRED. */
	info_ptr = png_create_info_struct(png_ptr);
	if (info_ptr == NULL) {
		fclose(fp);
		png_destroy_read_struct(&png_ptr, png_infopp_NULL, png_infopp_NULL);
		return false;
	}
	/* Set error handling if you are
	 * using the setjmp/longjmp method
	 * (this is the normal method of
	 * doing things with libpng).
	 * REQUIRED unless you  set up
	 * your own error handlers in
	 * the png_create_read_struct()
	 * earlier.
	 */
	if (setjmp(png_jmpbuf(png_ptr))) {
		/* Free all of the memory associated
		 * with the png_ptr and info_ptr */
		png_destroy_read_struct(&png_ptr, &info_ptr, png_infopp_NULL);
		fclose(fp);
		/* If we get here, we had a
		 * problem reading the file */
		return false;
	}
	/* Set up the output control if
	 * you are using standard C streams */
	png_init_io(png_ptr, fp);
	/* If we have already
	 * read some of the signature */
	png_set_sig_bytes(png_ptr, sig_read);
	/*
	 * If you have enough memory to read
	 * in the entire image at once, and
	 * you need to specify only
	 * transforms that can be controlled
	 * with one of the PNG_TRANSFORM_*
	 * bits (this presently excludes
	 * dithering, filling, setting
	 * background, and doing gamma
	 * adjustment), then you can read the
	 * entire image (including pixels)
	 * into the info structure with this
	 * call
	 *
	 * PNG_TRANSFORM_STRIP_16 |
	 * PNG_TRANSFORM_PACKING  forces 8 bit
	 * PNG_TRANSFORM_EXPAND forces to
	 *  expand a palette into RGB
	 */
	png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_STRIP_16 | PNG_TRANSFORM_PACKING | PNG_TRANSFORM_EXPAND, png_voidp_NULL);
	outWidth = info_ptr->width;
	outHeight = info_ptr->height;
	switch (info_ptr->color_type) {
		case PNG_COLOR_TYPE_RGBA:
			outHasAlpha = true;
			break;
		case PNG_COLOR_TYPE_RGB:
			outHasAlpha = false;
			break;
		default:
			std::cout << "Color type " << info_ptr->color_type << " not supported" << std::endl;
			png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
			fclose(fp);
			return false;
	}
	unsigned int row_bytes = png_get_rowbytes(png_ptr, info_ptr);
	*outData = (unsigned char*) malloc(row_bytes * outHeight);
	png_bytepp row_pointers = png_get_rows(png_ptr, info_ptr);
	for (int i = 0; i < outHeight; i++) {
		// note that png is ordered top to
		// bottom, but OpenGL expect it bottom to top
		// so the order or swapped
		memcpy(*outData+(row_bytes * (outHeight-1-i)), row_pointers[i], row_bytes);
	}
	/* Clean up after the read,
	 * and free any memory allocated */
	png_destroy_read_struct(&png_ptr, &info_ptr, png_infopp_NULL);
	/* Close the file */
	fclose(fp);
	/* That's it */
	return true;
}

Any help would be great!

Sponsor:

#2 Rld_   Members   -  Reputation: 1521

Like
0Likes
Like

Posted 08 April 2012 - 07:09 AM

You will want to make a class for your sprites so you can create different objects. In semi pseudo code:

class Sprite
{
public:
   Sprite();
   ~Sprite();
   int LoadImage(char* a_File);
}
///////////// implemenation
Sprite::Sprite()
{
   // init stuff you need
}
int Sprite::LoadImage(char* a_File)
{
   // load your image, apply it to whatever you want
}
//////////// usage ////////
int main()
{
   Sprite player;
   player.LoadImage("awesome.tga");
}

That way you don't have to swap images all the time. If you don't know how to create classes, there are tutorials that explain how it works, shouldn't be all that difficult to grasp.

#3 QW3RTY ROCKS   Members   -  Reputation: 100

Like
0Likes
Like

Posted 08 April 2012 - 06:54 PM

Ok I kind of get what you mean, but I still don't know how to reference what sprite I will be using when im in my drawscene function. I need to somehow specify what image glTexCoord2f uses as it just uses the last image loaded, and it would be to much of a performance hit to keep reloading an image from a file.

#4 Bacterius   Crossbones+   -  Reputation: 9286

Like
0Likes
Like

Posted 08 April 2012 - 10:40 PM

Ok I kind of get what you mean, but I still don't know how to reference what sprite I will be using when im in my drawscene function. I need to somehow specify what image glTexCoord2f uses as it just uses the last image loaded, and it would be to much of a performance hit to keep reloading an image from a file.

Well the standard way is loading all your PNG's into memory upon starting your program, and putting them into some kind of array. Then you can simply retrieve the correct PNG by knowing it's index, and since the PNG is in memory there will be no performance hit (note that if you are using a 3D API which uses the GPU, you will want to put the texture into graphics memory instead, but the API should handle that for you). Of course that's the naive method, unless it's a really simple game you simply cannot load everything into memory at once so you need to stream stuff from the disk repeatedly, but.. this should be good enough for now.

So yeah basically you want to load all of them and reference them using some kind of index (usually an integer, i.e. a 1D array)

The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

 

- Pessimal Algorithms and Simplexity Analysis


#5 QW3RTY ROCKS   Members   -  Reputation: 100

Like
0Likes
Like

Posted 09 April 2012 - 01:23 AM

I figured out my main problem. Before when I ran the program even if I said what texture to use it would still always use the one texture. I realised that I was setting what texture to use inside of glBegin and glEnd and it wouldn't work. Moving my funtions outside allowed me to switch between the textures. Thanks for all your help guys.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS