home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Extra 1996 #3 / AmigaPlus_CD-ROM-EXTRA_Nr.3.bin / aminet-spiele / denk&grübel / crazyclock / source / highwindow.c < prev    next >
C/C++ Source or Header  |  1993-10-12  |  5KB  |  198 lines

  1. /*
  2.  * highwindow.c V1.1
  3.  *
  4.  * highscore window handling
  5.  *
  6.  * (c) 1992-1993 Holger Brunst
  7.  */
  8.  
  9. #include <CClock.h>
  10.  
  11. /* HighScore structure */
  12. struct HighEntry {
  13.                   char  name[HIGH_STRLEN+1];
  14.                   ULONG time;
  15.                  };
  16.  
  17. #define HIGH_ENTRIES    10
  18. struct HighScore {
  19.                   struct HighEntry     entry[HIGH_ENTRIES];
  20.                   long                 checksum;
  21.                  };
  22.  
  23. /* Window data */
  24. #define WIN_X   128
  25. #define WIN_Y   38      
  26. #define WIN_W   384
  27. #define WIN_H   124
  28. #define WINDOW_IDCMP (IDCMP_REFRESHWINDOW|IDCMP_CLOSEWINDOW) 
  29. static struct Window    *win;
  30. static struct RastPort  *rast;
  31.  
  32. /* Highscore data */
  33. static struct HighScore     score;
  34. static struct TextExtent    textex;
  35. static char                 time[8];
  36. static char *numbers[10] = {
  37.                             " 1.", " 2.", " 3.", " 4.", " 5.",
  38.                             " 6.", " 7.", " 8.", " 9.", "10." 
  39.                            };
  40.  
  41. /* Calculate & draw a highscore list */ 
  42. static void DisplayHighScore(void)
  43. {
  44.  short  i, yoffset = 24, len;
  45.  long   sec;
  46.  
  47.  SetAPen(rast, 1);
  48.  /* Draw highscore list */
  49.  for (i = 0; i < HIGH_ENTRIES; i++) {
  50.   /* convert seconds to time string */
  51.   sec = score.entry[i].time;
  52.  
  53.   /* Last entry if time less than 0 */
  54.   if (sec == ~0) break;
  55.   sprintf(time, "%01ld:%02ld:%02ld", sec/3600, sec/60 - (sec/3600) * 60, sec % 60); 
  56.  
  57.   /* Cut off names that are too long */
  58.   len = TextFit(rast,
  59.                 score.entry[i].name,
  60.                 strlen(score.entry[i].name),
  61.                 &textex, NULL,
  62.                 +1, 240, 9);
  63.  
  64.   /* Draw highscore */
  65.   Move(rast, 40-TextLength(rast, numbers[i], 3), yoffset);  
  66.   Text(rast, numbers[i], 3); 
  67.   Move(rast, 48, yoffset);  
  68.   Text(rast, score.entry[i].name, len);
  69.   Move(rast, 296+(62-TextLength(rast, time, 7))/2, yoffset);  
  70.   Text(rast, time, 7);
  71.   yoffset += 10;
  72.  }
  73. }
  74.  
  75. BOOL OpenHighWindow(void)
  76. {
  77.  /* Open Window */
  78.  if (win = OpenWindowTags(NULL, WA_Left, WIN_X,
  79.                                 WA_Top, WIN_Y,
  80.                                 WA_Width, WIN_W,
  81.                                 WA_Height, WIN_H,
  82.                                 WA_Title,  GAME_NAME" Highscore",
  83.                                 WA_PubScreen, Screen,
  84.                                 WA_Flags, WFLG_SMART_REFRESH|WFLG_ACTIVATE|
  85.                                           WFLG_DRAGBAR|WFLG_DEPTHGADGET|
  86.                                           WFLG_CLOSEGADGET|WFLG_RMBTRAP,
  87.                                 TAG_DONE)) {
  88.  
  89.   /* Get rast-port */
  90.   rast = win->RPort;
  91.  
  92.   /* Show highscore */
  93.   DisplayHighScore();
  94.  
  95.   /* Set IDCMPPort */
  96.   win->UserPort = IDCMPPort;
  97.   win->UserData = HandleHighWindowIDCMP;
  98.   ModifyIDCMP(win, WINDOW_IDCMP);
  99.  
  100.   /* Ok. */
  101.   return (TRUE);
  102.  }
  103.  /* failure */
  104.  return (FALSE);
  105. }
  106.  
  107. static void CloseHighWindow(void)
  108. {
  109.  CloseWindowSafely(win);
  110. }
  111.  
  112. void *HandleHighWindowIDCMP(struct IntuiMessage *msg)
  113. {
  114.  switch (msg->Class) {
  115.   case IDCMP_CLOSEWINDOW: /* First reply message */
  116.                           GT_ReplyIMsg(msg);
  117.                           CloseHighWindow();
  118.                           return (TRUE);
  119.                           break;
  120.  
  121.   case IDCMP_REFRESHWINDOW: GT_BeginRefresh(win);
  122.                             GT_EndRefresh(win, TRUE);
  123.                             break;
  124.  }
  125.  return (NULL);
  126. }
  127.  
  128. /* Ask whether a score is a highscore or not */ 
  129. BOOL AskHighScore(ULONG time)
  130. {
  131.  /* Highscore? */
  132.  if (time < score.entry[HIGH_ENTRIES-1].time)
  133.   /* Yes */
  134.   return (TRUE);
  135.  /* No */
  136.  return (FALSE);
  137. }
  138.  
  139. /* Insert a highscore */
  140. void InsertHighScore(char *name, ULONG time)
  141. {
  142.  short i = HIGH_ENTRIES, ins;
  143.  
  144.  /* Find position to insert highscore */
  145.  while ((i > 0) && (time < score.entry[i-1].time)) --i;
  146.  
  147.  /* Is it a highscore? */
  148.  if (i < HIGH_ENTRIES) {
  149.   /* Yes, craete space by shifting worse scores down */
  150.   ins = i;
  151.   for (i = HIGH_ENTRIES-1; i > ins; i--) {
  152.    strcpy(score.entry[i].name, score.entry[i-1].name);
  153.    score.entry[i].time = score.entry[i-1].time;
  154.   }
  155.   /* Copy new entry to correct postion */
  156.   strcpy(score.entry[ins].name, name);
  157.   score.entry[ins].time = time;
  158.  }
  159. }
  160.  
  161. /* Load highscore list */
  162. void LoadHighScore(void)
  163. {
  164.  short i;
  165.  struct FileHandle    *doshandle;
  166.  
  167.  /* Open highscore */
  168.  if (doshandle = (struct FileHandle *) Open(HIGH_NAME, MODE_OLDFILE)) {
  169.   /* Read highscore */
  170.   if (Read((BPTR) doshandle, &score, sizeof (score)) == sizeof (score)) {
  171.    Close((BPTR) doshandle);
  172.    return();
  173.   }  
  174.   Close((BPTR) doshandle);
  175.  }
  176.  
  177.  /* Default highscore */
  178.  sprintf(score.entry[0].name, "Holger Brunst");
  179.  score.entry[0].time = 40;
  180.  for (i = 1; i < HIGH_ENTRIES; i++) {
  181.   score.entry[i].name[0] = NULL;
  182.   score.entry[i].time = ~0;
  183.  }
  184. }
  185.  
  186. /* Save highscore list */
  187. void SaveHighScore(void)
  188. {
  189.  struct FileHandle    *doshandle;
  190.  
  191.  /* Open highscore */
  192.  if (doshandle = (struct FileHandle *) Open(HIGH_NAME, MODE_NEWFILE)) {
  193.   /* Write highscore */
  194.   Write((BPTR) doshandle, &score, sizeof (score));
  195.   Close((BPTR) doshandle);
  196.  }
  197.