Game: Merged tick() into loop(), removed getFps().
This commit is contained in:
parent
e20c99b89f
commit
d923e94541
2 changed files with 7 additions and 43 deletions
|
@ -19,18 +19,13 @@
|
|||
/// Goal amount of frames per second.
|
||||
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).
|
||||
*/
|
||||
Game::Game(sf::RenderWindow& window) :
|
||||
mWindow(window),
|
||||
mView(Vector2f(0, 0), mWindow.getView().getSize()),
|
||||
//mFps("test"),
|
||||
mTileManager(mWorld),
|
||||
mElapsed(0),
|
||||
mTileManager(mWorld),
|
||||
mQuit(false),
|
||||
mPaused(false) {
|
||||
mWindow.setFramerateLimit(FPS_GOAL);
|
||||
|
@ -73,38 +68,24 @@ Game::~Game() {
|
|||
*/
|
||||
void
|
||||
Game::loop() {
|
||||
sf::Uint32 left = 0;
|
||||
while (!mQuit) {
|
||||
|
||||
input();
|
||||
|
||||
for (; !mPaused && (left >= TICKS_GOAL); left -= TICKS_GOAL) {
|
||||
Character::think(TICKS_GOAL);
|
||||
mWorld.step();
|
||||
|
||||
mCollection.checkDelete();
|
||||
int elapsed = mClock.restart().asMilliseconds();
|
||||
if (mPaused) {
|
||||
elapsed = 0;
|
||||
}
|
||||
|
||||
//mFps.setString(getFps());
|
||||
Character::think(elapsed);
|
||||
mCollection.checkDelete();
|
||||
|
||||
tick();
|
||||
left += mElapsed;
|
||||
mWorld.step();
|
||||
|
||||
render();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves ticks since last call.
|
||||
*/
|
||||
void
|
||||
Game::tick() {
|
||||
mElapsed = mClock.restart().asMilliseconds();
|
||||
if (mPaused) {
|
||||
mElapsed = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles general game input.
|
||||
*/
|
||||
|
@ -230,15 +211,5 @@ Game::render() {
|
|||
// Render GUI and static stuff.
|
||||
mWindow.setView(mWindow.getDefaultView());
|
||||
|
||||
//mWindow.draw(mFps);
|
||||
|
||||
mWindow.display();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns current FPS as string.
|
||||
*/
|
||||
String
|
||||
Game::getFps() {
|
||||
return str((mElapsed != 0) ? 1000.0f / mElapsed : 0.0f, 2);
|
||||
}
|
||||
|
|
|
@ -39,36 +39,29 @@ public:
|
|||
private:
|
||||
void input();
|
||||
void render();
|
||||
void tick();
|
||||
|
||||
void keyDown(const sf::Event& event);
|
||||
void keyUp(const sf::Event& event);
|
||||
void mouseUp(const sf::Event& event);
|
||||
|
||||
void generate();
|
||||
String getFps();
|
||||
sf::Vector2<float> convertCoordinates(int x, int y);
|
||||
|
||||
// Private variables.
|
||||
private:
|
||||
static const int FPS_GOAL;
|
||||
static const float TICKS_GOAL;
|
||||
|
||||
World mWorld;
|
||||
|
||||
sf::RenderWindow& mWindow;
|
||||
sf::Clock mClock;
|
||||
sf::View mView;
|
||||
//sf::Text mFps;
|
||||
|
||||
Collection mCollection;
|
||||
TileManager mTileManager;
|
||||
Pathfinder mPathfinder;
|
||||
std::unique_ptr<Player> mPlayer;
|
||||
|
||||
/// Milliseconds since the last tick.
|
||||
sf::Uint32 mElapsed;
|
||||
|
||||
bool mQuit;
|
||||
bool mPaused;
|
||||
};
|
||||
|
|
Reference in a new issue