113 lines
3.1 KiB
C++
113 lines
3.1 KiB
C++
#include "Game.hpp"
|
|
|
|
#include <numeric>
|
|
|
|
#include <SFML/System.hpp>
|
|
#include <SFML/Window/ContextSettings.hpp>
|
|
#include <SFML/Window/Event.hpp>
|
|
|
|
#include "Engine/Utils/Perfs.hpp"
|
|
#include "Engine/Utils/Timers.hpp"
|
|
|
|
using std::make_shared;
|
|
using std::make_unique;
|
|
|
|
#define TARGET_FPS (1000/60)
|
|
|
|
|
|
std::unique_ptr<SysTimer> mPerfsTimer = nullptr;
|
|
|
|
Game::Game(bool dbg) : mbDbgModeEnabled(dbg) {
|
|
InitWindows();
|
|
|
|
mCockpitUI = make_unique<CockpitUI>();
|
|
mWorldUI = make_unique<WorldUI>();
|
|
|
|
if (mbDbgModeEnabled) {
|
|
mDbgUI = make_unique<DebugUI>();
|
|
mPerfsTimer = make_unique<SysTimer>();
|
|
mPerfsTimer->Reset();
|
|
}
|
|
}
|
|
|
|
void Game::InitWindows() {
|
|
// Create default game window
|
|
sf::ContextSettings sWindowSettings;
|
|
sWindowSettings.depthBits = 0;
|
|
sWindowSettings.stencilBits = 0;
|
|
sWindowSettings.antialiasingLevel = 8;
|
|
sWindowSettings.sRgbCapable = true;
|
|
|
|
mMainWindow = make_shared<sf::RenderWindow>(
|
|
sf::VideoMode(1280, 720), "ProtoTank",
|
|
(sf::Style::Close | sf::Style::Titlebar),
|
|
sWindowSettings
|
|
);
|
|
mMainWindow->setVerticalSyncEnabled(false); // Never use simultaneously with framerate limiter
|
|
mMainWindow->setFramerateLimit(60);
|
|
mMainWindow->setKeyRepeatEnabled(false);
|
|
mMainWindow->setMouseCursorVisible(true);
|
|
}
|
|
|
|
GAME_STATUS Game::Tick(SysTimer& time) {
|
|
static std::vector<double> msTicksMonitoring;
|
|
static double mPrevdelta = 0;
|
|
static unsigned int mFrameCnt = 0;
|
|
double mDelta = time.GetDeltaTime();
|
|
|
|
if (mDbgUI != nullptr && mPerfsTimer != nullptr) {
|
|
msTicksMonitoring.push_back((mDelta - mPrevdelta) / TARGET_FPS);
|
|
if (mPerfsTimer->GetDeltaTime() >= 1000) { // Re-evaluate CPU usage every seconds (for FP'S')
|
|
double cpuUsage = std::accumulate(msTicksMonitoring.begin(), msTicksMonitoring.end(), 0) / msTicksMonitoring.size();
|
|
|
|
mDbgUI->UpdateDebugData(cpuUsage, (mDelta - mPrevdelta), mFrameCnt, (PerfsGetVirtMem() / 1000), (PerfsGetPhysMem() / 1000));
|
|
|
|
mFrameCnt = 0;
|
|
msTicksMonitoring.clear();
|
|
mPerfsTimer->Reset();
|
|
}
|
|
}
|
|
|
|
sf::Event event;
|
|
while (mMainWindow->pollEvent(event)) { // Wait for next frame (fixed FPS)
|
|
if (event.type == sf::Event::Closed)
|
|
mMainWindow->close();
|
|
}
|
|
|
|
if (mDelta >= TARGET_FPS) { //TODO: Replace with global var and use dynamic window loader
|
|
Update();
|
|
Render();
|
|
|
|
mFrameCnt++;
|
|
time.Reset();
|
|
mPrevdelta = -mDelta;
|
|
} else {
|
|
mPrevdelta = mDelta;
|
|
}
|
|
|
|
sf::sleep(sf::milliseconds(TARGET_FPS - (mDelta - mPrevdelta)));
|
|
|
|
if (mMainWindow->isOpen())
|
|
return GAME_RUNNING;
|
|
else
|
|
return GAME_QUIT;
|
|
}
|
|
|
|
void Game::Update() {
|
|
|
|
}
|
|
|
|
void Game::Render() {
|
|
// Clear the draw buffer
|
|
mMainWindow->clear(sf::Color::Black);
|
|
|
|
// Draw the arena view
|
|
mWorldUI->Draw(mMainWindow);
|
|
// Draw the UI above
|
|
mCockpitUI->Draw(mMainWindow);
|
|
// Draw the debug informations if enabled
|
|
if (mDbgUI != nullptr) mDbgUI->DrawDebugData(mMainWindow);
|
|
|
|
// Present the draw buffer
|
|
mMainWindow->display();
|
|
} |