From ce53f20f9a78b01966c91838ee3b80b4c674bc35 Mon Sep 17 00:00:00 2001 From: Patrik Gornicz Date: Sun, 16 Nov 2008 20:33:23 -0500 Subject: [PATCH] created basic threads --- src/debug.h | 7 ++- src/main.cpp | 157 +++++++++++++++-------------------------------------------- 2 files changed, 46 insertions(+), 118 deletions(-) diff --git a/src/debug.h b/src/debug.h index 1fb0131..517c0c6 100644 --- a/src/debug.h +++ b/src/debug.h @@ -18,6 +18,11 @@ #ifndef DEBUG_H #define DEBUG_H +#include +using std::cout; +using std::cerr; +using std::endl; + void DPF(int level, const char* pstr); // comment out when not debugging @@ -27,7 +32,7 @@ void DPF(int level, const char* pstr); #define WARNINGS // comment out to prevent FPS and UPS printing -//#define FPSUPS +#define FPSUPS #endif // DEBUG_H diff --git a/src/main.cpp b/src/main.cpp index a9984f6..d34f420 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -38,7 +38,6 @@ void sighandler( int sig ); void run(); void clean(); -void blockUpdate(); void updateFPSCounters(); void handleInput(); @@ -52,9 +51,7 @@ bool is_Running; /* Values for the main game loop * target_UPS := the amount of updates that is wanted in one second - * last_Update := stores the time of the last update - * update_Sum := used to determine the updates needs for this run - + * * ups := updates per second for the last second * fps := frames per second for the last second * update_Count := counts this seconds updates @@ -62,28 +59,13 @@ bool is_Running; * last_Second := stores the time of the last second, used for ups and fps */ int target_UPS = 100; -long int last_Block_Update; -float update_Sum = 0; -int ups=100, fps=100; +int ups, fps; int update_Count, draw_Count; long int last_Second; - -float target_time_steps_per_second = 1000; -float times; -float time_steps_Count; - - -// experiment with rolling averages -float rUpdate = 100; -float rDraw = 100; -float rInput = 100; -float rRun = 100; - -float num = 10; - -float total = 0; +int startUpdateThread(void*); +int startDrawThread(void*); /// ***** MAIN Method ***** int main(int argc, char** args) @@ -133,62 +115,12 @@ void clean() void run() { is_Running = true; - last_Block_Update = tickCountMicro(); - last_Second = tickCountMicro(); - - while(is_Running) - { - long int time; - - time = tickCountMicro(); - blockUpdate(); - updateFPSCounters(); - draw(); - time = tickCountMicro() - time; - rRun = (rRun * (num-1) + time) / num; + SDL_Thread* updates = SDL_CreateThread(startUpdateThread, NULL); - //cout << "total:\t" << total << endl; - //cout << "rR:\t" << rRun << endl; - //total = 0; - } -} - -void blockUpdate() -{ - long int start = tickCountMicro(); - long int diff = start - last_Block_Update; - - //cout << "Block" << endl; - - // Calculate the updates that should be run for the next draw - update_Sum += (float)(diff * target_UPS) / 1000000; + startDrawThread(NULL); - // insures the float to int cast is done once. - int iupdate_sum = (int)update_Sum; - - // TODO the main run loop needs to be tested and pruned - if (iupdate_sum > 0) - { - // Calculate a time step that spreads the updates out as much as - // possible used because really quick updates are nearly wasted - - //float time_step = ((float)diff) / iupdate_sum / 1000; - //float time_step = 1000 / (100000 / rUpdate) / iupdate_sum; - //float time_step = 1000 / ups / iupdate_sum; - float time_step = 10; - - // run the updates - for (int i = 0; i < iupdate_sum; i++) - { - handleInput(); - update(time_step); - } - - // remove the updates that were run from the sum - update_Sum -= iupdate_sum; - last_Block_Update = tickCountMicro(); - } + SDL_WaitThread(updates, NULL); } void updateFPSCounters() @@ -198,77 +130,68 @@ void updateFPSCounters() { ups = update_Count; fps = draw_Count; - times = time_steps_Count; - update_Count = 0; - draw_Count = 0; - time_steps_Count = 0; + + // NOT thread safe, but they're estimates anyways + update_Count -= ups; + draw_Count -= fps; last_Second = tickCountMicro(); #ifdef FPSUPS cout << "ups:\t" << ups << endl; cout << "fps:\t" << fps << endl; - cout << "times:\t" << times << endl; #endif } } void handleInput() { - long int time; - - time = tickCountMicro(); - input::update(); + input::update(); - game::handleInput(); + game::handleInput(); - if(cfg::endGame()) - is_Running = false; - time = tickCountMicro() - time; - - rInput = (rInput*(num-1) + time) /num; - - total += rInput; + if(cfg::endGame()) + is_Running = false; } void update(float time_step) { - long int time; - + game::update(time_step); update_Count++; - time_steps_Count += time_step; - - time = tickCountMicro(); - game::update(time_step); - time = tickCountMicro() - time; - - rUpdate = (rUpdate * (num-1) + time) / num; - - //cout << "ts:\t" << time_step << endl; - //cout << "ru:\t" << rUpdate << endl; - - total += rUpdate; } void draw() { - long int time; + game::draw(); - draw_Count++; + SDL_GL_SwapBuffers(); + + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - time = tickCountMicro(); - game::draw(); + draw_Count++; +} - SDL_GL_SwapBuffers(); +int startUpdateThread(void*) +{ + while(is_Running) + { + handleInput(); + update(10); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + updateFPSCounters(); - //SDL_Delay(10); - time = tickCountMicro() - time; + SDL_Delay(10); + } - rDraw = (rDraw*(num-1) + time) /num; + return 0; +} - //cout << "rd:\t" << rDraw << endl; +int startDrawThread(void*) +{ + while(is_Running) + { + draw(); + } - total += rDraw; + return 0; } -- 2.10.2