Sign in to follow this  
coderWalker

Variables Initializing every step

Recommended Posts

I have a class "Player" and it is going to have all the commands for the player. I have it detecting key input to move the player but something is wrong. As you can see in the code below int a =0 then in the step() command it is incremented. I then used printf() to write the a's to a file and thier all 0. What here is reinitializing all my variables every step?

I have been looking over this for a week now and am completely stumped, someone please spot the answer.

Code uses C++ with SDL and OpenGL

Thanks in advance,
CoderWalker

Player.h
class player
{
public:
player(float ix,float iy,float iz);
void step();
void input();
float x,y,z;
private:
float xVel,yVel,zVel;
float xRot,yRot,zRot;
float xFocus,yFocus,zFocus;
float zRotVel;
chunkLoc currentChunk;
int a;
};







Player.cpp
#include "Global.h"
player::player(float ix,float iy,float iz)
{
////Initilize Variables
//0 -20 10
x=ix;
y=iy;
z=iz;
xRot=0;
yRot=0;
zRot=0;
zRotVel=0;
currentChunk.x=0;
currentChunk.y=0;
a=0;
}

void player::step()
{
////Physics Calculations

//zVel=-1;
//z-=0.1;
zRot+=zRotVel;
//glRotatef(zRot,0,0,1);
printf("zRotVel=%d zRot=%d a=%d\n",zRotVel,zRot,a);
a++;
}

void player::input()
{
////Handle Key Presses
if( event.type == SDL_KEYDOWN )
{
//Adjust the velocity
switch( event.key.keysym.sym )
{
//case SDLK_UP: xVel -= SQUARE_WIDTH / 2; break;
//case SDLK_DOWN: xVel += SQUARE_WIDTH / 2; break;
case SDLK_LEFT: zRotVel -= 2; break;
case SDLK_RIGHT: zRotVel += 2; break;
}
}
//If a key was released
else if( event.type == SDL_KEYUP )
{
//Adjust the velocity
switch( event.key.keysym.sym )
{
//case SDLK_UP: yVel += SQUARE_HEIGHT / 2; break;
//case SDLK_DOWN: yVel -= SQUARE_HEIGHT / 2; break;
case SDLK_LEFT: zRotVel += 2; break;
case SDLK_RIGHT: zRotVel -= 2; break;
}
}
//step();
}







excerpt from Main.cpp


player* player1;

void initGame()
{
texDirt.tex=GLTex(load_image("resources\\textures\\dirt.png"));
texGrass.tex=GLTex(load_image("resources\\textures\\grass.png"));
Map = new map();
//player1= new player(0,-20,10);
player1= new player(0,0,2); <--------------------------------------
}

