[Tux4kids-commits] r1461 - tuxmath/branches/lan/src

David Bruce dbruce-guest at alioth.debian.org
Wed Aug 26 02:20:45 UTC 2009


Author: dbruce-guest
Date: 2009-08-26 02:20:45 +0000 (Wed, 26 Aug 2009)
New Revision: 1461

Modified:
   tuxmath/branches/lan/src/game.c
   tuxmath/branches/lan/src/globals.h
   tuxmath/branches/lan/src/highscore.c
   tuxmath/branches/lan/src/highscore.h
   tuxmath/branches/lan/src/options.c
   tuxmath/branches/lan/src/options.h
   tuxmath/branches/lan/src/titlescreen.c
Log:
adding support for run-time selection between lan and local game
- basically, fixing the lan branch of game.c so it can still play the "regular" game.

Modified: tuxmath/branches/lan/src/game.c
===================================================================
--- tuxmath/branches/lan/src/game.c	2009-08-23 00:13:14 UTC (rev 1460)
+++ tuxmath/branches/lan/src/game.c	2009-08-26 02:20:45 UTC (rev 1461)
@@ -42,6 +42,7 @@
 #include "titlescreen.h"
 #include "options.h"
 #include "SDL_extras.h"
+#include "throttle.h"
 #include "network.h"
 
 #define FPS 15                     /* 15 frames per second */
@@ -220,7 +221,9 @@
 
 int game(void)
 {
-  Uint32 last_time, now_time;
+//  Uint32 last_time, now_time;
+  char buf[NET_BUF_LEN];
+  char command[NET_BUF_LEN];
 
 #ifdef TUXMATH_DEBUG
   fprintf(stderr, "Entering game():\n");
@@ -261,13 +264,8 @@
   /* --- MAIN GAME LOOP: --- */
   do
   {
-    char buf[NET_BUF_LEN];
- 
-    char command[NET_BUF_LEN];
-    
     /* reset or increment various things with each loop: */
     frame++;
-    last_time = SDL_GetTicks();
     old_tux_img = tux_img;
     tux_pressing = 0;
 
@@ -321,18 +319,9 @@
     }
 #endif
 
+
     /* Pause (keep frame-rate event) */
-    now_time = SDL_GetTicks();
-    if (now_time < last_time + MS_PER_FRAME)
-    {
-      //Prevent any possibility of a time wrap-around
-      // (this is a very unlikely problem unless there is an SDL bug
-      //  or you leave tuxmath running for 49 days...)
-      now_time = (last_time+MS_PER_FRAME) - now_time;  // this holds the delay
-      if (now_time > MS_PER_FRAME)
-        now_time = MS_PER_FRAME;
-      SDL_Delay(now_time);
-    }
+    Throttle(MS_PER_FRAME);
 
   }
   while(GAME_IN_PROGRESS == game_status);
@@ -366,7 +355,6 @@
       do
       {
         frame++;
-        last_time = SDL_GetTicks();
 
         while (SDL_PollEvent(&event) > 0)
         {
@@ -381,8 +369,6 @@
         if (current_bkgd() )
           SDL_BlitSurface(current_bkgd(), NULL, screen, NULL);
 
-
-
         /* draw flashing victory message: */
         if (((frame / 2) % 4))
         {
@@ -416,11 +402,7 @@
 /*        draw_console_image(tux_img);*/
 
         SDL_Flip(screen);
-
-        now_time = SDL_GetTicks();
-
-        if (now_time < last_time + MS_PER_FRAME)
-          SDL_Delay(last_time + MS_PER_FRAME - now_time);
+        Throttle(MS_PER_FRAME);
       }
       while (looping);
       break;
@@ -446,7 +428,6 @@
       do
       {
         frame++;
-        last_time = SDL_GetTicks();
 
         while (SDL_PollEvent(&event) > 0)
         {
@@ -461,10 +442,7 @@
         SDL_BlitSurface(images[IMG_GAMEOVER], NULL, screen, &dest_message);
         SDL_Flip(screen);
 
-        now_time = SDL_GetTicks();
-
-        if (now_time < last_time + MS_PER_FRAME)
-          SDL_Delay(last_time + MS_PER_FRAME - now_time);
+        Throttle(MS_PER_FRAME);
       }
       while (looping);
 

Modified: tuxmath/branches/lan/src/globals.h
===================================================================
--- tuxmath/branches/lan/src/globals.h	2009-08-23 00:13:14 UTC (rev 1460)
+++ tuxmath/branches/lan/src/globals.h	2009-08-26 02:20:45 UTC (rev 1461)
@@ -51,6 +51,7 @@
 #define DEFAULT_MENU_SOUND 1
 #define DEFAULT_MENU_MUSIC 1
 #define DEFAULT_FULLSCREEN 1
+#define DEFAULT_LAN_MODE 0
 #define DEFAULT_USE_BKGD 1
 #define DEFAULT_HELP_MODE 0
 #define DEFAULT_DEMO_MODE 0

Modified: tuxmath/branches/lan/src/highscore.c
===================================================================
--- tuxmath/branches/lan/src/highscore.c	2009-08-23 00:13:14 UTC (rev 1460)
+++ tuxmath/branches/lan/src/highscore.c	2009-08-26 02:20:45 UTC (rev 1461)
@@ -845,19 +845,21 @@
         }
       }
     }
