Added Framerate Limit Code

This commit is contained in:
James Turk 2003-01-04 05:16:01 +00:00
parent 6ebe4a222b
commit 8219992471
9 changed files with 87 additions and 15 deletions

View File

@ -13,7 +13,7 @@
File: ZE_ZEngine.h <br>
Description: Header file for ZEngine class, the core of the ZEngine. <br>
Author(s): James Turk <br>
$Id: ZE_ZEngine.h,v 1.8 2002/12/29 06:50:19 cozman Exp $<br>
$Id: ZE_ZEngine.h,v 1.9 2003/01/04 05:16:02 cozman Exp $<br>
\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.

View File

@ -13,7 +13,7 @@
File: ZE_ZEngine.cpp <br>
Description: Implementation source file for ZEngine library main singleton class. <br>
Author(s): James Turk <br>
$Id: ZE_ZEngine.cpp,v 1.9 2002/12/29 06:52:07 cozman Exp $<br>
$Id: ZE_ZEngine.cpp,v 1.10 2003/01/04 05:16:01 cozman Exp $<br>
\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;

View File

@ -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

View File

@ -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()

View File

@ -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()

View File

@ -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

View File

@ -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()

View File

@ -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]);

View File

@ -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()