home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / XAP / XGAMES / XHEXTRIS.TAR / wmio.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-04-27  |  7.9 KB  |  305 lines

  1. /*
  2.  * hextris Copyright 1990 David Markley, dm3e@+andrew.cmu.edu, dam@cs.cmu.edu
  3.  *
  4.  * Permission to use, copy, modify, and distribute, this software and its
  5.  * documentation for any purpose is hereby granted without fee, provided that
  6.  * the above copyright notice appear in all copies and that both that
  7.  * copyright notice and this permission notice appear in supporting
  8.  * documentation, and that the name of the copyright holders be used in
  9.  * advertising or publicity pertaining to distribution of the software with
  10.  * specific, written prior permission, and that no fee is charged for further
  11.  * distribution of this software, or any modifications thereof.  The copyright
  12.  * holder make no representations about the suitability of this software for
  13.  * any purpose.  It is provided "as is" without express or implied warranty.
  14.  *
  15.  * THE COPYRIGHT HOLDER DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  16.  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  17.  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  18.  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  19.  * DATA, PROFITS, QPA OR GPA, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 
  20.  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  21.  * PERFORMANCE OF THIS SOFTWARE.
  22.  */
  23.  
  24. /* This file contains the Andrew Window Manager I/O handling routines
  25.  * for hextris.
  26.  */
  27.  
  28. #include <wmclient.h>
  29. #include <stdio.h>
  30. #include <strings.h>
  31. #include <sys/time.h>
  32. #include <system.h>
  33. #include "header.h"
  34.  
  35. /* Macros to make 4.2 BSD select compatible with 4.3 BSD select */
  36. #ifndef FD_SET
  37. #define fd_set int
  38. #define FD_SET(fd,fdset) (*(fdset) |= (1<<(fd)))
  39. #define FD_CLR(fd,fdset) (*(fdset) &= ~(1<<(fd)))
  40. #define FD_ISSET(fd, fdset) (*(fdset) & (1<<(fd)))
  41. #define FD_ZERO(fdset) (*(fdset) = 0)
  42. #endif
  43.  
  44. program(hextris);
  45.  
  46. struct font *hexfont, *basefont;
  47. int Black, White;
  48. /* The following variables are required by hextris */
  49. int score = 0, rows = 0, game_over = 1, game_view = 1;
  50. high_score_t high_scores[MAXHIGHSCORES];
  51. position_t grid[MAXROW][MAXCOLUMN];
  52. piece_t npiece, piece;
  53. char *name, *log_name;
  54.  
  55. FlagRedraw() {
  56.     wm_ClearWindow();
  57.     redraw_game(grid,&npiece,&piece,&score,&rows,game_view,
  58.         high_scores);
  59. }
  60.  
  61. main(argc,argv)
  62. int argc;
  63. char **argv;
  64. {
  65.     int i, inverse = 0, pleasure = 0;
  66.     struct timeval tp, ltp;
  67.     struct timezone tzp;
  68.     double intvl = 0, newintvl;
  69.     fd_set fdst;
  70.     
  71. #ifdef AFS
  72.     Authenticate();
  73.     bePlayer();
  74. #endif
  75.     for (i = 1; i < argc; i++) {
  76.     if (! strcmp(argv[i],"-i")) {
  77.         inverse = 1;
  78.         continue;
  79.     }
  80.     if (! strcmp(argv[i],"-p"))
  81.       pleasure = 1;
  82.     }
  83.     if (inverse) {
  84.     Black = f_white;
  85.     White = f_black;
  86.     } else {
  87.     Black = f_black;
  88.     White = f_white;
  89.     }
  90.     if ((log_name = (char *)getenv("USER")) == NULL)
  91.       log_name = "anon";
  92.     if ((name = (char *)getenv("XHEXNAME")) == NULL)
  93.       name = log_name;
  94.     printf("\nWelcome, %s...\n",name);
  95.     gettimeofday(&tp, &tzp);
  96.     srandom((int)(tp.tv_usec));
  97. #ifdef LOG
  98.     strcpy(log_message,log_name);
  99.     strcat(log_message,"\t");
  100.     strcat(log_message,SYS_NAME);
  101.     strcat(log_message,"\t0.98\twm");
  102. #endif
  103.  
  104.     if (wm_NewWindow(0) == NULL || fork())
  105.       exit(0); 
  106.     wm_EnableInput();
  107.     wm_SetRawInput();
  108.     wm_AddMenu("Hextris,New Game:N");
  109.     wm_AddMenu("Hextris,Quit:Q");
  110.     basefont = wm_DefineFont("Andy12b");
  111.     hexfont=wm_DefineFont("/afs/andrew.cmu.edu/usr0/games/fonts/wm/hex10.fwm");
  112.     clear_display();
  113.     redraw_game(grid,&npiece,&piece,&score,&rows,game_view,high_scores);
  114.     fflush(winout);
  115.  
  116.     while(1) {
  117.     if (! game_over)
  118.       if ((game_over = update_drop(grid,&npiece,&piece,&score,&rows))) {
  119.           read_high_scores(high_scores);
  120.           if (is_high_score(name, log_name, score, rows, high_scores))
  121.         write_high_scores(high_scores,log_name);
  122.           read_high_scores(high_scores);
  123.       }
  124.     gettimeofday(<p, NULL);
  125.     if (pleasure) {
  126.         score = 0;
  127.         intvl = 400000;
  128.     } else
  129.       intvl = 100000+(200000-((rows > 40) ? 20 : (rows/2))*10000);
  130.     while (1) {
  131.         gettimeofday(&tp, NULL);
  132.         newintvl = intvl - (((tp.tv_sec - ltp.tv_sec)*1000000)+
  133.           (tp.tv_usec - ltp.tv_usec));
  134.         if (newintvl <= 0)
  135.           break;
  136.         tp.tv_sec = 0;
  137.         tp.tv_usec = newintvl;
  138.         FD_ZERO(&fdst);
  139.         FD_SET(fileno(winin),&fdst);
  140.         if(select(fileno(winin)+1,&fdst,0,0,&tp))
  141.           do_choice(getc(winin),grid,&npiece,&piece,&score,&rows,
  142.             &game_over,&game_view,high_scores);
  143.  
  144.     }
  145.     }
  146. }
  147.  
  148. /* This is required by hextris!
  149.  *
  150.  * This clears the window.
  151.  */
  152. clear_display()
  153. {
  154.     wm_ClearWindow();
  155. }
  156.  
  157. /* This is required by hextris!
  158.  *
  159.  * This displays the current score and rows completed.
  160.  */
  161. display_scores(score,rows)
  162. int *score, *rows;
  163. {
  164.     int x,y;
  165.     char scores[40];
  166.  
  167.     wm_SelectFont(basefont);
  168.     y = 250;
  169.     x = (MAXCOLUMN + 5) * 20;
  170.     wm_SetFunction(White);
  171.     wm_RasterSmash(x,y-20,MAXCOLUMN*20,50);
  172.     wm_SetFunction(Black);
  173.     sprintf(scores,"Score: %6d", *score);
  174.     wm_DrawString(x,y,wm_AtLeft | wm_AtBaseline,scores);
  175.     sprintf(scores,"Rows: %3d", *rows);
  176.     y += 20;
  177.     wm_DrawString(x,y,wm_AtLeft | wm_AtBaseline,scores);
  178.     fflush(winout);
  179. }
  180.  
  181. /* This is required by hextris!
  182.  *
  183.  * This displays the help information.
  184.  */
  185. display_help()
  186. {
  187.     int y, x, i;
  188.     static char *message[] = { "The keys to press are:",
  189.                  "J,j,4 - move left.",
  190.                  "L,l,6 - move right.",
  191.                  "K,k,5 - rotate ccw.",
  192.                  "I,i,8 - rotate cw.",
  193.                  "space,0 - drop.",
  194.                  "N,n - new game.",
  195.                  "P,p - pause game.",
  196.                  "U,u - unpause game.",
  197.                  "R,r - redisplay game.",
  198.                  "H,h - show high scores.",
  199.                  "G,g - show game.",
  200.                  "Q,q - quit game.",
  201.                  " ",
  202.                  "--------------------",
  203.                  "Created By:",
  204.                  "  David Markley",
  205.                  "Font By:",
  206.                  "  Jon Slenk" };
  207.  
  208.  
  209.     y = 315;
  210.     x = (MAXCOLUMN + 4) * 20;
  211.     wm_SelectFont(basefont);
  212.     wm_SetFunction(Black);
  213.     for (i = 0; i < 19; i++)
  214.       wm_DrawString(x,y+(i*17),wm_AtLeft | wm_AtBaseline,message[i]);
  215.     fflush(winout);
  216. }
  217.  
  218. /* This is required by hextris!
  219.  *
  220.  * This displays the high score list.
  221.  */
  222. display_high_scores(high_scores)
  223. high_score_t high_scores[MAXHIGHSCORES];
  224. {
  225.     int y, i;
  226.     static int x[5] = {5,30,150,200,300};
  227.     static char *header[] = {"#","Name","UID","Score","Rows"};
  228.     char message[40];
  229.  
  230.     wm_ClearWindow();
  231.     wm_SelectFont(basefont);
  232.     wm_SetFunction(Black);
  233.     y = 40;
  234.     for (i = 0; i < 5; i++)
  235.       wm_DrawString(x[i],y,wm_AtLeft | wm_AtBaseline,header[i]);
  236.     y = 60;
  237.     for (i = 0; i < ((MAXHIGHSCORES > 40) ? 40 : MAXHIGHSCORES); i++) {
  238.     itoa(i+1,message);
  239.     wm_DrawString(x[0],y+(i*17),wm_AtLeft | wm_AtBaseline,message);
  240.     wm_DrawString(x[1],y+(i*17),wm_AtLeft | wm_AtBaseline,
  241.               high_scores[i].name);
  242.     wm_DrawString(x[2],y+(i*17),wm_AtLeft | wm_AtBaseline,
  243.               high_scores[i].userid);
  244.     itoa(high_scores[i].score,message);
  245.     wm_DrawString(x[3],y+(i*17),wm_AtLeft | wm_AtBaseline,message);
  246.     itoa(high_scores[i].rows,message);
  247.     wm_DrawString(x[4],y+(i*17),wm_AtLeft | wm_AtBaseline,message);
  248.     }
  249.     fflush(winout);
  250. }
  251.  
  252. /* This is required by hextris!
  253.  *
  254.  * This displays the next piece to be dropped.
  255.  */
  256. show_next_piece(npiece)
  257. piece_t *npiece;
  258. {
  259.     piece_t tpiece;
  260.  
  261.     tpiece.type = npiece->type;
  262.     tpiece.rotation = npiece->rotation;
  263.     tpiece.row = 4;
  264.     tpiece.column = MAXCOLUMN+6;
  265.     wm_SetFunction(White);
  266.     wm_RasterSmash((MAXCOLUMN+3)*20,0,200,230);
  267.     wm_SetFunction(Black);
  268.     init_piece(&tpiece);
  269.     fflush(winout);
  270. }
  271.  
  272. /* This is required by hextris!
  273.  *
  274.  * This draws one hex at the specified row and column specified.
  275.  */
  276. draw_hex(row,column,fill,type)
  277. int row,column,fill,type;
  278. {
  279.     int x,y;
  280.     char hex[2];
  281.  
  282.     x = 20 + column * 20;
  283.     y = 20 + row * 26 + (column & 1) * 13;
  284.     hex[0] = 'b' + type;
  285.     hex[1] = '\0';
  286.     if (fill) {
  287.     wm_SetFunction(Black);
  288.     } else {
  289.     wm_SetFunction(White);
  290.     strcpy(hex,"a");
  291.     }
  292.     wm_SelectFont(hexfont);
  293.     wm_DrawString(x,y,wm_AtLeft | wm_AtBaseline,hex);
  294.     fflush(winout);
  295. }
  296.  
  297. /* This is required by hextris!
  298.  *
  299.  * This ends the game by closing everything down and exiting.
  300.  */
  301. end_game()
  302. {
  303.     exit(0);
  304. }
  305.