-   //FIXME - so we pull all the messages out of the socket and ignore anything
-   //that isn't "GO_TO_GAME" - why are we ignoring them?  We cannot assume the
-   //server is going to send us what we expect. At a minimum, we need to 
-   //print any unrecognized messages to stderr with a warning - DSB
-   while(!check_messages(buf))
-   {
-     if(strncmp(buf,"GO_TO_GAME",strlen("GO_TO_GAME"))==0)
-     {
-       finished = 1;
-       playsound(SND_TOCK);
-       break;
-     }     
-   }
+
+    //FIXME - so we pull all the messages out of the socket and ignore anything
+    //that isn't "GO_TO_GAME" - why are we ignoring them?  We cannot assume the
+    //server is going to send us what we expect. At a minimum, we need to 
+    //print any unrecognized messages to stderr with a warning - DSB
+    while(!check_messages(buf))
+    {
+      if(strncmp(buf,"GO_TO_GAME",strlen("GO_TO_GAME"))==0)
+      {
+        finished = 1;
+        playsound(SND_TOCK);
+        break;
+      }     
+    }
+
     /* --- make tux blink --- */
     switch (frame % TUX6)
     {
@@ -891,7 +893,8 @@
 
 }
 
-void detecting_servers(const char* heading, const char* sub)
+
+int detecting_servers(const char* heading, const char* sub)
 {
   
   SDL_Rect loc;
@@ -966,11 +969,38 @@
   SDL_UpdateRect(screen, 0, 0, 0, 0);
 
 
-
   while (!finished)
   {
     start = SDL_GetTicks();
 
+    //Scan local network to find running server:
+    servers_found = LAN_DetectServers();
+    if(servers_found < 1)
+    {
+      printf("No server could be found - returning.\n");
+      /* Turn off SDL Unicode lookup (because has some overhead): */
+      SDL_EnableUNICODE(SDL_DISABLE);
+      FreeSprite(Tux);
+      return 0;
+    }
+    else if(servers_found  == 1)  //One server - connect without player intervention
+    {
+      printf("Single server found - connecting automatically...");
+
+      if(!LAN_AutoSetup(0))  //i.e.first (and only) entry in list
+      {
+        printf("LAN_AutoSetup() failed - returning.\n");
+        /* Turn off SDL Unicode lookup (because has some overhead): */
+        SDL_EnableUNICODE(SDL_DISABLE);
+        FreeSprite(Tux);
+        return 0;
+      }
+      finished = 1;
+      break;  //So we quit scanning as soon as we connect
+      printf("connected\n");
+    } 
+
+
     while (SDL_PollEvent(&event)) 
     {
       switch (event.type)
@@ -992,27 +1022,7 @@
         }
       }
     }
