From 8baa4b2f0b559c9c48fba41b8dc4a4d5f92f728b Mon Sep 17 00:00:00 2001 From: Patrik Gornicz Date: Sun, 16 Nov 2008 22:41:07 -0500 Subject: [PATCH] update thread setup with a good cap --- src/Makefile | 2 +- src/config/reader.cpp | 8 ++++---- src/main.cpp | 27 ++++++++++++++++++++++----- src/ticks.cpp | 6 +++--- src/ticks.h | 8 +++++--- 5 files changed, 35 insertions(+), 16 deletions(-) diff --git a/src/Makefile b/src/Makefile index b9dc85d..56bb7d6 100644 --- a/src/Makefile +++ b/src/Makefile @@ -9,7 +9,7 @@ PRFFLAGS := ${DBGFLAGS} -pg MYFLAGS := -Wall -pedantic -ansi VALFLAGS := --leak-check=full -CXXFLAGS := ${MYFLAGS} ${DBGFLAGS} +CXXFLAGS := ${MYFLAGS} ${OPTFLAGS} CXX := g++ diff --git a/src/config/reader.cpp b/src/config/reader.cpp index ba353ca..a981719 100644 --- a/src/config/reader.cpp +++ b/src/config/reader.cpp @@ -118,11 +118,11 @@ bool extractLine(const string& str, string* name, string* value) int char_pos = 0; - int name_start; - int name_end; + int name_start = 0; + int name_end = 0; - int value_start; - int value_end; + int value_start = 0; + int value_end = 0; const char* c_str = str.c_str(); diff --git a/src/main.cpp b/src/main.cpp index d34f420..1309824 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -59,10 +59,15 @@ bool is_Running; * last_Second := stores the time of the last second, used for ups and fps */ int target_UPS = 100; +float update_Wait = 0; int ups, fps; int update_Count, draw_Count; -long int last_Second; +MICRO last_Second; + +const int min_wait_milli = 20; +const int min_wait_micro = min_wait_milli * 1000; +const float game_step = 10; int startUpdateThread(void*); int startDrawThread(void*); @@ -118,6 +123,7 @@ void run() SDL_Thread* updates = SDL_CreateThread(startUpdateThread, NULL); + // for some reason this needs to be on the main thread ...? startDrawThread(NULL); SDL_WaitThread(updates, NULL); @@ -173,14 +179,25 @@ void draw() int startUpdateThread(void*) { + last_Second = tickCountMicro(); + while(is_Running) { - handleInput(); - update(10); + MICRO time = tickCountMicro(); + handleInput(); + update(game_step); + + updateFPSCounters(); + time = tickCountMicro() - time; - updateFPSCounters(); + float wait = (1000000.0 / target_UPS - time); + update_Wait += 0 < wait ? wait : 0; - SDL_Delay(10); + while(min_wait_micro < update_Wait) + { + update_Wait -= min_wait_micro; + SDL_Delay(min_wait_milli); + } } return 0; diff --git a/src/ticks.cpp b/src/ticks.cpp index 3766c2a..5bcefd6 100644 --- a/src/ticks.cpp +++ b/src/ticks.cpp @@ -24,7 +24,7 @@ /// ***** Public Methods ***** // returns the current microseconds from unix time -long int tickCountMicro() +MICRO tickCountMicro() { struct timeval tv; gettimeofday(&tv, 0); @@ -33,13 +33,13 @@ long int tickCountMicro() } // returns the current milliseconds from unix time -long int tickCountMilli() +MICRO tickCountMilli() { return tickCountMicro() / 1000; } // returns the current seconds from unix time -long int tickCountSec() +MICRO tickCountSec() { return tickCountMicro() / 1000000; } diff --git a/src/ticks.h b/src/ticks.h index 4f9ef0b..3c89a2e 100644 --- a/src/ticks.h +++ b/src/ticks.h @@ -20,13 +20,15 @@ /// ***** Public Methods ***** +typedef long int MICRO; + // returns the current seconds from program start -long int tickCountSec(); +MICRO tickCountSec(); // returns the current milliseconds from program start -long int tickCountMilli(); +MICRO tickCountMilli(); // returns the current microseconds from program start -long int tickCountMicro(); +MICRO tickCountMicro(); #endif // TICKS_H -- 2.10.2