home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Games / xrobots / score.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-03  |  7.3 KB  |  290 lines

  1. /*
  2.  * score.c  --  xrobots
  3.  *
  4.  * 
  5.  * Copyright 1989 Brian Warkentine
  6.  * 
  7.  * Permission to use, copy, modify, distribute, and sell this software and its
  8.  * documentation for any purpose is hereby granted without fee, provided that
  9.  * the above copyright notice appear in all copies and that both that
  10.  * copyright notice and this permission notice appear in supporting
  11.  * documentation, and that the author's name not be used in advertising or
  12.  * publicity pertaining to distribution of the software without specific,
  13.  * written prior permission.  The author makes no representations about the
  14.  * suitability of this software for any purpose.  It is provided "as is"
  15.  * without express or implied warranty.
  16.  * 
  17.  * THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING 
  18.  * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL THE 
  19.  * AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY 
  20.  * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN 
  21.  * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 
  22.  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  23.  * 
  24.  * The author's current employer has no connection with this software, as it
  25.  * was written before being employed at Sun.  The following address is for 
  26.  * contacting the author only and does not imply any responsibility on the 
  27.  * behalf of Sun Microsystems.
  28.  * 
  29.  * Contact the author at:
  30.  *     Brian Warkentine  (brianw@Sun.COM)
  31.  *     Sun Microsystems
  32.  *     2550 Garcia Avenue
  33.  *     Mountain View, Ca 94043-1100
  34.  *
  35.  *----------------------------------------------------------------------
  36.  * flock() is used to stop a race condition within... if you don't 
  37.  * have a bsd-like flock(), you could probably comment it out.
  38.  * If SYSV is defined, this will be #ifdef'd out.
  39.  * The race condition (multiple users writing to the score file) is 
  40.  * probably rare.
  41.  */
  42.  
  43. #include <X11/Intrinsic.h>
  44. #include <X11/StringDefs.h>
  45. #include <X11/Shell.h>
  46.  
  47. #ifdef R3
  48. #include <X11/Box.h>
  49. #include <X11/Command.h>
  50. #include <X11/Label.h>
  51. #else
  52. #include <X11/Xaw/Box.h>
  53. #include <X11/Xaw/Command.h>
  54. #include <X11/Xaw/Label.h>
  55. #endif
  56.  
  57. #include <X11/Xos.h>    /* brings in <sys/file.h> */
  58. #include <stdio.h>
  59. #include "xrobots.h"
  60.  
  61. /*----------------------------------------------------------------------*/
  62.  
  63. typedef struct {
  64.   char     score[6], 
  65.     name[26];
  66. } SCORE;
  67.  
  68. static SCORE scores[MAXSCORES];
  69.  
  70. void     show_scores(), 
  71.     new_high_score(), 
  72.     load_scores(), 
  73.     write_out_scores();
  74.  
  75. static FILE *scorefile = 0;
  76. char *score_filename;
  77.  
  78. /*----------------------------------------------------------------------*/
  79.  
  80. void
  81. check_score(current_score)
  82.   int current_score;
  83. {
  84.  
  85.   load_scores();
  86.  
  87.   if(current_score > atoi(scores[MAXSCORES-1].score)) {
  88.     new_high_score(current_score);
  89.     write_out_scores();
  90.   }
  91.   if(scorefile) {
  92. #ifndef SYSV
  93.     flock(scorefile->_file, LOCK_UN);
  94. #endif
  95.     fclose(scorefile);
  96.     show_scores();
  97.   }
  98. }
  99.  
  100.  
  101. static void
  102. load_scores()
  103. {
  104.   int i = 0;
  105.  
  106.   if( !(scorefile = fopen(score_filename,"r+")) ) {
  107.     scorefile = fopen(score_filename, "w");
  108.     return;
  109.   }
  110. #ifndef SYSV
  111.   flock(scorefile->_file, LOCK_EX);
  112. #endif
  113.   while( fgets(scores[i].score,6,scorefile)     /* get score */
  114.       && fgets(scores[i].name,26,scorefile)     /* get name */
  115.       && fgetc(scorefile))            /* and newline */
  116.   {
  117.     scores[i].score[5] = '\0';
  118.     scores[i].name[25] = '\0';
  119.     ++i;
  120.     if( i > MAXSCORES ) break;
  121.   }
  122. }
  123.  
  124.  
  125. static void
  126. new_high_score(current_score)
  127.   int current_score;
  128. {
  129.   int i;
  130.   char textscore[6],
  131.        name[26];
  132.  
  133.   sprintf(textscore,"%5d",current_score);
  134.   strncpy(name,getenv("USER"),25);
  135.   name[25] = '\0';
  136.  
  137.   for(i=MAXSCORES-2;i>=0;i--)
  138.     if( current_score < atoi(scores[i].score) ) {
  139.          /* move new score into i+1 slot */
  140.       strcpy(scores[i+1].score,textscore);
  141.       strcpy(scores[i+1].name,name);
  142.       return;
  143.     } else {
  144.       strcpy(scores[i+1].score,scores[i].score);
  145.       strcpy(scores[i+1].name,scores[i].name);
  146.     }
  147.   /* if it got here, there is a new number 1 score */
  148.   strcpy(scores[0].score,textscore);
  149.   strcpy(scores[0].name,name);
  150.  
  151. }
  152.  
  153.  
  154. static void
  155. write_out_scores()
  156. {
  157.   int i;
  158.  
  159.   if( !scorefile )
  160.     return;
  161.   rewind(scorefile);
  162.   for(i=0;i<MAXSCORES;i++)
  163.     fprintf(scorefile,"%5s%25s\n",scores[i].score,scores[i].name);
  164. }
  165.  
  166.  
  167. /*----------------------------------------------------------------------*/
  168.  
  169.  
  170. Widget score_popup;
  171. Widget score_labels[MAXSCORES];
  172.  
  173. static Arg arglist_score_title[] = {
  174.   {  XtNborderWidth, (XtArgVal) 0  },
  175.   {  XtNresize, (XtArgVal) False  },
  176.   {  XtNwidth, (XtArgVal) 300   },
  177.   {  XtNheight, (XtArgVal) 30   },
  178.   {  XtNlabel, (XtArgVal) "High Scores"  },
  179.   {  XtNjustify, (XtArgVal) XtJustifyCenter  },
  180. };
  181.  
  182. static Arg arglist_score_label[] = {
  183.   {  XtNlabel, (XtArgVal) 0  },
  184.   {  XtNborderWidth, (XtArgVal) 0  },
  185.   {  XtNresize, (XtArgVal) False  },
  186.   {  XtNwidth, (XtArgVal) 300   },
  187.   {  XtNjustify, (XtArgVal) XtJustifyCenter  },
  188. };
  189.  
  190. static Arg arglist_popdown[] = {
  191. /*  {  XtNborderWidth, (XtArgVal) 2  },*/
  192.   {  XtNresize, (XtArgVal) False  },
  193.   {  XtNwidth, (XtArgVal) 300   },
  194.   {  XtNlabel, (XtArgVal) "Pop Down"  },
  195.   {  XtNjustify, (XtArgVal) XtJustifyCenter  },
  196. };
  197.  
  198.  
  199. /*ARGSUSED*/
  200. static XtCallbackProc 
  201. popdown_callback(w, closure, call_data)
  202.   Widget w;
  203.   caddr_t closure;
  204.   caddr_t call_data;
  205. {
  206.   XtPopdown(score_popup);
  207. }
  208.  
  209.  
  210. void
  211. create_high_score_popup(parent)
  212.   Widget parent;
  213. {
  214.   int i;
  215.   Widget score_box, popdown;
  216.  
  217.   score_popup = XtCreatePopupShell(
  218.                                    "score_popup",
  219.                                    transientShellWidgetClass,
  220.                                    parent, 0,0);
  221.  
  222.   score_box = XtCreateManagedWidget(
  223.                                     "score_box",
  224.                                     boxWidgetClass,
  225.                                     score_popup,
  226.                                     0,0);
  227.  
  228.   (void)XtCreateManagedWidget(
  229.                                     "score_title",
  230.                                     labelWidgetClass,
  231.                                     score_box,
  232.                                     arglist_score_title,
  233.                                     XtNumber(arglist_score_title));
  234.  
  235.   for(i=0;i<MAXSCORES;i++) {
  236.     score_labels[i] = XtCreateManagedWidget(
  237.                                     "alabel",
  238.                                     labelWidgetClass,
  239.                                     score_box,
  240.                                     arglist_score_label,
  241.                                     XtNumber(arglist_score_label));
  242.   }
  243.  
  244.   popdown = XtCreateManagedWidget(
  245.                                     "popdown",
  246.                                     commandWidgetClass,
  247.                                     score_box,
  248.                                     arglist_popdown,
  249.                                     XtNumber(arglist_popdown));
  250.   XtAddCallback(popdown,XtNcallback,popdown_callback,0);
  251. }
  252.  
  253.  
  254.  
  255. void
  256. show_scores()
  257. {
  258.   int i;
  259.   char tmp_str[31];
  260.   Arg tmp_arg;
  261.  
  262.   for(i = 0;i<MAXSCORES;i++) {
  263.     sprintf(tmp_str,"%5s  %25s", scores[i].score, scores[i].name);
  264.     XtSetArg(tmp_arg,XtNlabel,tmp_str);
  265.     XtSetValues(score_labels[i],&tmp_arg,1);
  266.   }
  267.   XtPopup(score_popup, XtGrabNone);
  268. }
  269.  
  270.  
  271. /*----------------------------------------------------------------------*/
  272.  
  273.  
  274. /*XtCallbackProc*/
  275. void
  276. show_scores_callback()
  277. {
  278.   load_scores();
  279.  
  280.   if(scorefile) {
  281. #ifndef SYSV
  282.     flock(scorefile->_file, LOCK_UN);
  283. #endif
  284.     fclose(scorefile);
  285.     show_scores();
  286.   }
  287. }
  288.  
  289.  
  290.