SLUDGE: Objectify Timing

This commit is contained in:
Simei Yin 2017-07-17 12:14:40 +02:00
parent 4c4690a1e3
commit 5d554d36ed
5 changed files with 34 additions and 27 deletions

View File

@ -188,7 +188,7 @@ int main_loop(const char *filename)
startNewFunctionNum(0, 0, NULL, noStack); startNewFunctionNum(0, 0, NULL, noStack);
Init_Timer(); g_sludge->_timer.init();
weAreDoneSoQuit = 0; weAreDoneSoQuit = 0;
while (!weAreDoneSoQuit) { while (!weAreDoneSoQuit) {
@ -197,7 +197,7 @@ int main_loop(const char *filename)
handleInput(); handleInput();
sludgeDisplay(); sludgeDisplay();
handleSoundLists(); handleSoundLists();
Wait_Frame(); g_sludge->_timer.waitFrame();
} }
killSoundStuff(); killSoundStuff();

View File

@ -29,6 +29,7 @@
#include "gui/debugger.h" #include "gui/debugger.h"
#include "sludge/console.h" #include "sludge/console.h"
#include "sludge/timing.h"
namespace Sludge { namespace Sludge {
@ -62,6 +63,9 @@ public:
Common::String fatalMessage; Common::String fatalMessage;
Common::String fatalInfo; Common::String fatalInfo;
// timer
Timer _timer;
SludgeEngine(OSystem *syst, const SludgeGameDescription *gameDesc); SludgeEngine(OSystem *syst, const SludgeGameDescription *gameDesc);
virtual ~SludgeEngine(); virtual ~SludgeEngine();

View File

@ -71,7 +71,6 @@ int languageNum = -1;
int gameVersion; int gameVersion;
int specialSettings; int specialSettings;
FILETIME fileTime; FILETIME fileTime;
extern int desiredfps;
bool captureAllKeys = false; bool captureAllKeys = false;
byte brightnessLevel = 255; byte brightnessLevel = 255;
@ -221,7 +220,7 @@ bool initSludge(const Common::String &filename) {
debug(kSludgeDebugDataLoad, "winHeight : %i", winHeight); debug(kSludgeDebugDataLoad, "winHeight : %i", winHeight);
specialSettings = fp->readByte(); specialSettings = fp->readByte();
debug(kSludgeDebugDataLoad, "specialSettings : %i", specialSettings); debug(kSludgeDebugDataLoad, "specialSettings : %i", specialSettings);
desiredfps = 1000 / fp->readByte(); g_sludge->_timer.setDesiredfps(1000 / fp->readByte());
readString(fp); // Unused - was used for registration purposes. readString(fp); // Unused - was used for registration purposes.

View File

@ -25,36 +25,31 @@
namespace Sludge { namespace Sludge {
int desiredfps = 300; //holds desired frames per second void Timer::init(void) {
_desired_frame_time = 1000 / _desiredfps;
uint32 starttime, endtime; _starttime = g_system->getMillis();
uint32 desired_frame_time;
void Init_Timer(void) {
desired_frame_time = 1000 / desiredfps;
starttime = g_system->getMillis();
} }
void Init_Special_Timer(int t) { void Timer::initSpecial(int t) {
desired_frame_time = 1000 / t; _desired_frame_time = 1000 / t;
starttime = g_system->getMillis(); _starttime = g_system->getMillis();
} }
void Wait_Frame(void) { void Timer::waitFrame(void) {
static uint32 addNextTime = 0; static uint32 addNextTime = 0;
uint32 timetaken; uint32 timetaken;
for (;;) { for (;;) {
endtime = g_system->getMillis(); _endtime = g_system->getMillis();
timetaken = addNextTime + endtime - starttime; timetaken = addNextTime + _endtime - _starttime;
if (timetaken >= desired_frame_time) break; if (timetaken >= _desired_frame_time) break;
g_system->delayMillis(1); g_system->delayMillis(1);
} }
addNextTime = timetaken - desired_frame_time; addNextTime = timetaken - _desired_frame_time;
if (addNextTime > desired_frame_time) addNextTime = desired_frame_time; if (addNextTime > _desired_frame_time) addNextTime = _desired_frame_time;
starttime = endtime; _starttime = _endtime;
} }
} // End of namespace Sludge } // End of namespace Sludge

View File

@ -24,11 +24,20 @@
namespace Sludge { namespace Sludge {
void Init_Timer(void); class Timer {
void Init_Special_Timer(int t); private:
void Get_Start_Time(void); int _desiredfps; // desired frames per second
void Get_End_Time(void); uint32 _starttime, _endtime;
void Wait_Frame(void); uint32 _desired_frame_time;
public:
void setDesiredfps(int t) { _desiredfps = t; }
void init(void);
void initSpecial(int t);
void waitFrame(void);
Timer():_desiredfps(300), _starttime(0), _endtime(0), _desired_frame_time(0){}
};
} // End of namespace Sludge } // End of namespace Sludge