Implemented frame counter

This commit is contained in:
Maximilian Giller 2023-06-17 20:47:09 +02:00
parent 27b6e1b324
commit aa288edd69
5 changed files with 71 additions and 5 deletions

View file

@ -98,7 +98,7 @@ set(SOURCES
src/levels.hpp
src/sprites/tiling/tilemap.cpp
src/sprites/tiling/tilemap.hpp
src/sprites/tiling/tilemap_config.hpp src/sprites/tiling/tileset_config.hpp src/sprites/tiling/tileset.cpp src/sprites/tiling/tileset.hpp)
src/sprites/tiling/tilemap_config.hpp src/sprites/tiling/tileset_config.hpp src/sprites/tiling/tileset.cpp src/sprites/tiling/tileset.hpp src/game/frame_counter.cpp src/game/frame_counter.hpp)
set(PHYSICS_00_SOURCES
src/prototypes/physics_00.cpp)

View file

@ -0,0 +1,36 @@
#include "frame_counter.hpp"
#include "../logging/easylogging++.h"
unsigned int FrameCounter::getFps() const
{
return lastFps;
}
void FrameCounter::addFrame()
{
liveFrameCount++;
timeSinceLastFpsUpdate += clock.restart();
auto const second = sf::seconds(1);
if (timeSinceLastFpsUpdate < second)
{
return;
}
lastFps = liveFrameCount;
liveFrameCount = 0;
timeSinceLastFpsUpdate -= second;
if (printFpsInConsole)
{
LOG(INFO) << "FPS: " << lastFps;
}
}
void FrameCounter::reset()
{
clock.restart();
timeSinceLastFpsUpdate = sf::Time::Zero;
liveFrameCount = 0;
lastFps = 0;
}

View file

@ -0,0 +1,25 @@
#ifndef HOLESOME_FRAME_COUNTER_HPP
#define HOLESOME_FRAME_COUNTER_HPP
#include <SFML/System/Clock.hpp>
class FrameCounter
{
public:
void reset();
void addFrame();
unsigned int getFps() const;
bool printFpsInConsole = true;
private:
sf::Clock clock;
sf::Time timeSinceLastFpsUpdate = sf::Time::Zero;
unsigned int liveFrameCount = 0;
unsigned int lastFps = 0;
};
#endif //HOLESOME_FRAME_COUNTER_HPP

View file

@ -15,11 +15,11 @@ void Game::run()
{
sf::Clock clock;
sf::Time TimeSinceLastUpdate = sf::seconds(0);
frameCounter->reset();
LOG(INFO) << "Game loop started ...";
while (window->isOpen())
{
InputMapper::getInstance()->processEvents();
TimeSinceLastUpdate += clock.restart();
while (TimeSinceLastUpdate >= FRAME_TIME)
@ -29,6 +29,7 @@ void Game::run()
update();
InputMapper::getInstance()->processEvents();
}
drawFrame();
}
@ -53,9 +54,10 @@ void Game::drawFrame()
}
window->display();
frameCounter->addFrame();
}
void Game::addGameObject(const std::shared_ptr<GameObject>& gameObject)
void Game::addGameObject(const std::shared_ptr<GameObject> &gameObject)
{
gameObjectsBuffer.push_back(gameObject);
}
@ -63,14 +65,14 @@ void Game::addGameObject(const std::shared_ptr<GameObject>& gameObject)
void Game::update()
{
// Add new game objects
for (const auto& gameObject: gameObjectsBuffer)
for (const auto &gameObject: gameObjectsBuffer)
{
gameObjects.push_back(gameObject);
}
gameObjectsBuffer.clear();
// Basic Updates
for (const auto& gameObject: gameObjects)
for (const auto &gameObject: gameObjects)
{
if (gameObject->getActive())
{

View file

@ -8,6 +8,7 @@
#include "input/input_mapper.h"
#include "camera/tracking_view.h"
#include "level/level_config.hpp"
#include "frame_counter.hpp"
class TrackingView;
@ -42,6 +43,8 @@ private:
LevelConfig loadedLevelConfig = {};
std::shared_ptr<FrameCounter> frameCounter = std::make_shared<FrameCounter>();
void drawFrame();
void update();