1 /* Variations on RockDodger
2 * Copyright (C) 2004 Joshua Grams <josh@qualdan.com>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include <SDL_keysym.h>
34 struct highscore g_scores[2][N_SCORES] = {
57 static char *titles[2] = { "Normal\n", "Easy\n" };
60 int cur_score = -1; // which score we're currently entering.
63 read_high_score_table()
68 f = open_score_file("r");
70 // If the file exists, read from it
73 for(i = 0; i<N_SCORES; i++) {
74 fscanf(f, "%d %31[^\n]\n", &g_scores[j][i].score, g_scores[j][i].name);
82 write_high_score_table()
87 f = open_score_file("w");
89 // If the file exists, write to it
91 fprintf(f, titles[j]);
92 for(i = 0; i<N_SCORES; i++) {
93 fprintf (f, "%d %.31s\n", g_scores[j][i].score, g_scores[j][i].name);
101 score_rank(int score)
105 for(i=0; i<N_SCORES; i++) {
106 if(score > g_scores[g_easy][i].score) return i;
112 new_high_score(int score)
114 cur_score = -1; // assume not a new high score
115 if(score <= g_scores[g_easy][LOW_SCORE].score) return false;
116 read_high_score_table();
117 cur_score = score_rank(score);
118 return cur_score >= 0;
122 insert_score(int score)
126 // Move all lower scores down a notch, losing lowest score forever.
127 if(strcmp(g_scores[g_easy][cur_score].name, "-") != 0)
128 for(i=LOW_SCORE; i>cur_score; i--)
129 g_scores[g_easy][i] = g_scores[g_easy][i-1];
131 // initialize new score entry.
132 g_scores[g_easy][cur_score].score = score;
133 for(i=0; i<32; i++) g_scores[g_easy][cur_score].name[i] = 0;
138 snprintscore(char *s, size_t n, int score)
140 int min = score/60000;
141 int sec = score/1000%60;
142 int tenths = score%1000/100;
144 return snprintf(s, n, "%2d:%.2d.%d", min, sec, tenths);
146 return snprintf(s, n, "%2d.%d", sec, tenths);
154 int r = snprintf(s, 16, "Time: ");
155 snprintscore(s+r, 16-r, score);
156 font_write(XSIZE-250, 0, s);
160 display_scores(uint32_t x, uint32_t y)
163 int i,h = font_height();
165 font_write(x+30, y, "High scores");
167 if(g_easy) font_write(x+75,y,"(easy)");
168 else font_write(x+60,y,"(normal)");
169 for(i = 0; i<N_SCORES; i++) {
171 snprintf(t, 1024, "#%1d",i+1);
173 snprintscore(t, 1024, g_scores[g_easy][i].score);
174 font_write(x+50, y, t);
175 if(i == cur_score) snprintf(t, 1024, "%s_", g_scores[g_easy][i].name);
176 else snprintf(t, 1024, "%s", g_scores[g_easy][i].name);
177 font_write(x+180, y, t);
182 process_score_input(SDL_keysym *key)
187 name = g_scores[g_easy][cur_score].name;
190 if(key->sym == SDLK_BACKSPACE) {
191 if(n > 0) name[--n]=0;
193 if(key->sym == SDLK_RETURN) {
194 SDL_EnableUNICODE(0);
201 if(key->unicode >= 32 && key->unicode <= 126) {
202 name[n++] = key->unicode;