JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
* Switched over to using SFont 2.03, which doesn't have tons of
[vor.git] / score.c
1 #include "config.h"
2
3 #include "SFont.h"
4 #include "file.h"
5 #include "score.h"
6
7 #include <SDL.h>
8 #include <SDL_keysym.h>
9 #include <stdint.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13
14 // High score table
15 struct highscore g_scores[N_SCORES] = {
16         {13000,"Pad"},
17         {12500,"Pad"},
18         {6500,"Pad"},
19         {5000,"Pad"},
20         {3000,"Pad"},
21         {2500,"Pad"},
22         {2000,"Pad"},
23         {1500,"Pad"}
24 };
25
26 extern SFont_Font *g_font;
27
28 int cur_score; // which score we're currently entering.
29
30 void
31 read_high_score_table()
32 {
33         FILE *f;
34         int i;
35         
36         f = open_score_file("r");
37         if(f) {
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);
43                 }
44                 fclose(f);
45         }
46 }
47
48 void
49 write_high_score_table()
50 {
51         FILE *f;
52         int i;
53         
54         f = open_score_file("w");
55         if(f) {
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);
59                 }
60                 fclose(f);
61         }
62 }
63
64 int
65 score_rank(int score)
66 {
67         int i;
68
69         for(i=0; i<N_SCORES; i++) {
70                 if(score > g_scores[i].score) return i;
71         }
72         return -1;
73 }
74
75 int
76 new_high_score(int score)
77 {
78         int i;
79
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;
85
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];
88
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;
92         return true;
93 }
94
95 int
96 snprintscore(char *s, size_t n, int score)
97 {
98         int min = score/60000;
99         int sec = score/1000%60;
100         int tenths = score%1000/100;
101         if(min) {
102                 return snprintf(s, n, "%2d:%.2d.%d", min, sec, tenths);
103         } else {
104                 return snprintf(s, n, "%2d.%d", sec, tenths);
105         }
106 }
107
108 int
109 snprintscore_line(char *s, size_t n, int score)
110 {
111         int r = snprintf(s, n, "Time: ");
112         return r + snprintscore(s+r, n-r, score);
113 }
114
115 void
116 display_scores(SDL_Surface *s, uint32_t x, uint32_t y)
117 {
118         char t[1024];
119         int i,h = SFont_TextHeight(g_font);
120
121         SFont_Write(s,g_font,x+30,y,"High scores");
122         y += h;
123         for(i = 0; i<N_SCORES; i++) {
124                 y += h;
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);
131         }
132 }
133
134 int
135 process_score_input(void)
136 {
137         char *name;
138         int c,k,n;
139         SDL_Event e;
140         
141         name = g_scores[cur_score].name;
142         n = strlen(name);
143
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);
150                         return false;
151                 }
152                 else name[n++] = c;
153         }
154         return true;
155 }