1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Development I'm going insane

Discussion in 'Software' started by Cerberus90, 25 Mar 2011.

  1. Cerberus90

    Cerberus90 Car Spannerer

    Joined:
    23 Apr 2009
    Posts:
    7,666
    Likes Received:
    208
    Making a basic openGL based game.
    I had a basic thing working, but textures weren't working.
    So I moved my drawing functions out of my classes, into my main display function, and made the necessary amendments for getting the variables for where objects are drawn.

    I have my main pongGame class.

    which in my main is a pongGame* pong.

    I then use pong = new pongGame();

    my pongGame class then has a Player* p1, which in the pongGame constructor is created using p1 = new Player();, etc etc, same for my other objects.

    This worked previously.

    However now, I'm getting bad pointers and variables not being initialised.


    Anyone got any ideas on what to look for / how to fix this?

    I can't understand it, its the basic bits not working, that were working before and haven't been touched.
     
  2. azrael-

    azrael- I'm special...

    Joined:
    18 May 2008
    Posts:
    3,852
    Likes Received:
    124
    Have you tried doing a clean and/or rebuild? Other than that I'd have to see some code.
     
  3. tehBoris

    tehBoris What's a Dremel?

    Joined:
    30 Jan 2011
    Posts:
    616
    Likes Received:
    25
    I think we need a bit more code to figure it out. C++ can be mysterious.
     
  4. Cerberus90

    Cerberus90 Car Spannerer

    Joined:
    23 Apr 2009
    Posts:
    7,666
    Likes Received:
    208
    I've tried a rebuild, didn't do anything.

    I'll try and get a bit of code posted up later. I mean, its the basics that have stopped working. They work in my previous version, I didn't touch them in this version, and now they don't work, :D
     
  5. Cerberus90

    Cerberus90 Car Spannerer

    Joined:
    23 Apr 2009
    Posts:
    7,666
    Likes Received:
    208
    Right, here's some snippets.

    main.cpp
    Code:
    
    
    pongGame* pong;
    
    openGL display, reshape, keyboard and mouse functions.
    
    int main(int argc, char** argv)
    {
    [INDENT]pong = new pongGame()[/INDENT]
    [INDENT]glut stuff[/INDENT]
    }
    
    pongGame.h
    Code:
    class pongGame {
    public:
    constructors and get and set methods etc
    Player* getP1();  this returns p1
    
    protected:
    
    Player* p1;
    Player* p2;
    
    ball* b1;
    
    };
    

    pongGame.cpp
    Code:
    pongGame::pongGame()
    {
    	p1 = new Player();
    	p2 = new Player();
    
    	b1 = new ball();
    	
    
    }
    
    
    My player constructor sets up some initial values, like sets the score to 0, and sets the initial x and y positions.

    However, when a call to pongGame->getP1() is made, it comes back as saying that the thing hasn't been initialized, as all the variables say "expression cannot be evaluated" in the output window at the bottom of Visual studio.
     
  6. Plugs

    Plugs Minimodder

    Joined:
    5 Nov 2009
    Posts:
    528
    Likes Received:
    64
    Are you doing any dodgey pointer arithmetic anywhere?
    Possible overwriting your game/players?
     
  7. Cerberus90

    Cerberus90 Car Spannerer

    Joined:
    23 Apr 2009
    Posts:
    7,666
    Likes Received:
    208
    nope.

    weird thing is, the exact same code works in my other project, but not in this one, even though I've not touched this code, only other drawing stuff.


    Here's a screenshot of the error :
    my pong = new pongGame(); call is before the pong->init(); call, so the constructor should have created p1, p2 and b1.


    [​IMG]
     
    Last edited: 26 Mar 2011
  8. mjb501

    mjb501 What's a Dremel?

    Joined:
    20 Jun 2010
    Posts:
    37
    Likes Received:
    7
    Could you put a break point in at line 10 of pongGame.cpp and then run the program. If the code is working propperly it should hit that before getting into the pong->init(), call.

    If it does hit line 10 first then use F11 to step through and the code line by line untill you reach the point where it crashes. Everytime you step thorugh keep an eye on what teh values of p1, p2 and b1 is in the watch window.

    If you still can't find out were it's going wrong, could you put up a screenshot of main.cpp and what is in the output windows and i'll take a look.
     
  9. Cerberus90

    Cerberus90 Car Spannerer

    Joined:
    23 Apr 2009
    Posts:
    7,666
    Likes Received:
    208
    It runs fine through till the p1->init().

    If I set some values in the constructor, like score = 0. the score is 0 after the new Player() call.

    After the p1 = new Player() call, this is the value for p1 :

    p1 0x00346418 {paddleHeight=-4.3160208e+008 yPaddlePos=-4.3160208e+008 xPaddlePos=-4.3160208e+008 ...} Player *

    obviously, the paddleHeight and other variables haven't been initialised yet, that is done in the init() function. However, if I set these values in the constructor, it makes no difference to the error.

    Here's a screenshot if I put the break point at the start of the pongGame init function (i changed the name to initPong just to see if something was conflicting with the name)

    [​IMG]
     
    Last edited: 26 Mar 2011
  10. Cerberus90

    Cerberus90 Car Spannerer

    Joined:
    23 Apr 2009
    Posts:
    7,666
    Likes Received:
    208
    Here's my main.cpp, rather large due to all the drawing stuff

    Code:
    #include <iostream>
    #include <gl/glut.h>
    #include "pongGame.h"
    #include "CTargaImage.h"
    
    #include "Skybox.h"
    
    
    pongGame* pong;
    
    
    
    CSkybox skybox;
    
    //textureLoader texLoad;
    
    
    	GLuint m_wallTex;
    	GLuint m_paddleTex[2];
    	GLuint m_baseTex;
    
    int rot = 0;
    int roty = 0;
    int diffx = 0;
    int lastx = 0;
    int lasty = 0;
    int m_down = 0;
    
    //time in milliseconds between updates
    const int Timer = 50;
    
    void init(void)
    {
    
    
    	glEnable(GL_TEXTURE_2D);
    
    	CTargaImage image;
    
    	if(!image.Load("paddle1.tga")) { std::cout << "error loading paddle1.tga" << std::endl;}
    	glGenTextures(1, &m_paddleTex[0]);
    	glBindTexture(GL_TEXTURE_2D, m_paddleTex[0]);
    	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image.GetWidth(), image.GetHeight(), 0, GL_RGB, GL_UNSIGNED_BYTE, image.GetImage());
    	image.Release();
    
    	if(!image.Load("paddle2.tga")) { std::cout << "error loading paddle2.tga" << std::endl;}
    	glGenTextures(2, &m_paddleTex[1]);
    	glBindTexture(GL_TEXTURE_2D, m_paddleTex[1]);
    	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image.GetWidth(), image.GetHeight(), 0, GL_RGB, GL_UNSIGNED_BYTE, image.GetImage());
    	image.Release();
    
    	image.Load("tower.tga");
    	glGenTextures(1, &m_wallTex);
    	glEnable(GL_TEXTURE_2D );
    	glBindTexture(GL_TEXTURE_2D, m_wallTex);
    	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    	//glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image.GetWidth(), image.GetHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, image.GetImage());
    	image.Release();
    
    	image.Load("pongTableTex2.tga");
    	glGenTextures(1, &m_baseTex);
    	glBindTexture(GL_TEXTURE_2D, m_baseTex);
    	gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, image.GetWidth(), image.GetHeight(), GL_RGB, GL_UNSIGNED_BYTE, image.GetImage());
    	image.Release();
    
    
    	
    
    	skybox.Initialize(40);
    	skybox.LoadTextures("skyTop.tga","skyb.tga","sky2.tga","sky4.tga","sky1.tga","sky3.tga");
    	
    	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
    	glShadeModel(GL_FLAT);
    	glEnable(GL_DEPTH_TEST);
    	glEnable(GL_TEXTURE_2D);
    
    	pong->initPong();
    	
    
    }
    
    
    
    
    void Display(void)
    {
    	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    	glClearColor(0,0,0,0);
    	glEnable(GL_TEXTURE_2D);
    	//glLoadIdentity();
    
    	glPushMatrix();
    	
    		skybox.Render(0,0,-5);
    
    		
    		glTranslatef(0.0,-12,-18);
    		glTranslatef(0,0,-1);
    
    
    
    		glPushMatrix();
    
    				glBindTexture(GL_TEXTURE_2D, m_baseTex);
    					glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    					glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    				glBegin(GL_QUADS);
    						glTexCoord2i(0,0); glVertex3f(-16.0,0.0,-1.0);
    						glTexCoord2i(0,1); glVertex3f(-16.0,24.0,-1.0);
    						glTexCoord2i(1,1); glVertex3f(16.0,24.0,-1.0);
    						glTexCoord2i(1,0); glVertex3f(16.0,0.0,-1.0);
    
    				glEnd();
    			//draw walls around base
    				glBindTexture(GL_TEXTURE_2D, m_wallTex);
    					glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    					glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    					//glColor3f(0.4,0.4,0.0);
    					glBegin(GL_QUADS);
    					glTexCoord2i(0, 0); glVertex3f(-16.0, 23.0, -2.0);
    					glTexCoord2i(0, 1); glVertex3f(-16.0, 23.0, 8.0);
    					glTexCoord2i(1, 1); glVertex3f(16.0, 23.0, 8.0);
    					glTexCoord2i(1, 0); glVertex3f(16.0, 23.0, -2.0);
    
    					glTexCoord2i(0, 0); glVertex3f(-16.0, 0.0, -1.0);
    					glTexCoord2i(0, 1); glVertex3f(-16.0, 0.0, 8.0);
    					glTexCoord2i(1, 1); glVertex3f(-16.0, 23.0, 8.0);
    					glTexCoord2i(1, 0); glVertex3f(-16.0, 23.0, -1.0);
    
    					glTexCoord2i(0, 0); glVertex3f(16.0, 0.0, -1.0);
    					glTexCoord2i(0, 1); glVertex3f(16.0, 0.0, 8.0);
    					glTexCoord2i(1, 1); glVertex3f(16.0, 23.0, 8.0);
    					glTexCoord2i(1, 0); glVertex3f(16.0, 23.0, -1.0);
    
    					glTexCoord2i(0, 0); glVertex3f(-16.0, 23.0, -2.0);
    					glTexCoord2i(0, 1); glVertex3f(-16.0, 23.0, 2.0);
    					glTexCoord2i(1, 1); glVertex3f(16.0, 23.0, 2.0);
    					glTexCoord2i(1, 0); glVertex3f(16.0, 23.0, -2.0);
    				glEnd();
    
    
    		glPopMatrix();
    
    			
    
    			glPushMatrix();
    				glTranslatef(0,12,0);
    				glTranslatef(-14,pong->getP1()->getYpaddlePos(),0);
    
    				//draw Player 1 paddle
    				glBindTexture(GL_TEXTURE_2D, m_paddleTex[0]);
    				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    				glBegin(GL_QUADS);
    					//base of paddle, used for 2D
    					glTexCoord2i(0,0); glVertex3f(0.0,-(pong->getP1()->getPaddleHeight()/2),0);
    					glTexCoord2i(0,1); glVertex3f(0.0,pong->getP1()->getPaddleHeight()/2,0);
    					glTexCoord2i(1,1); glVertex3f(0.5,pong->getP1()->getPaddleHeight()/2,0);
    					glTexCoord2i(1,0); glVertex3f(0.5,-(pong->getP1()->getPaddleHeight()/2),0);
    				glEnd();
    				
    				glBindTexture(GL_TEXTURE_2D, m_paddleTex[0]);
    				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    				glBegin(GL_QUADS); //turn the paddles from 2D into 3D
    					//top of paddle
    					glTexCoord2i(0,0); glVertex3f(0.0,-(pong->getP1()->getPaddleHeight()/2),0.5);
    					glTexCoord2i(0,1); glVertex3f(0.0,pong->getP1()->getPaddleHeight()/2,0.5);
    					glTexCoord2i(1,1); glVertex3f(0.5,pong->getP1()->getPaddleHeight()/2,0.5);
    					glTexCoord2i(1,0); glVertex3f(0.5,-(pong->getP1()->getPaddleHeight()/2),0.5);
    				glEnd();
    
    				glBindTexture(GL_TEXTURE_2D, m_paddleTex[1]);
    				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    				//end
    				//glColor3f(0,255,0);
    				glBegin(GL_QUADS);
    					glVertex3f(0.0,-(pong->getP1()->getPaddleHeight()/2),0.0);
    					glVertex3f(0.0,-(pong->getP1()->getPaddleHeight()/2),0.5);
    					glVertex3f(0.5,-(pong->getP1()->getPaddleHeight()/2),0.5);
    					glVertex3f(0.5,-(pong->getP1()->getPaddleHeight()/2),0.0);
    
    					//other end
    					glVertex3f(0.0,(pong->getP1()->getPaddleHeight()/2),0.0);
    					glVertex3f(0.0,(pong->getP1()->getPaddleHeight()/2),0.5);
    					glVertex3f(0.5,(pong->getP1()->getPaddleHeight()/2),0.5);
    					glVertex3f(0.5,(pong->getP1()->getPaddleHeight()/2),0.0);
    				glEnd();
    
    				glBindTexture(GL_TEXTURE_2D, m_paddleTex[1]);
    				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    				glBegin(GL_QUADS);
    					//front face (ball collides with this face
    					glTexCoord2i(0,0); glVertex3f(0.5,-(pong->getP1()->getPaddleHeight()/2),0.0);
    					glTexCoord2i(0,1); glVertex3f(0.5,-(pong->getP1()->getPaddleHeight()/2),0.5);
    					glTexCoord2i(1,1); glVertex3f(0.5,(pong->getP1()->getPaddleHeight()/2),0.5);
    					glTexCoord2i(1,0); glVertex3f(0.5,(pong->getP1()->getPaddleHeight()/2),0.0);
    
    					//back face
    					glVertex3f(0.0,-(pong->getP1()->getPaddleHeight()/2),0.0);
    					glVertex3f(0.0,-(pong->getP1()->getPaddleHeight()/2),0.5);
    					glVertex3f(0.0,(pong->getP1()->getPaddleHeight()/2),0.5);
    					glVertex3f(0.0,(pong->getP1()->getPaddleHeight()/2),0.0);
    
    				glEnd();
    			glPopMatrix();
    
    
    			glPushMatrix();
    				glTranslatef(0,12,0);
    				glTranslatef(13.5,pong->getP2()->getYpaddlePos(),0);
    
    				glBindTexture(GL_TEXTURE_2D, m_paddleTex[0]);
    				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    				glBegin(GL_QUADS);
    					//base of paddle, used for 2D
    					glTexCoord2i(0,0); glVertex3f(0.0,-(pong->getP2()->getPaddleHeight()/2),0);
    					glTexCoord2i(0,1); glVertex3f(0.0,pong->getP2()->getPaddleHeight()/2,0);
    					glTexCoord2i(1,1); glVertex3f(0.5,pong->getP2()->getPaddleHeight()/2,0);
    					glTexCoord2i(1,0); glVertex3f(0.5,-(pong->getP2()->getPaddleHeight()/2),0);
    				glEnd();
    				
    				glBindTexture(GL_TEXTURE_2D, m_paddleTex[0]);
    				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    				glBegin(GL_QUADS); //turn the paddles from 2D into 3D
    					//top of paddle
    					glTexCoord2i(0,0); glVertex3f(0.0,-(pong->getP2()->getPaddleHeight()/2),0.5);
    					glTexCoord2i(0,1); glVertex3f(0.0,pong->getP2()->getPaddleHeight()/2,0.5);
    					glTexCoord2i(1,1); glVertex3f(0.5,pong->getP2()->getPaddleHeight()/2,0.5);
    					glTexCoord2i(1,0); glVertex3f(0.5,-(pong->getP2()->getPaddleHeight()/2),0.5);
    				glEnd();
    
    				glBindTexture(GL_TEXTURE_2D, m_paddleTex[1]);
    				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    				//end
    				//glColor3f(0,255,0);
    				glBegin(GL_QUADS);
    					glTexCoord2i(0,0); glVertex3f(0.0,-(pong->getP2()->getPaddleHeight()/2),0.0);
    					glTexCoord2i(0,1); glVertex3f(0.0,-(pong->getP2()->getPaddleHeight()/2),0.5);
    					glTexCoord2i(1,1); glVertex3f(0.5,-(pong->getP2()->getPaddleHeight()/2),0.5);
    					glTexCoord2i(1,0); glVertex3f(0.5,-(pong->getP2()->getPaddleHeight()/2),0.0);
    
    					//other end
    					glTexCoord2i(0,0); glVertex3f(0.0,(pong->getP2()->getPaddleHeight()/2),0.0);
    					glTexCoord2i(0,1); glVertex3f(0.0,(pong->getP2()->getPaddleHeight()/2),0.5);
    					glTexCoord2i(1,1); glVertex3f(0.5,(pong->getP2()->getPaddleHeight()/2),0.5);
    					glTexCoord2i(1,0); glVertex3f(0.5,(pong->getP2()->getPaddleHeight()/2),0.0);
    
    				glEnd();
    
    				glBindTexture(GL_TEXTURE_2D, m_paddleTex[1]);
    				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    				glBegin(GL_QUADS);
    					//front face (ball collides with this face
    					glTexCoord2i(0,0); glVertex3f(0.5,-(pong->getP2()->getPaddleHeight()/2),0.0);
    					glTexCoord2i(0,1); glVertex3f(0.5,-(pong->getP2()->getPaddleHeight()/2),0.5);
    					glTexCoord2i(1,1); glVertex3f(0.5,(pong->getP2()->getPaddleHeight()/2),0.5);
    					glTexCoord2i(1,0); glVertex3f(0.5,(pong->getP2()->getPaddleHeight()/2),0.0);
    
    					//back face
    					glTexCoord2i(0,0); glVertex3f(0.0,-(pong->getP2()->getPaddleHeight()/2),0.0);
    					glTexCoord2i(0,1); glVertex3f(0.0,-(pong->getP2()->getPaddleHeight()/2),0.5);
    					glTexCoord2i(1,1); glVertex3f(0.0,(pong->getP2()->getPaddleHeight()/2),0.5);
    					glTexCoord2i(1,0); glVertex3f(0.0,(pong->getP2()->getPaddleHeight()/2),0.0);
    				glEnd();
    
    		glPopMatrix();
    
    
    
    		glPushMatrix();
    		//draw ball
    		glTranslatef(0,12,0);
    		glColor3f(1.0,1.0,1.0);
    			glTranslatef(pong->getBall()->getBallX(),pong->getBall()->getBallY(),pong->getHeight(pong->getBall()->getBallX(), pong->getBall()->getBallY()));
    				glBegin(GL_QUADS);
    					glVertex3f(-0.2,-0.2,0.0);
    					glVertex3f(-0.2,0.2,0.0);
    					glVertex3f(0.2,0.2,0.0);
    					glVertex3f(0.2,-0.2,0.0);
    
    					glVertex3f(-0.2,-0.2,0.2);
    					glVertex3f(-0.2,0.2,0.2);
    					glVertex3f(0.2,0.2,0.2);
    					glVertex3f(0.2,-0.2,0.2);
    
    					glVertex3f(-0.2,-0.2,0.0);
    					glVertex3f(-0.2,-0.2,0.2);
    					glVertex3f(0.2,-0.2,0.2);
    					glVertex3f(0.2,-0.2,0.0);
    
    					glVertex3f(-0.2,0.2,0.0);
    					glVertex3f(-0.2,0.2,0.2);
    					glVertex3f(0.2,0.2,0.2);
    					glVertex3f(0.2,0.2,0.0);
    				glEnd();
    
    		glPopMatrix();
    
    
    
    
    	
    		
    	//pong->drawGame();
    	glPopMatrix();
    
    	glutSwapBuffers();
    	
    
    
    
    }
    
    void keyboard(unsigned char key, int x, int y)
    {
    	
    	switch(key)
    	{
    	case 'w' : pong->processInput('w'); glutPostRedisplay(); break;
    
    	case 's': pong->processInput('s'); glutPostRedisplay(); break;
    
    	case 'o': pong->processInput('o'); glutPostRedisplay(); break;
    
    	case 'l': pong->processInput('l'); glutPostRedisplay(); break;
    
    
    	}
    
    }
    
    void mouseButton(int button, int state, int x, int y)
    {
    	if(button == GLUT_LEFT_BUTTON) {
    		if(state == GLUT_UP) {  //button released
    			
    			m_down = 0;
    			
    		}
    		else { //button down
    			m_down = 1;
    			lastx = x;
    			lasty = y;
    		}
    
    
    	}
    
    
    
    }
    
    void mouseMovement(int x, int y)
    {
    	
    	if(m_down) {
    		
    		
    		int diffx = (x - lastx) * 0.01f;
    		int diffy = (y - lasty) * 0.01f;
    		//std::cout << diffx << std::endl;
    		
    		//lastx = x;
    				
    		rot += diffx;
    		roty += diffy;
    		//glRotatef(diffy, 1,0,0);
    		glRotatef(diffx,0,0,1);
    		
    			glRotatef(diffy,1,0,0);
    			
    		
    		glutPostRedisplay();
    	}
    	
    }
    
    void reshape(int w, int h)
    {
    	glViewport(0, 0, (GLsizei) w, (GLsizei) h);
    	glMatrixMode(GL_PROJECTION);
    	glLoadIdentity();
    	gluPerspective(85,w/h,0.1, 100);
    	glMatrixMode(GL_MODELVIEW);
    	glLoadIdentity();
    	glTranslatef(0,10,-25);
    	//glRotatef(diffy,1,0,0);
    	
    
    
    }
    
    void update(int value)
    {
    	pong->updateGame((float)Timer/1000);
    	glutPostRedisplay();
    	glutTimerFunc(Timer,update,0);
    }
    
    
    
    
    int main(int argc, char** argv)
    {
    	//texLoad.init();
    	
    
    	glutInit(&argc, argv);
    	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    	glutInitWindowSize(1024,768);
    	glutCreateWindow("Pong");
    	pong = new pongGame();
    	
    	init();
    	glutDisplayFunc(Display);
    	glutReshapeFunc(reshape);
    	glutKeyboardFunc(keyboard);
    	glutMouseFunc(mouseButton);
    	glutMotionFunc(mouseMovement);
    	glutTimerFunc(Timer,update,0);
    
    	glutMainLoop();
    
    	return 1;
    
    
    
    
    
    }
    
    
     
  11. Plugs

    Plugs Minimodder

    Joined:
    5 Nov 2009
    Posts:
    528
    Likes Received:
    64
    Did you find the point where to pointer p1 suddenly dies? (i.e. after the breakpoint did you keep stepping through to the point where it was crashing earlier)
     
  12. Cerberus90

    Cerberus90 Car Spannerer

    Joined:
    23 Apr 2009
    Posts:
    7,666
    Likes Received:
    208
    yeah, it crashes at p1->init() in the pongGame->initPong() function which is called in the init() function in my main.

    After the construcor for pongGame has been executed, the pointer has a value, but then when it gets to this p1->init().

    the main obviously still knows the pointer for the pongGame, as the pongGame->initPong() function call works, when it gets into this function though, the p1, p2, b1 and even seems the 'this' pointer has lost its value.

    ah, just ran through it again.

    In the pongGame constructor, the players and ball are assigned values for the pointers, but then when it gets back into the main, these pointers then have no value.


    ignore that above, the pointers do have values.
     
    Last edited: 26 Mar 2011
  13. Cerberus90

    Cerberus90 Car Spannerer

    Joined:
    23 Apr 2009
    Posts:
    7,666
    Likes Received:
    208
    Right, I think I've identified the problem, seems a bit odd.

    My main is structured like so below.

    I've removed any code that has no bearing on pongGame to make it easier to see.

    pong has pointers when it is created in the int main(), however, when the init() function is called in main(), the pointers in pong are then not there.

    Its as if because the init() function is above the main where pong is intialised, the call to pong in the init, doesn't know what pong is as its above the main().

    (some of above may not make sense, tricky trying to get my explanation down into text, :D )

    Code:
    #include <iostream>
    #include <gl/glut.h>
    #include "pongGame.h"
    #include "CTargaImage.h"
    
    #include "Skybox.h"
    
    
    pongGame* pong;
    
    
    
    CSkybox skybox;
    
    //textureLoader texLoad;
    
    
    	GLuint m_wallTex;
    	GLuint m_paddleTex[2];
    	GLuint m_baseTex;
    
    int rot = 0;
    int roty = 0;
    int diffx = 0;
    int lastx = 0;
    int lasty = 0;
    int m_down = 0;
    
    //time in milliseconds between updates
    const int Timer = 50;
    
    void init(void)
    {
    	pong->initPong();
    
    }
    
    void Display(void)
    {
    
    }
    
    void keyboard(unsigned char key, int x, int y)
    {
    
    }
    
    void mouseButton(int button, int state, int x, int y)
    {
    
    }
    
    void mouseMovement(int x, int y)
    {
    	
    }
    
    void reshape(int w, int h)
    {
    
    }
    
    void update(int value)
    {
    	pong->updateGame((float)Timer/1000);
    	glutPostRedisplay();
    	glutTimerFunc(Timer,update,0);
    }
    
    int main(int argc, char** argv)
    {
    	glutInit(&argc, argv);
    	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    	glutInitWindowSize(1024,768);
    	glutCreateWindow("Pong");
    	pong = new pongGame();
    	
    	init();
    	glutDisplayFunc(Display);
    	glutReshapeFunc(reshape);
    	glutKeyboardFunc(keyboard);
    	glutMouseFunc(mouseButton);
    	glutMotionFunc(mouseMovement);
    	glutTimerFunc(Timer,update,0);
    
    	glutMainLoop();
    
    	return 1;
    
    }
    
     
  14. mjb501

    mjb501 What's a Dremel?

    Joined:
    20 Jun 2010
    Posts:
    37
    Likes Received:
    7
    It does sound strange although there is nothing like C++ to generate wierd and wonderfull errors.

    Could you do the following:

    1. In main.cpp put a breakpoint on the init() function call in the int main(int argc, char** argv) function. Also put one on pong->initPong(); in the init function.

    2. Run the program and when the debugger hits the breakpoint, hover your mouse cursor of the pong = new PongGame();
    This should bring up a box which has text similar to this:

    pong | 0x00884998 {p1=0x008849e0 p2=0x00884a20 }

    Either memorise the numbers or when you mouse over that text it will go blue, right click and select copy. Once you have have done this press F5

    3. It should now hit the breakpoint in the init function. Hover over pong again and see if the values are the same. If the are hit F5 and when you reach the this->p1->init(); p1 one should be the same value as p1 when you first looked.

    If the values are always the same then its probably something in init method in player.cpp, if the aren't the same then somewhere something is calling the constructor after pong = new PongGame()
     
  15. Plugs

    Plugs Minimodder

    Joined:
    5 Nov 2009
    Posts:
    528
    Likes Received:
    64
    but pong is global pointer, why would it matter about the order of the functions

    init is called by main, so init is required to be declared before main
    and both reference pong, so it is required to be declared before both

    but as mentioned... C++ does like being a weirdo at times

    there must be something deeper we are missing... or something completely obvious
     
  16. Cerberus90

    Cerberus90 Car Spannerer

    Joined:
    23 Apr 2009
    Posts:
    7,666
    Likes Received:
    208
    This is weird.

    At the first breakpoint, init() in the main function :

    pong = 0x003357c0 {p1=0x00336410 p2=0x00336460 b1=0x003364b0 ...}

    then at the second breakpoint, the pong->initPong() function inside the init() function:

    pong = 0x00000003 {p1=??? p2=??? b1=??? ...}
     
  17. Cerberus90

    Cerberus90 Car Spannerer

    Joined:
    23 Apr 2009
    Posts:
    7,666
    Likes Received:
    208
    I may have just found the problem.

    I ran through my init() function, and after every line, check the value of pong.

    I get to this line :

    glGenTextures(2, &m_paddleTex[1]);

    and after that, pong's pointer is no longer valid.

    That '2', should be 1, as I'm only generating one texture.


    And yes, it now works.


    Thanks for the help guys. I'll have to remember to meticulously go through the debug on every line next time.

    I think that 2 must have been causing it to override some memory or something. Quite why it had the effect of destroying the pong pointers I'm not sure, I'd have though it'd just make those textures not work, or done something strange with the textures.
     
  18. Plugs

    Plugs Minimodder

    Joined:
    5 Nov 2009
    Posts:
    528
    Likes Received:
    64
    *ding ding*
    we have a winner

    glad you found your error :D
     
  19. mjb501

    mjb501 What's a Dremel?

    Joined:
    20 Jun 2010
    Posts:
    37
    Likes Received:
    7
    Gald you found the problem!

    Unfortunately C++ is able to do exactly that, this essentially a buffer overflow (You are trying to load more stuff into memory that you have allocated room for).

    The detailed explaination is quite complicated, but basically as m_paddleTex[1] is only big enough to store a single texture and the program was trying to load two, when the second texture is loaded the next thing in memory gets overwritten with the second texture (i.e. the pongGame). Because the pongGame is a pointer it doesnt actually know that the data has been overwritten, it just knows that there is data there so thats why you wont get any compile time warnings.

    These sorts of bugs are the worst to identify and fix as you have to carefully look at the memory locations to see that the data is still there.

    I'm not sure if openGL functions have this (i have only done DirectX) but standard library functions in C++ tend to have two versions e.g.: strcat and strcat_s, with the _S version preventing buffer overflows . This is very useful as when you start doing any string manipulation in C++ you can very easily do buffer overflows, which can break your program in weird and wonderful ways.

    Also, the debugger in visual studio fixes alot of error automatically when you run the program in debug mode as it initialises pointer to default values such as 0xCDCDCDCD, so remember to also run it in release as tehre may be other bugs hidden which the debugger fixes (so it will crash when run without visual studio)
     
  20. azrael-

    azrael- I'm special...

    Joined:
    18 May 2008
    Posts:
    3,852
    Likes Received:
    124
    @Cerberus: Glad you found your bug! :)

    @mjb: The "_s" versions of standard C functions aren't standard, but a Microsoft invention. Using them yields non-portable code (well, you could probably do a search'n'replace, but still). We generally avoid using those functions at work.

    By the way, a good way to locate stack corruption is to put some string literals around the affected area, and then keep a close watch on them in debugging mode.
     

Share This Page