qw3rty_rocks

Member
  • Content count

    11
  • Joined

  • Last visited

Community Reputation

100 Neutral

About qw3rty_rocks

  • Rank
    Member

Personal Information

  • Interests
    Programming
  1. multiple png textures in 2d game

    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.
  2. multiple png textures in 2d game

    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.
  3. multiple png textures in 2d game

    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: [url="http://blog.nobel-joergensen.com/2010/11/07/loading-a-png-as-texture-in-opengl-using-libpng/"]http://blog.nobel-jo...l-using-libpng/[/url] 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: [code]//=================================================================// //=================================================================// //========================== 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; }[/code] And then the file which deals with png files: [code]#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; }[/code] Any help would be great!
  4. variable problems

    Well you see the +3 was because +1 did not work.
  5. variable problems

    Hey well i fixed my problem. It was just an issue which I was modifying memory I was not supposed to. I had to change the following: [code]terrain1.resize(map_height*map_width);[/code] to [code]terrain1.resize(map_height*map_width+3);[/code] now I'm trying to work on my collision detection system (which is not going to well) anyway I thank you guys for your help.
  6. variable problems

    ok I found the problem line of code. [code]fscanf(input_file, "%d", &map_height); //line 29[/code] The problem is that I don't know whats wrong with it. If I comment it out the program runs but if I put it back in it stops working? what do I do now?
  7. variable problems

    I don't use a debugger edit: I used gdb and I ran the program and it said: [b]Program received signal SIGSEGV, Segmentation fault. 0x00345a04 in _IO_vfscanf () from /lib/i386-linux-gnu/libc.so.6[/b] so this might mean its having problems at line 38 which is below:[b] [/b] [code]fscanf(input_file, "%d", &terrain1[i].type);[/code]
  8. variable problems

    Using vectors still give me segmentation fault? Here is updated code: [code]#include <iostream> #include <stdlib.h> #include <fstream> #include <GL/glut.h> #include <vector> using namespace std; struct object{ float x; float y; float vspeed; float hspeed; }; struct terrainbox{ float x; float y; int type; }; int map_height = 0; int map_width = 0; //terrainbox terrain1[map_height][map_width]; //terrainbox *terrain1 = new terrainbox[map_height*map_width]; vector<terrainbox> terrain1; void loadMap(){ FILE *input_file = fopen("data/maps/test.ssm", "r"); fscanf(input_file, "%d", &map_height); fscanf(input_file, "%d", &map_width); terrain1.resize(map_height*map_width); int i; for(int y=0;y<map_height;y++){ for(int x=0;x<map_width;x++){ i++; terrain1[i].x = x; terrain1[i].y = y; fscanf(input_file, "%d", &terrain1[i].type); } } return; } object player1; void handleKeypress(unsigned char key, //The key that was pressed int x, int y) { //The current mouse coordinates switch (key) { case 27: //Escape key exit(0); //Exit the program } } void initRendering() { glEnable(GL_DEPTH_TEST); glShadeModel(GL_SMOOTH); player1.x = -2.0; player1.y = 1.5; player1.vspeed = 0.07; } void drawScene() { //Clear information from last draw glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); //Switch to the drawing perspective glLoadIdentity(); //Reset the drawing perspective glBegin(GL_QUADS); for(int i=0;i<map_height;i++){ glVertex3f(terrain1[i].x+1, terrain1[i].y, -14.0f); glVertex3f(terrain1[i].x, terrain1[i].y, -14.0f); glVertex3f(terrain1[i].x, terrain1[i].y+1, -14.0f); glVertex3f(terrain1[i].x+1, terrain1[i].y+1, -14.0f); } //player glVertex3f(player1.x+0.5f, player1.y, -14.0f); glVertex3f(player1.x, player1.y, -14.0f); glVertex3f(player1.x, player1.y+0.5f, -14.0f); glVertex3f(player1.x+0.5f, player1.y+0.5f, -14.0f); /*glVertex3f(-0.5f, 0.5f, -14.0f); glVertex3f(-2.5f, 0.5f, -14.0f); glVertex3f(-2.5f, 1.5f, -14.0f); glVertex3f(-0.5f, 1.5f, -14.0f);*/ glEnd(); glutSwapBuffers(); //Send the 3D scene to the screen } void handleResize(int w, int h) { //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 200.0); //The far z clipping coordinate } void update(int value){ player1.y += player1.vspeed; player1.vspeed -= 0.0015; glutPostRedisplay(); glutTimerFunc(25, update, 0); } int main(int argc, char** argv){ loadMap(); glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(700, 700); glutCreateWindow("Platformer"); initRendering(); glutKeyboardFunc(handleKeypress); glutDisplayFunc(drawScene); glutReshapeFunc(handleResize); glutTimerFunc(25, update, 0); glutMainLoop(); return 0; }[/code]
  9. variable problems

    This variable is where my map is getting loaded into so it can't be a constant but with the other method it compiles but it exits with segmentation fault So I think I will paste my code: [code]#include <iostream> #include <stdlib.h> #include <fstream> #include <GL/glut.h> using namespace std; struct object{ float x; float y; float vspeed; float hspeed; }; struct terrainbox{ float x; float y; int type; }; int map_height = 0; int map_width = 0; //terrainbox terrain1[map_height][map_width]; terrainbox *terrain1 = new terrainbox[map_height*map_width]; void loadMap(){ FILE *input_file = fopen("data/maps/test.ssm", "r"); fscanf(input_file, "%d", &map_height); fscanf(input_file, "%d", &map_width); int x; int y; for(int i=0;i<map_height*map_width;i++){ y++; x++; terrain1[i].x = x; terrain1[i].y = y; fscanf(input_file, "%d", &terrain1[i].type); } return; } object player1; void handleKeypress(unsigned char key, //The key that was pressed int x, int y) { //The current mouse coordinates switch (key) { case 27: //Escape key delete [] terrain1; terrain1 = NULL; exit(0); //Exit the program } } void initRendering() { glEnable(GL_DEPTH_TEST); glShadeModel(GL_SMOOTH); player1.x = -2.0; player1.y = 1.5; player1.vspeed = 0.07; } void drawScene() { //Clear information from last draw glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); //Switch to the drawing perspective glLoadIdentity(); //Reset the drawing perspective glBegin(GL_QUADS); for(int i=0;i<map_height;i++){ glVertex3f(terrain1[i].x+1, terrain1[i].y, -14.0f); glVertex3f(terrain1[i].x, terrain1[i].y, -14.0f); glVertex3f(terrain1[i].x, terrain1[i].y+1, -14.0f); glVertex3f(terrain1[i].x+1, terrain1[i].y+1, -14.0f); } //player glVertex3f(player1.x+0.5f, player1.y, -14.0f); glVertex3f(player1.x, player1.y, -14.0f); glVertex3f(player1.x, player1.y+0.5f, -14.0f); glVertex3f(player1.x+0.5f, player1.y+0.5f, -14.0f); /*glVertex3f(-0.5f, 0.5f, -14.0f); glVertex3f(-2.5f, 0.5f, -14.0f); glVertex3f(-2.5f, 1.5f, -14.0f); glVertex3f(-0.5f, 1.5f, -14.0f);*/ glEnd(); glutSwapBuffers(); //Send the 3D scene to the screen } void handleResize(int w, int h) { //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 200.0); //The far z clipping coordinate } void update(int value){ player1.y += player1.vspeed; player1.vspeed -= 0.0015; glutPostRedisplay(); glutTimerFunc(25, update, 0); } int main(int argc, char** argv){ loadMap(); glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(700, 700); glutCreateWindow("Platformer"); initRendering(); glutKeyboardFunc(handleKeypress); glutDisplayFunc(drawScene); glutReshapeFunc(handleResize); glutTimerFunc(25, update, 0); glutMainLoop(); return 0; }[/code] What do you guys think?
  10. I have trying to fix a problem with one of my variables. I have tried declaring it like so, as a global variable: [b]terrainbox terrain1[map_height][map_width];[/b] 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: [b]error: array bound is not an integer constant before ‘]’ token[/b] If more information is needed just ask.[b] [/b]
  11. Compress data

    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.