JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
renames:
[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 "config.h"
20
21 #include "SFont.h"
22 #include "file.h"
23 #include "score.h"
24
25 #include <SDL.h>
26 #include <SDL_keysym.h>
27 #include <stdint.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 // High score table
33 struct highscore g_scores[N_SCORES] = {
34         {1*60*1000,"-"},
35         {45*1000,"-"},
36         {30*1000,"-"},
37         {20*1000,"-"},
38         {10*1000,"-"},
39         {7*1000,"-"},
40         {5*1000,"-"},
41         {3*1000,"-"}
42 };
43
44 extern SFont_Font *g_font;
45
46 int cur_score; // which score we're currently entering.
47
48 void
49 read_high_score_table()
50 {
51         FILE *f;
52         int i;
53         
54         f = open_score_file("r");
55         if(f) {
56                 // If the file exists, read from it
57                 for(i = 0; i<N_SCORES; i++) {
58                         g_scores[i].score = 0;
59                         g_scores[i].name[0] = 0;
60                         fscanf(f, "%d %31[^\n]", &g_scores[i].score, g_scores[i].name);
61                 }
62                 fclose(f);
63         }
64 }
65
66 void
67 write_high_score_table()
68 {
69         FILE *f;
70         int i;
71         
72         f = open_score_file("w");
73         if(f) {
74                 // If the file exists, write to it
75                 for(i = 0; i<N_SCORES; i++) {
76                         fprintf (f, "%d %.31s\n", g_scores[i].score, g_scores[i].name);
77                 }
78                 fclose(f);
79         }
80 }
81
82 int
83 score_rank(int score)
84 {
85         int i;
86
87         for(i=0; i<N_SCORES; i++) {
88                 if(score > g_scores[i].score) return i;
89         }
90         return -1;
91 }
92
93 int
94 new_high_score(int score)
95 {
96         int i;
97
98         cur_score = -1;  // assume not a new high score
99         if(score <= g_scores[LOW_SCORE].score) return false;
100         read_high_score_table();
101         cur_score = score_rank(score);
102         if(cur_score < 0) return false;
103
104         // Move all lower scores down a notch, losing lowest score forever.
105         for(i=LOW_SCORE; i>cur_score; i--) g_scores[i] = g_scores[i-1];
106
107         // initialize new score entry.
108         g_scores[cur_score].score = score;
109         for(i=0; i<32; i++) g_scores[cur_score].name[i] = 0;
110         return true;
111 }
112
113 int
114 snprintscore(char *s, size_t n, int score)
115 {
116         int min = score/60000;
117         int sec = score/1000%60;
118         int tenths = score%1000/100;
119         if(min) {
120                 return snprintf(s, n, "%2d:%.2d.%d", min, sec, tenths);
121         } else {
122                 return snprintf(s, n, "%2d.%d", sec, tenths);
123         }
124 }
125
126 int
127 snprintscore_line(char *s, size_t n, int score)
128 {
129         int r = snprintf(s, n, "Time: ");
130         return r + snprintscore(s+r, n-r, score);
131 }
132
133 void
134 display_scores(SDL_Surface *s, uint32_t x, uint32_t y)
135 {
136         char t[1024];
137         int i,h = SFont_TextHeight(g_font);
138
139         SFont_Write(s,g_font,x+30,y,"High scores");
140         y += h;
141         for(i = 0; i<N_SCORES; i++) {
142                 y += h;
143                 snprintf(t, 1024, "#%1d",i+1);
144                 SFont_Write(s, g_font, x, y, t);
145                 snprintscore(t, 1024, g_scores[i].score);
146                 SFont_Write(s, g_font, x+50, y, t);
147                 snprintf(t, 1024, "%s", g_scores[i].name);
148                 SFont_Write(s, g_font, x+180, y, t);
149         }
150 }
151
152 int
153 process_score_input(void)
154 {
155         char *name;
156         int c,k,n;
157         SDL_Event e;
158         
159         name = g_scores[cur_score].name;
160         n = strlen(name);
161
162         while(SDL_PollEvent(&e) && e.type == SDL_KEYDOWN) {
163                 c = e.key.keysym.unicode;
164                 k = e.key.keysym.sym;
165                 if(k == SDLK_BACKSPACE && n > 0) name[n--]=0;
166                 else if(e.key.keysym.sym == SDLK_RETURN) {
167                         SDL_EnableUNICODE(0);
168                         return false;
169                 }
170                 else name[n++] = c;
171         }
172         return true;
173 }