From cc0f0022aa8bd7394fdaabe8215eda92a44d4d0e Mon Sep 17 00:00:00 2001 From: Vectozavr <60608292+vectozavr@users.noreply.github.com> Date: Sat, 6 Nov 2021 01:55:34 +0700 Subject: [PATCH] pushGLStates & popGLStates optimizations --- Source.cpp | 2 +- engine/Engine.cpp | 12 +++++++----- engine/utils/Timer.cpp | 8 ++++---- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/Source.cpp b/Source.cpp index 6525480..1737a7f 100644 --- a/Source.cpp +++ b/Source.cpp @@ -10,7 +10,7 @@ using namespace std; int main() { Shooter game; - game.create(1280, 720, ShooterConsts::PROJECT_NAME, false); + game.create(1280, 720, ShooterConsts::PROJECT_NAME, true); //game.create(1920, 1080, ShooterConsts::PROJECT_NAME, true, Consts::BACKGROUND_COLOR, sf::Style::Fullscreen); //game.create(2048, 1152, ShooterConsts::PROJECT_NAME, false); diff --git a/engine/Engine.cpp b/engine/Engine.cpp index c3ecc63..a2af2c5 100644 --- a/engine/Engine.cpp +++ b/engine/Engine.cpp @@ -69,6 +69,7 @@ void Engine::create(int screenWidth, int screenHeight, const std::string &name, } delete[] view; } else { + screen->pushGLStates(); // clear triangles from previous frame camera->clear(); // project triangles to the camera plane @@ -81,6 +82,7 @@ void Engine::create(int screenWidth, int screenHeight, const std::string &name, } _triPerSec = camera->buffSize() * Time::fps(); + screen->popGLStates(); } Time::stopTimer("d projections"); @@ -147,11 +149,11 @@ void Engine::printDebugInfo() const { float yPos = 300; int height = 50; - double totalTime = Time::elapsedTimerMilliseconds("d all"); + double totalTime = Time::elapsedTimerSeconds("d all"); double timeSum = 0; int i = 0; for (auto &[timerName, timer] : Time::timers()) { - int width = timerWidth * timer.elapsedMilliseconds() / totalTime; + int width = timerWidth * timer.elapsedSeconds() / totalTime; if (timerName == "d all" || timerName[0] != 'd') { continue; @@ -168,13 +170,13 @@ void Engine::printDebugInfo() const { screen->drawText( timerName.substr(2, timerName.size()) + ":\t" + - std::to_string((int) (1.0 / timer.elapsedMilliseconds())) + " / s \t (" + - std::to_string((int) (100 * timer.elapsedMilliseconds() / totalTime)) + "%)", + std::to_string((int) (1.0 / timer.elapsedSeconds())) + " / s \t (" + + std::to_string((int) (100 * timer.elapsedSeconds() / totalTime)) + "%)", Vec2D{xPos + 10, yPos + height * i + 5}, 30, sf::Color(0, 0, 0, 150)); i++; - timeSum += timer.elapsedMilliseconds(); + timeSum += timer.elapsedSeconds(); } int width = timerWidth * (totalTime - timeSum) / totalTime; diff --git a/engine/utils/Timer.cpp b/engine/utils/Timer.cpp index adbeb88..4296ebf 100644 --- a/engine/utils/Timer.cpp +++ b/engine/utils/Timer.cpp @@ -17,6 +17,10 @@ void Timer::stop() { } double Timer::elapsedMilliseconds() const { + return elapsedSeconds()*1000; +} + +double Timer::elapsedSeconds() const { high_resolution_clock::time_point endTime; if(_isRunning) { @@ -27,7 +31,3 @@ double Timer::elapsedMilliseconds() const { return duration(endTime - _startTime).count(); } - -double Timer::elapsedSeconds() const { - return elapsedMilliseconds() / 1000.0; -}