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

akash gangil gangil-guest at alioth.debian.org
Mon Jun 8 19:06:28 UTC 2009


Author: gangil-guest
Date: 2009-06-08 19:06:28 +0000 (Mon, 08 Jun 2009)
New Revision: 1016

Modified:
   tuxmath/branches/lan/src/Makefile.am
   tuxmath/branches/lan/src/game.c
   tuxmath/branches/lan/src/mathcards.c
   tuxmath/branches/lan/src/network.c
   tuxmath/branches/lan/src/network.h
   tuxmath/branches/lan/src/titlescreen.c
Log:
Adding two new functions in network.c , working still the same

Modified: tuxmath/branches/lan/src/Makefile.am
===================================================================
--- tuxmath/branches/lan/src/Makefile.am	2009-06-08 01:12:25 UTC (rev 1015)
+++ tuxmath/branches/lan/src/Makefile.am	2009-06-08 19:06:28 UTC (rev 1016)
@@ -40,6 +40,7 @@
 	linewrap.c	\
 	loaders.c	\
 	audio.c 	\
+        network.c       \
 	mathcards.c	\
 	campaign.c	\
 	multiplayer.c	\
@@ -49,8 +50,7 @@
 	SDL_rotozoom.c	\
 	lessons.c	\
 	scandir.c	\
-	pixels.c	\
-        network.c
+	pixels.c	
 
 
 # HACK "TuxMath" is the Windows program, whereas "tuxmath" is the Unix program
@@ -72,6 +72,7 @@
 	highscore.h 	\
 	linewrap.h	\
 	loaders.h	\
+        network.h       \
 	mathcards.h 	\
 	options.h	\
 	setup.h		\
@@ -86,8 +87,8 @@
 	gettext.h	\
 	scandir.h	\
 	pixels.h	\
-	compiler.h	\
-        network.h
+	compiler.h	
+        
 
 
 WINDRES=@WINDRES@

Modified: tuxmath/branches/lan/src/game.c
===================================================================
--- tuxmath/branches/lan/src/game.c	2009-06-08 01:12:25 UTC (rev 1015)
+++ tuxmath/branches/lan/src/game.c	2009-06-08 19:06:28 UTC (rev 1016)
@@ -40,6 +40,7 @@
 #include "titlescreen.h"
 #include "options.h"
 #include "SDL_extras.h"
+#include "network.h"
 
 #define FPS 15                     /* 15 frames per second */
 #define MS_PER_FRAME (1000 / FPS)
@@ -2500,6 +2501,7 @@
   static int prev_city = -1;
   int i, found;
   float y_spacing;
+  extern int n;
 
   /* Look for a free comet slot and see if all live comets are far */
   /* enough down to avoid overlap and keep formulas legible:       */
@@ -2531,12 +2533,26 @@
 
   /* 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;
-  }
+//  if(n==1)
+ // {
+    if (!MC_NextQuestion(&(comets[found].flashcard)))
+    {
+      /* no more questions available - cannot create comet.  */
+      return 0;
+    }
+//    if(!SendQuestion(&(comets[found].flashcard)))
+//  {
+//    printf("Unable to send Question\n");
+// }
+//   }
 
+//   if(n==0)
+//   { 
+//     SDL_Delay(5000);
+//     if(!ReceiveQuestion(&(comets[found].flashcard)))
+//     printf("unable to recv question\n");
+//    }
+
   /* If we make it to here, create a new comet!                  */
 
   comets[found].answer = comets[found].flashcard.answer;

Modified: tuxmath/branches/lan/src/mathcards.c
===================================================================
--- tuxmath/branches/lan/src/mathcards.c	2009-06-08 01:12:25 UTC (rev 1015)
+++ tuxmath/branches/lan/src/mathcards.c	2009-06-08 19:06:28 UTC (rev 1016)
@@ -22,7 +22,8 @@
 #include <math.h>
 #include <time.h>
 
-#include "mathcards.h"
+//#include "mathcards.h"
+#include "network.h"
 
 /* extern'd constants */
 
@@ -490,7 +491,7 @@
 
   /* 'draw' - copy over the first question */
   copy_card(&question_list->card, fc);
