changeset 5581:5d395f25a8d2 draft

(svn r8028) -Fix: overflow of ticks was not handled properly, possibly resulting a non-reacting gameserver/gameclient.
author rubidium <rubidium@openttd.org>
date Wed, 10 Jan 2007 15:00:20 +0000
parents 1c57c05f51bd
children 2d685f2e1fe7
files src/video/cocoa_v.m src/video/dedicated_v.c src/video/sdl_v.c src/video/win32_v.c
diffstat 4 files changed, 21 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/src/video/cocoa_v.m
+++ b/src/video/cocoa_v.m
@@ -652,8 +652,8 @@
 
 static void QZ_GameLoop(void)
 {
-	uint32 next_tick = GetTick() + 30;
-	uint32 cur_ticks;
+	uint32 cur_ticks = GetTick();
+	uint32 next_tick = cur_ticks + 30;
 	uint32 pal_tick = 0;
 #ifdef _DEBUG
 	uint32 et0, et, st0, st;
@@ -680,6 +680,7 @@
 	CSleep(1);
 
 	for (;;) {
+		uint32 prev_cur_ticks; // to check for wrapping
 		InteractiveRandom(); // randomness
 
 		while (QZ_PollEvent()) {}
@@ -698,11 +699,8 @@
 		}
 
 		cur_ticks = GetTick();
-		if ((_fast_forward && !_pause) || cur_ticks > next_tick)
-			next_tick = cur_ticks;
-
-		if (cur_ticks == next_tick) {
-			next_tick += 30;
+		if (cur_ticks >= next_tick || (_fast_forward && !_pause) || cur_ticks < prev_cur_ticks) {
+			next_tick = cur_ticks + 30;
 
 			_ctrl_pressed = !!(_cocoa_video_data.current_mods & NSControlKeyMask);
 			_shift_pressed = !!(_cocoa_video_data.current_mods & NSShiftKeyMask);
--- a/src/video/dedicated_v.c
+++ b/src/video/dedicated_v.c
@@ -223,10 +223,8 @@
 
 static void DedicatedVideoMainLoop(void)
 {
-	uint32 next_tick;
-	uint32 cur_ticks;
-
-	next_tick = GetTime() + 30;
+	uint32 cur_ticks = GetTime();
+	uint32 next_tick = cur_ticks + 30;
 
 	/* Signal handlers */
 #ifdef UNIX
@@ -268,15 +266,15 @@
 	}
 
 	while (!_exit_game) {
+		uint32 prev_cur_ticks = cur_ticks; // to check for wrapping
 		InteractiveRandom(); // randomness
 
 		if (!_dedicated_forks)
 			DedicatedHandleKeyInput();
 
 		cur_ticks = GetTime();
-
-		if (cur_ticks >= next_tick) {
-			next_tick += 30;
+		if (cur_ticks >= next_tick || cur_ticks < prev_cur_ticks) {
+			next_tick = cur_ticks + 30;
 
 			GameLoop();
 			_screen.dst_ptr = _dedicated_video_mem;
--- a/src/video/sdl_v.c
+++ b/src/video/sdl_v.c
@@ -422,14 +422,15 @@
 
 static void SdlVideoMainLoop(void)
 {
-	uint32 next_tick = SDL_CALL SDL_GetTicks() + 30;
-	uint32 cur_ticks;
+	uint32 cur_ticks = SDL_CALL SDL_GetTicks();
+	uint32 next_tick = cur_ticks + 30;
 	uint32 pal_tick = 0;
 	uint32 mod;
 	int numkeys;
 	Uint8 *keys;
 
 	for (;;) {
+		uint32 prev_cur_ticks = cur_ticks; // to check for wrapping
 		InteractiveRandom(); // randomness
 
 		while (PollEvent() == -1) {}
@@ -451,11 +452,8 @@
 		}
 
 		cur_ticks = SDL_CALL SDL_GetTicks();
-		if ((_fast_forward && !_pause) || cur_ticks > next_tick)
-			next_tick = cur_ticks;
-
-		if (cur_ticks == next_tick) {
-			next_tick += 30;
+		if (cur_ticks >= next_tick || (_fast_forward && !_pause) || cur_ticks < prev_cur_ticks) {
+			next_tick = cur_ticks + 30;
 
 			_ctrl_pressed  = !!(mod & KMOD_CTRL);
 			_shift_pressed = !!(mod & KMOD_SHIFT);
--- a/src/video/win32_v.c
+++ b/src/video/win32_v.c
@@ -786,11 +786,14 @@
 static void Win32GdiMainLoop(void)
 {
 	MSG mesg;
-	uint32 next_tick = GetTickCount() + 30, cur_ticks;
+	uint32 cur_ticks = GetTickCount();
+	uint32 next_tick = cur_ticks + 30;
 
 	_wnd.running = true;
 
 	for (;;) {
+		uint32 prev_cur_ticks = cur_ticks; // to check for wrapping
+
 		while (PeekMessage(&mesg, NULL, 0, 0, PM_REMOVE)) {
 			InteractiveRandom(); // randomness
 			DispatchMessage(&mesg);
@@ -810,11 +813,8 @@
 		}
 
 		cur_ticks = GetTickCount();
-		if ((_fast_forward && !_pause) || cur_ticks > next_tick)
-			next_tick = cur_ticks;
-
-		if (cur_ticks == next_tick) {
-			next_tick += 30;
+		if (cur_ticks >= next_tick || (_fast_forward && !_pause) || cur_ticks < prev_cur_ticks) {
+			next_tick = cur_ticks + 30;
 			_ctrl_pressed = _wnd.has_focus && GetAsyncKeyState(VK_CONTROL)<0;
 			_shift_pressed = _wnd.has_focus && GetAsyncKeyState(VK_SHIFT)<0;
 #ifdef _DEBUG