-      //Scan local network to find running server:
-      servers_found = LAN_DetectServers();
-      if(servers_found < 1)
-      {
-        printf("No server could be found - exiting.\n");
-        exit(EXIT_FAILURE);
-      }
-      else if(servers_found  == 1)  //One server - connect without player intervention
-      {
-        printf("Single server found - connecting automatically...");
 
-        if(!LAN_AutoSetup(0))  //i.e.first (and only) entry in list
-        {
-          printf("setup_client() failed - exiting.\n");
-          exit(EXIT_FAILURE);
-        }
-        finished = 1;
-        break;
-        printf("connected\n");
-      } 
-
     /* --- make tux blink --- */
     switch (frame % TUX6)
     {
@@ -1039,11 +1049,12 @@
     frame++;
   }  // End of while (!finished) loop
 
-  FreeSprite(Tux);
 
   /* Turn off SDL Unicode lookup (because has some overhead): */
   SDL_EnableUNICODE(SDL_DISABLE);
+  FreeSprite(Tux);
 
+  return 1;
 }
 
 

Modified: tuxmath/branches/lan/src/highscore.h
===================================================================
--- tuxmath/branches/lan/src/highscore.h	2009-08-23 00:13:14 UTC (rev 1460)
+++ tuxmath/branches/lan/src/highscore.h	2009-08-26 02:20:45 UTC (rev 1461)
@@ -21,7 +21,7 @@
 void HighScoreNameEntry(char* pl_name);
 void NameEntry(char* pl_name, const char* heading, const char* sub);
 void Standby(const char* heading, const char* sub);
-void detecting_servers(const char* heading, const char* sub);
+int detecting_servers(const char* heading, const char* sub);
 void Ready(const char* heading);
 
 int check_score_place(int diff_level, int new_score);

Modified: tuxmath/branches/lan/src/options.c
===================================================================
--- tuxmath/branches/lan/src/options.c	2009-08-23 00:13:14 UTC (rev 1460)
+++ tuxmath/branches/lan/src/options.c	2009-08-26 02:20:45 UTC (rev 1461)
@@ -97,6 +97,7 @@
   global_options->iopts[USE_KEYPAD] = DEFAULT_USE_KEYPAD;
   global_options->iopts[USE_IGLOOS] = DEFAULT_USE_IGLOOS;
   strncpy(game_options->current_font_name, DEFAULT_FONT_NAME, sizeof(game_options->current_font_name));
+  game_options->lan_mode = DEFAULT_LAN_MODE;
   game_options->use_bkgd = DEFAULT_USE_BKGD;
   game_options->help_mode = DEFAULT_HELP_MODE;
   game_options->demo_mode = DEFAULT_DEMO_MODE;
@@ -227,6 +228,13 @@
   strncpy(game_options->current_font_name, font_name, sizeof(game_options->current_font_name));
 }
 
+
+void Opts_SetLanMode(int val)
+{
+  game_options->lan_mode = int_to_bool(val);
+}
+
+
 void Opts_SetUseBkgd(int val)
 {
   game_options->use_bkgd = int_to_bool(val);
@@ -598,6 +606,18 @@
   return (const char*) game_options->current_font_name;
 }
 
