Rev 4484 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4484 | Rev 5098 | ||
---|---|---|---|
1 | // top10wnd.cpp |
1 | // top10wnd.cpp |
2 | 2 | ||
3 | #include "kosSyst.h" |
3 | #include "kosSyst.h" |
4 | #include "KosFile.h" |
4 | #include "KosFile.h" |
5 | #include "gfxdef.h" |
5 | #include "gfxdef.h" |
6 | #include "gameWnd.h" |
6 | #include "gameWnd.h" |
7 | #include "mcarray.h" |
7 | #include "mcarray.h" |
8 | #include "top10wnd.h" |
8 | #include "top10wnd.h" |
9 | #include "lang.h" |
9 | #include "lang.h" |
10 | 10 | ||
11 | // |
11 | // |
12 | char top10FilePath[MAX_PATH]; |
12 | char top10FilePath[MAX_PATH]; |
13 | 13 | ||
14 | // |
14 | // |
15 | struct hiScoreHero |
15 | struct hiScoreHero |
16 | { |
16 | { |
17 | char name[12]; |
17 | char name[12]; |
18 | Dword score; |
18 | Dword score; |
19 | // |
19 | // |
20 | hiScoreHero() |
20 | hiScoreHero() |
21 | { |
21 | { |
22 | // |
22 | // |
23 | this->ClearName(); |
23 | this->ClearName(); |
24 | this->score = 0; |
24 | this->score = 0; |
25 | }; |
25 | }; |
26 | // |
26 | // |
27 | void ClearName() |
27 | void ClearName() |
28 | { |
28 | { |
29 | memset( (Byte *)(this->name), '.', sizeof(this->name) ); |
29 | memset( (Byte *)(this->name), '.', sizeof(this->name) ); |
30 | }; |
30 | }; |
31 | }; |
31 | }; |
32 | 32 | ||
33 | // |
33 | // |
34 | hiScoreHero heroTbl[TOP_TBL_SIZE]; |
34 | hiScoreHero heroTbl[TOP_TBL_SIZE]; |
35 | 35 | ||
36 | // |
36 | // |
37 | struct hiScoreFile |
37 | struct hiScoreFile |
38 | { |
38 | { |
39 | Byte block[512]; |
39 | Byte block[512]; |
40 | kosFileInfo fi; |
40 | kosFileInfo fi; |
41 | // |
41 | // |
42 | hiScoreFile() |
42 | hiScoreFile() |
43 | { |
43 | { |
44 | int i; |
44 | int i; |
45 | 45 | ||
46 | // |
46 | // |
47 | this->fi.OffsetLow = this->fi.OffsetHigh = 0; |
47 | this->fi.OffsetLow = this->fi.OffsetHigh = 0; |
48 | this->fi.dataCount = 0; |
48 | this->fi.dataCount = 0; |
49 | this->fi.bufferPtr = this->block; |
49 | this->fi.bufferPtr = this->block; |
50 | this->fi.rwMode = 0; |
50 | this->fi.rwMode = 0; |
51 | memcpy( this->fi.fileURL, top10FilePath, strlen( top10FilePath ) + 1); |
51 | memcpy( this->fi.fileURL, top10FilePath, strlen( top10FilePath ) + 1); |
52 | // |
52 | // |
53 | for ( i = 0; i < ( sizeof( this->block ) / sizeof( Dword ) ); i++ ) |
53 | for ( i = 0; i < ( sizeof( this->block ) / sizeof( Dword ) ); i++ ) |
54 | { |
54 | { |
55 | // |
55 | // |
56 | ((Dword *)(this->block))[i] = rtlRand(); |
56 | ((Dword *)(this->block))[i] = rtlRand(); |
57 | } |
57 | } |
58 | }; |
58 | }; |
59 | // |
59 | // |
60 | virtual ~hiScoreFile() |
60 | virtual ~hiScoreFile() |
61 | {} |
61 | {} |
62 | // |
62 | // |
63 | bool LoadFromDisk() |
63 | bool LoadFromDisk() |
64 | { |
64 | { |
65 | bool result; |
65 | bool result; |
66 | int i; |
66 | int i; |
67 | Dword j, k; |
67 | Dword j, k; |
68 | Byte *bPtr; |
68 | Byte *bPtr; |
69 | 69 | ||
70 | // |
70 | // |
71 | this->fi.rwMode = FO_READ; |
71 | this->fi.rwMode = FO_READ; |
72 | this->fi.OffsetLow = this->fi.OffsetHigh = 0; |
72 | this->fi.OffsetLow = this->fi.OffsetHigh = 0; |
73 | this->fi.dataCount = 512; |
73 | this->fi.dataCount = 512; |
74 | result = kos_FileSystemAccess( &(this->fi) ) == 0; |
74 | result = kos_FileSystemAccess( &(this->fi) ) == 0; |
75 | // |
75 | // |
76 | if ( result ) |
76 | if ( result ) |
77 | { |
77 | { |
78 | // äåêîäèðóåì |
78 | // äåêîäèðóåì |
79 | rtlSrand( ((Dword *)(this->block))[(sizeof(this->block) / sizeof(Dword)) - 1] ); |
79 | rtlSrand( ((Dword *)(this->block))[(sizeof(this->block) / sizeof(Dword)) - 1] ); |
80 | // |
80 | // |
81 | for ( i = 0; i < (sizeof( heroTbl ) * 5); i++ ) |
81 | for ( i = 0; i < (sizeof( heroTbl ) * 5); i++ ) |
82 | { |
82 | { |
83 | // íå òðîãàåì ïîñëåäíèé Dword |
83 | // íå òðîãàåì ïîñëåäíèé Dword |
84 | j = rtlRand() % (sizeof(this->block) - 7); |
84 | j = rtlRand() % (sizeof(this->block) - 7); |
85 | k = ( rtlRand() % 31 ) + 1; |
85 | k = ( rtlRand() % 31 ) + 1; |
86 | // |
86 | // |
87 | bPtr = this->block + j; |
87 | bPtr = this->block + j; |
88 | // |
88 | // |
89 | __asm{ |
89 | __asm{ |
90 | mov edx, bPtr |
90 | mov edx, bPtr |
91 | mov ecx, k |
91 | mov ecx, k |
92 | mov eax, [edx] |
92 | mov eax, [edx] |
93 | bswap eax |
93 | bswap eax |
94 | ror eax, cl |
94 | ror eax, cl |
95 | mov [edx], eax |
95 | mov [edx], eax |
96 | } |
96 | } |
97 | } |
97 | } |
98 | // |
98 | // |
99 | rtlSrand( kos_GetSystemClock() ); |
99 | rtlSrand( kos_GetSystemClock() ); |
100 | } |
100 | } |
101 | // |
101 | // |
102 | return result; |
102 | return result; |
103 | }; |
103 | }; |
104 | // |
104 | // |
105 | bool SaveToDisk() |
105 | bool SaveToDisk() |
106 | { |
106 | { |
107 | int i; |
107 | int i; |
108 | Dword *rndList; |
108 | Dword *rndList; |
109 | Byte *bPtr; |
109 | Byte *bPtr; |
110 | Dword k, keyLock; |
110 | Dword k, keyLock; |
111 | 111 | ||
112 | // |
112 | // |
113 | rndList = new Dword[(sizeof( heroTbl ) * 5) * 2]; |
113 | rndList = new Dword[(sizeof( heroTbl ) * 5) * 2]; |
114 | // |
114 | // |
115 | keyLock = rtlRand(); |
115 | keyLock = rtlRand(); |
116 | // |
116 | // |
117 | for ( i = 0; i < (sizeof( heroTbl ) * 5); i++ ) |
117 | for ( i = 0; i < (sizeof( heroTbl ) * 5); i++ ) |
118 | { |
118 | { |
119 | // |
119 | // |
120 | rndList[i * 2] = rtlRand() % (sizeof(this->block) - 7); |
120 | rndList[i * 2] = rtlRand() % (sizeof(this->block) - 7); |
121 | rndList[(i * 2) + 1] = ( rtlRand() % 31 ) + 1; |
121 | rndList[(i * 2) + 1] = ( rtlRand() % 31 ) + 1; |
122 | } |
122 | } |
123 | // |
123 | // |
124 | for ( i = (sizeof( heroTbl ) * 5) - 1; i >= 0; i-- ) |
124 | for ( i = (sizeof( heroTbl ) * 5) - 1; i >= 0; i-- ) |
125 | { |
125 | { |
126 | // |
126 | // |
127 | bPtr = this->block + rndList[i * 2]; |
127 | bPtr = this->block + rndList[i * 2]; |
128 | k = rndList[(i * 2) + 1]; |
128 | k = rndList[(i * 2) + 1]; |
129 | // |
129 | // |
130 | __asm{ |
130 | __asm{ |
131 | mov edx, bPtr |
131 | mov edx, bPtr |
132 | mov ecx, k |
132 | mov ecx, k |
133 | mov eax, [edx] |
133 | mov eax, [edx] |
134 | rol eax, cl |
134 | rol eax, cl |
135 | bswap eax |
135 | bswap eax |
136 | mov [edx], eax |
136 | mov [edx], eax |
137 | } |
137 | } |
138 | } |
138 | } |
139 | // |
139 | // |
140 | delete rndList; |
140 | delete rndList; |
141 | // |
141 | // |
142 | ((Dword *)(this->block))[(sizeof(this->block) / sizeof(Dword)) - 1] = keyLock; |
142 | ((Dword *)(this->block))[(sizeof(this->block) / sizeof(Dword)) - 1] = keyLock; |
143 | // |
143 | // |
144 | this->fi.rwMode = FO_WRITE; |
144 | this->fi.rwMode = FO_WRITE; |
145 | this->fi.dataCount = 512; |
145 | this->fi.dataCount = 512; |
146 | return kos_FileSystemAccess( &( this->fi) ) == 0; |
146 | return kos_FileSystemAccess( &( this->fi) ) == 0; |
147 | }; |
147 | }; |
148 | }; |
148 | }; |
149 | 149 | ||
150 | 150 | ||
151 | /// |
151 | /// |
152 | hiScoreFile *top10Heroes = NULL; |
152 | hiScoreFile *top10Heroes = NULL; |
153 | 153 | ||
154 | // |
154 | // |
155 | #if LANG == RUS |
155 | #if LANG == RUS |
156 | char Top10WndTitle[] = "Top 10\0"; |
156 | char Top10WndTitle[] = "Top 10\0"; |
157 | char top10str1[] = "ENTER - ¨¬ï Ok."; |
157 | char top10str1[] = "ENTER - ¨¬ï Ok."; |
158 | char top10str2[] = "ESC - ¢ë室 ¢ ¬¥î"; |
158 | char top10str2[] = "ESC - ¢ë室 ¢ ¬¥î"; |
159 | #else |
159 | #else |
160 | char Top10WndTitle[] = "Top 10\0"; |
160 | char Top10WndTitle[] = "Top 10\0"; |
161 | char top10str1[] = "Enter - name Ok."; |
161 | char top10str1[] = "Enter - name Ok."; |
162 | char top10str2[] = "Esc - leave to menu"; |
162 | char top10str2[] = "Esc - leave to menu"; |
163 | #endif |
163 | #endif |
164 | int enterName = -1; |
164 | int enterName = -1; |
165 | int enterCharNdx = 0; |
165 | int enterCharNdx = 0; |
166 | 166 | ||
167 | 167 | ||
168 | // |
168 | // |
169 | void ReleaseTop10() |
169 | void __cdecl ReleaseTop10() |
170 | { |
170 | { |
171 | // |
171 | // |
172 | if ( top10Heroes != NULL ) |
172 | if ( top10Heroes != NULL ) |
173 | { |
173 | { |
174 | // |
174 | // |
175 | memcpy( top10Heroes->block, heroTbl, sizeof(heroTbl) ); |
175 | memcpy( top10Heroes->block, heroTbl, sizeof(heroTbl) ); |
176 | // |
176 | // |
177 | top10Heroes->SaveToDisk(); |
177 | top10Heroes->SaveToDisk(); |
178 | // |
178 | // |
179 | delete top10Heroes; |
179 | delete top10Heroes; |
180 | } |
180 | } |
181 | } |
181 | } |
182 | 182 | ||
183 | 183 | ||
184 | // |
184 | // |
185 | void PrepareTop10() |
185 | void PrepareTop10() |
186 | { |
186 | { |
187 | // |
187 | // |
188 | top10Heroes = new hiScoreFile; |
188 | top10Heroes = new hiScoreFile; |
189 | // |
189 | // |
190 | atexit( ReleaseTop10 ); |
190 | atexit( ReleaseTop10 ); |
191 | // |
191 | // |
192 | if ( top10Heroes->LoadFromDisk() ) |
192 | if ( top10Heroes->LoadFromDisk() ) |
193 | { |
193 | { |
194 | // |
194 | // |
195 | memcpy( heroTbl, top10Heroes->block, sizeof(heroTbl) ); |
195 | memcpy( heroTbl, top10Heroes->block, sizeof(heroTbl) ); |
196 | } |
196 | } |
197 | } |
197 | } |
198 | 198 | ||
199 | 199 | ||
200 | // |
200 | // |
201 | void SetUpTop10() |
201 | void SetUpTop10() |
202 | { |
202 | { |
203 | int i, j; |
203 | int i, j; |
204 | Byte keyCode; |
204 | Byte keyCode; |
205 | 205 | ||
206 | // |
206 | // |
207 | while ( kos_CheckForEvent() == 2 ) kos_GetKey( keyCode ); |
207 | while ( kos_CheckForEvent() == 2 ) kos_GetKey( keyCode ); |
208 | // |
208 | // |
209 | kos_SetKeyboardDataMode( KM_CHARS ); |
209 | kos_SetKeyboardDataMode( KM_CHARS ); |
210 | // |
210 | // |
211 | kos_ChangeWindow( -1, -1, TOP10_WND_SIZE_X, TOP10_WND_SIZE_Y ); |
211 | kos_ChangeWindow( -1, -1, TOP10_WND_SIZE_X, TOP10_WND_SIZE_Y ); |
212 | // |
212 | // |
213 | for ( i = 0; i < TOP_TBL_SIZE; i++ ) |
213 | for ( i = 0; i < TOP_TBL_SIZE; i++ ) |
214 | { |
214 | { |
215 | // |
215 | // |
216 | if ( heroTbl[i].score < playerScore ) |
216 | if ( heroTbl[i].score < playerScore ) |
217 | { |
217 | { |
218 | // |
218 | // |
219 | for ( j = TOP_TBL_SIZE - 1; j > i; j-- ) |
219 | for ( j = TOP_TBL_SIZE - 1; j > i; j-- ) |
220 | { |
220 | { |
221 | // |
221 | // |
222 | heroTbl[j] = heroTbl[j-1]; |
222 | heroTbl[j] = heroTbl[j-1]; |
223 | } |
223 | } |
224 | // |
224 | // |
225 | heroTbl[i].ClearName(); |
225 | heroTbl[i].ClearName(); |
226 | heroTbl[i].score = playerScore; |
226 | heroTbl[i].score = playerScore; |
227 | // |
227 | // |
228 | enterName = i; |
228 | enterName = i; |
229 | enterCharNdx = 0; |
229 | enterCharNdx = 0; |
230 | // |
230 | // |
231 | break; |
231 | break; |
232 | } |
232 | } |
233 | } |
233 | } |
234 | } |
234 | } |
235 | 235 | ||
236 | 236 | ||
237 | // |
237 | // |
238 | void DrawTop10Window() |
238 | void DrawTop10Window() |
239 | { |
239 | { |
240 | int i; |
240 | int i; |
241 | 241 | ||
242 | // |
242 | // |
243 | kos_DefineAndDrawWindow( |
243 | kos_DefineAndDrawWindow( |
244 | 100, 100, |
244 | 100, 100, |
245 | TOP10_WND_SIZE_X, TOP10_WND_SIZE_Y, |
245 | TOP10_WND_SIZE_X, TOP10_WND_SIZE_Y, |
246 | 0x34, 0, // Relative coordinates, Skinned fixed size window, window has caption |
246 | 0x34, 0, // Relative coordinates, Skinned fixed size window, window has caption |
247 | 0, 0x2040A0, |
247 | 0, 0x2040A0, |
248 | Top10WndTitle |
248 | Top10WndTitle |
249 | ); |
249 | ); |
250 | 250 | ||
251 | kos_ChangeWindowCaption(Top10WndTitle); |
251 | kos_ChangeWindowCaption(Top10WndTitle); |
252 | 252 | ||
253 | for ( i = 0; i < TOP_TBL_SIZE; i++ ) |
253 | for ( i = 0; i < TOP_TBL_SIZE; i++ ) |
254 | { |
254 | { |
255 | // |
255 | // |
256 | kos_WriteTextToWindow( |
256 | kos_WriteTextToWindow( |
257 | 6, 0 + 2 + (i * 10), |
257 | 6, 0 + 2 + (i * 10), |
258 | 0x0, enterName != i ? 0xFFFFFF : 0x00FF00, |
258 | 0x0, enterName != i ? 0xFFFFFF : 0x00FF00, |
259 | heroTbl[i].name, |
259 | heroTbl[i].name, |
260 | sizeof( heroTbl[0].name ) |
260 | sizeof( heroTbl[0].name ) |
261 | ); |
261 | ); |
262 | // |
262 | // |
263 | kos_DisplayNumberToWindow( |
263 | kos_DisplayNumberToWindow( |
264 | heroTbl[i].score, |
264 | heroTbl[i].score, |
265 | 8, |
265 | 8, |
266 | 112, 0 + 2 + (i * 10), |
266 | 112, 0 + 2 + (i * 10), |
267 | 0xFFFF55, |
267 | 0xFFFF55, |
268 | nbDecimal, |
268 | nbDecimal, |
269 | false |
269 | false |
270 | ); |
270 | ); |
271 | } |
271 | } |
272 | // |
272 | // |
273 | kos_WriteTextToWindow( |
273 | kos_WriteTextToWindow( |
274 | 6, 0 + 6 + (i * 10), |
274 | 6, 0 + 6 + (i * 10), |
275 | 0x10, 0x1060D0, |
275 | 0x10, 0x1060D0, |
276 | enterName >= 0 ? top10str1 : top10str2, |
276 | enterName >= 0 ? top10str1 : top10str2, |
277 | enterName >= 0 ? sizeof(top10str1) - 1 : sizeof(top10str2) - 1 |
277 | enterName >= 0 ? sizeof(top10str1) - 1 : sizeof(top10str2) - 1 |
278 | ); |
278 | ); |
279 | } |
279 | } |
280 | 280 | ||
281 | 281 | ||
282 | // èãðîâîé ïðîöåññ |
282 | // èãðîâîé ïðîöåññ |
283 | void Top10Loop() |
283 | void Top10Loop() |
284 | { |
284 | { |
285 | Byte keyCode; |
285 | Byte keyCode; |
286 | Dword buttonID; |
286 | Dword buttonID; |
287 | 287 | ||
288 | // |
288 | // |
289 | SetUpTop10(); |
289 | SetUpTop10(); |
290 | // |
290 | // |
291 | while ( true ) |
291 | while ( true ) |
292 | { |
292 | { |
293 | switch ( kos_WaitForEvent() ) |
293 | switch ( kos_WaitForEvent() ) |
294 | { |
294 | { |
295 | // |
295 | // |
296 | case 1: |
296 | case 1: |
297 | DrawTop10Window(); |
297 | DrawTop10Window(); |
298 | break; |
298 | break; |
299 | // |
299 | // |
300 | case 2: |
300 | case 2: |
301 | // |
301 | // |
302 | kos_GetKey( keyCode ); |
302 | kos_GetKey( keyCode ); |
303 | // |
303 | // |
304 | if ( enterName < 0 ) |
304 | if ( enterName < 0 ) |
305 | { |
305 | { |
306 | // |
306 | // |
307 | if ( keyCode == 0x1b ) |
307 | if ( keyCode == 0x1b ) |
308 | { |
308 | { |
309 | // |
309 | // |
310 | return; |
310 | return; |
311 | } |
311 | } |
312 | } |
312 | } |
313 | else |
313 | else |
314 | { |
314 | { |
315 | // |
315 | // |
316 | switch ( keyCode ) |
316 | switch ( keyCode ) |
317 | { |
317 | { |
318 | // |
318 | // |
319 | case 13: |
319 | case 13: |
320 | // |
320 | // |
321 | enterName = -1; |
321 | enterName = -1; |
322 | break; |
322 | break; |
323 | // |
323 | // |
324 | case 8: |
324 | case 8: |
325 | // |
325 | // |
326 | if ( enterCharNdx > 0 ) |
326 | if ( enterCharNdx > 0 ) |
327 | { |
327 | { |
328 | // |
328 | // |
329 | heroTbl[enterName].name[--enterCharNdx] = '.'; |
329 | heroTbl[enterName].name[--enterCharNdx] = '.'; |
330 | } |
330 | } |
331 | break; |
331 | break; |
332 | // |
332 | // |
333 | default: |
333 | default: |
334 | if ( keyCode >= 0x20 ) |
334 | if ( keyCode >= 0x20 ) |
335 | { |
335 | { |
336 | // |
336 | // |
337 | heroTbl[enterName].name[enterCharNdx++] = keyCode; |
337 | heroTbl[enterName].name[enterCharNdx++] = keyCode; |
338 | // |
338 | // |
339 | if ( enterCharNdx >= sizeof(heroTbl[0].name) ) |
339 | if ( enterCharNdx >= sizeof(heroTbl[0].name) ) |
340 | { |
340 | { |
341 | // |
341 | // |
342 | enterName = -1; |
342 | enterName = -1; |
343 | } |
343 | } |
344 | } |
344 | } |
345 | break; |
345 | break; |
346 | } |
346 | } |
347 | // |
347 | // |
348 | DrawTop10Window(); |
348 | DrawTop10Window(); |
349 | } |
349 | } |
350 | // |
350 | // |
351 | break; |
351 | break; |
352 | 352 | ||
353 | case 3: |
353 | case 3: |
354 | if ( kos_GetButtonID( buttonID ) ) |
354 | if ( kos_GetButtonID( buttonID ) ) |
355 | { |
355 | { |
356 | // |
356 | // |
357 | switch ( buttonID ) |
357 | switch ( buttonID ) |
358 | { |
358 | { |
359 | // |
359 | // |
360 | case 1: |
360 | case 1: |
361 | kos_ExitApp(); |
361 | kos_ExitApp(); |
362 | default: |
362 | default: |
363 | break; |
363 | break; |
364 | } |
364 | } |
365 | } |
365 | } |
366 | 366 | ||
367 | default: |
367 | default: |
368 | break; |
368 | break; |
369 | } |
369 | } |
370 | } |
370 | } |
371 | }>>>>>>> |
371 | }>>>>>>> |