-
+ 
   /* 'discard' - take first question node out of list and free it */
   question_list = remove_node(question_list, question_list);
   free_node(ptr);

Modified: tuxmath/branches/lan/src/network.c
===================================================================
--- tuxmath/branches/lan/src/network.c	2009-06-08 01:12:25 UTC (rev 1015)
+++ tuxmath/branches/lan/src/network.c	2009-06-08 19:06:28 UTC (rev 1016)
@@ -8,18 +8,129 @@
 #include <unistd.h>
 #include "SDL.h"
 #include "SDL_net.h"
+#include "network.h"
+#include "SDL.h"
 
 
-
 //*** ipaddress of the server and the port would be taken by the user @ the time he selects "LAN multiplayer" from the options menu..
 
 //***also should I fix the port beforehand or ask it from the user...
 
 
-int lan_client_connect(char *host,char *port)            //here "host" is either the hostname or the ipaddress(of the server) in string
+extern char host[1024];
+extern char port[1024];
+
+int SendQuestion(MC_FlashCard* fc)
 {
-char message[]="Client got connected";
-int len;
+char *ch;
+IPaddress ip; //int *remoteip;
+TCPsocket server,client;
+//Uint32 ipaddr;
+Uint16 portnum;
+
+//char message[1024];
+//char waiting[]="WAITING FOR OTHER PLAYER(minimum 2 players required)";
+
+// initialize SDL
+if(SDL_Init(0)==-1)
+{
+printf("SDL_Init: %s\n",SDL_GetError());
+exit(1);
+}
+
+// initialize SDL_net
+if(SDLNet_Init()==-1)
+{
+printf("SDLNet_Init: %s\n",SDLNet_GetError());
+exit(2);
+}
+
+
+portnum=(Uint16)strtol(port,NULL,0);
+
+// Resolve the argument into an IPaddress type
+if(SDLNet_ResolveHost(&ip,NULL,portnum)==-1)
+{
+printf("SDLNet_ResolveHost: %s\n",SDLNet_GetError());
+exit(3);
+}
+
+// open the server socket																		
+server=SDLNet_TCP_Open(&ip);
+if(!server)
+{
+printf("SDLNet_TCP_Open: %s\n",SDLNet_GetError());
+return 1;
+}
+
+while(1)
+{
+
+// try to accept a connection
+ client=SDLNet_TCP_Accept(server);
+ if(!client)
+  {    // no connection accepted
+      //printf("SDLNet_TCP_Accept: %s\n",SDLNet_GetError());
+    SDL_Delay(100); //sleep 1/10th of a second
+    continue;
+  }
+  
+
+              // read the buffer from client
+		SDLNet_TCP_Send(client,fc->formula_string,4);
+		SDLNet_TCP_Recv(client,ch,1);
+                if(*ch=='1')
+                 {
+                  SDLNet_TCP_Send(client,fc->answer_string,4);
+		  SDLNet_TCP_Recv(client,ch,1);
+                  if(*ch=='1')
+                   { 
+                    SDLNet_TCP_Send(client,&(fc->answer),4);
+		    SDLNet_TCP_Recv(client,ch,1);
+                    if(*ch=='1')
+                    {
+                     SDLNet_TCP_Send(client,&(fc->difficulty),4);
+                     SDLNet_TCP_Recv(client,ch,1);
+                     if(*ch=='1')
+                      {		
+			break;
+                      }
+                    }
+                  }
+                 }
+                 
+       
+
+                //if(!len)
+		//{
+		//	printf("SDLNet_TCP_Recv: %s\n",SDLNet_GetError());
+		//	continue;
+		//}
+
+		// print out the message
+		//printf("Received: %.*s\n",len,message);
+
+		//if(message[0]=='Q')
+		//{
+		//	printf("Quitting on a Q received\n");
+		//	break;
+		//}
+                //break;
+}
+
+//	SDLNet_TCP_Close(client);	
+//	SDLNet_TCP_Close(server);
+        // shutdown SDL_net
+//	SDLNet_Quit();
+
+
+return 0;
+}
+
+
+int ReceiveQuestion(MC_FlashCard* fc)
+{
+
 IPaddress ip;
 TCPsocket sock;
 Uint16 portnum;
@@ -55,6 +166,87 @@
 printf("SDLNet_TCP_Open: %s\n",SDLNet_GetError());
 exit(4);
 }
