[Xbubble-commits] xbubble/src game.c,1.1,1.2

Martin Quinson xbubble-devel@lists.alioth.debian.org
Wed, 27 Apr 2005 14:31:40 +0000


Update of /cvsroot/xbubble/xbubble/src
In directory haydn:/tmp/cvs-serv26338

Modified Files:
	game.c 
Log Message:
allow key press to skip the animations; piky cleanups

Index: game.c
===================================================================
RCS file: /cvsroot/xbubble/xbubble/src/game.c,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- game.c	5 Dec 2004 20:40:59 -0000	1.1
+++ game.c	27 Apr 2005 14:31:37 -0000	1.2
@@ -119,13 +119,36 @@
 Game current_game = NULL;
 sigjmp_buf restart_thinking;
 
-char *player_name[][2] = {
+const char *player_name[][2] = {
   { PLAYER1_TAG, "" },
   { PLAYER1_TAG, PLAYER2_TAG },
   { PLAYER1_TAG, COMPUTER_TAG },
   { DEMO_TAG, DEMO_TAG }};
 
-void display_cups( Game game, int n, int on ) {
+/* When the timer is running, sleep up to ms time, or until a key gets pressed.
+ * To be called when the timer is running.
+ * Returns: whether it was interrupted. */
+static int interupted_sleep(long ms,Game game) {
+  long i;
+
+  block_timer();
+  if (game->state != FINISHED && game->state != STOPPED) 
+     fail("interupted_sleep works only when the game is finished or stopped, it's %d\n",game->state);
+  game->key_pressed = 0;
+  for ( i = ms/50; i > 0; i-- ) {
+     if (game->key_pressed) {
+	unblock_timer();
+	return 1;
+     }
+     unblock_timer();
+     timer_sleep(50);
+     block_timer();
+  }
+  unblock_timer();
+  return 0;
+}
+
+static void display_cups( Game game, int n, int on ) {
   
   Pixmap back;
   int i, x, y, width, height, pos = 0;  
@@ -323,7 +346,8 @@
 
   game->pause_pressed = 0;
   game->escape_pressed = 0;
-  game->key_pressed = 0;
+  if (game->state != FINISHED && game->state != STOPPED) /* keypress skips the animations */
+     game->key_pressed = 0;
 
   while ( XPending(display) ) {
     XNextEvent( display, &event);
@@ -604,7 +628,7 @@
     else
       animate_game( game, frame_duration/1000 );
     break;
-    
+
   default:
     break;
   }
@@ -685,7 +709,7 @@
 }
  
 static void show_blinking_cups( Game game ) {
-  int i;
+  int i,done=0;
   int on = 1;
   int winner = -1;
   if ( game->multi_player ) {
@@ -694,26 +718,27 @@
      if ( game->result == PLAYER1_LOST )
        winner = 1;
   }
+
   /* display blinking cup for 3 seconds */
-  for ( i = 0; i < 15; i++ ) {
-    if (( winner >= 0 )&&( game->mode != DEMO )) {
+  for ( i = 0; i < 15 && !done; i++ ) {
+    if (( winner >= 0 ) && ( game->mode != DEMO )) {
+
       block_timer();
        
-      /* Mt: The following does not do what I want (get out of the loop on key press)  
-      process_x_events( game );
-      if (game->escape_pressed || game->key_pressed) {
-	 unblock_timer();
-	 return;
+      /* Skip the animation on key press */
+      if (game->key_pressed) {
+	 done = 1;
       }
-       */
 	 
       display_cups( game, winner, on );
-       XClearWindow( display, game->cup_box[winner] );
+      XClearWindow( display, game->cup_box[winner] );
       unblock_timer();
       on = 1 - on;
     }
-    timer_sleep(200);
+    done = done || interupted_sleep(200,game);
   }
+  if (( winner >= 0 ) && ( game->mode != DEMO )) 
+    display_cups( game, winner, 1 );
 }
 
 static void computer_think( Game game ) {
@@ -738,6 +763,7 @@
 }
 
 enum GameResult play_game( Game game ) {
+  int i, pressed;
   enum GameState state;
   current_game = game;
   game->state = STOPPED;
@@ -751,10 +777,11 @@
   if ( game->mode != DEMO ) {
     block_timer();           /* enter critical section */
     show_start_boxes(game);
+    game->key_pressed = 0;
     unblock_timer();         /* exit critical section */
-    
-    timer_sleep( 1500 );
-    
+
+    interupted_sleep(1500,game);
+     
     block_timer();
     hide_game_msg(game);
     unblock_timer();
@@ -796,3 +823,4 @@
   current_game = NULL;
   return game->result;
 }
+