Tutorial: moving sunnyboys around killing master, common datestring
===================================================================

Multiplayer program, where every player moves his own sunnyboy with arrow keys.
Up to 4 players are can connect, virtual players replace missing players.
Clients touching master's sunnyboy will kill the master.
A date string is displayed, set by master and sent via common-block.


step 0
The global declaration of the program in tut-network2.h,
used by master player and client players, differs from tut-network1.h
by adding the common-block definitions:

==>
/* struct for common-block,
** contains a string (char array) for master's datestring
*/
struct {
  char * date;    // date string with arraysize "date_size"
  int date_size;  // array size of "date"
} pl_comm;
<== The function set_variables() adds: ==>
  /* common-block */
  memset(&pl_comm,0,sizeof(pl_comm));
  pl_comm.date_size=40;  // datestring with arraysize 40
  if (vg_nw_setcommon(NWVAR_CHAR,pl_comm.date_size,"date") < 1) {return(-1);}
<== The function get_pointers() adds: ==>
  /* common-block */
  pl_comm.date=(char *)vg_nw_getcommon("date");
<== step 1a The master player (tut-network2.c) sets the network variables, starts the network-server, connects to it, sets the pointers, initializes the pointer values and sends the initial packet to the clients. It's nearly the same code as in tut-network1.c (refer to tut-network1.html). The code of function init_values() sets another start position of the master player: ==>
    if (i1==vg_nw_myplayer()) {  // put master into right upper corner
      pl_data[i1-1].xpos=vg_bitmap_width(pl_data[i1-1].sunboy)/2;
      pl_data[i1-1].ypos=vg_bitmap_height(pl_data[i1-1].sunboy)/2;
    }
<== and adds the initialization of the common-block: ==>
  /* set values for common-block */
  pl_comm.date[0]='\0';  // clear datestring
<== step 1b The client players (tut-network2_client.c) set the network variables, connect to the network-server, set the pointers and receive the initial packet from the master. It's the same code as in tut-network1_client.c (refer to tut-network1.html). step 2a The master's game loop now examinates keystrokes - from the master himself - from the client (or virtual player, if no client present) - moves the sunnyboy according to keystrokes - sends the (new) values back to the network-server - draws the sunnyboys of all players on screen Additionally the master - sets the date string to the common-block and sends it - gives out the date string ==>
    /* (go through all players) ... */

    /* set timestring: common variable pl_comm.date */
    {time_t zt=time(NULL);
     char * ptr;
     snprintf(pl_comm.date,pl_comm.date_size,"%s",ctime(&zt));
     if ((ptr=strchr(pl_comm.date,'\n'))!=NULL) {*ptr='\0';}  // kill Newline
     pl_comm.date[pl_comm.date_size-1]='\0';  // prevent overflow
     vg_nw_sendcommon();  // send common-block to network-server
    }

    /* give out common date */
    vg_draw_text(NULL,RGB_WHITE,0,0,pl_comm.date,NULL,RGB_TRANS);

    /* (flush all out to window) ... */
<== Additional in function getkeys() we check whether the master runs into another player: ==>
      /* (save master request values in x,y) ... */

      /* check whether master clashed with another sunnyboy */
      {int i1;
       for (i1=1; i1 <= vg_nw_maxplayer(); i1++) {
         if (i1==plno) {continue;}  // don't check with yourself
         if (vg_nw_isalive(i1)) {  /* player is alive */
           int x1,y1,x2,y2;
           /* start position of master sunnyboy */
           x1=pl_data[plno-1].xpos;
           y1=pl_data[plno-1].ypos;
           /* position of other sunnyboy */
           x2=pl_data[i1-1].xpos;
           y2=pl_data[i1-1].ypos;
           /* check bitmap overlapping */
           if (vg_bitmap_overlap(NULL,pl_data[plno-1].sunboy,x1,y1,x1+x,y1+y,pl_data[i1-1].sunboy,x2,y2,1)) {
             /* master sunnyboy clashed */
             vg_nw_setdead(plno);  // set dead to inform network-server
             return(0);  // do not end the game (wait for the second keystroke)
           }
         }
       }
      }
<== step 2b The client's game loop - calls vg_key_update() to get keystrokes and send them and receive master's network data - checks for space key, if the player wants to exit - draws the sunnyboys of all players on screen Additionally the client - gives out the date string ==>
    /* (go through all players) ... */

    /* give out common date */
    vg_draw_text(NULL,RGB_WHITE,0,0,pl_comm.date,NULL,RGB_TRANS);

    /* (flush all out to window) ... */
<== The whole program tut-network2.c tut-network2.h tut-network2_client.c
[Previous] - [Index] - [Next]