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[N_SCORES] = {
45 extern SFont_Font *g_font;
47 int cur_score = -1; // which score we're currently entering.
50 read_high_score_table()
55 f = open_score_file("r");
57 // If the file exists, read from it
58 for(i = 0; i<N_SCORES; i++) {
59 fscanf(f, "%d %31[^\n]", &g_scores[i].score, g_scores[i].name);
66 write_high_score_table()
71 f = open_score_file("w");
73 // If the file exists, write to it
74 for(i = 0; i<N_SCORES; i++) {
75 fprintf (f, "%d %.31s\n", g_scores[i].score, g_scores[i].name);
86 for(i=0; i<N_SCORES; i++) {
87 if(score > g_scores[i].score) return i;
93 new_high_score(int score)
97 cur_score = -1; // assume not a new high score
98 if(score <= g_scores[LOW_SCORE].score) return false;
99 read_high_score_table();
100 cur_score = score_rank(score);
101 if(cur_score < 0) return false;
103 // Move all lower scores down a notch, losing lowest score forever.
104 for(i=LOW_SCORE; i>cur_score; i--) g_scores[i] = g_scores[i-1];
106 // initialize new score entry.
107 g_scores[cur_score].score = score;
108 for(i=0; i<32; i++) g_scores[cur_score].name[i] = 0;
113 snprintscore(char *s, size_t n, int score)
115 int min = score/60000;
116 int sec = score/1000%60;
117 int tenths = score%1000/100;
119 return snprintf(s, n, "%2d:%.2d.%d", min, sec, tenths);
121 return snprintf(s, n, "%2d.%d", sec, tenths);
129 int r = snprintf(s, 16, "Time: ");
130 snprintscore(s+r, 16-r, score);
131 SFont_Write(surf_screen, g_font, XSIZE-250, 0, s);
135 display_scores(SDL_Surface *s, uint32_t x, uint32_t y)
138 int i,h = SFont_TextHeight(g_font);
140 SFont_Write(s,g_font,x+30,y,"High scores");
142 for(i = 0; i<N_SCORES; i++) {
144 snprintf(t, 1024, "#%1d",i+1);
145 SFont_Write(s, g_font, x, y, t);
146 snprintscore(t, 1024, g_scores[i].score);
147 SFont_Write(s, g_font, x+50, y, t);
148 if(i == cur_score) snprintf(t, 1024, "%s_", g_scores[i].name);
149 else snprintf(t, 1024, "%s", g_scores[i].name);
150 SFont_Write(s, g_font, x+180, y, t);
155 process_score_input(void)
161 name = g_scores[cur_score].name;
164 while(SDL_PollEvent(&e) && e.type == SDL_KEYDOWN) {
165 c = e.key.keysym.unicode;
166 k = e.key.keysym.sym;
167 if(k == SDLK_BACKSPACE) {
168 if(n > 0) name[--n]=0;
170 if(k == SDLK_RETURN) {
171 SDL_EnableUNICODE(0);
174 } else name[n++] = c;