+	
+
+        SDLNet_TCP_Recv(sock,fc->formula_string,4); // add 1 for the NULL
+	SDLNet_TCP_Send(sock,"1",1); 
+	SDLNet_TCP_Recv(sock,fc->answer_string,4);
+        SDLNet_TCP_Send(sock,"1",1);
+        SDLNet_TCP_Recv(sock,&(fc->answer),4);
+        SDLNet_TCP_Send(sock,"1",1);
+        SDLNet_TCP_Recv(sock,&(fc->difficulty),4);
+//	SDLNet_TCP_Send(sock,"1",1);
+ 
+
+
+ //       SDLNet_TCP_Close(sock);
+	
+	// shutdown SDL_net
+//	SDLNet_Quit();
+
+
+
+	return(0);
+}
+
+
+
+
+
+
+
+int lan_client_connect(char *host,char *port)            //here "host" is either the hostname or the ipaddress(of the server) in string
+{
+char message[]="Client got connected";
+int len;
+IPaddress ip;
+TCPsocket sock;
+Uint16 portnum;
+SDL_Event event;
+
+portnum=(Uint16) strtol(port,NULL,0);
+
+// initialize SDL
+if(SDL_Init(0)==-1)
+{
+printf("SDL_Init: %s\n",SDL_GetError());
+exit(1);
+}
+
+// initialize SDL_net
+if(SDLNet_Init()==-1)
+{
+printf("SDLNet_Init: %s\n",SDLNet_GetError());
+exit(2);
+}
+
+
+// Resolve the argument into an IPaddress type
+if(SDLNet_ResolveHost(&ip,host,portnum)==-1)
+{
+printf("SDLNet_ResolveHost: %s\n",SDLNet_GetError());
+exit(3);
+}
+
+//connect to the "host" @ port "portnum"
+sock=SDLNet_TCP_Open(&ip);
+while(!sock)
+{
+
+ while( SDL_PollEvent( &event ) ) 
+  {
+    if(event.type==SDL_KEYDOWN)
+     {
+       if(event.key.keysym.sym==SDLK_ESCAPE)
+       return 7;
+     }
+  }
+ if(!sock)
+ {
+ printf("SDLNet_TCP_Open: %s\n",SDLNet_GetError());
+ continue;
+ }
+}
 	len=strlen(message);
 
 	// strip the newline
@@ -95,6 +287,8 @@
 //Uint32 ipaddr;
 Uint16 portnum;
 int len;
+SDL_Event event;
+
 char message[1024];
 char waiting[]="WAITING FOR OTHER PLAYER(minimum 2 players required)";
 
@@ -130,11 +324,18 @@
 return 1;
 }
 
