8 #include <SDL_keysym.h>
15 struct highscore g_scores[N_SCORES] = {
26 extern SFont_Font *g_font;
28 int cur_score; // which score we're currently entering.
31 read_high_score_table()
36 f = open_score_file("r");
38 // If the file exists, read from it
39 for(i = 0; i<N_SCORES; i++) {
40 g_scores[i].score = 0;
41 g_scores[i].name[0] = 0;
42 fscanf(f, "%d %31[^\n]", &g_scores[i].score, g_scores[i].name);
49 write_high_score_table()
54 f = open_score_file("w");
56 // If the file exists, write to it
57 for(i = 0; i<N_SCORES; i++) {
58 fprintf (f, "%d %.31s\n", g_scores[i].score, g_scores[i].name);
69 for(i=0; i<N_SCORES; i++) {
70 if(score > g_scores[i].score) return i;
76 new_high_score(int score)
80 cur_score = -1; // assume not a new high score
81 if(score <= g_scores[LOW_SCORE].score) return false;
82 read_high_score_table();
83 cur_score = score_rank(score);
84 if(cur_score < 0) return false;
86 // Move all lower scores down a notch, losing lowest score forever.
87 for(i=LOW_SCORE; i>cur_score; i--) g_scores[i] = g_scores[i-1];
89 // initialize new score entry.
90 g_scores[cur_score].score = score;
91 for(i=0; i<32; i++) g_scores[cur_score].name[i] = 0;
96 snprintscore(char *s, size_t n, int score)
98 int min = score/60000;
99 int sec = score/1000%60;
100 int tenths = score%1000/100;
102 return snprintf(s, n, "%2d:%.2d.%d", min, sec, tenths);
104 return snprintf(s, n, "%2d.%d", sec, tenths);
109 snprintscore_line(char *s, size_t n, int score)
111 int r = snprintf(s, n, "Time: ");
112 return r + snprintscore(s+r, n-r, score);
116 display_scores(SDL_Surface *s, uint32_t x, uint32_t y)
119 int i,h = SFont_TextHeight(g_font);
121 SFont_Write(s,g_font,x+30,y,"High scores");
123 for(i = 0; i<N_SCORES; i++) {
125 snprintf(t, 1024, "#%1d",i+1);
126 SFont_Write(s, g_font, x, y, t);
127 snprintscore(t, 1024, g_scores[i].score);
128 SFont_Write(s, g_font, x+50, y, t);
129 snprintf(t, 1024, "%s", g_scores[i].name);
130 SFont_Write(s, g_font, x+180, y, t);
135 process_score_input(void)
141 name = g_scores[cur_score].name;
144 while(SDL_PollEvent(&e) && e.type == SDL_KEYDOWN) {
145 c = e.key.keysym.unicode;
146 k = e.key.keysym.sym;
147 if(k == SDLK_BACKSPACE && n > 0) name[n--]=0;
148 else if(e.key.keysym.sym == SDLK_RETURN) {
149 SDL_EnableUNICODE(0);