int main( int argc, char *argv[] )
{
bool quit = false;
if( init() == false ) { return 1; }
initGame(); <--------------------------------------
fps = new Timer();
//update update1("Coby Walker");
//Main Loop
while( quit == false )
{
//Start the frame timer
fps->start();

//While there are events to handle
while( SDL_PollEvent( &event ) )
{
//Handle key presses
player1->input(); <--------------------------------------
if( event.type == SDL_QUIT )
{
quit = true;
}
}
//Move the square
player1->step(); <--------------------------------------







stdout.txt (printf's output)

zRotVel=0 zRot=0 a=0
zRotVel=0 zRot=0 a=0
zRotVel=0 zRot=0 a=0
zRotVel=0 zRot=0 a=0
zRotVel=0 zRot=0 a=0
zRotVel=0 zRot=0 a=0
zRotVel=0 zRot=0 a=0
zRotVel=0 zRot=0 a=0
zRotVel=0 zRot=0 a=0
zRotVel=0 zRot=0 a=0
zRotVel=0 zRot=0 a=0
zRotVel=0 zRot=0 a=0
zRotVel=0 zRot=0 a=0
zRotVel=0 zRot=0 a=0
zRotVel=0 zRot=0 a=0
zRotVel=0 zRot=0 a=0
zRotVel=0 zRot=0 a=0
zRotVel=0 zRot=0 a=0
zRotVel=0 zRot=0 a=0
zRotVel=0 zRot=0 a=0
zRotVel=0 zRot=0 a=0
zRotVel=0 zRot=0 a=0
zRotVel=0 zRot=1073741824 a=0
zRotVel=0 zRot=1073741824 a=0
zRotVel=0 zRot=1073741824 a=0
zRotVel=0 zRot=1073741824 a=0
zRotVel=0 zRot=1073741824 a=0
zRotVel=0 zRot=1073741824 a=0
zRotVel=0 zRot=1073741824 a=0
zRotVel=0 zRot=1073741824 a=0
zRotVel=0 zRot=1073741824 a=0
zRotVel=0 zRot=1073741824 a=0
zRotVel=0 zRot=-1073741824 a=0
zRotVel=0 zRot=-1073741824 a=0
zRotVel=0 zRot=-1073741824 a=0
zRotVel=0 zRot=-1073741824 a=0
zRotVel=0 zRot=-1073741824 a=0
zRotVel=0 zRot=-1073741824 a=0
zRotVel=0 zRot=-1073741824 a=0
zRotVel=0 zRot=-1073741824 a=0
zRotVel=0 zRot=0 a=0
zRotVel=0 zRot=1073741824 a=0
zRotVel=0 zRot=1073741824 a=0
zRotVel=0 zRot=1073741824 a=0
zRotVel=0 zRot=1073741824 a=0
zRotVel=0 zRot=1073741824 a=0
zRotVel=0 zRot=1073741824 a=0
zRotVel=0 zRot=1073741824 a=0
zRotVel=0 zRot=1073741824 a=0
zRotVel=0 zRot=0 a=0
zRotVel=0 zRot=-1073741824 a=0
zRotVel=0 zRot=-1073741824 a=0
zRotVel=0 zRot=-1073741824 a=0
zRotVel=0 zRot=-1073741824 a=0
zRotVel=0 zRot=0 a=0
zRotVel=0 zRot=0 a=0
zRotVel=0 zRot=0 a=0
zRotVel=0 zRot=0 a=0
zRotVel=0 zRot=0 a=0
zRotVel=0 zRot=0 a=0
zRotVel=0 zRot=0 a=0
zRotVel=0 zRot=0 a=0
zRotVel=0 zRot=0 a=0
zRotVel=0 zRot=0 a=0
zRotVel=0 zRot=0 a=0
zRotVel=0 zRot=0 a=0
zRotVel=0 zRot=0 a=0
zRotVel=0 zRot=0 a=0
zRotVel=0 zRot=0 a=0
zRotVel=0 zRot=0 a=0
zRotVel=0 zRot=0 a=0
zRotVel=0 zRot=0 a=0
zRotVel=0 zRot=0 a=0
zRotVel=0 zRot=0 a=0
zRotVel=0 zRot=0 a=0
zRotVel=0 zRot=0 a=0
zRotVel=0 zRot=0 a=0
zRotVel=0 zRot=0 a=0
zRotVel=0 zRot=0 a=0
zRotVel=0 zRot=0 a=0
zRotVel=0 zRot=0 a=0
zRotVel=0 zRot=0 a=0
zRotVel=0 zRot=0 a=0
zRotVel=0 zRot=0 a=0
zRotVel=0 zRot=0 a=0



Share this post


Link to post
Share on other sites
Unless there is a reason to be using C code, I'd suggest using ostreams and overload the << operator for player.

in .h

#include <iostream>

friend std::ostream& operator<<(std::ostream& toOut, const player& p);

in .cpp

friend std::ostream& operator<<(std::ostream& toOut, const player& p)
{
toOut << "zRotVel = " << p.zRotVal << "\tzRot = " << p.zRot << "\ta = " << p.a;
}

in step function you can now do
std::cout << (*this) << std::endl;

Share this post


Link to post
Share on other sites
Quote:
Original post by Hodgman
I don't know why a isn't printing correctly, but zRotVel and zRot are floats, but you're trying to print them using "%d" instead of "%f", which will screw up printf.
I had to double check (pun not intended) but floats are passed as doubles in variadic functions so that is almost certainly causing the problem. Basically, instead of 12 bytes being passed to printf 20 bytes are being passed to printf (bogus hex values chosen for demonstration purposes):

0x11 0x22 0x33 0x44 << float 1a
0x55 0x66 0x77 0x88 << float 1b
0x11 0x22 0x33 0x44 << float 2a
0x55 0x66 0x77 0x88 << float 2b
0x11 0x22 0x33 0x44 << int

So float 1a, 1b, and 2a are being output instead of what is intended.

And this is why the C++ stream classes are almost always a better choice.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this