PSyMastR
\m/O__O\m/
Coding in C++, using SDL.
Hey everyone, so I decided to clean up my code and make it neater (using classes for sprites, instead of just a crapton of variables and lines of code in main). I somehow lost control of my player, where I get the data from an event poll. Before I had one event poll in a main loop, with bools for each key (false if not pressed, true if pressed). I then moved a seperate control code to my player_sprite class, and it no longer worked. I want to be able to call player.handleInput(), and have it detect key presses, but with the same code as before (worked totally fine) it did not. Just wondering if anyone here can help. In the main loop, I am doing it like this:
CODE
while(!game_ended)
{
player.handleInput();
player.draw();
}
Here is my handleInput function from my player_sprite class:
CODE
void handleInput()
{
bool key_up, key_down, key_left, key_right, key_enter;
SDL_Event event;
while (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_KEYDOWN:
{
//IF ESCAPE is pressed, then quit
if(event.key.keysym.sym == SDLK_DOWN)
{
key_down = true;
}
else if(event.key.keysym.sym == SDLK_UP)
{
key_up = true;
}
else if(event.key.keysym.sym == SDLK_LEFT)
{
key_left = true;
}
else if(event.key.keysym.sym == SDLK_RIGHT)
{
key_right = true;
}
else if(event.key.keysym.sym == SDLK_RETURN)
{
key_enter = true;
}
break;
}
case SDL_KEYUP:
{
if(event.key.keysym.sym == SDLK_DOWN)
{
key_down = false;
}
else if(event.key.keysym.sym == SDLK_UP)
{
key_up = false;
}
else if(event.key.keysym.sym == SDLK_LEFT)
{
key_left = false;
}
else if(event.key.keysym.sym == SDLK_RIGHT)
{
key_right = false;
}
else if(event.key.keysym.sym == SDLK_RETURN)
{
key_enter = false;
}
}
}
}
//Player X Value Controls
if(key_left)
{
velocity_x -= ACCEL_CONST;
if(velocity_x < -1.0 * ACCEL_MAX)
{
velocity_x = -1.0 * ACCEL_MAX;
}
else if(velocity_x > 0)
{
velocity_x = 0;
}
}
else if(key_right)
{
velocity_x += ACCEL_CONST;
if(velocity_x > ACCEL_MAX)
{
velocity_x = ACCEL_MAX;
}
else if(velocity_x < 0)
{
velocity_x = 0;
}
}
if(!key_left && !key_right)
{
if(velocity_x != 0)
{
if(velocity_x > 0)
{
velocity_x -= ACCEL_CONST;
}
else if(velocity_x < -0)
{
velocity_x += ACCEL_CONST;
}
}
}
//Player Y Value Controls
if(key_up)
{
velocity_y -= ACCEL_CONST;
if(velocity_y < -1.0 * ACCEL_MAX)
{
velocity_y = -1.0 * ACCEL_MAX;
}
else if(velocity_y > 0)
{
velocity_y = 0;
}
}
else if(key_down)
{
velocity_y += ACCEL_CONST;
if(velocity_y > ACCEL_MAX)
{
velocity_y = ACCEL_MAX;
}
else if(velocity_y < 0)
{
velocity_y = 0;
}
}
if(!key_up && !key_down)
{
if(velocity_y != 0)
{
if(velocity_y > 0)
{
velocity_y -= ACCEL_CONST;
}
else if(velocity_y < 0)
{
velocity_y += ACCEL_CONST;
}
}
}
x_coord += velocity_x;
y_coord += velocity_y;
//Check screen bounds for x
if(x_coord < 0)
{
x_coord = 0;
}
else if(x_coord > SCREEN_WIDTH)
{
x_coord = SCREEN_WIDTH;
}
//Check screen bounds for y
if(y_coord < 0)
{
y_coord = 0;
}
else if(y_coord > SCREEN_HEIGHT)
{
y_coord = SCREEN_HEIGHT;
}
}
Is this the correct method, or is there a much better way to do this. (It worked fine until I classed my character)
Hey everyone, so I decided to clean up my code and make it neater (using classes for sprites, instead of just a crapton of variables and lines of code in main). I somehow lost control of my player, where I get the data from an event poll. Before I had one event poll in a main loop, with bools for each key (false if not pressed, true if pressed). I then moved a seperate control code to my player_sprite class, and it no longer worked. I want to be able to call player.handleInput(), and have it detect key presses, but with the same code as before (worked totally fine) it did not. Just wondering if anyone here can help. In the main loop, I am doing it like this:
CODE
while(!game_ended)
{
player.handleInput();
player.draw();
}
Here is my handleInput function from my player_sprite class:
CODE
void handleInput()
{
bool key_up, key_down, key_left, key_right, key_enter;
SDL_Event event;
while (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_KEYDOWN:
{
//IF ESCAPE is pressed, then quit
if(event.key.keysym.sym == SDLK_DOWN)
{
key_down = true;
}
else if(event.key.keysym.sym == SDLK_UP)
{
key_up = true;
}
else if(event.key.keysym.sym == SDLK_LEFT)
{
key_left = true;
}
else if(event.key.keysym.sym == SDLK_RIGHT)
{
key_right = true;
}
else if(event.key.keysym.sym == SDLK_RETURN)
{
key_enter = true;
}
break;
}
case SDL_KEYUP:
{
if(event.key.keysym.sym == SDLK_DOWN)
{
key_down = false;
}
else if(event.key.keysym.sym == SDLK_UP)
{
key_up = false;
}
else if(event.key.keysym.sym == SDLK_LEFT)
{
key_left = false;
}
else if(event.key.keysym.sym == SDLK_RIGHT)
{
key_right = false;
}
else if(event.key.keysym.sym == SDLK_RETURN)
{
key_enter = false;
}
}
}
}
//Player X Value Controls
if(key_left)
{
velocity_x -= ACCEL_CONST;
if(velocity_x < -1.0 * ACCEL_MAX)
{
velocity_x = -1.0 * ACCEL_MAX;
}
else if(velocity_x > 0)
{
velocity_x = 0;
}
}
else if(key_right)
{
velocity_x += ACCEL_CONST;
if(velocity_x > ACCEL_MAX)
{
velocity_x = ACCEL_MAX;
}
else if(velocity_x < 0)
{
velocity_x = 0;
}
}
if(!key_left && !key_right)
{
if(velocity_x != 0)
{
if(velocity_x > 0)
{
velocity_x -= ACCEL_CONST;
}
else if(velocity_x < -0)
{
velocity_x += ACCEL_CONST;
}
}
}
//Player Y Value Controls
if(key_up)
{
velocity_y -= ACCEL_CONST;
if(velocity_y < -1.0 * ACCEL_MAX)
{
velocity_y = -1.0 * ACCEL_MAX;
}
else if(velocity_y > 0)
{
velocity_y = 0;
}
}
else if(key_down)
{
velocity_y += ACCEL_CONST;
if(velocity_y > ACCEL_MAX)
{
velocity_y = ACCEL_MAX;
}
else if(velocity_y < 0)
{
velocity_y = 0;
}
}
if(!key_up && !key_down)
{
if(velocity_y != 0)
{
if(velocity_y > 0)
{
velocity_y -= ACCEL_CONST;
}
else if(velocity_y < 0)
{
velocity_y += ACCEL_CONST;
}
}
}
x_coord += velocity_x;
y_coord += velocity_y;
//Check screen bounds for x
if(x_coord < 0)
{
x_coord = 0;
}
else if(x_coord > SCREEN_WIDTH)
{
x_coord = SCREEN_WIDTH;
}
//Check screen bounds for y
if(y_coord < 0)
{
y_coord = 0;
}
else if(y_coord > SCREEN_HEIGHT)
{
y_coord = SCREEN_HEIGHT;
}
}
Is this the correct method, or is there a much better way to do this. (It worked fine until I classed my character)