Subversion Repositories Kolibri OS

Rev

Rev 4481 | Go to most recent revision | Details | Last modification | View Log | RSS feed

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