diff --git a/include/ZE_ZEngine.h b/include/ZE_ZEngine.h
index bd18ed8..dd20f04 100644
--- a/include/ZE_ZEngine.h
+++ b/include/ZE_ZEngine.h
@@ -13,7 +13,7 @@
File: ZE_ZEngine.h
Description: Header file for ZEngine class, the core of the ZEngine.
Author(s): James Turk
-$Id: ZE_ZEngine.h,v 1.8 2002/12/29 06:50:19 cozman Exp $
+$Id: ZE_ZEngine.h,v 1.9 2003/01/04 05:16:02 cozman Exp $
\file ZE_ZEngine.h
\brief Definition file for core ZEngine class.
@@ -172,7 +172,8 @@ class ZEngine
/*!
\brief Update display contents.
- Swap OpenGL buffers, and update screen. Must be called every frame.
+ Swap OpenGL buffers, and update screen, if a desired framerate is set it will delay to stay under that rate.
+ Must be called every frame.
**/
void Update();
@@ -209,6 +210,10 @@ class ZEngine
bool mPaused;
//! Keep track of if ZEngine should unpause on active event.
bool mUnpauseOnActive;
+ //! Value framerate strives to be at, set by SetDesiredFramerate.
+ Uint8 mDesiredFramerate;
+ //! Time scheduled for next update (used for framerate locked movement).
+ Uint32 mNextUpdate;
//! Keep track of time game was last paused.
Uint32 mLastPause;
//! Keep track of total globally paused time.
@@ -252,11 +257,40 @@ class ZEngine
/*!
\brief Get Seconds Per Frame.
- Get double that describes the time passed between screen updates. (used for Framerate Independant Movement)
+ Get double that describes the time passed between screen updates. (should be used for Framerate Independant Movement)
\return Time between screen updates.
**/
double GetFrameTime();
+ /*!
+ \brief Get Frames Per Second.
+
+ Get double representing current (approximate) FPS. This value is always the same as 1/GetFrameTime().
+ \since 0.8.2
+ \return Current Framerate.
+ **/
+ double GetFramerate();
+
+ /*!
+ \brief Set Desired Framerate.
+
+ Sets desired framerate, if engine gets ahead of desired rate during a frame it will stall in Update until
+ current framerate is closer to that desired. Acceptable values are 1-255, setting this value to 0 will disable this
+ feature. (Desired framerate is disabled upon initialization of ZEngine.)
+ \since 0.8.2
+ \param rate Desired framerate 1-255, or 0 to disable.
+ **/
+ void SetDesiredFramerate(Uint8 rate);
+
+ /*!
+ \brief Get Desired Framerate.
+
+ Get desired framerate set by SetDesiredFramerate.
+ \since 0.8.2
+ \return Current setting for desired framerate.
+ **/
+ Uint8 GetDesiredFramerate();
+
/*!
\brief Check Engine Paused State.
diff --git a/src/ZE_ZEngine.cpp b/src/ZE_ZEngine.cpp
index 92cb902..1d098fc 100644
--- a/src/ZE_ZEngine.cpp
+++ b/src/ZE_ZEngine.cpp
@@ -13,7 +13,7 @@
File: ZE_ZEngine.cpp
Description: Implementation source file for ZEngine library main singleton class.
Author(s): James Turk
-$Id: ZE_ZEngine.cpp,v 1.9 2002/12/29 06:52:07 cozman Exp $
+$Id: ZE_ZEngine.cpp,v 1.10 2003/01/04 05:16:01 cozman Exp $
\file ZE_ZEngine.cpp
\brief Central source file for ZEngine.
@@ -52,7 +52,8 @@ ZEngine::ZEngine()
mKeyPress[k] = false;
mUnpauseOnActive = mPaused = false;
- mLastPause = mPausedTime = mLastTime = 0;
+ mDesiredFramerate = 0;
+ mNextUpdate = mLastPause = mPausedTime = mLastTime = 0;
mSecPerFrame = 0.0;
}
@@ -210,6 +211,13 @@ void ZEngine::Update()
mSecPerFrame = (GetTime()-mLastTime)/1000.0;
mLastTime = GetTime();
+
+ if(mDesiredFramerate)
+ {
+ if(mLastTime < mNextUpdate)
+ SDL_Delay(mNextUpdate-mLastTime);
+ mNextUpdate = GetTime()+(1000/mDesiredFramerate);
+ }
}
void ZEngine::Clear(float red, float green, float blue, float alpha)
@@ -279,6 +287,21 @@ double ZEngine::GetFrameTime()
return mSecPerFrame;
}
+double ZEngine::GetFramerate()
+{
+ return 1/mSecPerFrame;
+}
+
+void ZEngine::SetDesiredFramerate(Uint8 rate)
+{
+ mDesiredFramerate = rate;
+}
+
+Uint8 ZEngine::GetDesiredFramerate()
+{
+ return mDesiredFramerate;
+}
+
bool ZEngine::IsPaused()
{
return mPaused;
diff --git a/test/ZFontTest.cpp b/test/ZFontTest.cpp
index 9bcc60c..d1d23f5 100644
--- a/test/ZFontTest.cpp
+++ b/test/ZFontTest.cpp
@@ -17,7 +17,7 @@ void Initialize()
{
ZEngine *engine = ZEngine::GetInstance();
ZConfigFile cfg("tests.zcf");
- int w,h,bpp;
+ int w,h,bpp,rate;
bool fs;
string title;
@@ -26,9 +26,11 @@ void Initialize()
bpp = cfg.GetInt("ZFontTest","bpp",32);
fs = cfg.GetBool("ZFontTest","fullscreen",false);
title = cfg.GetString("ZFontTest","title","ZFont Test");
+ rate = cfg.GetInt("ZFontTest","framerate",60);
engine->SetupDisplay(w,h,bpp,fs);
engine->CreateDisplay(title);
+ engine->SetDesiredFramerate(rate);
}
void Test()
@@ -36,7 +38,7 @@ void Test()
ZEngine *engine = ZEngine::GetInstance();
//Open and Setup all the Fonts and Create Images//
- ZImage text[5];
+ ZImage text[6];
ZFont almonte("data/almontew.ttf",48), axaxax("data/axaxax.ttf",32), betsy("data/betsy.ttf",64);
almonte.SetColor(255,0,0);
almonte.DrawText("This is the font test.",text[0]);
@@ -58,10 +60,11 @@ void Test()
engine->CheckEvents();
if(engine->KeyIsPressed(SDLK_ESCAPE))
engine->RequestQuit();
+ betsy.DrawText(FormatStr("FPS: %.2f",engine->GetFramerate()),text[5]);
engine->Clear(); //clear screen
//draw the images//
- for(int i=0; i <= 4; i++)
+ for(int i=0; i <= 5; i++)
text[i].Draw(10.0f*i,50.0f*i);
engine->Update(); //update the screen
diff --git a/test/ZImageTest.cpp b/test/ZImageTest.cpp
index d60962f..950e41c 100644
--- a/test/ZImageTest.cpp
+++ b/test/ZImageTest.cpp
@@ -17,7 +17,7 @@ void Initialize()
{
ZEngine *engine = ZEngine::GetInstance();
ZConfigFile cfg("tests.zcf");
- int w,h,bpp;
+ int w,h,bpp,rate;
bool fs;
string title;
@@ -26,9 +26,11 @@ void Initialize()
bpp = cfg.GetInt("ZImageTest","bpp",32);
fs = cfg.GetBool("ZImageTest","fullscreen",true);
title = cfg.GetString("ZImageTest","title","ZImage Test");
+ rate = cfg.GetInt("ZImageTest","framerate",60);
engine->SetupDisplay(w,h,bpp,fs);
engine->CreateDisplay(title);
+ engine->SetDesiredFramerate(rate);
}
void Test()
diff --git a/test/ZMouseTest.cpp b/test/ZMouseTest.cpp
index 3739cb2..a781b90 100644
--- a/test/ZMouseTest.cpp
+++ b/test/ZMouseTest.cpp
@@ -17,7 +17,7 @@ void Initialize()
{
ZEngine *engine = ZEngine::GetInstance();
ZConfigFile cfg("tests.zcf");
- int w,h,bpp;
+ int w,h,bpp,rate;
bool fs;
string title;
@@ -26,9 +26,11 @@ void Initialize()
bpp = cfg.GetInt("ZMouseTest","bpp",32);
fs = cfg.GetBool("ZMouseTest","fullscreen",false);
title = cfg.GetString("ZMouseTest","title","ZMouse Test");
+ rate = cfg.GetInt("ZMouseTest","framerate",60);
engine->SetupDisplay(w,h,bpp,fs);
engine->CreateDisplay(title);
+ engine->SetDesiredFramerate(rate);
}
void Test()
diff --git a/test/ZMusicTest.cpp b/test/ZMusicTest.cpp
index 390a0c0..e599ac4 100644
--- a/test/ZMusicTest.cpp
+++ b/test/ZMusicTest.cpp
@@ -17,7 +17,7 @@ void Initialize()
{
ZEngine *engine = ZEngine::GetInstance();
ZConfigFile cfg("tests.zcf");
- int w,h,bpp;
+ int w,h,bpp,rate;
bool fs;
string title;
@@ -26,9 +26,11 @@ void Initialize()
bpp = cfg.GetInt("ZMusicTest","bpp",32);
fs = cfg.GetBool("ZMusicTest","fullscreen",false);
title = cfg.GetString("ZMusicTest","title","ZMusic Test");
+ rate = cfg.GetInt("ZMusicTest","framerate",60);
engine->SetupDisplay(w,h,bpp,fs);
engine->CreateDisplay(title);
+ engine->SetDesiredFramerate(rate);
}
void Test()
@@ -51,6 +53,7 @@ void Test()
{
engine->CheckEvents();
+ engine->Update();
} while(!engine->QuitRequested());
}
else //this is the actual example
diff --git a/test/ZRectTest.cpp b/test/ZRectTest.cpp
index 8c445a0..8c01747 100644
--- a/test/ZRectTest.cpp
+++ b/test/ZRectTest.cpp
@@ -17,7 +17,7 @@ void Initialize()
{
ZEngine *engine = ZEngine::GetInstance();
ZConfigFile cfg("tests.zcf");
- int w,h,bpp;
+ int w,h,bpp,rate;
bool fs;
string title;
@@ -26,9 +26,11 @@ void Initialize()
bpp = cfg.GetInt("ZRectTest","bpp",32);
fs = cfg.GetBool("ZRectTest","fullscreen",false);
title = cfg.GetString("ZRectTest","title","ZRect Test");
+ rate = cfg.GetInt("ZRectTest","framerate",60);
engine->SetupDisplay(w,h,bpp,fs);
engine->CreateDisplay(title);
+ engine->SetDesiredFramerate(rate);
}
void Test()
diff --git a/test/ZSoundTest.cpp b/test/ZSoundTest.cpp
index f0739c3..411c20e 100644
--- a/test/ZSoundTest.cpp
+++ b/test/ZSoundTest.cpp
@@ -17,7 +17,7 @@ void Initialize()
{
ZEngine *engine = ZEngine::GetInstance();
ZConfigFile cfg("tests.zcf");
- int w,h,bpp;
+ int w,h,bpp,rate;
bool fs;
string title;
@@ -26,9 +26,11 @@ void Initialize()
bpp = cfg.GetInt("ZSoundTest","bpp",32);
fs = cfg.GetBool("ZSoundTest","fullscreen",false);
title = cfg.GetString("ZSoundTest","title","ZSound Test");
+ rate = cfg.GetInt("ZSoundTest","framerate",60);
engine->SetupDisplay(w,h,bpp,fs);
engine->CreateDisplay(title);
+ engine->SetDesiredFramerate(rate);
}
void Test()
@@ -79,7 +81,6 @@ void Test()
if(engine->KeyIsPressed(SDLK_DOWN))
sample[sampleNum].SetVolume(sample[sampleNum].Volume()-1);
-
font.DrawText(FormatStr("Volume: %d",sample[sampleNum].Volume()),text[4]);
font.DrawText(FormatStr("Sample: %s",name[sampleNum].c_str()),text[5]);
diff --git a/test/ZTimerTest.cpp b/test/ZTimerTest.cpp
index 93b3053..d16235b 100644
--- a/test/ZTimerTest.cpp
+++ b/test/ZTimerTest.cpp
@@ -17,7 +17,7 @@ void Initialize()
{
ZEngine *engine = ZEngine::GetInstance();
ZConfigFile cfg("tests.zcf");
- int w,h,bpp;
+ int w,h,bpp,rate;
bool fs;
string title;
@@ -26,9 +26,11 @@ void Initialize()
bpp = cfg.GetInt("ZTimerTest","bpp",32);
fs = cfg.GetBool("ZTimerTest","fullscreen",false);
title = cfg.GetString("ZTimerTest","title","ZTimer Test");
+ rate = cfg.GetInt("ZTimerTest","framerate",60);
engine->SetupDisplay(w,h,bpp,fs);
engine->CreateDisplay(title);
+ engine->SetDesiredFramerate(rate);
}
void Test()