[Tux4kids-commits] r1555 - tuxmath/trunk/src
David Bruce
dbruce-guest at alioth.debian.org
Fri Sep 25 03:10:30 UTC 2009
Author: dbruce-guest
Date: 2009-09-25 03:10:30 +0000 (Fri, 25 Sep 2009)
New Revision: 1555
Modified:
tuxmath/trunk/src/game.c
tuxmath/trunk/src/highscore.c
tuxmath/trunk/src/network.c
tuxmath/trunk/src/server.c
tuxmath/trunk/src/testclient.c
tuxmath/trunk/src/throttle.c
tuxmath/trunk/src/throttle.h
tuxmath/trunk/src/titlescreen.c
Log:
Converted Throttle() to thread-safe version
Modified: tuxmath/trunk/src/game.c
===================================================================
--- tuxmath/trunk/src/game.c 2009-09-25 03:10:24 UTC (rev 1554)
+++ tuxmath/trunk/src/game.c 2009-09-25 03:10:30 UTC (rev 1555)
@@ -229,6 +229,8 @@
int game(void)
{
+ Uint32 timer = 0;
+
DEBUGMSG(debug_game, "Entering game():\n");
//see if the option matches the actual screen
@@ -314,7 +316,7 @@
/* Pause (keep frame-rate event) */
- Throttle(MS_PER_FRAME);
+ Throttle(MS_PER_FRAME, &timer);
}
while(GAME_IN_PROGRESS == game_status);
@@ -394,7 +396,7 @@
/* draw_console_image(tux_img);*/
SDL_Flip(screen);
- Throttle(MS_PER_FRAME);
+ Throttle(MS_PER_FRAME, &timer);
}
while (looping);
break;
@@ -430,7 +432,7 @@
SDL_BlitSurface(images[IMG_GAMEOVER], NULL, screen, &dest_message);
SDL_Flip(screen);
- Throttle(MS_PER_FRAME);
+ Throttle(MS_PER_FRAME, &timer);
}
while (looping);
Modified: tuxmath/trunk/src/highscore.c
===================================================================
--- tuxmath/trunk/src/highscore.c 2009-09-25 03:10:24 UTC (rev 1554)
+++ tuxmath/trunk/src/highscore.c 2009-09-25 03:10:30 UTC (rev 1555)
@@ -731,6 +731,7 @@
SDL_Rect okRect;
int finished = 0;
Uint32 frame = 0;
+ Uint32 timer = 0;
const int BG_Y = 100;
const int BG_WIDTH = 400;
const int BG_HEIGHT = 200;
@@ -844,7 +845,7 @@
}
HandleTitleScreenAnimations();
- Throttle(20);
+ Throttle(20, &timer);
frame++;
} // End of while (!finished) loop
@@ -860,6 +861,7 @@
SDL_Rect loc;
int finished = 0;
Uint32 frame = 0;
+ Uint32 timer = 0;
const int BG_Y = 100;
const int BG_WIDTH = 400;
const int BG_HEIGHT = 200;
@@ -985,7 +987,7 @@
}
HandleTitleScreenAnimations();
- Throttle(20);
+ Throttle(20, &timer);
frame++;
} // End of while (!finished) loop
@@ -1009,6 +1011,7 @@
int tux_frame = 0;
Uint32 frame = 0;
Uint32 start = 0;
+ Uint32 timer = 0;
int servers_found = 0;
@@ -1149,10 +1152,7 @@
}
/* Wait so we keep frame rate constant: */
- while ((SDL_GetTicks() - start) < 33)
- {
- SDL_Delay(20);
- }
+ Throttle(20, &timer);
frame++;
} // End of while (!finished) loop
Modified: tuxmath/trunk/src/network.c
===================================================================
--- tuxmath/trunk/src/network.c 2009-09-25 03:10:24 UTC (rev 1554)
+++ tuxmath/trunk/src/network.c 2009-09-25 03:10:30 UTC (rev 1555)
@@ -48,7 +48,7 @@
int seconds = 0;
int num_servers = 0;
int i = 0;
-
+ Uint32 timer = 0;
//zero out old server list
for(i = 0; i < MAX_SERVERS; i++)
servers[i].ip.host = 0;
@@ -114,7 +114,7 @@
}
}
//Make sure we always scan at least one but not more than five seconds:
- Throttle(1000); //repeat once per second
+ Throttle(1000, &timer); //repeat once per second
seconds++;
if(seconds < 1)
done = 0;
Modified: tuxmath/trunk/src/server.c
===================================================================
--- tuxmath/trunk/src/server.c 2009-09-25 03:10:24 UTC (rev 1554)
+++ tuxmath/trunk/src/server.c 2009-09-25 03:10:30 UTC (rev 1555)
@@ -115,6 +115,8 @@
/* servermain.c, that consists solely of a call to RunServer(). */
int RunServer(int argc, char* argv[])
{
+ Uint32 timer = 0;
+
printf("Started tuxmathserver, waiting for client to connect:\n>\n");
server_handle_command_args(argc, argv);
@@ -145,7 +147,7 @@
/* NOTE almost certainly could make this longer wtihout noticably */
/* affecting performance, but even throttling to 1 msec/loop cuts */
/* CPU from 100% to ~2% on my desktop - DSB */
- Throttle(5); //min loop time 5 msec
+ Throttle(5, &timer); //min loop time 5 msec
}
/* ----- Free resources before exiting: ------- */
@@ -216,6 +218,8 @@
// setup_server() - all the things needed to get server running:
int setup_server(void)
{
+ Uint32 timer = 0;
+
//Initialize SDL and SDL_net:
if(SDL_Init(0) == -1)
{
@@ -277,7 +281,7 @@
{
if(read_stdin_nonblock(server_name, NAME_SIZE))
name_recvd = 1;
- Throttle(10);
+ Throttle(10, &timer);
}
if(!name_recvd)
printf("No name entered within timeout, will use default: %s\n",
Modified: tuxmath/trunk/src/testclient.c
===================================================================
--- tuxmath/trunk/src/testclient.c 2009-09-25 03:10:24 UTC (rev 1554)
+++ tuxmath/trunk/src/testclient.c 2009-09-25 03:10:30 UTC (rev 1555)
@@ -67,6 +67,7 @@
char buffer[NET_BUF_LEN]; // for command-line input
int servers_found = 0;
int server_number = -1;
+ Uint32 timer = 0;
//Scan local network to find running server:
servers_found = LAN_DetectServers();
@@ -177,7 +178,7 @@
}
}
//Limit loop to once per 10 msec so we don't eat all CPU
- Throttle(10);
+ Throttle(10, &timer);
}
LAN_Cleanup();
@@ -351,6 +352,7 @@
int ans = 0;
MC_FlashCard* fc = NULL;
char buf[NET_BUF_LEN];
+ Uint32 timer = 0;
printf("\nStarting Tux, of the Math Command Line ;-)\n");
printf("Waiting for other players to be ready...\n\n");
@@ -422,7 +424,7 @@
} //input wasn't any of our keywords
} // Input was received
- Throttle(10); //so don't eat all CPU
+ Throttle(10, &timer); //so don't eat all CPU
} //End of game loop
switch(game_status)
Modified: tuxmath/trunk/src/throttle.c
===================================================================
--- tuxmath/trunk/src/throttle.c 2009-09-25 03:10:24 UTC (rev 1554)
+++ tuxmath/trunk/src/throttle.c 2009-09-25 03:10:30 UTC (rev 1555)
@@ -14,14 +14,15 @@
#include "SDL.h"
-/* NOTE this is not thread-safe. For multithread use we need to store */
-/* last_t somewhere outside the function - DSB */
+/* NOTE now store the time elsewhere to make function thread-safe */
-void Throttle(int loop_msec)
+void Throttle(int loop_msec, Uint32* last_t)
{
- static Uint32 last_t; //Will be zero first time through
Uint32 now_t, wait_t;
+ if(!last_t)
+ return;
+
//Target loop time must be between 0 and 1000 msec:
if(loop_msec < 0)
loop_msec = 0;
@@ -30,9 +31,9 @@
//See if we need to wait:
now_t = SDL_GetTicks();
- if (now_t < (last_t + loop_msec))
+ if (now_t < (*last_t + loop_msec))
{
- wait_t = (last_t + loop_msec) - now_t;
+ wait_t = (*last_t + loop_msec) - now_t;
//Avoid problem if we somehow wrap past uint32 size (at 49.7 days!)
if(wait_t < 0)
wait_t = 0;
@@ -40,5 +41,5 @@
wait_t = loop_msec;
SDL_Delay(wait_t);
}
- last_t = SDL_GetTicks();
+ *last_t = SDL_GetTicks();
}
Modified: tuxmath/trunk/src/throttle.h
===================================================================
--- tuxmath/trunk/src/throttle.h 2009-09-25 03:10:24 UTC (rev 1554)
+++ tuxmath/trunk/src/throttle.h 2009-09-25 03:10:30 UTC (rev 1555)
@@ -12,9 +12,13 @@
#ifndef THROTTLE_H
#define THROTTLE_H
+
+#include "SDL.h"
+
// This simple function uses SDL_Delay() to wait to return until 'loop_msec'
// milliseconds after it returned the last time. Per SDL docs, the granularity
// is likely no better than 10 msec
-void Throttle(int loop_msec);
+// NOTE Uint32* last_t arg added to make function thread-safe
+void Throttle(int loop_msec, Uint32* last_t);
#endif
Modified: tuxmath/trunk/src/titlescreen.c
===================================================================
--- tuxmath/trunk/src/titlescreen.c 2009-09-25 03:10:24 UTC (rev 1554)
+++ tuxmath/trunk/src/titlescreen.c 2009-09-25 03:10:30 UTC (rev 1555)
@@ -493,6 +493,7 @@
SDL_Surface *s1, *s2, *s3, *s4;
SDL_Rect loc;
int finished = 0;
+ Uint32 timer = 0;
/* To adjust font size: */
float scale = screen->w / 640;
@@ -584,7 +585,7 @@
HandleTitleScreenAnimations();
/* Wait so we keep frame rate constant: */
- Throttle(20);
+ Throttle(20, &timer);
} // End of while (!finished) loop
SDL_FreeSurface(s1);
More information about the Tux4kids-commits
mailing list