Game: Merged tick() into loop(), removed getFps().

This commit is contained in:
Felix Ableitner 2012-12-22 01:24:44 +01:00
parent e20c99b89f
commit d923e94541
2 changed files with 7 additions and 43 deletions

View file

@ -19,18 +19,13 @@
/// Goal amount of frames per second. /// Goal amount of frames per second.
const int Game::FPS_GOAL = 60; const int Game::FPS_GOAL = 60;
/// Milliseconds per tick at FPS_GOAL.
const float Game::TICKS_GOAL = 1000 / Game::FPS_GOAL;
/** /**
* Initializes game, including window and objects (sprites). * Initializes game, including window and objects (sprites).
*/ */
Game::Game(sf::RenderWindow& window) : Game::Game(sf::RenderWindow& window) :
mWindow(window), mWindow(window),
mView(Vector2f(0, 0), mWindow.getView().getSize()), mView(Vector2f(0, 0), mWindow.getView().getSize()),
//mFps("test"),
mTileManager(mWorld), mTileManager(mWorld),
mElapsed(0),
mQuit(false), mQuit(false),
mPaused(false) { mPaused(false) {
mWindow.setFramerateLimit(FPS_GOAL); mWindow.setFramerateLimit(FPS_GOAL);
@ -73,38 +68,24 @@ Game::~Game() {
*/ */
void void
Game::loop() { Game::loop() {
sf::Uint32 left = 0;
while (!mQuit) { while (!mQuit) {
input(); input();
for (; !mPaused && (left >= TICKS_GOAL); left -= TICKS_GOAL) { int elapsed = mClock.restart().asMilliseconds();
Character::think(TICKS_GOAL); if (mPaused) {
elapsed = 0;
}
Character::think(elapsed);
mCollection.checkDelete();
mWorld.step(); mWorld.step();
mCollection.checkDelete();
}
//mFps.setString(getFps());
tick();
left += mElapsed;
render(); render();
} }
} }
/**
* Saves ticks since last call.
*/
void
Game::tick() {
mElapsed = mClock.restart().asMilliseconds();
if (mPaused) {
mElapsed = 0;
}
}
/** /**
* Handles general game input. * Handles general game input.
*/ */
@ -230,15 +211,5 @@ Game::render() {
// Render GUI and static stuff. // Render GUI and static stuff.
mWindow.setView(mWindow.getDefaultView()); mWindow.setView(mWindow.getDefaultView());
//mWindow.draw(mFps);
mWindow.display(); mWindow.display();
} }
/**
* Returns current FPS as string.
*/
String
Game::getFps() {
return str((mElapsed != 0) ? 1000.0f / mElapsed : 0.0f, 2);
}

View file

@ -39,36 +39,29 @@ public:
private: private:
void input(); void input();
void render(); void render();
void tick();
void keyDown(const sf::Event& event); void keyDown(const sf::Event& event);
void keyUp(const sf::Event& event); void keyUp(const sf::Event& event);
void mouseUp(const sf::Event& event); void mouseUp(const sf::Event& event);
void generate(); void generate();
String getFps();
sf::Vector2<float> convertCoordinates(int x, int y); sf::Vector2<float> convertCoordinates(int x, int y);
// Private variables. // Private variables.
private: private:
static const int FPS_GOAL; static const int FPS_GOAL;
static const float TICKS_GOAL;
World mWorld; World mWorld;
sf::RenderWindow& mWindow; sf::RenderWindow& mWindow;
sf::Clock mClock; sf::Clock mClock;
sf::View mView; sf::View mView;
//sf::Text mFps;
Collection mCollection; Collection mCollection;
TileManager mTileManager; TileManager mTileManager;
Pathfinder mPathfinder; Pathfinder mPathfinder;
std::unique_ptr<Player> mPlayer; std::unique_ptr<Player> mPlayer;
/// Milliseconds since the last tick.
sf::Uint32 mElapsed;
bool mQuit; bool mQuit;
bool mPaused; bool mPaused;
}; };