Skip to content

Commit

Permalink
#2 fix continuous keyboard input and calculate delta
Browse files Browse the repository at this point in the history
  • Loading branch information
kmfarley11 committed Jun 12, 2016
1 parent 9027ca4 commit eec3272
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 40 deletions.
79 changes: 49 additions & 30 deletions source/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "Game.h"

#include <fstream>
#include <ctime>

// global vars for easy config
// For now we need to maintain the 432*384 ratio because of scaling
Expand Down Expand Up @@ -126,6 +127,7 @@ bool Game::handleInput()
bool success = false;
if (sdlIsLoaded())
{
// Poll events for single-hit keys, mouse events, window events, etc... (continuous key presses are utilized in a different fashion)
SDL_Event event;
while (SDL_PollEvent(&event))
{
Expand All @@ -141,39 +143,43 @@ bool Game::handleInput()
{
running = false;
}
}
}

// player facing updates via keyboard input (arrow keys)
// map scrolls according to direction + collision processing + window / sprite scaling
// TODO: implement collision detection and abstract to the room class
std::string direction = "n";
const SDL_Rect roomBox = *room.box();
// player facing updates via keyboard input (arrow keys)
// map scrolls according to direction + collision processing + window / sprite scaling
// TODO: implement collision detection and abstract to the room class
std::string direction = "n";
const SDL_Rect roomBox = *room.box();

if (event.key.keysym.sym == SDLK_LEFT)
{
direction = "l";
room.box(roomBox.x + xScaled / 2, roomBox.y, roomBox.w, roomBox.h);
}
if (event.key.keysym.sym == SDLK_RIGHT)
{
direction = "r";
room.box(roomBox.x - xScaled / 2, roomBox.y, roomBox.w, roomBox.h);
}
if (event.key.keysym.sym == SDLK_UP)
{
direction = "u";
room.box(roomBox.x, roomBox.y + yScaled / 2, roomBox.w, roomBox.h);
}
if (event.key.keysym.sym == SDLK_DOWN)
{
direction = "d";
room.box(roomBox.x, roomBox.y - yScaled / 2, roomBox.w, roomBox.h);
}

player.setDirection(direction);
}
success = true;
// continuous-response keys (scan keyboard snapshot)
const Uint8* keystate = SDL_GetKeyboardState(NULL);
if (keystate[SDL_SCANCODE_LEFT])
{
direction = "l";
room.box(roomBox.x + xScaled / 2, roomBox.y, roomBox.w, roomBox.h);
}
if (keystate[SDL_SCANCODE_RIGHT])
{
direction = "r";
room.box(roomBox.x - xScaled / 2, roomBox.y, roomBox.w, roomBox.h);
}
if (keystate[SDL_SCANCODE_UP])
{
direction = "u";
room.box(roomBox.x, roomBox.y + yScaled / 2, roomBox.w, roomBox.h);
}
if (keystate[SDL_SCANCODE_DOWN])
{
direction = "d";
room.box(roomBox.x, roomBox.y - yScaled / 2, roomBox.w, roomBox.h);
}

player.setDirection(direction);

success = true;
}

return success;
}

Expand All @@ -182,6 +188,12 @@ bool Game::drawScene()
bool success = false;
if (sdlIsLoaded())
{
// keep track of start and end time to smooth out fps
clock_t startRender;
clock_t endRender;
startRender = clock();

// render the room first
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, room.texture(), NULL, room.box());

Expand All @@ -192,11 +204,18 @@ bool Game::drawScene()
player.togglePlayerAnimation(renderer);
}

// then render the player
SDL_RenderCopy(renderer, player.texture(), NULL, player.box());
SDL_RenderPresent(renderer);

endRender = clock();
clock_t delta = endRender - startRender;
int deltaMilli = (delta / CLOCKS_PER_SEC) * 1000;

//std::cout << "delta = " << delta << " ticks (" << deltaMilli << " milliseconds)" << std::endl;

// reduce the frame rate for debugging. there should be a cap eventually...
SDL_Delay(150);
SDL_Delay(150 - deltaMilli); //1000/30 = 30 fps...

success = true;
}
Expand Down
6 changes: 3 additions & 3 deletions source/Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ Player::Player()
{
// initialize texture and info here
// set direction of animation to front facing, no previous state
directionToAnimate.push_back('d');
directionToAnimate.push_back('-');
directionToAnimate.push_back('n');
directionToAnimate.push_back('d'); // player facing (d = down = towards screen)
directionToAnimate.push_back('-'); // separator
directionToAnimate.push_back('n'); // prev

movePlayer = false;
forceAnimation = false;
Expand Down
12 changes: 5 additions & 7 deletions source/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,14 @@
int main(int argc, char* argv[])
{
// create our game object and have it start and init our window etc.
Game *game = new Game();
game->initGame();
while (game->isRunning())
Game game;
game.initGame();
while (game.isRunning())
{
game->handleInput();
game->drawScene();
game.handleInput();
game.drawScene();
}

// if we are done then execute teardown
delete game;
game = NULL;
return 0;
}

0 comments on commit eec3272

Please sign in to comment.