+
+int Opts_LanMode(void)
+{
+  if (!game_options)
+  {
+    fprintf(stderr, "\nOpts_LanMode(): game_options not valid!\n");
+    return GAME_OPTS_INVALID;
+  }
+  return game_options->lan_mode;
+}
+
+
 int Opts_UseBkgd(void)
 {
   if (!game_options)

Modified: tuxmath/branches/lan/src/options.h
===================================================================
--- tuxmath/branches/lan/src/options.h	2009-08-23 00:13:14 UTC (rev 1460)
+++ tuxmath/branches/lan/src/options.h	2009-08-26 02:20:45 UTC (rev 1461)
@@ -49,6 +49,7 @@
 typedef struct game_option_type {
   /* general game options */
   char current_font_name[FONT_NAME_LENGTH];
+  int lan_mode;
   int use_bkgd;
   int help_mode;
   int demo_mode;
@@ -121,6 +122,7 @@
 void Opts_SetGlobalOpt(unsigned int index, int val);
 
 void Opts_SetFontName(char* font_name);
+void Opts_SetLanMode(int val);
 void Opts_SetUseBkgd(int val);
 void Opts_SetHelpMode(int val);
 void Opts_SetDemoMode(int val);
@@ -154,6 +156,7 @@
 
 /* "Get" functions for tuxmath options struct: */
 const char* Opts_FontName(void);
+int Opts_LanMode(void);
 int Opts_UseBkgd(void);
 int Opts_HelpMode(void);
 int Opts_DemoMode(void);

Modified: tuxmath/branches/lan/src/titlescreen.c
===================================================================
--- tuxmath/branches/lan/src/titlescreen.c	2009-08-23 00:13:14 UTC (rev 1460)
+++ tuxmath/branches/lan/src/titlescreen.c	2009-08-26 02:20:45 UTC (rev 1461)
@@ -869,7 +869,6 @@
 int run_lan_menu(void)
 {
   int mode = -1;
-  int b;
   int servers_found = 0;  
 
   const char* menu_text[3] =
@@ -886,7 +885,7 @@
   while (1)
   {
     //choose mode
-    mode = choose_menu_item(menu_text,modesprites,3,NULL,NULL);
+    mode = choose_menu_item(menu_text, modesprites, 3, NULL, NULL);
     if (mode == 2 || mode == -1)
       break;
 
@@ -895,41 +894,22 @@
     
     if(mode == 1)
     {
-      detecting_servers(_("Detecting Servers"),_("Please Wait"));
-
-/*      if(servers_found < 1)
+      if(detecting_servers(_("Detecting Servers"),_("Please Wait")))
       {
-        printf("No server could be found - exiting.\n");
-        exit(EXIT_FAILURE);
-      }
-      else if(servers_found  == 1)  //One server - connect without player intervention
-      {
-        printf("Single server found - connecting automatically...");
 
-        if(!LAN_AutoSetup(0))  //i.e.first (and only) entry in list
-        {
-          printf("setup_client() failed - exiting.\n");
-          exit(EXIT_FAILURE);
-        }
 
-        printf("connected\n");
-      } 
-*/
-       NameEntry(player_name, _("Enter your Name:"),
-                       _(""));
+      NameEntry(player_name, _("Enter your Name:"),
+                      _(""));
 
-//      if(!LAN_Setup(host, DEFAULT_PORT))
-//      {
-//        printf("Unable to connect to the server\n");
-//        LAN_Cleanup();
-//        return 0;
-//      }    
-
       LAN_SetName(player_name);
       Ready(_("Click OK when Ready"));
       LAN_StartGame();
       Standby(_("Waiting For Other Players"),_("To Connect"));
+
+      Opts_SetLanMode(1);  // Tells game() we are playing over network
       game();
+      Opts_SetLanMode(0);  // Go back to local play
+      }
     }   
 
   }
@@ -1031,7 +1011,7 @@
 
   //This function takes care of all the drawing and receives
   //user input:
-  choice = choose_menu_item(menu_text,sprites,7,NULL,NULL);
+  choice = choose_menu_item(menu_text, sprites, 7, NULL, NULL);
 
   while (choice >= 0) {
     if (choice < NUM_MATH_COMMAND_LEVELS) {
@@ -1040,12 +1020,13 @@
       {
         audioMusicUnload();
 #ifdef HAVE_LIBSDL_NET
-detecting_servers(_("Detecting Servers"),_("Please Wait"));
-       NameEntry(player_name, _("Enter your Name:"),
+        detecting_servers(_("Detecting Servers"),_("Please Wait"));
+        NameEntry(player_name, _("Enter your Name:"),
                        _(""));
-      LAN_SetName(player_name);
-      Ready(_("Click OK when Ready"));
-      LAN_StartGame();
+        LAN_SetName(player_name);
+        Ready(_("Click OK when Ready"));
+        LAN_StartGame();
+        Opts_SetLanMode(1);
 // if(!LAN_Setup("localhost", DEFAULT_PORT))
 //   {
 //     printf("Unable to connect to the server\n");
@@ -1056,6 +1037,8 @@
 //   LAN_SetName("player A");
 #endif
         game();
+        Opts_SetLanMode(0);
+
         RecalcTitlePositions();
         if (Opts_GetGlobalOpt(MENU_MUSIC)) {
           audioMusicLoad( "tuxi.ogg", -1 );




More information about the Tux4kids-commits mailing list