Rev 5276 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
5276 | ZblCoder | 1 | #include "kosSyst.h" |
2 | #include "func.h" |
||
3 | #include |
||
4 | |||
5 | char kosCmdLine[257]; |
||
6 | char kosExePath[1024]; |
||
7 | extern "C" char exeStack[]; |
||
8 | char exeStack[16384]; |
||
9 | |||
10 | #define atexitBufferSize 32 |
||
11 | |||
12 | #ifndef SMALLLIBC_NO_ATEXIT |
||
13 | // |
||
14 | void (__cdecl *atExitList[atexitBufferSize])(); |
||
15 | int atExitFnNum = 0; |
||
16 | // |
||
17 | int __cdecl atexit( void (__cdecl *func )( void )) |
||
18 | { |
||
19 | // |
||
20 | if ( atExitFnNum < atexitBufferSize ) |
||
21 | { |
||
22 | // |
||
23 | atExitList[atExitFnNum++] = func; |
||
24 | return 0; |
||
25 | } |
||
26 | else |
||
27 | { |
||
28 | return 1; |
||
29 | } |
||
30 | } |
||
31 | #endif |
||
32 | |||
33 | float Point::Lingrh() |
||
34 | { |
||
35 | return sqrtd(this->X * this->X + this->Y * this->Y); |
||
36 | } |
||
37 | |||
38 | float Point::Angle() |
||
39 | { |
||
40 | return atan2(this->X, this->Y); |
||
41 | } |
||
42 | |||
43 | // |
||
44 | Dword RandomSeed = 0; |
||
45 | // |
||
46 | void rtlSrand( Dword seed ) |
||
47 | { |
||
48 | RandomSeed = seed; |
||
49 | } |
||
50 | // |
||
51 | Dword rtlRand( void ) |
||
52 | { |
||
53 | //маска 0x80000776 |
||
54 | |||
55 | Dword dwi, i; |
||
56 | |||
57 | for ( i = 0; i < 32; i++ ) |
||
58 | { |
||
59 | |||
60 | dwi = RandomSeed & 0x80000776; |
||
61 | |||
62 | __asm{ |
||
63 | mov eax, dwi |
||
64 | mov edx, eax |
||
65 | bswap eax |
||
66 | xor eax, edx |
||
67 | xor al, ah |
||
68 | setpo al |
||
69 | movzx eax, al |
||
70 | mov dwi, eax |
||
71 | } |
||
72 | |||
73 | RandomSeed = ( RandomSeed << 1 ) | ( dwi & 1 ); |
||
74 | } |
||
75 | |||
76 | return RandomSeed; |
||
77 | } |
||
78 | |||
79 | void* __cdecl memcpy( void *dst, const void *src, size_t bytesCount ) |
||
80 | { |
||
81 | __asm{ |
||
82 | mov edi, dst |
||
83 | mov eax, dst |
||
84 | mov esi, src |
||
85 | mov ecx, bytesCount |
||
86 | rep movsb |
||
87 | } |
||
88 | } |
||
89 | |||
90 | // |
||
91 | void memset( Byte *dst, Byte filler, Dword count ) |
||
92 | { |
||
93 | // |
||
94 | __asm{ |
||
95 | mov edi, dst |
||
96 | mov al, filler |
||
97 | mov ecx, count |
||
98 | rep stosb |
||
99 | } |
||
100 | } |
||
101 | |||
102 | |||
103 | // |
||
104 | Dword rtlInterlockedExchange( Dword *target, Dword value ) |
||
105 | { |
||
106 | // Dword result; |
||
107 | |||
108 | // |
||
109 | __asm{ |
||
110 | mov eax, value |
||
111 | mov ebx, target |
||
112 | xchg eax, [ebx] |
||
113 | // mov result, eax |
||
114 | } |
||
115 | // |
||
116 | // return result; |
||
117 | } |
||
118 | |||
119 | |||
120 | ////////////////////////////////////////////////////////////////////// |
||
121 | // |
||
122 | // копирование строки |
||
123 | // |
||
124 | |||
125 | char * __cdecl strcpy( char *target, const char *source ) |
||
126 | { |
||
127 | char *result = target; |
||
128 | |||
129 | while( target[0] = source[0] ) |
||
130 | { |
||
131 | target++; |
||
132 | source++; |
||
133 | } |
||
134 | |||
135 | return result; |
||
136 | } |
||
137 | |||
138 | |||
139 | ////////////////////////////////////////////////////////////////////// |
||
140 | // |
||
141 | // реверсивный поиск символа |
||
142 | // |
||
143 | |||
144 | char * __cdecl strrchr( const char * string, int c ) |
||
145 | { |
||
146 | char *cPtr; |
||
147 | |||
148 | // |
||
149 | for ( cPtr = (char *)string + strlen( string ); cPtr >= string; cPtr-- ) |
||
150 | { |
||
151 | // |
||
152 | if ( *cPtr == c ) return cPtr; |
||
153 | } |
||
154 | // |
||
155 | return NULL; |
||
156 | } |
||
157 | |||
158 | |||
159 | ////////////////////////////////////////////////////////////////////// |
||
160 | // |
||
161 | // определение длины строки |
||
162 | // |
||
163 | |||
164 | int __cdecl strlen( const char *line ) |
||
165 | { |
||
166 | int i; |
||
167 | |||
168 | for( i=0; line[i] != 0; i++ ); |
||
169 | return i; |
||
170 | } |
||
171 | |||
172 | |||
173 | |||
174 | ////////////////////////////////////////////////////////////////////// |
||
175 | // |
||
176 | // перевод шестнадцатиричного числа в символ |
||
177 | // |
||
178 | |||
179 | unsigned int num2hex( unsigned int num ) |
||
180 | { |
||
181 | if( num < 10 ) |
||
182 | return num + '0'; |
||
183 | return num - 10 + 'A'; |
||
184 | } |
||
185 | |||
186 | |||
187 | inline void __declspec(noreturn) kos_sysfuncm1(void) |
||
188 | { |
||
189 | __asm or eax, -1 |
||
190 | __asm int 0x40 |
||
191 | } |
||
192 | |||
193 | // функция -1 завершения процесса |
||
194 | void kos_ExitApp() |
||
195 | { |
||
196 | #ifndef SMALLLIBC_NO_ATEXIT |
||
197 | int i; |
||
198 | |||
199 | // |
||
200 | for ( i = atExitFnNum - 1; i >= 0; i-- ) |
||
201 | { |
||
202 | // |
||
203 | atExitList[i](); |
||
204 | } |
||
205 | #endif |
||
206 | // |
||
207 | kos_sysfuncm1(); |
||
208 | } |
||
209 | |||
210 | static void __declspec(noinline) __fastcall kos_sysfunc0(Dword _ecx, Dword _edx, Dword _ebx, Dword _esi, Dword _edi) |
||
211 | { |
||
212 | __asm xor eax, eax |
||
213 | __asm mov ebx, _ebx |
||
214 | __asm mov esi, _esi |
||
215 | __asm mov edi, _edi |
||
216 | __asm int 0x40 |
||
217 | } |
||
218 | |||
219 | // функция 0 |
||
220 | void kos_DefineAndDrawWindow( |
||
221 | Word x, Word y, |
||
222 | Word sizeX, Word sizeY, |
||
223 | Byte mainAreaType, |
||
224 | Dword mainAreaColour, |
||
225 | Byte headerType, |
||
226 | Dword headerColour, |
||
227 | Dword borderColour |
||
228 | ) |
||
229 | { |
||
230 | Dword arg1, arg2, arg3, arg4; |
||
231 | |||
232 | // |
||
233 | arg1 = ( x << 16 ) + sizeX; |
||
234 | arg2 = ( y << 16 ) + sizeY; |
||
235 | arg3 = ( mainAreaType << 24 ) | mainAreaColour; |
||
236 | arg4 = ( headerType << 24 ) | headerColour; |
||
237 | // |
||
238 | kos_sysfunc0(arg2, arg3, arg1, arg4, borderColour); |
||
239 | } |
||
240 | |||
241 | |||
242 | // функция 1 поставить точку |
||
243 | void kos_PutPixel( Dword x, Dword y, Dword colour ) |
||
244 | { |
||
245 | // |
||
246 | __asm{ |
||
247 | push 1 |
||
248 | pop eax |
||
249 | mov ebx, x |
||
250 | mov ecx, y |
||
251 | mov edx, colour |
||
252 | int 0x40 |
||
253 | } |
||
254 | } |
||
255 | |||
256 | inline Dword kos_sysfunc2(void) |
||
257 | { |
||
258 | __asm push 2 |
||
259 | __asm pop eax |
||
260 | __asm int 0x40 |
||
261 | } |
||
262 | |||
263 | // функция 2 получить код нажатой клавиши |
||
264 | bool kos_GetKey( Byte &keyCode ) |
||
265 | { |
||
266 | Dword result = kos_sysfunc2(); |
||
267 | // |
||
268 | keyCode = result >> 8; |
||
269 | // |
||
270 | return ( result & 0xFF ) == 0; |
||
271 | } |
||
272 | |||
273 | |||
274 | // функция 3 получить время |
||
275 | Dword kos_GetSystemClock() |
||
276 | { |
||
277 | // Dword result; |
||
278 | |||
279 | // |
||
280 | __asm{ |
||
281 | push 3 |
||
282 | pop eax |
||
283 | int 0x40 |
||
284 | // mov result, eax |
||
285 | } |
||
286 | // |
||
287 | // return result; |
||
288 | } |
||
289 | |||
290 | static void __declspec(noinline) __fastcall kos_sysfunc4(Dword _ecx, const char* _edx, Dword _ebx, Dword _esi) |
||
291 | { |
||
292 | __asm push 4 |
||
293 | __asm pop eax |
||
294 | __asm mov ebx, [_ebx] |
||
295 | __asm mov esi, [_esi] |
||
296 | __asm int 0x40 |
||
297 | } |
||
298 | |||
299 | // функция 4 |
||
300 | void kos_WriteTextToWindow( |
||
301 | Word x, |
||
302 | Word y, |
||
303 | Byte fontType, |
||
304 | Dword textColour, |
||
305 | const char *textPtr, |
||
306 | Dword textLen |
||
307 | ) |
||
308 | { |
||
309 | Dword arg1, arg2; |
||
310 | |||
311 | // |
||
312 | arg1 = ( x << 16 ) | y; |
||
313 | arg2 = ( fontType << 24 ) | textColour; |
||
314 | // |
||
315 | kos_sysfunc4(arg2, textPtr, arg1, textLen); |
||
316 | } |
||
317 | |||
318 | // функция 5 пауза, в сотых долях секунды |
||
319 | void kos_Pause( Dword value ) |
||
320 | { |
||
321 | // |
||
322 | __asm{ |
||
323 | push 5 |
||
324 | pop eax |
||
325 | mov ebx, value |
||
326 | int 0x40 |
||
327 | } |
||
328 | } |
||
329 | |||
330 | // функция 7 нарисовать изображение |
||
331 | void kos_PutImage( RGB * imagePtr, Word sizeX, Word sizeY, Word x, Word y ) |
||
332 | { |
||
333 | Dword arg1, arg2; |
||
334 | |||
335 | // |
||
336 | arg1 = ( sizeX << 16 ) | sizeY; |
||
337 | arg2 = ( x << 16 ) | y; |
||
338 | // |
||
339 | __asm{ |
||
340 | push 7 |
||
341 | pop eax |
||
342 | mov ebx, imagePtr |
||
343 | mov ecx, arg1 |
||
344 | mov edx, arg2 |
||
345 | int 0x40 |
||
346 | } |
||
347 | } |
||
348 | |||
349 | |||
350 | |||
351 | // функция 8 определить кнопку |
||
352 | void kos_DefineButton( Word x, Word y, Word sizeX, Word sizeY, Dword buttonID, Dword colour ) |
||
353 | { |
||
354 | Dword arg1, arg2; |
||
355 | |||
356 | // |
||
357 | arg1 = ( x << 16 ) | sizeX; |
||
358 | arg2 = ( y << 16 ) | sizeY; |
||
359 | // |
||
360 | __asm{ |
||
361 | push 8 |
||
362 | pop eax |
||
363 | mov ebx, arg1 |
||
364 | mov ecx, arg2 |
||
365 | mov edx, buttonID |
||
366 | mov esi, colour |
||
367 | int 0x40 |
||
368 | } |
||
369 | } |
||
370 | |||
371 | |||
372 | // функция 9 - информация о процессе |
||
373 | Dword kos_ProcessInfo( sProcessInfo *targetPtr, Dword processID ) |
||
374 | { |
||
375 | // Dword result; |
||
376 | |||
377 | // |
||
378 | __asm{ |
||
379 | push 9 |
||
380 | pop eax |
||
381 | mov ebx, targetPtr |
||
382 | mov ecx, processID |
||
383 | int 0x40 |
||
384 | // mov result, eax |
||
385 | } |
||
386 | // |
||
387 | // return result; |
||
388 | } |
||
389 | |||
390 | |||
391 | // функция 10 |
||
392 | Dword kos_WaitForEvent() |
||
393 | { |
||
394 | // Dword result; |
||
395 | |||
396 | __asm{ |
||
397 | push 10 |
||
398 | pop eax |
||
399 | int 0x40 |
||
400 | // mov result, eax |
||
401 | } |
||
402 | |||
403 | // return result; |
||
404 | } |
||
405 | |||
406 | |||
407 | // функция 11 |
||
408 | Dword kos_CheckForEvent() |
||
409 | { |
||
410 | // Dword result; |
||
411 | |||
412 | __asm{ |
||
413 | push 11 |
||
414 | pop eax |
||
415 | int 0x40 |
||
416 | // mov result, eax |
||
417 | } |
||
418 | |||
419 | // return result; |
||
420 | } |
||
421 | |||
422 | |||
423 | // функция 12 |
||
424 | void kos_WindowRedrawStatus( Dword status ) |
||
425 | { |
||
426 | __asm{ |
||
427 | push 12 |
||
428 | pop eax |
||
429 | mov ebx, status |
||
430 | int 0x40 |
||
431 | } |
||
432 | } |
||
433 | |||
434 | |||
435 | // функция 13 нарисовать полосу |
||
436 | void kos_DrawBar( Word x, Word y, Word sizeX, Word sizeY, Dword colour ) |
||
437 | { |
||
438 | Dword arg1, arg2; |
||
439 | |||
440 | // |
||
441 | arg1 = ( x << 16 ) | sizeX; |
||
442 | arg2 = ( y << 16 ) | sizeY; |
||
443 | // |
||
444 | __asm{ |
||
445 | push 13 |
||
446 | pop eax |
||
447 | mov ebx, arg1 |
||
448 | mov ecx, arg2 |
||
449 | mov edx, colour |
||
450 | int 0x40 |
||
451 | } |
||
452 | } |
||
453 | |||
454 | |||
455 | // функция 17 |
||
456 | bool kos_GetButtonID( Dword &buttonID ) |
||
457 | { |
||
458 | Dword result; |
||
459 | |||
460 | // |
||
461 | __asm{ |
||
462 | push 17 |
||
463 | pop eax |
||
464 | int 0x40 |
||
465 | mov result, eax |
||
466 | } |
||
467 | // |
||
468 | buttonID = result >> 8; |
||
469 | // |
||
470 | return (result & 0xFF) == 0; |
||
471 | } |
||
472 | |||
473 | |||
474 | // функция 23 |
||
475 | Dword kos_WaitForEventTimeout( Dword timeOut ) |
||
476 | { |
||
477 | // Dword result; |
||
478 | |||
479 | __asm{ |
||
480 | push 23 |
||
481 | pop eax |
||
482 | mov ebx, timeOut |
||
483 | int 0x40 |
||
484 | // mov result, eax |
||
485 | } |
||
486 | |||
487 | // return result; |
||
488 | } |
||
489 | |||
490 | |||
491 | // получение информации о состоянии "мыши" функция 37 |
||
492 | void kos_GetMouseState( Dword & buttons, int & cursorX, int & cursorY ) |
||
493 | { |
||
494 | Dword mB; |
||
495 | Word curX; |
||
496 | Word curY; |
||
497 | sProcessInfo sPI; |
||
498 | |||
499 | // |
||
500 | __asm{ |
||
501 | push 37 |
||
502 | pop eax |
||
503 | xor ebx, ebx |
||
504 | int 0x40 |
||
505 | mov curY, ax |
||
506 | shr eax, 16 |
||
507 | mov curX, ax |
||
508 | push 37 |
||
509 | pop eax |
||
510 | push 2 |
||
511 | pop ebx |
||
512 | int 0x40 |
||
513 | mov mB, eax |
||
514 | } |
||
515 | // |
||
516 | kos_ProcessInfo( &sPI ); |
||
517 | // |
||
5474 | ZblCoder | 518 | int left = (int)((sPI.processInfo.x_size - 384) / 2); |
519 | int top = sPI.processInfo.y_size - 384 - left; |
||
5276 | ZblCoder | 520 | buttons = mB; |
5474 | ZblCoder | 521 | cursorX = curX - sPI.processInfo.x_start - left; |
522 | cursorY = curY - sPI.processInfo.y_start - top; |
||
5276 | ZblCoder | 523 | } |
524 | |||
525 | |||
526 | // функция 40 установить маску событий |
||
527 | void kos_SetMaskForEvents( Dword mask ) |
||
528 | { |
||
529 | // |
||
530 | __asm{ |
||
531 | push 40 |
||
532 | pop eax |
||
533 | mov ebx, mask |
||
534 | int 0x40 |
||
535 | } |
||
536 | } |
||
537 | |||
538 | |||
539 | // функция 47 вывести в окно приложения число |
||
540 | void kos_DisplayNumberToWindow( |
||
541 | Dword value, |
||
542 | Dword digitsNum, |
||
543 | Word x, |
||
544 | Word y, |
||
545 | Dword colour, |
||
546 | eNumberBase nBase, |
||
547 | bool valueIsPointer |
||
548 | ) |
||
549 | { |
||
550 | Dword arg1, arg2; |
||
551 | |||
552 | // |
||
553 | arg1 = ( valueIsPointer ? 1 : 0 ) | |
||
554 | ( ((Byte)nBase) << 8 ) | |
||
555 | ( ( digitsNum & 0x1F ) << 16 ); |
||
556 | arg2 = ( x << 16 ) | y; |
||
557 | // |
||
558 | __asm{ |
||
559 | push 47 |
||
560 | pop eax |
||
561 | mov ebx, arg1 |
||
562 | mov ecx, value |
||
563 | mov edx, arg2 |
||
564 | mov esi, colour |
||
565 | int 0x40 |
||
566 | } |
||
567 | } |
||
568 | |||
569 | |||
570 | // функция 70 доступ к файловой системе |
||
571 | Dword kos_FileSystemAccess( kosFileInfo *fileInfo ) |
||
572 | { |
||
573 | // Dword result; |
||
574 | |||
575 | // |
||
576 | __asm{ |
||
577 | mov eax, 70 |
||
578 | mov ebx, fileInfo |
||
579 | int 0x40 |
||
580 | // push 70 |
||
581 | // pop eax |
||
582 | // mov ebx, fileInfo |
||
583 | // int 0x40 |
||
584 | // mov result, eax |
||
585 | } |
||
586 | // |
||
587 | // return result; |
||
588 | } |
||
589 | |||
590 | |||
591 | // функция 63 вывод символя в окно отладки |
||
592 | void kos_DebugOutChar( char ccc ) |
||
593 | { |
||
594 | // |
||
595 | __asm{ |
||
596 | push 63 |
||
597 | pop eax |
||
598 | push 1 |
||
599 | pop ebx |
||
600 | mov cl, ccc |
||
601 | int 0x40 |
||
602 | } |
||
603 | } |
||
604 | |||
605 | |||
606 | // функция 66 режим получения данных от клавиатуры |
||
607 | void kos_SetKeyboardDataMode( Dword mode ) |
||
608 | { |
||
609 | // |
||
610 | __asm{ |
||
611 | push 66 |
||
612 | pop eax |
||
613 | push 1 |
||
614 | pop ebx |
||
615 | mov ecx, mode |
||
616 | int 0x40 |
||
617 | } |
||
618 | } |
||
619 | |||
620 | |||
621 | // вывод строки в окно отладки |
||
622 | void rtlDebugOutString( char *str ) |
||
623 | { |
||
624 | // |
||
625 | for ( ; str[0] != 0; str++ ) |
||
626 | { |
||
627 | kos_DebugOutChar( str[0] ); |
||
628 | } |
||
629 | // |
||
630 | kos_DebugOutChar( 13 ); |
||
631 | kos_DebugOutChar( 10 ); |
||
632 | } |
||
633 | |||
634 | |||
635 | // функция 64 изменение количества памяти, выделенной для программы |
||
636 | bool kos_ApplicationMemoryResize( Dword targetSize ) |
||
637 | { |
||
638 | Dword result; |
||
639 | |||
640 | // |
||
641 | __asm{ |
||
642 | push 64 |
||
643 | pop eax |
||
644 | push 1 |
||
645 | pop ebx |
||
646 | mov ecx, targetSize |
||
647 | int 0x40 |
||
648 | mov result, eax |
||
649 | } |
||
650 | // |
||
651 | return result == 0; |
||
652 | } |
||
653 | |||
654 | |||
655 | // функция 67 изменить параметры окна, параметр == -1 не меняется |
||
656 | void kos_ChangeWindow( Dword x, Dword y, Dword sizeX, Dword sizeY ) |
||
657 | { |
||
658 | // |
||
659 | __asm{ |
||
660 | push 67 |
||
661 | pop eax |
||
662 | mov ebx, x |
||
663 | mov ecx, y |
||
664 | mov edx, sizeX |
||
665 | mov esi, sizeY |
||
666 | int 0x40 |
||
667 | } |
||
668 | } |
||
669 | |||
670 | void kos_InitHeap() |
||
671 | { |
||
672 | __asm{ |
||
673 | push 68 |
||
674 | pop eax |
||
675 | push 11 |
||
676 | pop ebx |
||
677 | int 0x40 |
||
678 | } |
||
679 | } |
||
680 | |||
681 | |||
682 | |||
683 | // вызов статических инициализаторов |
||
684 | typedef void (__cdecl *_PVFV)(void); |
||
685 | extern "C" _PVFV __xc_a[]; |
||
686 | extern "C" _PVFV __xc_z[]; |
||
687 | #pragma comment(linker, "/merge:.CRT=.rdata") |
||
688 | // |
||
689 | void __cdecl crtStartUp() |
||
690 | { |
||
691 | #ifndef SMALLLIBC_NO_INIT |
||
692 | // вызываем инициализаторы по списку |
||
693 | for ( _PVFV *pbegin = __xc_a; pbegin < __xc_z; pbegin++ ) |
||
694 | { |
||
695 | // |
||
696 | (**pbegin)(); |
||
697 | } |
||
698 | #endif |
||
699 | // инициализируем генератор случайных чисел |
||
700 | // если надо для приложения, делать это в kos_Main() |
||
701 | //rtlSrand( kos_GetSystemClock() ); |
||
702 | // вызов главной функции приложения |
||
703 | kos_Main(); |
||
704 | // выход |
||
705 | kos_ExitApp(); |
||
706 | }>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>>><>>> |
||
707 |