From ca8754a3a2296ff2842c781d1936dd47cecb3c9f Mon Sep 17 00:00:00 2001 From: pixel Date: Sun, 9 Mar 2003 21:19:43 +0000 Subject: FPS --- include/engine.h | 4 ++++ lib/engine.cc | 20 ++++++++++++++++++++ src/test.cc | 5 ++++- 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/include/engine.h b/include/engine.h index 64e823d..f950314 100644 --- a/include/engine.h +++ b/include/engine.h @@ -21,6 +21,7 @@ namespace mogltk { static int mouseX(); static int mouseY(); static int mousebuttons(); + static double FPS(); private: static bool inited; static bool postsetuped; @@ -30,6 +31,9 @@ namespace mogltk { static int mx; static int my; static int mbuttons; + static int frames; + static double curfps; + static Uint32 curticks; static void updatemouse(); }; }; diff --git a/lib/engine.cc b/lib/engine.cc index ed52d8c..2d53beb 100644 --- a/lib/engine.cc +++ b/lib/engine.cc @@ -5,6 +5,11 @@ bool mogltk::engine::inited = false, mogltk::engine::postsetuped = false; bool mogltk::engine::appactive = false, mogltk::engine::cursorvisible = false, mogltk::engine::quitrequest = false; int mogltk::engine::mx, mogltk::engine::my, mogltk::engine::mbuttons; +int mogltk::engine::frames; +double mogltk::engine::curfps = -1; +Uint32 mogltk::engine::curticks; + +#define UPDATERATE 1000 int mogltk::engine::setup() throw(GeneralException) { if (inited) { @@ -28,6 +33,7 @@ int mogltk::engine::postsetup() throw(GeneralException) { } SDL_EnableUNICODE(1); + curticks = SDL_GetTicks(); postsetuped = true; @@ -110,6 +116,16 @@ void mogltk::engine::pollevents() { if (!postsetuped) postsetup(); + if (appactive) { + Uint32 ticks = SDL_GetTicks(); + frames++; + if (ticks - curticks > UPDATERATE) { + curfps = (double) frames * 1000 / (ticks - curticks); + frames = 0; + curticks = ticks; + } + } + while(true) { if (hastoreturn) if (!SDL_PollEvent(NULL)) { @@ -197,3 +213,7 @@ int mogltk::engine::mouseY() { int mogltk::engine::mousebuttons() { return mbuttons; } + +double mogltk::engine::FPS() { + return curfps; +} diff --git a/src/test.cc b/src/test.cc index e56b6f4..64ed569 100644 --- a/src/test.cc +++ b/src/test.cc @@ -12,7 +12,6 @@ CODE_BEGINS virtual int startup() throw (GeneralException) { - verbosity = M_INFO; mogltk::glbase::setup(); new Archive("datas.paq"); Input * fonte = new Input("font-2.bin"); @@ -24,6 +23,7 @@ virtual int startup() throw (GeneralException) { delete cursor; mogltk::engine::setcursorvisible(true); + mogltk::engine::setappactive(true); Input * pattern = new Input("pattern6.tex"); mogltk::texture * mytex = new mogltk::texture(pattern, true); @@ -82,6 +82,9 @@ virtual int startup() throw (GeneralException) { "It works!!\n" "I can't believe it!\n" ); + + font.putcursor(550, 450); + font.printf("FPS: %.2f", mogltk::engine::FPS()); s->draw(mogltk::engine::mouseX(), mogltk::engine::mouseY()); -- cgit v1.2.3