-game_set_start_message(waiting, "", "", "");
+
 printf("%s\n",waiting);
 while(1)
 {
-
+  while( SDL_PollEvent( &event ) ) 
+  {
+    if(event.type==SDL_KEYDOWN)
+     {
+       if(event.key.keysym.sym==SDLK_ESCAPE)
+       return 7;
+     }
+  }
 // try to accept a connection
  client=SDLNet_TCP_Accept(server);
  if(!client)
@@ -155,6 +356,7 @@
 
 		// print out the message
 		printf("Received: %.*s\n",len,message);
+                break;
 
 		if(message[0]=='Q')
 		{

Modified: tuxmath/branches/lan/src/network.h
===================================================================
--- tuxmath/branches/lan/src/network.h	2009-06-08 01:12:25 UTC (rev 1015)
+++ tuxmath/branches/lan/src/network.h	2009-06-08 19:06:28 UTC (rev 1016)
@@ -9,7 +9,11 @@
 
 */
 
+#include "mathcards.h"
+
 int lan_server_connect(char *port);
 int lan_client_connect(char *host,char *port);
+int SendQuestion(MC_FlashCard* fc);
+int ReceiveQuestion(MC_FlashCard* fc);
  
 #endif // NETWORK_H

Modified: tuxmath/branches/lan/src/titlescreen.c
===================================================================
--- tuxmath/branches/lan/src/titlescreen.c	2009-06-08 01:12:25 UTC (rev 1015)
+++ tuxmath/branches/lan/src/titlescreen.c	2009-06-08 19:06:28 UTC (rev 1016)
@@ -50,6 +50,8 @@
 SDL_Rect srcupdate[MAX_UPDATES];
 SDL_Rect dstupdate[MAX_UPDATES];
 int numupdates = 0; // tracks how many blits to be done
+char host[1024]="NULL";
+char port[1024];
 
 // Colors we use:
 SDL_Color black;
@@ -71,6 +73,7 @@
 char **lesson_list_titles = NULL;
 char **lesson_list_filenames = NULL;
 int num_lessons = 0;
+int n=0;
 
 
 /* --- media for menus --- */
@@ -195,7 +198,7 @@
 int run_lan_menu(void);
 int run_server_menu(void);
 int handle_easter_egg(const SDL_Event* evt);
-void Standby(const char* heading, const char* sub,char *host,char *port);
+int Standby(const char* heading, const char* sub,char *host,char *port);
 
 
 /***********************************************************/
@@ -786,6 +789,7 @@
   int mode = -1;
   int difficulty = -1;
   char npstr[HIGH_SCORE_NAME_LENGTH * 3];
+  
 
   const char* menu_text[3] =
     {N_("Score Sweep"),
@@ -856,9 +860,7 @@
 int run_lan_menu(void)
 {
   int mode = -1;
- char host[1024]="NULL";
-  char port[1024];
-
+  int b;
   
   const char* menu_text[3] =
     {N_("Host"),
@@ -890,7 +892,10 @@
    // lan_client_set_parameter(HOST, host);
    // lan_client_set_parameter(PORT, port);
    //  if((lan_client_connect(host,port))==0)
-   Standby(_("No Host...=("),_("Press Esc to go back"),host,port);    // this function is defined in highscore.c...
+   b=Standby(_("No Host...=("),_("Press Esc to go back"),host,port);
+   if(b==7)
+   return 0;
+   else    
    game();
    }   
 
@@ -905,9 +910,8 @@
 {
 
   int difficulty = -1;
-   char port[1024];
-
-
+   n=1;
+   int g;
   //just leech settings from arcade modes
   const char* diff_menu_text[NUM_MATH_COMMAND_LEVELS + 1] =
     {N_("Space Cadet"),
@@ -938,7 +942,10 @@
      else
      {NameEntry(port, _("Enter the PORT"),
                        _(""));
-      Standby(_("Waiting for other player"),_("Press Esc to go back"),NULL,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;
@@ -2719,7 +2726,7 @@
 
 
 
-void Standby(const char* heading, const char* sub,char *host,char *port)
+int Standby(const char* heading, const char* sub,char *host,char *port)
 {
   
   SDL_Rect loc;
@@ -2727,11 +2734,13 @@
            stopRect;
 
  
-  int finished = 0,l;
+  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;
@@ -2819,20 +2828,26 @@
     {
       if(host==NULL)
       {l=lan_server_connect(port);
-       printf("###############%d\##############n",l);  
+       if(l==7)
+       return 7;
+       printf("###############%d##############\n",l);
+       return 0;  
       }
       else
       {l=lan_client_connect(host,port);
        printf("##############%d#################\n",l);
+       return 0;
       }
      
-     
-        if(!l)
-        {printf("HOORAAAAAAAAAAAY!!!!!!!!!!");
-        return 0;
+
+         if(!l)
+        {
+         printf("HOORAAAAAAAAAAAY!!!!!!!!!!");
+         return 0;
          break;
          }
       
+      
  
       switch (event.type)
       {
@@ -2902,13 +2917,6 @@
 
 }
 
-        if(!l)
-        {printf("HOORAAAAAAAAAAAY!!!!!!!!!!");
-        return 0;
-        break;
-        
-        }
-      
  
 } // End of while (!finished) loop
 




More information about the Tux4kids-commits mailing list