JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
added area back to get_shape, bounce() uses area.
[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 "score.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 = -1; // 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                         fscanf(f, "%d %31[^\n]", &g_scores[i].score, g_scores[i].name);
59                 }
60                 fclose(f);
61         }
62 }
63
64 void
65 write_high_score_table()
66 {
67         FILE *f;
68         int i;
69         
70         f = open_score_file("w");
71         if(f) {
72                 // If the file exists, write to it
73                 for(i = 0; i<N_SCORES; i++) {
74                         fprintf (f, "%d %.31s\n", g_scores[i].score, g_scores[i].name);
75                 }
76                 fclose(f);
77         }
78 }
79
80 int
81 score_rank(int score)
82 {
83         int i;
84
85         for(i=0; i<N_SCORES; i++) {
86                 if(score > g_scores[i].score) return i;
87         }
88         return -1;
89 }
90
91 int
92 new_high_score(int score)
93 {
94         int i;
95
96         cur_score = -1;  // assume not a new high score
97         if(score <= g_scores[LOW_SCORE].score) return false;
98         read_high_score_table();
99         cur_score = score_rank(score);
100         if(cur_score < 0) return false;
101
102         // Move all lower scores down a notch, losing lowest score forever.
103         for(i=LOW_SCORE; i>cur_score; i--) g_scores[i] = g_scores[i-1];
104
105         // initialize new score entry.
106         g_scores[cur_score].score = score;
107         for(i=0; i<32; i++) g_scores[cur_score].name[i] = 0;
108         return true;
109 }
110
111 int
112 snprintscore(char *s, size_t n, int score)
113 {
114         int min = score/60000;
115         int sec = score/1000%60;
116         int tenths = score%1000/100;
117         if(min) {
118                 return snprintf(s, n, "%2d:%.2d.%d", min, sec, tenths);
119         } else {
120                 return snprintf(s, n, "%2d.%d", sec, tenths);
121         }
122 }
123
124 int
125 snprintscore_line(char *s, size_t n, int score)
126 {
127         int r = snprintf(s, n, "Time: ");
128         return r + snprintscore(s+r, n-r, score);
129 }
130
131 void
132 display_scores(SDL_Surface *s, uint32_t x, uint32_t y)
133 {
134         char t[1024];
135         int i,h = SFont_TextHeight(g_font);
136
137         SFont_Write(s,g_font,x+30,y,"High scores");
138         y += h;
139         for(i = 0; i<N_SCORES; i++) {
140                 y += h;
141                 snprintf(t, 1024, "#%1d",i+1);
142                 SFont_Write(s, g_font, x, y, t);
143                 snprintscore(t, 1024, g_scores[i].score);
144                 SFont_Write(s, g_font, x+50, y, t);
145                 if(i == cur_score) snprintf(t, 1024, "%s_", g_scores[i].name);
146                 else snprintf(t, 1024, "%s", g_scores[i].name);
147                 SFont_Write(s, g_font, x+180, y, t);
148         }
149 }
150
151 int
152 process_score_input(void)
153 {
154         char *name;
155         int c,k,n;
156         SDL_Event e;
157         
158         name = g_scores[cur_score].name;
159         n = strlen(name);
160
161         while(SDL_PollEvent(&e) && e.type == SDL_KEYDOWN) {
162                 c = e.key.keysym.unicode;
163                 k = e.key.keysym.sym;
164                 if(k == SDLK_BACKSPACE) {
165                         if(n > 0) name[--n]=0;
166                 } else {
167                         if(k == SDLK_RETURN) {
168                                 SDL_EnableUNICODE(0);
169                                 cur_score = -1;
170                                 return false;
171                         } else name[n++] = c;
172                 }
173         }
174         return true;
175 }