JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
* rocks.c (bucket): rewrote to work with either division rounding mode (symmetric...
[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 <stdint.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include "SFont.h"
27
28 #include "common.h"
29 #include "config.h"
30 #include "file.h"
31 #include "score.h"
32
33 // High score table
34 struct highscore g_scores[N_SCORES] = {
35         {1*60*1000,"-"},
36         {45*1000,"-"},
37         {30*1000,"-"},
38         {20*1000,"-"},
39         {10*1000,"-"},
40         {7*1000,"-"},
41         {5*1000,"-"},
42         {3*1000,"-"}
43 };
44
45 extern SFont_Font *g_font;
46
47 int cur_score = -1; // which score we're currently entering.
48
49 void
50 read_high_score_table()
51 {
52         FILE *f;
53         int i;
54         
55         f = open_score_file("r");
56         if(f) {
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);
60                 }
61                 fclose(f);
62         }
63 }
64
65 void
66 write_high_score_table()
67 {
68         FILE *f;
69         int i;
70         
71         f = open_score_file("w");
72         if(f) {
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);
76                 }
77                 fclose(f);
78         }
79 }
80
81 int
82 score_rank(int score)
83 {
84         int i;
85
86         for(i=0; i<N_SCORES; i++) {
87                 if(score > g_scores[i].score) return i;
88         }
89         return -1;
90 }
91
92 int
93 new_high_score(int score)
94 {
95         int i;
96
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;
102
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];
105
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;
109         return true;
110 }
111
112 int
113 snprintscore(char *s, size_t n, int score)
114 {
115         int min = score/60000;
116         int sec = score/1000%60;
117         int tenths = score%1000/100;
118         if(min) {
119                 return snprintf(s, n, "%2d:%.2d.%d", min, sec, tenths);
120         } else {
121                 return snprintf(s, n, "%2d.%d", sec, tenths);
122         }
123 }
124
125 int
126 snprintscore_line(char *s, size_t n, int score)
127 {
128         int r = snprintf(s, n, "Time: ");
129         return r + snprintscore(s+r, n-r, score);
130 }
131
132 void
133 display_scores(SDL_Surface *s, uint32_t x, uint32_t y)
134 {
135         char t[1024];
136         int i,h = SFont_TextHeight(g_font);
137
138         SFont_Write(s,g_font,x+30,y,"High scores");
139         y += h;
140         for(i = 0; i<N_SCORES; i++) {
141                 y += h;
142                 snprintf(t, 1024, "#%1d",i+1);
143                 SFont_Write(s, g_font, x, y, t);
144                 snprintscore(t, 1024, g_scores[i].score);
145                 SFont_Write(s, g_font, x+50, y, t);
146                 if(i == cur_score) snprintf(t, 1024, "%s_", g_scores[i].name);
147                 else 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) {
166                         if(n > 0) name[--n]=0;
167                 } else {
168                         if(k == SDLK_RETURN) {
169                                 SDL_EnableUNICODE(0);
170                                 cur_score = -1;
171                                 return false;
172                         } else name[n++] = c;
173                 }
174         }
175         return true;
176 }