JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
removed ship fade in code (unused)
[vor.git] / score.c
1 /* Variations on RockDodger
2  * Copyright (C) 2004 Joshua Grams <josh@qualdan.com>
3  *
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.
8  *
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.
13  *
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
17  */
18
19 #include <SDL.h>
20 #include <SDL_keysym.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include "SFont.h"
26
27 #include "common.h"
28 #include "config.h"
29 #include "file.h"
30 #include "globals.h"
31 #include "score.h"
32
33 // High score table
34 struct highscore g_scores[2][N_SCORES] = {
35         {
36                 {1*60*1000,"-"},
37                 {45*1000,"-"},
38                 {30*1000,"-"},
39                 {20*1000,"-"},
40                 {10*1000,"-"},
41                 {7*1000,"-"},
42                 {5*1000,"-"},
43                 {3*1000,"-"}
44         },
45         {
46                 {1*60*1000,"-"},
47                 {45*1000,"-"},
48                 {30*1000,"-"},
49                 {20*1000,"-"},
50                 {10*1000,"-"},
51                 {7*1000,"-"},
52                 {5*1000,"-"},
53                 {3*1000,"-"}
54         }
55 };
56
57 static char *titles[2] = { "Normal\n", "Easy\n" };
58
59 extern SFont_Font *g_font;
60
61 int g_easy = 0;
62 int cur_score = -1; // which score we're currently entering.
63
64 void
65 read_high_score_table()
66 {
67         FILE *f;
68         int i, j;
69         
70         f = open_score_file("r");
71         if(f) {
72                 // If the file exists, read from it
73                 for(j=0; j<2; j++) {
74                         fscanf(f, titles[j]);
75                         for(i = 0; i<N_SCORES; i++) {
76                                 fscanf(f, "%d %31[^\n]\n", &g_scores[j][i].score, g_scores[j][i].name);
77                         }
78                 }
79                 fclose(f);
80         }
81 }
82
83 void
84 write_high_score_table()
85 {
86         FILE *f;
87         int i, j;
88         
89         f = open_score_file("w");
90         if(f) {
91                 // If the file exists, write to it
92                 for(j=0; j<2; j++) {
93                         fprintf(f, titles[j]);
94                         for(i = 0; i<N_SCORES; i++) {
95                                 fprintf (f, "%d %.31s\n", g_scores[j][i].score, g_scores[j][i].name);
96                         }
97                 }
98                 fclose(f);
99         }
100 }
101
102 int
103 score_rank(int score)
104 {
105         int i;
106
107         for(i=0; i<N_SCORES; i++) {
108                 if(score > g_scores[g_easy][i].score) return i;
109         }
110         return -1;
111 }
112
113 int
114 new_high_score(int score)
115 {
116         cur_score = -1;  // assume not a new high score
117         if(score <= g_scores[g_easy][LOW_SCORE].score) return false;
118         read_high_score_table();
119         cur_score = score_rank(score);
120         return cur_score >= 0;
121 }
122
123 int
124 insert_score(int score)
125 {
126         int i;
127
128         // Move all lower scores down a notch, losing lowest score forever.
129         if(strcmp(g_scores[g_easy][cur_score].name, "-") != 0)
130                 for(i=LOW_SCORE; i>cur_score; i--)
131                         g_scores[g_easy][i] = g_scores[g_easy][i-1];
132
133         // initialize new score entry.
134         g_scores[g_easy][cur_score].score = score;
135         for(i=0; i<32; i++) g_scores[g_easy][cur_score].name[i] = 0;
136         return true;
137 }
138
139 int
140 snprintscore(char *s, size_t n, int score)
141 {
142         int min = score/60000;
143         int sec = score/1000%60;
144         int tenths = score%1000/100;
145         if(min) {
146                 return snprintf(s, n, "%2d:%.2d.%d", min, sec, tenths);
147         } else {
148                 return snprintf(s, n, "%2d.%d", sec, tenths);
149         }
150 }
151
152 void
153 show_score(void)
154 {
155         char s[16];
156         int r = snprintf(s, 16, "Time: ");
157         snprintscore(s+r, 16-r, score);
158         SFont_Write(surf_screen, g_font, XSIZE-250, 0, s);
159 }
160
161 void
162 display_scores(SDL_Surface *s, uint32_t x, uint32_t y)
163 {
164         char t[1024];
165         int i,h = SFont_TextHeight(g_font);
166
167         SFont_Write(s,g_font,x+30,y,"High scores");
168         y += h;
169         if(g_easy) SFont_Write(s,g_font,x+75,y,"(easy)");
170         else SFont_Write(s,g_font,x+60,y,"(normal)");
171         for(i = 0; i<N_SCORES; i++) {
172                 y += h;
173                 snprintf(t, 1024, "#%1d",i+1);
174                 SFont_Write(s, g_font, x, y, t);
175                 snprintscore(t, 1024, g_scores[g_easy][i].score);
176                 SFont_Write(s, g_font, x+50, y, t);
177                 if(i == cur_score) snprintf(t, 1024, "%s_", g_scores[g_easy][i].name);
178                 else snprintf(t, 1024, "%s", g_scores[g_easy][i].name);
179                 SFont_Write(s, g_font, x+180, y, t);
180         }
181 }
182
183 int
184 process_score_input(void)
185 {
186         char *name;
187         int c,k,n;
188         SDL_Event e;
189         
190         name = g_scores[g_easy][cur_score].name;
191         n = strlen(name);
192
193         while(SDL_PollEvent(&e) && e.type == SDL_KEYDOWN) {
194                 c = e.key.keysym.unicode;
195                 k = e.key.keysym.sym;
196                 if(k == SDLK_BACKSPACE) {
197                         if(n > 0) name[--n]=0;
198                 } else {
199                         if(k == SDLK_RETURN) {
200                                 SDL_EnableUNICODE(0);
201                                 cur_score = -1;
202                                 return false;
203                         } else name[n++] = c;
204                 }
205         }
206         return true;
207 }