home *** CD-ROM | disk | FTP | other *** search
- /**********************************************************************
- * smscores.c
- *
- * Hi-score handling module for the StarMines
- **********************************************************************
- This file is part of
-
- STK -- The sprite toolkit -- version 1.1
-
- Copyright (C) Jari Karjala 1991
-
- The sprite toolkit (STK) is a FreeWare toolkit for creating high
- resolution sprite graphics with PCompatible hardware. This toolkit
- is provided as is without any warranty or such thing. See the file
- COPYING for further information.
-
- **********************************************************************
- **********************************************************************/
-
- #include <stdio.h>
- #include "smscores.h"
-
- static SCORE_ENTRY scores[SCORE_COUNT] =
- {
- "Dude SkyRunner", 97868L,
- "Obi Wan Kebab", 45735L,
- "San Holo", 22342L,
- "Death Valley", 10230L,
- "Chewcabba", 1205L,
- };
-
- static int next_score;
-
- /**********************************************************************
- * Initialize the hiscores. Read them from file, if file found.
- **********************************************************************/
- void smscores_init(void)
- {
- FILE *score_file;
-
- if ((score_file=fopen(SCORE_FILE, "rb"))!=NULL) {
- fread(scores, sizeof(SCORE_ENTRY), SCORE_COUNT, score_file);
- fclose(score_file);
- }
- }
-
- /**********************************************************************
- * Check whether the score if high enough
- * Return: 1 if score is big enough.
- **********************************************************************/
- int smscores_check(long score)
- {
- int i;
-
- for (i=0; i<SCORE_COUNT; i++)
- if (scores[i].score < score)
- break;
-
- return i;
- }
-
- /**********************************************************************
- * Add the given entry to the list, if it is high enough.
- * Return: 0 if success, nonzero if score was too low.
- **********************************************************************/
- int smscores_add(SCORE_ENTRY *entry)
- {
- int i,j;
- FILE *score_file;
-
- for (i=0; i<SCORE_COUNT; i++)
- if (scores[i].score <= entry->score)
- break;
-
- if (i==SCORE_COUNT)
- return -1; /* too low a score */
-
- if (scores[i].score == entry->score) {
- if (scores[i].name[0])
- return -2; /* same score and have a valid name already */
- }
- else /** make room for a new entry **/
- for (j=SCORE_COUNT-1; j>i; j--)
- scores[j] = scores[j-1];
-
- scores[i] = *entry;
-
- if ((score_file=fopen(SCORE_FILE, "wb"))!=NULL) {
- fwrite(scores, sizeof(SCORE_ENTRY), SCORE_COUNT, score_file);
- fclose(score_file);
- }
-
- return 0;
- }
-
- /**********************************************************************
- * Return the highest score.
- **********************************************************************/
- SCORE_ENTRY *smscores_get_first(void)
- {
- next_score = 1;
- return &scores[0];
- }
-
- /**********************************************************************
- * Return the next scores.
- **********************************************************************/
- SCORE_ENTRY *smscores_get_next(void)
- {
- if (next_score<SCORE_COUNT)
- return &scores[next_score++];
- else
- return NULL;
- }
-