Skip to content

Instantly share code, notes, and snippets.

@Quipyowert2
Created July 30, 2024 05:32
Show Gist options
  • Save Quipyowert2/42308631b1531a4e8a509bd1f166d17b to your computer and use it in GitHub Desktop.
Save Quipyowert2/42308631b1531a4e8a509bd1f166d17b to your computer and use it in GitHub Desktop.
Measure Frames per Second in Globulation 2
diff --git a/libgag/src/GUIBase.cpp b/libgag/src/GUIBase.cpp
index 36277e6b..9e3f2de1 100644
--- a/libgag/src/GUIBase.cpp
+++ b/libgag/src/GUIBase.cpp
@@ -525,8 +525,8 @@ namespace GAGGUI
// wait timer
frameWaitTime=static_cast<Sint64>(SDL_GetTicks64())-static_cast<Sint64>(frameStartTime);
frameWaitTime=stepLength-frameWaitTime;
- if (frameWaitTime>0)
- SDL_Delay(frameWaitTime);
+ //if (frameWaitTime>0)
+ //SDL_Delay(frameWaitTime);
}
// destroy screen event
diff --git a/libgag/src/GraphicContext.cpp b/libgag/src/GraphicContext.cpp
index 4a1dd120..9fc9c65f 100644
--- a/libgag/src/GraphicContext.cpp
+++ b/libgag/src/GraphicContext.cpp
@@ -38,6 +38,7 @@
#include <valarray>
#include <cstdlib>
#include <memory>
+#include <chrono>
#ifdef HAVE_CONFIG_H
#include <config.h>
@@ -2142,6 +2143,7 @@ namespace GAGCore
{
SDL_GLContext context = SDL_GL_CreateContext(window);
SDL_GL_MakeCurrent(window, context);
+ SDL_GL_SetSwapInterval(0);
}
// set _glFormat
if ((optionFlags & USEGPU) && (_gc->sdlsurface->format->BitsPerPixel != 32))
@@ -2231,6 +2233,28 @@ namespace GAGCore
}
}
+ /* Frames per second. CalcAverageTick function is by KPexEA https://stackoverflow.com/a/87732 */
+ #define MAXSAMPLES 100
+ int tickindex = 0;
+ int ticksum = 0;
+ int ticklist[MAXSAMPLES];
+
+ /* need to zero out the ticklist array before starting */
+ /* average will ramp up until the buffer is full */
+ /* returns average ticks per frame over the MAXSAMPLES last frames */
+
+ double CalcAverageTick(int newtick)
+ {
+ ticksum -= ticklist[tickindex]; /* subtract value falling off */
+ ticksum += newtick; /* add new value */
+ ticklist[tickindex] = newtick; /* save new value so it can be subtracted later */
+ if (++tickindex == MAXSAMPLES) /* inc buffer index */
+ tickindex = 0;
+
+ /* return average */
+ return((double)ticksum / MAXSAMPLES);
+ }
+
void GraphicContext::nextFrame(void)
{
DrawableSurface::nextFrame();
@@ -2245,6 +2269,22 @@ namespace GAGCore
cursorManager.draw(this, mx, my);
}
+ //static Sint64 lastTick = 0;
+ //Uint64 now = SDL_GetTicks64();
+ //double averageTick = CalcAverageTick(SDL_GetTicks64() - lastTick);
+ using namespace std::chrono;
+ static steady_clock::time_point lastTick;
+ steady_clock::time_point now = steady_clock::now();
+ milliseconds elapsed = duration_cast<milliseconds>(now - lastTick);
+ double averageTick = CalcAverageTick(elapsed.count());
+ static int counter = 0;
+ if (counter++ == 20)
+ {
+ std::cout << "FPS: " << 1000 / averageTick << " frame time: " << elapsed.count() << std::endl;
+ counter = 0;
+ }
+ lastTick = steady_clock::now();
+
#ifdef HAVE_OPENGL
if (optionFlags & GraphicContext::USEGPU)
{
diff --git a/src/Engine.cpp b/src/Engine.cpp
index 6e22bfdf..c7483707 100644
--- a/src/Engine.cpp
+++ b/src/Engine.cpp
@@ -533,7 +533,7 @@ int Engine::run(void)
//Any inconsistancies in the delays will be smoothed throughout the following frames,
Uint64 delay = std::max<Sint64>(0, needToBeTime - currentTime);
- SDL_Delay(delay);
+ //SDL_Delay(delay);
// we set CPU stats
// net->setLeftTicks(computationAvailableTicks);//We may have to tell others IP players to wait for our slow computer.
@Quipyowert2
Copy link
Author

Note: GraphicContext::2269 is the method GraphicContext::nextFrame, GraphicContext::2146 is in GraphicContext::setRes
The diff doesn't show what method some of the changes are in because the start of the methods are indented, so instead Git says "namespace GAGCore" instead of the name of the enclosing method.
Globulation 2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment