Jump to content

  • Log In with Google      Sign In   
  • Create Account

QW3RTY ROCKS

Member Since 07 Apr 2011
Offline Last Active Apr 09 2012 01:20 AM

Topics I've Started

multiple png textures in 2d game

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!

variable problems

17 July 2011 - 02:01 AM

I have trying to fix a problem with one of my variables. I have tried declaring it like so, as a global variable:

terrainbox terrain1[map_height][map_width];

If I declare it in a funtion then it has no problems at all but I need it to be global and it is giving me the following error:
error: array bound is not an integer constant before ‘]’ token

If more information is needed just ask.



Compress data

07 April 2011 - 01:33 AM

So i am using SDL and I want to be able to compress all data into one file. I want to do this to stop people from editing the file and I want the game to still be able to access the data.
The data would include pictures and a font.

PARTNERS