1 /*
2 * SPEED - by Shawn Hargreaves, 1999
3 *
4 * Hiscore table.
5 */
6
7 #include <stdio.h>
8 #include <string.h>
9 #include <allegro5/allegro.h>
10 #include <allegro5/allegro_font.h>
11 #include <allegro5/allegro_primitives.h>
12
13 #include "speed.h"
14
15
16
17 #define NUM_SCORES 8
18 #define MAX_NAME_LEN 24
19
20
21 /* the score table */
22 static int scores[NUM_SCORES] =
23 {
24 666, 512, 440, 256, 192, 128, 64, 42
25 };
26
27 static char names[NUM_SCORES][MAX_NAME_LEN+1];
28
29 static char yourname[MAX_NAME_LEN+1] = "";
30
31
32
33 /* initialises the hiscore system */
init_hiscore()34 void init_hiscore()
35 {
36 ALLEGRO_PATH *path;
37 ALLEGRO_CONFIG *cfg;
38 char buf1[256];
39 int i;
40
41 path = al_get_standard_path(ALLEGRO_USER_DATA_PATH);
42 if (!path) return;
43 al_make_directory(al_path_cstr(path, ALLEGRO_NATIVE_PATH_SEP));
44
45 al_set_path_filename(path, "speed.rec");
46
47 cfg = al_load_config_file(al_path_cstr(path, ALLEGRO_NATIVE_PATH_SEP));
48 if (!cfg)
49 cfg = al_create_config();
50
51 for (i=0; i<NUM_SCORES; i++) {
52 sprintf(buf1, "score%d", i+1);
53 scores[i] = get_config_int(cfg, "hiscore", buf1, scores[i]);
54
55 sprintf(buf1, "name%d", i+1);
56 strncpy(names[i], get_config_string(cfg, "hiscore", buf1, "Shawn Hargreaves"), MAX_NAME_LEN);
57 names[i][MAX_NAME_LEN] = 0;
58 }
59
60 al_destroy_config(cfg);
61 al_destroy_path(path);
62 }
63
64
65
66 /* shuts down the hiscore system */
shutdown_hiscore()67 void shutdown_hiscore()
68 {
69 ALLEGRO_PATH *path;
70 ALLEGRO_CONFIG *cfg;
71 char buf1[256];
72 int i;
73
74 path = al_get_standard_path(ALLEGRO_USER_DATA_PATH);
75 if (!path) return;
76 al_make_directory(al_path_cstr(path, ALLEGRO_NATIVE_PATH_SEP));
77
78 al_set_path_filename(path, "speed.rec");
79
80 cfg = al_create_config();
81
82 for (i=0; i<NUM_SCORES; i++) {
83 sprintf(buf1, "score%d", i+1);
84 set_config_int(cfg, "hiscore", buf1, scores[i]);
85
86 sprintf(buf1, "name%d", i+1);
87 al_set_config_value(cfg, "hiscore", buf1, names[i]);
88 }
89
90 al_save_config_file(al_path_cstr(path, ALLEGRO_NATIVE_PATH_SEP), cfg);
91
92 al_destroy_config(cfg);
93 al_destroy_path(path);
94 }
95
96
97
98 /* displays the text entry box */
draw_entry_box(int which,int64_t retrace_count)99 static void draw_entry_box(int which, int64_t retrace_count)
100 {
101 const int w = MAX_NAME_LEN*8+16;
102 const int h = 16;
103 ALLEGRO_BITMAP *b;
104 ALLEGRO_BITMAP *screen;
105 int SCREEN_W, SCREEN_H;
106 int x;
107
108 screen = al_get_target_bitmap();
109 SCREEN_W = al_get_bitmap_width(screen);
110 SCREEN_H = al_get_bitmap_height(screen);
111
112 b = create_memory_bitmap(w, h);
113 al_set_target_bitmap(b);
114 al_clear_to_color(makecol(0, 96, 0));
115 hline(0, w, h-1, makecol(0, 32, 0));
116 vline(0, w-1, h, makecol(0, 32, 0));
117
118 textprintf(font, 9, 5, makecol(0, 0, 0), "%s", yourname);
119 textprintf(font, 8, 4, makecol(255, 255, 255), "%s", yourname);
120
121 if (retrace_count & 8) {
122 x = strlen(yourname)*8 + 8;
123 rectfill(x, 12, x+7, 14, makecol(0, 0, 0));
124 }
125
126 al_set_target_bitmap(screen);
127 al_draw_bitmap(b, SCREEN_W/2-56, SCREEN_H/2+(which-NUM_SCORES/2)*16-4, 0);
128
129 al_destroy_bitmap(b);
130 }
131
132
133
134 /* displays the score table */
score_table()135 void score_table()
136 {
137 int SCREEN_W = al_get_display_width(screen);
138 int SCREEN_H = al_get_display_height(screen);
139 ALLEGRO_BITMAP *bmp, *b;
140 ALLEGRO_COLOR col;
141 int c, i, j, x, y;
142 int myscore = -1;
143
144 for (i=0; i<NUM_SCORES; i++) {
145 if (score > scores[i]) {
146 for (j=NUM_SCORES-1; j>i; j--) {
147 scores[j] = scores[j-1];
148 strcpy(names[j], names[j-1]);
149 }
150
151 scores[i] = score;
152 strcpy(names[i], yourname);
153
154 myscore = i;
155 break;
156 }
157 }
158
159 bmp = create_memory_bitmap(SCREEN_W, SCREEN_H);
160 al_set_target_bitmap(bmp);
161
162 for (i=0; i<SCREEN_W/2; i++) {
163 vline(SCREEN_W/2-i-1, 0, SCREEN_H, makecol(0, i*255/(SCREEN_W/2), 0));
164 vline(SCREEN_W/2+i, 0, SCREEN_H, makecol(0, i*255/(SCREEN_W/2), 0));
165 }
166
167 b = create_memory_bitmap(104, 8);
168 al_set_target_bitmap(b);
169 al_clear_to_color(al_map_rgba(0, 0, 0, 0));
170
171 textout(font, "HISCORE TABLE", 0, 0, makecol(0, 0, 0));
172 stretch_sprite(bmp, b, SCREEN_W/64+4, SCREEN_H/64+4, SCREEN_W*31/32, SCREEN_H/8);
173 stretch_sprite(bmp, b, SCREEN_W/64+4, SCREEN_H*55/64+4, SCREEN_W*31/32, SCREEN_H/8);
174
175 textout(font, "HISCORE TABLE", 0, 0, makecol(0, 64, 0));
176 stretch_sprite(bmp, b, SCREEN_W/64, SCREEN_H/64, SCREEN_W*31/32, SCREEN_H/8);
177 stretch_sprite(bmp, b, SCREEN_W/64, SCREEN_H*55/64, SCREEN_W*31/32, SCREEN_H/8);
178
179 al_destroy_bitmap(b);
180 al_set_target_bitmap(bmp);
181
182 for (i=0; i<NUM_SCORES; i++) {
183 y = SCREEN_H/2 + (i-NUM_SCORES/2) * 16;
184
185 textprintf(font, SCREEN_W/2-142, y+2, makecol(0, 0, 0), "#%d - %d", i+1, scores[i]);
186 textprintf(font, SCREEN_W/2-47, y+1, makecol(0, 0, 0), "%s", names[i]);
187
188 if (i == myscore)
189 col = makecol(255, 0, 0);
190 else
191 col = makecol(255, 255, 255);
192
193 textprintf(font, SCREEN_W/2-144, y, col, "#%d - %d", i+1, scores[i]);
194 textprintf(font, SCREEN_W/2-48, y, col, "%s", names[i]);
195 }
196
197 if (myscore >= 0)
198 draw_entry_box(myscore, 0);
199
200 bmp = replace_bitmap(bmp);
201
202 al_set_target_bitmap(al_get_backbuffer(screen));
203
204 start_retrace_count();
205
206 for (i=0; i<=SCREEN_H/16; i++) {
207 al_clear_to_color(makecol(0, 0, 0));
208
209 for (j=0; j<=16; j++) {
210 x = j*(SCREEN_W/16);
211 al_draw_bitmap_region(bmp, x, 0, i, SCREEN_H, x, 0, 0);
212
213 y = j*(SCREEN_H/16);
214 al_draw_bitmap_region(bmp, 0, y, SCREEN_W, i, 0, y, 0);
215 }
216
217 al_flip_display();
218
219 do {
220 poll_input_wait();
221 } while (retrace_count() < i*512/SCREEN_W);
222 }
223
224 while (joy_b1 || key[ALLEGRO_KEY_SPACE] || key[ALLEGRO_KEY_ENTER] || key[ALLEGRO_KEY_ESCAPE])
225 poll_input_wait();
226
227 if (myscore >= 0) {
228 clear_keybuf();
229
230 for (;;) {
231 poll_input_wait();
232
233 if ((joy_b1) && (yourname[0])) {
234 strcpy(names[myscore], yourname);
235 break;
236 }
237
238 if (keypressed()) {
239 c = readkey();
240
241 if (((c >> 8) == ALLEGRO_KEY_ENTER) && (yourname[0])) {
242 strcpy(names[myscore], yourname);
243 sfx_explode_player();
244 break;
245 }
246 else if (((c >> 8) == ALLEGRO_KEY_ESCAPE) && (names[myscore][0])) {
247 strcpy(yourname, names[myscore]);
248 sfx_ping(2);
249 break;
250 }
251 else if (((c >> 8) == ALLEGRO_KEY_BACKSPACE) && (strlen(yourname) > 0)) {
252 yourname[strlen(yourname)-1] = 0;
253 sfx_shoot();
254 }
255 else if (((c & 0xFF) >= ' ') && ((c & 0xFF) <= '~') && (strlen(yourname) < MAX_NAME_LEN)) {
256 yourname[strlen(yourname)+1] = 0;
257 yourname[strlen(yourname)] = (c & 0xFF);
258 sfx_explode_alien();
259 }
260 }
261
262 al_draw_bitmap(bmp, 0, 0, 0);
263 draw_entry_box(myscore, retrace_count());
264 al_flip_display();
265 }
266 }
267 else {
268 while (!key[ALLEGRO_KEY_SPACE] && !key[ALLEGRO_KEY_ENTER] && !key[ALLEGRO_KEY_ESCAPE] && !joy_b1) {
269 poll_input_wait();
270 al_draw_bitmap(bmp, 0, 0, 0);
271 al_flip_display();
272 }
273
274 sfx_ping(2);
275 }
276
277 stop_retrace_count();
278
279 al_destroy_bitmap(bmp);
280 }
281
282
283
284 /* returns the best score, for other modules to display */
get_hiscore()285 int get_hiscore()
286 {
287 return scores[0];
288 }
289
290