home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / grafik / sprites / demosrc / smscores.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-02-25  |  3.4 KB  |  115 lines

  1. /**********************************************************************
  2. * smscores.c
  3. * Hi-score handling module for the StarMines
  4. **********************************************************************
  5.                     This file is part of
  6.  
  7.           STK -- The sprite toolkit -- version 1.1
  8.  
  9.               Copyright (C) Jari Karjala 1991
  10.  
  11. The sprite toolkit (STK) is a FreeWare toolkit for creating high
  12. resolution sprite graphics with PCompatible hardware. This toolkit 
  13. is provided as is without any warranty or such thing. See the file
  14. COPYING for further information.
  15.  
  16. **********************************************************************
  17. **********************************************************************/
  18.  
  19. #include <stdio.h>
  20. #include "smscores.h"
  21.  
  22. static SCORE_ENTRY scores[SCORE_COUNT] = 
  23. {
  24.     "Dude SkyRunner",   97868L,
  25.     "Obi Wan Kebab",    45735L,
  26.     "San Holo",         22342L,
  27.     "Death Valley",     10230L,
  28.     "Chewcabba",        1205L,
  29. };
  30.  
  31. static int next_score;
  32.  
  33. /**********************************************************************
  34. * Initialize the hiscores. Read them from file, if file found.
  35. **********************************************************************/
  36. void smscores_init(void)
  37. {
  38.     FILE *score_file;
  39.     
  40.     if ((score_file=fopen(SCORE_FILE, "rb"))!=NULL) {
  41.         fread(scores, sizeof(SCORE_ENTRY), SCORE_COUNT, score_file);
  42.         fclose(score_file);
  43.     }
  44. }
  45.  
  46. /**********************************************************************
  47. * Check whether the score if high enough
  48. * Return: 1 if score is big enough.
  49. **********************************************************************/
  50. int smscores_check(long score)
  51. {
  52.     int i;
  53.     
  54.     for (i=0; i<SCORE_COUNT; i++)
  55.         if (scores[i].score < score)
  56.             break;
  57.  
  58.     return i;
  59. }
  60.  
  61. /**********************************************************************
  62. * Add the given entry to the list, if it is high enough.
  63. * Return: 0 if success, nonzero if score was too low.
  64. **********************************************************************/
  65. int smscores_add(SCORE_ENTRY *entry)
  66. {
  67.     int i,j;
  68.     FILE *score_file;
  69.     
  70.     for (i=0; i<SCORE_COUNT; i++)
  71.         if (scores[i].score <= entry->score)
  72.             break;
  73.         
  74.     if (i==SCORE_COUNT) 
  75.         return -1;  /* too low a score */
  76.     
  77.     if (scores[i].score == entry->score) {
  78.         if (scores[i].name[0])
  79.             return -2;  /* same score and have a valid name already */
  80.     }
  81.     else /** make room for a new entry **/
  82.         for (j=SCORE_COUNT-1; j>i; j--)
  83.             scores[j] = scores[j-1];
  84.     
  85.     scores[i] = *entry;
  86.     
  87.     if ((score_file=fopen(SCORE_FILE, "wb"))!=NULL) {
  88.         fwrite(scores, sizeof(SCORE_ENTRY), SCORE_COUNT, score_file);
  89.         fclose(score_file);
  90.     }
  91.     
  92.     return 0;
  93. }
  94.  
  95. /**********************************************************************
  96. * Return the highest score.
  97. **********************************************************************/
  98. SCORE_ENTRY *smscores_get_first(void)
  99. {
  100.     next_score = 1;
  101.     return &scores[0];
  102. }
  103.  
  104. /**********************************************************************
  105. * Return the next scores.
  106. **********************************************************************/
  107. SCORE_ENTRY *smscores_get_next(void)
  108. {
  109.     if (next_score<SCORE_COUNT)
  110.         return &scores[next_score++];    
  111.     else
  112.         return NULL;
  113. }
  114.