Rev 1 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1 | pmbaty | 1 | /* |
2 | * src/scr_getname.c |
||
3 | * |
||
4 | * Copyright (C) 1998-2002 BigOrno (bigorno@bigorno.net). All rights reserved. |
||
5 | * |
||
6 | * The use and distribution terms for this software are contained in the file |
||
7 | * named README, which can be found in the root of this distribution. By |
||
8 | * using this software in any fashion, you are agreeing to be bound by the |
||
9 | * terms of this license. |
||
10 | * |
||
11 | * You must not remove this notice, or any other, from this software. |
||
12 | */ |
||
13 | |||
9 | pmbaty | 14 | #include <stdlib.h> // Pierre-Marie Baty -- for qsort() |
15 | #include <string.h> // Pierre-Marie Baty -- for memcmp() |
||
16 | |||
1 | pmbaty | 17 | #include "system.h" |
18 | #include "game.h" |
||
19 | #include "screens.h" |
||
20 | |||
21 | #include "draw.h" |
||
22 | #include "control.h" |
||
23 | |||
24 | /* |
||
25 | * local vars |
||
26 | */ |
||
27 | static U8 seq = 0; |
||
28 | static U8 x, y, p; |
||
29 | static U8 name[10]; |
||
30 | |||
31 | #define TILE_POINTER '\072' |
||
32 | #define TILE_CURSOR '\073' |
||
33 | #define TOPLEFT_X 116 |
||
34 | #define TOPLEFT_Y 64 |
||
35 | #define NAMEPOS_X 120 |
||
36 | #define NAMEPOS_Y 160 |
||
37 | #define AUTOREPEAT_TMOUT 100 |
||
38 | |||
39 | |||
9 | pmbaty | 40 | // Pierre-Marie Baty -- external prototype |
41 | extern int sort_hscores (const void *q1, const void *q2); |
||
42 | |||
43 | |||
1 | pmbaty | 44 | /* |
45 | * prototypes |
||
46 | */ |
||
47 | static void pointer_show (U8); |
||
48 | static void name_update (void); |
||
49 | static void name_draw (void); |
||
50 | |||
51 | |||
52 | /* |
||
53 | * Get name |
||
54 | * |
||
55 | * return: 0 while running, 1 when finished. |
||
56 | */ |
||
57 | U8 screen_getname (void) |
||
58 | { |
||
59 | static U32 tm = 0; |
||
60 | U8 i, j; |
||
61 | |||
62 | if (seq == 0) |
||
63 | { |
||
64 | /* figure out if this is a high score */ |
||
65 | if (game_score < game_hscores[7].score) |
||
66 | return SCREEN_DONE; |
||
67 | |||
68 | /* prepare */ |
||
69 | draw_tilesBank = 0; |
||
70 | |||
71 | for (i = 0; i < 10; i++) |
||
72 | name[i] = '@'; |
||
73 | |||
74 | x = y = p = 0; |
||
75 | game_rects = &draw_SCREENRECT; |
||
76 | seq = 1; |
||
77 | } |
||
78 | |||
79 | switch (seq) |
||
80 | { |
||
81 | case 1: /* prepare screen */ |
||
82 | sysvid_clear (); |
||
83 | draw_setfb (76, 40); |
||
84 | draw_tilesListImm ((U8 *) "PLEASE@ENTER@YOUR@NAME\376"); |
||
85 | |||
86 | for (i = 0; i < 6; i++) |
||
87 | for (j = 0; j < 4; j++) |
||
88 | { |
||
89 | draw_setfb (TOPLEFT_X + i * 8 * 2, TOPLEFT_Y + j * 8 * 2); |
||
90 | draw_tile ((U8) ('A' + i + j * 6)); |
||
91 | } |
||
92 | |||
93 | draw_setfb (TOPLEFT_X, TOPLEFT_Y + 64); |
||
94 | draw_tilesListImm ((U8 *) "Y@Z@.@@@\074\373\374\375\376"); |
||
95 | name_draw (); |
||
96 | pointer_show (TRUE); |
||
97 | seq = 2; |
||
98 | |||
99 | break; |
||
100 | |||
101 | case 2: /* wait for key pressed */ |
||
102 | if (control_status & CONTROL_FIRE) |
||
103 | seq = 3; |
||
104 | |||
105 | if (control_status & CONTROL_UP) |
||
106 | { |
||
107 | if (y > 0) |
||
108 | { |
||
109 | pointer_show (FALSE); |
||
110 | y--; |
||
111 | pointer_show (TRUE); |
||
112 | tm = sys_gettime (); |
||
113 | } |
||
114 | seq = 4; |
||
115 | } |
||
116 | |||
117 | if (control_status & CONTROL_DOWN) |
||
118 | { |
||
119 | if (y < 4) |
||
120 | { |
||
121 | pointer_show (FALSE); |
||
122 | y++; |
||
123 | pointer_show (TRUE); |
||
124 | tm = sys_gettime (); |
||
125 | } |
||
126 | seq = 5; |
||
127 | } |
||
128 | |||
129 | if (control_status & CONTROL_LEFT) |
||
130 | { |
||
131 | if (x > 0) |
||
132 | { |
||
133 | pointer_show (FALSE); |
||
134 | x--; |
||
135 | pointer_show (TRUE); |
||
136 | tm = sys_gettime (); |
||
137 | } |
||
138 | seq = 6; |
||
139 | } |
||
140 | |||
141 | if (control_status & CONTROL_RIGHT) |
||
142 | { |
||
143 | if (x < 5) |
||
144 | { |
||
145 | pointer_show (FALSE); |
||
146 | x++; |
||
147 | pointer_show (TRUE); |
||
148 | tm = sys_gettime (); |
||
149 | } |
||
150 | seq = 7; |
||
151 | } |
||
152 | |||
153 | if (seq == 2) |
||
154 | sys_sleep (50); |
||
155 | |||
156 | break; |
||
157 | |||
158 | case 3: /* wait for FIRE released */ |
||
159 | if (!(control_status & CONTROL_FIRE)) |
||
160 | { |
||
161 | if (x == 5 && y == 4) |
||
162 | { |
||
163 | /* end */ |
||
164 | |||
9 | pmbaty | 165 | // Pierre-Marie Baty -- discard empty names |
166 | if (memcmp (name, "@@@@@@@@@@", 10) != 0) |
||
167 | { |
||
168 | // Pierre-Marie Baty -- look if this same name already exists in the hiscore table |
||
169 | for (i = 0; i < 8; i++) |
||
170 | if (memcmp (game_hscores[i].name, name, 10) == 0) |
||
171 | break; |
||
172 | if (i == 8) // new name ? |
||
173 | { |
||
174 | i = 0; |
||
1 | pmbaty | 175 | |
9 | pmbaty | 176 | while (game_score < game_hscores[i].score) |
177 | i++; |
||
1 | pmbaty | 178 | |
9 | pmbaty | 179 | j = 7; |
1 | pmbaty | 180 | |
9 | pmbaty | 181 | while (j > i) |
182 | { |
||
183 | game_hscores[j].score = game_hscores[j - 1].score; |
||
1 | pmbaty | 184 | |
9 | pmbaty | 185 | for (x = 0; x < 10; x++) |
186 | game_hscores[j].name[x] = game_hscores[j - 1].name[x]; |
||
1 | pmbaty | 187 | |
9 | pmbaty | 188 | j--; |
189 | } |
||
1 | pmbaty | 190 | |
9 | pmbaty | 191 | game_hscores[i].score = game_score; |
1 | pmbaty | 192 | |
9 | pmbaty | 193 | for (x = 0; x < 10; x++) |
194 | game_hscores[i].name[x] = name[x]; |
||
195 | } |
||
196 | else if (game_score > game_hscores[i].score) // existing name: update only if score is higher, else discard |
||
197 | { |
||
198 | game_hscores[i].score = game_score; |
||
199 | qsort (game_hscores, 8, sizeof (hscore_t), sort_hscores); |
||
200 | } |
||
201 | } |
||
202 | |||
1 | pmbaty | 203 | seq = 99; |
204 | } |
||
205 | else |
||
206 | { |
||
207 | name_update (); |
||
208 | name_draw (); |
||
209 | seq = 2; |
||
210 | } |
||
211 | } |
||
212 | else |
||
213 | sys_sleep (50); |
||
214 | |||
215 | break; |
||
216 | |||
217 | case 4: /* wait for UP released */ |
||
218 | if (!(control_status & CONTROL_UP) || sys_gettime () - tm > AUTOREPEAT_TMOUT) |
||
219 | seq = 2; |
||
220 | else |
||
221 | sys_sleep (50); |
||
222 | |||
223 | break; |
||
224 | |||
225 | case 5: /* wait for DOWN released */ |
||
226 | if (!(control_status & CONTROL_DOWN) || sys_gettime () - tm > AUTOREPEAT_TMOUT) |
||
227 | seq = 2; |
||
228 | else |
||
229 | sys_sleep (50); |
||
230 | |||
231 | break; |
||
232 | |||
233 | case 6: /* wait for LEFT released */ |
||
234 | if (!(control_status & CONTROL_LEFT) || sys_gettime () - tm > AUTOREPEAT_TMOUT) |
||
235 | seq = 2; |
||
236 | else |
||
237 | sys_sleep (50); |
||
238 | |||
239 | break; |
||
240 | |||
241 | case 7: /* wait for RIGHT released */ |
||
242 | if (!(control_status & CONTROL_RIGHT) || sys_gettime () - tm > AUTOREPEAT_TMOUT) |
||
243 | seq = 2; |
||
244 | else |
||
245 | sys_sleep (50); |
||
246 | |||
247 | break; |
||
248 | |||
249 | } |
||
250 | |||
251 | if (control_status & CONTROL_EXIT) /* check for exit request */ |
||
252 | return SCREEN_EXIT; |
||
253 | |||
254 | if (seq == 99) |
||
255 | { /* seq 99, we're done */ |
||
256 | sysvid_clear (); |
||
257 | seq = 0; |
||
258 | return SCREEN_DONE; |
||
259 | } |
||
260 | else |
||
261 | return SCREEN_RUNNING; |
||
262 | } |
||
263 | |||
264 | |||
265 | static void pointer_show (U8 show) |
||
266 | { |
||
267 | draw_setfb (TOPLEFT_X + x * 8 * 2, TOPLEFT_Y + y * 8 * 2 + 8); |
||
268 | draw_tile ((U8) ((show == TRUE) ? TILE_POINTER : '@')); |
||
269 | } |
||
270 | |||
271 | |||
272 | static void name_update (void) |
||
273 | { |
||
274 | U8 i; |
||
275 | |||
276 | i = x + y * 6; |
||
277 | |||
278 | if (i < 26 && p < 10) |
||
279 | name[p++] = 'A' + i; |
||
280 | if (i == 26 && p < 10) |
||
281 | name[p++] = '.'; |
||
282 | if (i == 27 && p < 10) |
||
283 | name[p++] = '@'; |
||
284 | if (i == 28 && p > 0) |
||
9 | pmbaty | 285 | name[p--] = '@'; |
1 | pmbaty | 286 | } |
287 | |||
288 | |||
289 | static void name_draw (void) |
||
290 | { |
||
291 | U8 i; |
||
292 | |||
293 | draw_setfb (NAMEPOS_X, NAMEPOS_Y); |
||
294 | for (i = 0; i < p; i++) |
||
295 | draw_tile (name[i]); |
||
296 | for (i = p; i < 10; i++) |
||
297 | draw_tile (TILE_CURSOR); |
||
298 | |||
299 | draw_setfb (NAMEPOS_X, NAMEPOS_Y + 8); |
||
300 | for (i = 0; i < 10; i++) |
||
301 | draw_tile ('@'); |
||
302 | draw_setfb (NAMEPOS_X + 8 * (p < 9 ? p : 9), NAMEPOS_Y + 8); |
||
303 | draw_tile (TILE_POINTER); |
||
304 | } |