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

akash gangil gangil-guest at alioth.debian.org
Wed Jul 15 19:56:05 UTC 2009


Author: gangil-guest
Date: 2009-07-15 19:56:04 +0000 (Wed, 15 Jul 2009)
New Revision: 1213

Modified:
   tuxmath/branches/lan/src/game.c
   tuxmath/branches/lan/src/titlescreen.c
Log:
Adding the #ifdef HAVE_LIBSDL_NET thing and a bit of cleanup 

Modified: tuxmath/branches/lan/src/game.c
===================================================================
--- tuxmath/branches/lan/src/game.c	2009-07-15 19:05:38 UTC (rev 1212)
+++ tuxmath/branches/lan/src/game.c	2009-07-15 19:56:04 UTC (rev 1213)
@@ -138,6 +138,7 @@
 static game_message s1, s2, s3, s4, s5;
 static int start_message_chosen = 0;
 
+static MC_FlashCard flash;
 
 typedef struct {
   int x_is_blinking;
@@ -183,6 +184,7 @@
 static int add_comet(void);
 static void add_score(int inc);
 static void reset_comets(void);
+static void copy_card(MC_FlashCard* src, MC_FlashCard* dest);
 
 static void game_mouse_event(SDL_Event event);
 static void game_key_event(SDLKey key);
@@ -199,59 +201,13 @@
 static void print_exit_conditions(void);
 static void print_status(void);
 #endif
-MC_FlashCard flash;
-void copy_card(MC_FlashCard* src, MC_FlashCard* dest)
-{
-  if (!src || !dest)
-    return;
-  mcdprintf("Copying '%s' to '%s', ", src->formula_string,dest->formula_string);
-  mcdprintf("copying '%s' to '%s'\n", src->answer_string, dest->answer_string);
-  strncpy(dest->formula_string, src->formula_string, MC_FORMULA_LEN);
-  strncpy(dest->answer_string, src->answer_string, MC_ANSWER_LEN);
-  mcdprintf("Card is: '%s', '%s'\n", dest->formula_string, dest->answer_string);
-  dest->answer = src->answer;
-  dest->difficulty = src->difficulty;
-  dest->question_id = src->question_id;
-}
 
 
-void seperate_commmand_and_buf(char command[NET_BUF_LEN],char buf[NET_BUF_LEN])
-{
-  int i;
-  /* Copy the command name out of the tab-delimited buffer: */
-  for (i = 0;
-  buf[i] != '\0' && buf[i] != '\t' && i < NET_BUF_LEN;
-                                      i++)
-  {
-    command[i] = buf[i];
-  }
-  command[i] = '\0';
-
-//#ifdef LAN_DEBUG
-//  printf("buf is %s\n", buf);
-//  printf("command is %s\n", command);
-//#endif
-
-}
-
 /* --- MAIN GAME FUNCTION!!! --- */
 
 
 int game(void)
 {
-  /*FIXME this will eventually be somewhere in the program-wide Setup() */
-  /* or perhaps in titlescreen.c                                        */
-  /*connecting to the server*/
-  if(!LAN_Setup("localhost", DEFAULT_PORT))
-  {
-    printf("Unable to connect to the server\n");
-    game_cleanup();
-    return 0;
-  }        
-
-  LAN_SetName("Player_1");
-
-
   Uint32 last_time, now_time;
 
 #ifdef TUXMATH_DEBUG
@@ -300,13 +256,13 @@
     {
       laser.alive--;
     }
-
+#ifdef HAVE_LIBSDL_NET
    while(!check_messages(buf))
    {
      seperate_commmand_and_buf(command,buf);
      game_handle_net_messages(buf,command);   
    }
-    
+#endif    
  
 
 
@@ -601,20 +557,20 @@
   /* to use MC_StartUsingWrongs() */
   /* NOTE MC_StartGame() will return 0 if the list length is zero due */
   /* (for example) to all math operations being deselected */
-//  if (!MC_StartGame())
-//  {
-//    tmdprintf("\nMC_StartGame() failed!");
-//    fprintf(stderr, "\nMC_StartGame() failed!");
-//    return 0;
-//  }
-
-   /*To function for the above 5 comments*/
-//   say_to_server("START_GAME");
+#ifdef HAVE_LIBSDL_NET
   if (!LAN_StartGame())
   {
     fprintf(stderr, "\nLAN_StartGame() failed!");
     return 0;
   }
+#else
+  if (!MC_StartGame())
+  {
+    tmdprintf("\nMC_StartGame() failed!");
+    fprintf(stderr, "\nMC_StartGame() failed!");
+    return 0;
+  }
+#endif
 
   /* Allocate memory */
   comets = NULL;  // set in case allocation fails partway through
@@ -772,8 +728,9 @@
 
 void game_cleanup(void)
 {
-  
+#ifdef HAVE_LIBSDL_NET  
   LAN_Cleanup();
+#endif
   /* Free background: */
   if (bkgd != NULL)
   {
@@ -1268,8 +1225,11 @@
   /* If there was an comet with this answer, destroy it! */
   if (lowest != -1)  /* -1 means no comet had this answer */
   {
+#ifdef HAVE_LIBSDL_NET
     LAN_AnsweredCorrectly(&(comets[lowest].flashcard));
-
+#else
+    MC_AnsweredCorrectly(&(comets[lowest].flashcard));
+#endif
     /* Store the time the question was present on screen (do this */
     /* in a way that avoids storing it if the time wrapped around */
     ctime = SDL_GetTicks();
@@ -1459,10 +1419,11 @@
           comets[i].expl < COMET_EXPL_END)
       {
         /* Tell MathCards about it - question not answered correctly: */
-        /* FIXME will need LAN_NotAnsweredCorrectly() here if using network */
-        //MC_NotAnsweredCorrectly(&(comets[i].flashcard));
-          LAN_NotAnsweredCorrectly(&(comets[i].flashcard));
-
+#ifdef HAVE_LIBSDL_NET
+         LAN_NotAnsweredCorrectly(&(comets[i].flashcard));
+#else 
+       MC_NotAnsweredCorrectly(&(comets[i].flashcard));
+#endif 
         /* Store the time the question was present on screen (do this */
         /* in a way that avoids storing it if the time wrapped around */
         ctime = SDL_GetTicks();
@@ -2360,43 +2321,29 @@
   }
 
   /* determine if game won (i.e. all questions in mission answered correctly): */
-//  if (MC_MissionAccomplished())
-//  {
-//    tmdprintf("Mission accomplished!\n");
-//    return GAME_OVER_WON;
-//  }
-
-
-// x=evaluate("MISSION_ACCOMPLISHED");
-//     if(x)
-//    {
-//      tmdprintf("Mission accomplished!\n");
-//      return GAME_OVER_WON;
-//    } 
-  
+#ifdef HAVE_LIBSDL_NET
     if(game_over_won)
      return GAME_OVER_WON;
-   
-
+#else
+  if (MC_MissionAccomplished())
+  {
+    tmdprintf("Mission accomplished!\n");
+    return GAME_OVER_WON;
+  }
+#endif
+  
   /* Could have situation where mathcards doesn't have more questions */
   /* even though not all questions answered correctly:                */
-//  if (!MC_TotalQuestionsLeft())
-//  {
-//    return GAME_OVER_OTHER;
-//  }
-
-    if(game_over_other)
+#ifdef HAVE_LIBSDL_NET
+  if(game_over_other)
      return GAME_OVER_OTHER;
-    
+#else  
+  if (!MC_TotalQuestionsLeft())
+  {
+    return GAME_OVER_OTHER;
+  }
+#endif
 
- //x=evaluate("TOTAL_QUESTIONS_LEFT");
-// printf("this is the value of total questions left..... %d ...\n",x);
-
-//    if(!x)
-//    {
-//     return GAME_OVER_OTHER;
-//    }
-
   /* Need to get out if no comets alive and MathCards has no questions left in list, */
   /* even though MathCards thinks there are still questions "in play".  */
   /* This SHOULD NOT HAPPEN and means we have a bug somewhere. */
@@ -2680,30 +2627,29 @@
 
   /* Get math question for new comet - the following function fills in */
   /* the flashcard struct that is part of the comet struct:            */
-//     if (!MC_NextQuestion(&(comets[found].flashcard)))
-//     {
-//      /* no more questions available - cannot create comet.  */
-//      return 0;
-//     }
+#ifdef HAVE_LIBSDL_NET
    LAN_NextQuestion(); // Let it be for now until we think of something else
+#else
+   if (!MC_NextQuestion(&(comets[found].flashcard)))
+   {
+     /* no more questions available - cannot create comet.  */
+     return 0;
+   }
+#endif   
    /* FIXME what we really need here is the capability within network.c to queue  */
    /* any questions that have been received from the server in check_messages(),  */
    /* and a function that gives us the next question in the local queue if there  */
    /* is one. We can't assume that it will arrive from the server right at the    */
    /* time we happen to need it to make a new comet. So I'm commenting out        */
    /* the 'say_to_server()' call as well - DSB                                     */
-/*Server replacement for the above 5 comments*/
-//   say_to_server("NEXT_QUESTION");
-//   printf("buf is %s\n",buf);
-//  if(strncmp(command,"SEND_QUESTION",strlen("SEND_QUESTION"))==0) 
-//   {
-//     if(!Make_Flashcard(buf, &(comets[found].flashcard)))
-//     {
-//       return 0;
-//     }
-    printf("Made the flashcard\n");
-    copy_card(&flash,&(comets[found].flashcard));
-
+#ifdef HAVE_LIBSDL_NET
+    copy_card(&flash,&(comets[found].flashcard)); //will be replaced on set up of new system
+#else
+     if(!Make_Flashcard(buf, &(comets[found].flashcard)))
+     {
+       return 0;
+     }
+#endif
      /* If we make it to here, create a new comet!*/
      comets[found].answer = comets[found].flashcard.answer;
      comets[found].alive = 1;
@@ -3534,6 +3480,40 @@
   }
 }
 
+/* Copy the command name out of the tab-delimited buffer: */
+
+void seperate_commmand_and_buf(char command[NET_BUF_LEN],char buf[NET_BUF_LEN])
+{
+  int i;
+    for (i = 0;
+  buf[i] != '\0' && buf[i] != '\t' && i < NET_BUF_LEN;
+                                      i++)
+  {
+    command[i] = buf[i];
+  }
+  command[i] = '\0';
+
+//#ifdef LAN_DEBUG
+//  printf("buf is %s\n", buf);
+//  printf("command is %s\n", command);
+//#endif
+
+}
+
+void copy_card(MC_FlashCard* src, MC_FlashCard* dest)
+{
+  if (!src || !dest)
+    return;
+  mcdprintf("Copying '%s' to '%s', ", src->formula_string,dest->formula_string);
+  mcdprintf("copying '%s' to '%s'\n", src->answer_string, dest->answer_string);
+  strncpy(dest->formula_string, src->formula_string, MC_FORMULA_LEN);
+  strncpy(dest->answer_string, src->answer_string, MC_ANSWER_LEN);
+  mcdprintf("Card is: '%s', '%s'\n", dest->formula_string, dest->answer_string);
+  dest->answer = src->answer;
+  dest->difficulty = src->difficulty;
+  dest->question_id = src->question_id;
+}
+
 void print_status(void)
 {
   int i;

Modified: tuxmath/branches/lan/src/titlescreen.c
===================================================================
--- tuxmath/branches/lan/src/titlescreen.c	2009-07-15 19:05:38 UTC (rev 1212)
+++ tuxmath/branches/lan/src/titlescreen.c	2009-07-15 19:56:04 UTC (rev 1213)
@@ -43,10 +43,6 @@
 #include "convert_utf.h" // for wide char to UTF-8 conversion
 #include "SDL_extras.h"
 
-
-
-//#include "lan_client.h"
-
 /* --- Data Structure for Dirty Blitting --- */
 SDL_Rect srcupdate[MAX_UPDATES];
 SDL_Rect dstupdate[MAX_UPDATES];
@@ -199,9 +195,7 @@
 int run_lan_menu(void);
 int run_server_menu(void);
 int handle_easter_egg(const SDL_Event* evt);
-int Standby(const char* heading, const char* sub,char *host,char *port);
 
-
 /***********************************************************/
 /*                                                         */
 /*       "Public functions" (callable throughout program)  */
@@ -711,6 +705,16 @@
         Opts_SetDemoMode(0);
         if (Opts_GetGlobalOpt(MENU_MUSIC))  //Turn menu music off for game
           {audioMusicUnload();}
+#ifdef HAVE_LIBSDL_NET
+ if(!LAN_Setup("localhost", DEFAULT_PORT))
+   {
+     printf("Unable to connect to the server\n");
+     LAN_Cleanup();
+     return 0;
+   }    
+
+   LAN_SetName("player A");
+#endif
         game();
         RecalcTitlePositions();
         if (Opts_GetGlobalOpt(MENU_MUSIC)) //Turn menu music back on
@@ -884,17 +888,22 @@
     run_server_menu();
     
     if(mode == 1)
-   { NameEntry(host, _("Enter the name"),
+    { NameEntry(host, _("Enter the name"),
                        _("(of the Host)"));
-    NameEntry(port, _("Enter you name"),
+     NameEntry(port, _("Enter you name"),
                        _(""));
-    
 
-   //  if((lan_client_connect(host,port))==0)
-   b=Standby(_("No Host...=("),_("Press Esc to go back"),host,port);
-   if(b==7)
-   return 0;
-   else    
+
+    if(!LAN_Setup(host, DEFAULT_PORT))
+   {
+     printf("Unable to connect to the server\n");
+     LAN_Cleanup();
+     return 0;
+   }    
+
+   LAN_SetName(port);
+
+
    game();
    }   
 
@@ -941,11 +950,7 @@
      else
      {NameEntry(port, _("Enter the PORT"),
                        _(""));
-      g=Standby(_("Waiting for other player"),_("Press Esc to go back"),NULL,port);
-      if(g==7)
-      return 0;
-      else
-   // lan_server_connect(port);
+    
        game();}
     break;
    }
@@ -1009,6 +1014,16 @@
       if (read_named_config_file(arcade_config_files[choice]))
       {
         audioMusicUnload();
+#ifdef HAVE_LIBSDL_NET
+ if(!LAN_Setup("localhost", DEFAULT_PORT))
+   {
+     printf("Unable to connect to the server\n");
+     LAN_Cleanup();
+     return 0;
+   }    
+
+   LAN_SetName("player A");
+#endif
         game();
         RecalcTitlePositions();
         if (Opts_GetGlobalOpt(MENU_MUSIC)) {
@@ -1069,7 +1084,16 @@
   if (read_user_config_file()) {
     if (Opts_GetGlobalOpt(MENU_MUSIC))
       audioMusicUnload();
+#ifdef HAVE_LIBSDL_NET
+ if(!LAN_Setup("localhost", DEFAULT_PORT))
+   {
+     printf("Unable to connect to the server\n");
+     LAN_Cleanup();
+     return 0;
+   }    
 
+   LAN_SetName("player A");
+#endif
     game();
     RecalcTitlePositions();
     write_user_config_file();
@@ -1215,6 +1239,16 @@
       if (read_named_config_file("demo"))
       {
         audioMusicUnload();
+#ifdef HAVE_LIBSDL_NET
+ if(!LAN_Setup("localhost", DEFAULT_PORT))
+   {
+     printf("Unable to connect to the server\n");
+     LAN_Cleanup();
+     return 0;
+   }    
+
+   LAN_SetName("player A");
+#endif
         game();
         RecalcTitlePositions();
         if (Opts_GetGlobalOpt(MENU_MUSIC)) {
@@ -1307,7 +1341,16 @@
       if (Opts_GetGlobalOpt(MENU_MUSIC))  //Turn menu music off for game
         {audioMusicUnload();}
 
+#ifdef HAVE_LIBSDL_NET
+ if(!LAN_Setup("localhost", DEFAULT_PORT))
+   {
+     printf("Unable to connect to the server\n");
+     LAN_Cleanup();
+     return 0;
+   }    
 
+   LAN_SetName("player A");
+#endif
       game();
       RecalcTitlePositions();
 
@@ -2724,208 +2767,3 @@
   }
 
 
-
-int Standby(const char* heading, const char* sub,char *host,char *port)
-{
-  
-  SDL_Rect loc;
-  SDL_Rect TuxRect,
-           stopRect;
-
- 
-  int finished = 0;
-  int tux_frame = 0;
-  int l;
-  Uint32 frame = 0;
-  Uint32 start = 0;
-  
-  
-  const int BG_Y = 100;
-  const int BG_WIDTH = 400;
-  const int BG_HEIGHT = 200;
-
-  sprite* Tux = LoadSprite("tux/bigtux", IMG_ALPHA);
-
-    
-
-  /* We need to get Unicode vals from SDL keysyms */
-  SDL_EnableUNICODE(SDL_ENABLE);
-
-
-  /* Draw background: */
-  if (current_bkg())
-    SDL_BlitSurface(current_bkg(), NULL, screen, NULL);
-
-  /* Red "Stop" circle in upper right corner to go back to main menu: */
-  if (images[IMG_STOP])
-  {
-    stopRect.w = images[IMG_STOP]->w;
-    stopRect.h = images[IMG_STOP]->h;
-    stopRect.x = screen->w - images[IMG_STOP]->w;
-    stopRect.y = 0;
-    SDL_BlitSurface(images[IMG_STOP], NULL, screen, &stopRect);
-  }
-
-  if (Tux && Tux->frame[0]) /* make sure sprite has at least one frame */
-  {
-    TuxRect.w = Tux->frame[0]->w;
-    TuxRect.h = Tux->frame[0]->h;
-    TuxRect.x = 0;
-    TuxRect.y = screen->h - Tux->frame[0]->h;
-  }
-
-  /* Draw translucent background for text: */
-  {
-    SDL_Rect bg_rect;
-    bg_rect.x = (screen->w)/2 - BG_WIDTH/2;
-    bg_rect.y = BG_Y;
-    bg_rect.w = BG_WIDTH;
-    bg_rect.h = BG_HEIGHT;
-    DrawButton(&bg_rect, 15, REG_RGBA);
-
-    bg_rect.x += 10;
-    bg_rect.y += 10;
-    bg_rect.w -= 20;
-    bg_rect.h = 180;
-    DrawButton(&bg_rect, 10, SEL_RGBA);
-  }
-
-  /* Draw heading: */
-  {
-    SDL_Surface* s = BlackOutline(_(heading),
-                                  DEFAULT_MENU_FONT_SIZE, &white);
-    if (s)
-    {
-      loc.x = (screen->w/2) - (s->w/2);
-      loc.y = 150;
-      SDL_BlitSurface(s, NULL, screen, &loc);
-      SDL_FreeSurface(s);
-    }
-
-    s = BlackOutline(_(sub),
-                     DEFAULT_MENU_FONT_SIZE, &white);
-    if (s)
-    {
-      loc.x = (screen->w/2) - (s->w/2);
-      loc.y = 170;
-      SDL_BlitSurface(s, NULL, screen, &loc);
-      SDL_FreeSurface(s);
-    }
-  }
-
-  /* and update: */
-  SDL_UpdateRect(screen, 0, 0, 0, 0);
-
-
-
-  while (!finished)
-  {
-   printf("I AM HERE!!!!!!!!!!!!!!!!!!!!!!!!"); 
-   start = SDL_GetTicks();
-
-    while (SDL_PollEvent(&event)) 
-    {
-      if(host==NULL)
-      {//l=lan_server_connect(port);
-       if(l==7)
-       return 7;
-       printf("###############%d##############\n",l);
-       return 0;  
-      }
-      else
-      {//l=lan_client_connect(host,port);
-       if(l==7)
-       return 7;
-       printf("##############%d#################\n",l);
-       return 0;
-      }
-     
-
-         if(!l)
-        {
-         printf("HOORAAAAAAAAAAAY!!!!!!!!!!");
-         return 0;
-         break;
-         }
-      
-      
- 
-      switch (event.type)
-      {
-        case SDL_QUIT:
-        {
-          cleanup();
-        }
-
-        case SDL_MOUSEBUTTONDOWN:
-        /* "Stop" button - go to main menu: */
-        { 
-          if (inRect(stopRect, event.button.x, event.button.y ))
-          {
-            finished = 1;
-            playsound(SND_TOCK);
-            break;
-          }
-        }
-        case SDL_KEYDOWN:
-        {
-
-          switch (event.key.keysym.sym)
-          {
-            case SDLK_ESCAPE:
-           
-            {
-              finished = 1;
-              playsound(SND_TOCK);
-              break;
-            }
-           
-            default:
-            continue;
-            /* For any other keys, if the key has a Unicode value, */
-            /* we add it to our string:                            */
-           
-           
-          }  /* end  'switch (event.key.keysym.sym)'  */
-
- 
-    /* --- make tux blink --- */
-    switch (frame % TUX6)
-    {
-      case 0:    tux_frame = 1; break;
-      case TUX1: tux_frame = 2; break;
-      case TUX2: tux_frame = 3; break;
-      case TUX3: tux_frame = 4; break;                        
-      case TUX4: tux_frame = 3; break;
-      case TUX5: tux_frame = 2; break;
-      default: tux_frame = 0;
-    }
-
-    if (Tux && tux_frame)
-    {
-      SDL_BlitSurface(Tux->frame[tux_frame - 1], NULL, screen, &TuxRect);
-      SDL_UpdateRect(screen, TuxRect.x, TuxRect.y, TuxRect.w, TuxRect.h);
-    }
-
-    /* Wait so we keep frame rate constant: */
-    while ((SDL_GetTicks() - start) < 33)
-    {
-      SDL_Delay(20);
-    }
-    frame++;
-  } 
-}
-
-}
-
- 
-} // End of while (!finished) loop
-
-  FreeSprite(Tux);
-
-  /* Turn off SDL Unicode lookup (because has some overhead): */
-  SDL_EnableUNICODE(SDL_DISABLE);
-
-}
-
-




More information about the Tux4kids-commits mailing list