From 861708b8e9de77c68972dd074b2d419eb2797beb Mon Sep 17 00:00:00 2001 From: "Kirill Lipatov (Leency)" Date: Tue, 6 Jul 2021 12:38:12 +0000 Subject: [PATCH] upload flappybird by zorggish git-svn-id: svn://kolibrios.org@9011 a494cfbc-eb01-0410-851d-a64ba20cac60 --- programs/games/flappybird/Release/flappybird | Bin 0 -> 2067 bytes programs/games/flappybird/flappybird.cpp | 436 +++++++++++ programs/games/flappybird/flappybird.sln | 20 + programs/games/flappybird/flappybird.vcxproj | 73 ++ .../flappybird/flappybird.vcxproj.filters | 27 + programs/games/flappybird/images.hpp | 44 ++ .../games/flappybird/smalllibc/doexe2.asm | 68 ++ programs/games/flappybird/smalllibc/func.cpp | 501 +++++++++++++ programs/games/flappybird/smalllibc/func.h | 129 ++++ programs/games/flappybird/smalllibc/init.asm | 31 + .../games/flappybird/smalllibc/kosFile.cpp | 132 ++++ programs/games/flappybird/smalllibc/kosFile.h | 26 + .../games/flappybird/smalllibc/kosSyst.cpp | 695 ++++++++++++++++++ programs/games/flappybird/smalllibc/kosSyst.h | 217 ++++++ programs/games/flappybird/smalllibc/math2.cpp | 83 +++ .../games/flappybird/smalllibc/mcsmemm.cpp | 373 ++++++++++ programs/games/flappybird/smalllibc/mcsmemm.h | 28 + programs/games/flappybird/smalllibc/mymath.h | 95 +++ .../games/flappybird/smalllibc/purecall.cpp | 11 + .../flappybird/smalllibc/smalllibc.vcxproj | 72 ++ .../smalllibc/smalllibc.vcxproj.filters | 60 ++ .../games/flappybird/smalllibc/sprintf.cpp | 228 ++++++ 22 files changed, 3349 insertions(+) create mode 100644 programs/games/flappybird/Release/flappybird create mode 100644 programs/games/flappybird/flappybird.cpp create mode 100644 programs/games/flappybird/flappybird.sln create mode 100644 programs/games/flappybird/flappybird.vcxproj create mode 100644 programs/games/flappybird/flappybird.vcxproj.filters create mode 100644 programs/games/flappybird/images.hpp create mode 100644 programs/games/flappybird/smalllibc/doexe2.asm create mode 100644 programs/games/flappybird/smalllibc/func.cpp create mode 100644 programs/games/flappybird/smalllibc/func.h create mode 100644 programs/games/flappybird/smalllibc/init.asm create mode 100644 programs/games/flappybird/smalllibc/kosFile.cpp create mode 100644 programs/games/flappybird/smalllibc/kosFile.h create mode 100644 programs/games/flappybird/smalllibc/kosSyst.cpp create mode 100644 programs/games/flappybird/smalllibc/kosSyst.h create mode 100644 programs/games/flappybird/smalllibc/math2.cpp create mode 100644 programs/games/flappybird/smalllibc/mcsmemm.cpp create mode 100644 programs/games/flappybird/smalllibc/mcsmemm.h create mode 100644 programs/games/flappybird/smalllibc/mymath.h create mode 100644 programs/games/flappybird/smalllibc/purecall.cpp create mode 100644 programs/games/flappybird/smalllibc/smalllibc.vcxproj create mode 100644 programs/games/flappybird/smalllibc/smalllibc.vcxproj.filters create mode 100644 programs/games/flappybird/smalllibc/sprintf.cpp diff --git a/programs/games/flappybird/Release/flappybird b/programs/games/flappybird/Release/flappybird new file mode 100644 index 0000000000000000000000000000000000000000..4915cfe8eb7dcf4dbe1ae64f835904339c6f9563 GIT binary patch literal 2067 zcmV+u2<-PuP(w=x82|u*00001Mv*2LLXK;4&&M|Uw75&Bzr9Li2s9MhLylL`=QsrG~kY452JFcY!XX-6ggw^yw z3Esdc2e?DR)r=kBV0k|%Ch69>dOPlH@!a#ePPVrn`Nmtpc!7*eE^DiKl+J0~;sk=T zLHma`(Sq4${7(Au1vxZ8`5-b3LGvj`iD7b^Q&sTaBEK{<50*iPePe;bTmUk#VXuAm!W_r>rG$(aI=grB89F30-C4GF zSVLPwSfd;nd32KYKXAw;HUvHfBjNrf6r2>2$9=&Mc}T%SS?Wl)+#Lt{mNnjvm&5 znQ7aDTn>XrIKtNk>d{tNvIoZ+YdsHkIzV{6&zJOcf3$Vrf*}&(e+h*r3>ysyuPy{* z+Cd-Tw3hY0_m8-}2Lg7KYLbXserY(vfpISsuG)$(uAT|)Cok+2v=qf}X5USJp%AYg zj_`}J?wB0MZS=qa;idD$;=L*3hDQ9FCLLmZH5%mmoz66UOOdjVhKL4_AD`d zW&fz8bWvTFE1g!aR{ISQkwLes54sNdYEm>UvAlUlZk?o>~iI1u` zeaOI3slF5ZK2MZu1iZ+)g}@i0=IJS<50bw2OSNFb5@kY3Oh$fy=2^R8Yc_EnUq^@u(*q>tjmR_5`BX zWq+{18r99PQR#~OLs8|Fe6=-SpYUOylj48!>IVy_aW4}6wT(PY7Kk4tt;KUG(w>}T z64>%aZX`*YFN$2Q+n$lGYi?1AjRj^%9X!*Csi{&N8aZugq(dOR*iI`M1_S_;Em-X@ zFI2#=_<2y2Ha}AFxBZ3MaxC&{e^bCcf+8(bJ%h>SZmc!@(hG*3Nu1WI=VJ zC{lq{P6NfFYB>H1xg4e#8fkAEA8gND(n|7g@^BS#pq)AN~L5(xlo(&LWo*%Nc zx-XJ07`}ZbYHlAQZ%PoX*$c@)QPBCdW=k?+Pc{9AYPpkV<{0ot)vY{v%4B!`=A zIMKGTbVyV z!h_o*fWRg-sJ1$@1>$+2y(FkStfIYjP5f>%Zxz{YG=UI8&`jN4ij}nf{XK2ZtY(_r z2)&=FH_qO-AirH@iUhzs>69+csdr33P#&plpsl&pDr4>hsh-DBPweM+%zPg&7eNa^ zQuPCk7>7ZPMO0N*>traqI;}8kPij;zu6@241c^FAbzB=s_|LQLHzFP;bEiFlCwCl? z;pY(T@`ByJ@Mmz&YP?4oDg8XyP+voPJgmJSwJ07gGCgIwGLEsc_rPokV7cLdrBi-V z{0Rba7b+A!mtvYg6fJ*Icx>`3{OJnq&XC*we{-9d5irvn?{%%|LGNvIGvU6r1vtx# zQ|A1R?**G63q8X-Y2vcR%K-jVt5zSlo`44s4gSbA(Wnh4YLU&lrO2JNU=P*~mfmw^ zgWytc4PriRS82E!pmArW6JxXdqD33<3CIRicWty&453hOFr$PK54)88q`8>u0;#S}hmOyi1*UxwXPy?Aif?X18+OqfMtT*5PI5+?R00AXV=`{cV literal 0 HcmV?d00001 diff --git a/programs/games/flappybird/flappybird.cpp b/programs/games/flappybird/flappybird.cpp new file mode 100644 index 0000000000..c66c0384d3 --- /dev/null +++ b/programs/games/flappybird/flappybird.cpp @@ -0,0 +1,436 @@ +#include +#include +#include "images.hpp" + +//Global const strings +const char HEADER_STRING[] = "Flappy bird"; +const char CONTROL_STRING[] = "SPACEBAR TO JUMP"; +const char GAMEOVER_STRING[] = "GAMEOVER"; +const char ANY_KEY_STRING[] = "Press any key for restart"; +const char SELECT_SPEED_STRING[] = "select the speed of the game"; +const char FAST_STRING[] = "1 FAST"; +const char SLOW_STRING[] = "2 SLOW"; + +//Global const variables +const int WINDOW_WIDTH = 400; +const int WINDOW_HEIGHT = 400; +const int BORDER_TOP = 24; +const int BORDER_LEFT = 5; +const int BORDER_RIGHT = 5; +const int BORDER_DOWN = 5; + +enum GameState +{ + GAMESTATE_MENU, + GAMESTATE_STARTED, + GAMESTATE_GAMEOVER +}; + +struct ScreenSize +{ + int width; + int height; +}; + +class Bird +{ +public: + static const int sizeX = 17; + static const int sizeY = 12; + static const int x = 100; + int prev_y; + int y; + int acceleration; + + inline void initialize() + { + y = WINDOW_HEIGHT / 2; + acceleration = 0; + } + + inline void move() + { + if (acceleration <= 30) + acceleration += 2; + prev_y = y; + y += acceleration / 10; + } + + inline void jump() + { + acceleration = -50; + } + + inline void draw() + { + kos_PutImage(birdImage, sizeX, sizeY, x, y); + } +}; + +class Tube +{ +public: + static const int width = 50; + static const int gapHeight = 100; + static const int headHeight = 18; + int x; + int gapY; + + inline void randomize() + { + x = WINDOW_WIDTH + 1; + gapY = rtlRand() % 200 + 50; + } + + inline void move() + { + x -= 2; + if (x < -width - 2) + randomize(); + } + + void draw() + { + int offset = x >= 0 ? 0 : -x; + int trim = x + width >= WINDOW_WIDTH - (BORDER_LEFT + BORDER_RIGHT - 1) ? WINDOW_WIDTH - x - width - (BORDER_LEFT + BORDER_RIGHT - 1) : 0; + int trimHead = x + width + 2 >= WINDOW_WIDTH - (BORDER_LEFT + BORDER_RIGHT - 1) ? WINDOW_WIDTH - x - width - 2 - (BORDER_LEFT + BORDER_RIGHT - 1) : 0; + + //top + for (int y = 0; y < gapY - headHeight; ++y) + kos_PutImage(tubeBodyImage + offset, width - offset + trim, 1, x + offset, y); + //head top + for (int y = gapY - headHeight; y < gapY; ++y) + kos_PutImage(tubeHeadImage + (width + 2) * (y - (gapY - headHeight)) + offset, (width + 2) - offset + trimHead, 1, x + offset, y); + //head down + for (int y = gapY + gapHeight; y < gapY + gapHeight + headHeight; ++y) + kos_PutImage(tubeHeadImage + (width + 2) * (y - (gapY + gapHeight)) + offset, (width + 2) - offset + trimHead, 1, x + offset, y); + //down + for (int y = gapY + gapHeight + headHeight; y < WINDOW_HEIGHT - (BORDER_TOP + BORDER_DOWN - 1); ++y) + kos_PutImage(tubeBodyImage + offset, width - offset + trim, 1, x + offset, y); + + } +}; + +//Global variables +int loopDelay; +GameState gameState; +char scoreString[] = "Score: "; +bool scoreChanged; +int score; +Bird bird; +int tubeNumber; +Tube tubes[3]; +int windowX; +int windowY; + +//Function prototypes +void kos_Main(); +void startGame(); +ScreenSize getScreenSize(); +void updateScoreString(); +void WriteBorderedText(Word x, Word y, Byte fontType, Dword textColor, const char* textPtr, Dword textLen, Dword borderColor, int borderSize); +inline bool checkAddScore(Tube tube); +inline bool checkCollision(Tube tube); + +void drawMenuWindow(); +void drawGameWindow(); +void redrawGameWindow(); +void drawGameoverWindow(); + +//Functions + +void startGame() +{ + kos_SetMaskForEvents(0x7); /// 111 in binary + + bird.initialize(); + + score = 0; + memset((Byte*)scoreString + 6, ' ', 3); + updateScoreString(); + + tubeNumber = 1; + tubes[0].randomize(); + + gameState = GAMESTATE_STARTED; + drawGameWindow(); +} + +ScreenSize getScreenSize() +{ + Dword result; + __asm { + push 14 //System function 14 + pop eax + int 0x40 + mov result, eax + } + ScreenSize screenSize; + screenSize.height = (result & 0xFFFF) + 1; //last two bytes + screenSize.width = (result >> 16) + 1; //first two bytes + return screenSize; +} + +void kos_Main() +{ + rtlSrand( kos_GetSystemClock() ); + + //Centring window + ScreenSize screenSize = getScreenSize(); + windowX = (screenSize.width - WINDOW_WIDTH) / 2; + windowY = (screenSize.height - WINDOW_HEIGHT) / 2; + + gameState = GAMESTATE_MENU; + + kos_SetMaskForEvents(0x27); // 100111 in binary + + while( true ) + { + switch (gameState) + { + case GAMESTATE_STARTED: + kos_Pause(loopDelay); + + bird.move(); + + //Adding new tubes + if ((tubeNumber == 1 || tubeNumber == 2) && (tubes[tubeNumber - 1].x < (WINDOW_WIDTH - WINDOW_WIDTH / 3))) + tubes[tubeNumber++].randomize(); + + //Processing all tubes + scoreChanged = false; + for (int i = 0; i < tubeNumber; ++i) + { + //Adding score + if (checkAddScore(tubes[i])) + { + ++score; + scoreChanged = true; + } + + //Check collision with bird + if (checkCollision(tubes[i])) + { + gameState = GAMESTATE_GAMEOVER; + continue; + } + + //Move tube + tubes[i].move(); + } + + if (scoreChanged) + updateScoreString(); + + //Cheking the bird is too high or low + if (bird.y + bird.sizeY > WINDOW_HEIGHT - (BORDER_TOP + BORDER_DOWN - 1) || bird.y < 0) + { + gameState = GAMESTATE_GAMEOVER; + continue; + } + + redrawGameWindow(); + + switch (kos_CheckForEvent()) + { + case 1: + drawGameWindow(); + break; + + case 2: // key pressed + Byte keyCode; + kos_GetKey(keyCode); + if (keyCode == 32) //if pressed key is spacebar + bird.jump(); + break; + + case 3: // button pressed; we have only one button, close + kos_ExitApp(); + } + break; + + case GAMESTATE_GAMEOVER: + drawGameoverWindow(); + + switch (kos_WaitForEvent()) + { + case 1: + drawGameoverWindow(); + break; + + case 2: + startGame(); + break; + + case 3: + kos_ExitApp(); + } + break; + + case GAMESTATE_MENU: + switch (kos_WaitForEvent()) + { + case 1: + drawMenuWindow(); + break; + + case 2: + Byte keyCode; + kos_GetKey(keyCode); + if (keyCode == 0x31 || keyCode == 0x61) //1 or NumPad1 + { + loopDelay = 1; + startGame(); + } + else if (keyCode == 0x32 || keyCode == 0x62) //2 or NumPad2 + { + loopDelay = 2; + startGame(); + } + break; + + case 3: + kos_ExitApp(); + + case 6: + Dword result; + __asm { + push 37 //Function 37 - work with mouse + pop eax + mov ebx, 3 //Subfunction 3 - states and events of the mouse buttons + int 0x40 + mov result, eax + } + result &= 0x100; //bit 8 is set = left button is pressed + if ( result ) + { + Dword coordinates; + __asm { + push 37 //Function 37 - work with mouse + pop eax + mov ebx, 1 //Subfunction 1 - coordinates of the mouse relative to the window + int 0x40 + mov coordinates, eax + } + int clickX = coordinates >> 16; + int clickY = coordinates & 0xFFFF; + if (clickX >= 100 && clickX < 390 && clickY >= 170 && clickY < 230) + { + loopDelay = 1; + startGame(); + } + else if (clickX >= 100 && clickX < 390 && clickY >= 270 && clickY < 330) + { + loopDelay = 2; + startGame(); + } + } + break; + } + break; + } + } +} + +void drawGameWindow() +{ + kos_WindowRedrawStatus(1); + kos_DefineAndDrawWindow(windowX, windowY, WINDOW_WIDTH, WINDOW_HEIGHT, 0x33, 0x00FFFF, 0, 0, (Dword)HEADER_STRING); + bird.draw(); + for (int i = 0; i < tubeNumber; ++i) + tubes[i].draw(); + kos_WriteTextToWindow(10, 10, 0x81, 0x000000, scoreString, 0); + kos_WriteTextToWindow(10, 30, 0x81, 0x000000, CONTROL_STRING, 0); + kos_WindowRedrawStatus(2); +} +void redrawGameWindow() +{ + kos_WindowRedrawStatus(1); + + //cleaning the screen + if (scoreChanged) + kos_DrawBar(80, 10, 50, 15, 0x00FFFF); + if (bird.y > bird.prev_y) + kos_DrawBar(bird.x, bird.prev_y, bird.sizeX, bird.y - bird.prev_y, 0x00FFFF); + else + kos_DrawBar(bird.x, bird.y + bird.sizeY, bird.sizeX, bird.prev_y - bird.y, 0x00FFFF); + + bird.draw(); + for (int i = 0; i < tubeNumber; ++i) + tubes[i].draw(); + + kos_WriteTextToWindow(10, 10, 0x81, 0x000000, scoreString, 0); + kos_WriteTextToWindow(10, 30, 0x81, 0x000000, CONTROL_STRING, 0); + kos_WindowRedrawStatus(2); +} + +void drawGameoverWindow() +{ + kos_WindowRedrawStatus(1); + kos_DefineAndDrawWindow(windowX, windowY, WINDOW_WIDTH, WINDOW_HEIGHT, 0x33, 0x000000, 0, 0, (Dword)HEADER_STRING); + kos_WriteTextToWindow(125, 50, 0x82, 0xFFFFFF, GAMEOVER_STRING, 0); + kos_WriteTextToWindow(135, 100, 0x81, 0xFFFFFF, scoreString, 0); + kos_WriteTextToWindow(50, 150, 0x81, 0xFFFFFF, ANY_KEY_STRING, 0); + kos_WindowRedrawStatus(2); +} + +void WriteBorderedText(Word x, Word y, Byte fontType, Dword textColor, const char *textPtr, Dword textLen, Dword borderColor, int borderSize) +{ + kos_WriteTextToWindow(x - borderSize, y - borderSize, fontType, borderColor, textPtr, textLen); + kos_WriteTextToWindow(x - borderSize, y + borderSize, fontType, borderColor, textPtr, textLen); + kos_WriteTextToWindow(x + borderSize, y - borderSize, fontType, borderColor, textPtr, textLen); + kos_WriteTextToWindow(x + borderSize, y + borderSize, fontType, borderColor, textPtr, textLen); + kos_WriteTextToWindow(x, y, fontType, textColor, textPtr, textLen); +} + +void drawMenuWindow() +{ + kos_WindowRedrawStatus(1); + kos_DefineAndDrawWindow(windowX, windowY, WINDOW_WIDTH, WINDOW_HEIGHT, 0x33, 0x00FFFF, 0, 0, (Dword)HEADER_STRING); + + WriteBorderedText(85, 40, 0x4, 0xFFFFFF, HEADER_STRING, 6, 0x000000, 2); + WriteBorderedText(185, 80, 0x84, 0xFFFFFF, HEADER_STRING + 7, 0, 0x000000, 2); + + RGB* pos = &tubeHeadImage[0]; + for (int x = 100 - 1; x >= 100 - Tube::headHeight; --x) + for (int y = 170; y < 170 + Tube::width + 2; ++y) + { + kos_PutPixel(x, y, (pos->r << 16) + (pos->g << 8) + (pos->b)); //first tube + kos_PutPixel(x, y+100, (pos->r << 16) + (pos->g << 8) + (pos->b)); //second tube + ++pos; + } + + //First button + for(int x = 100; x < WINDOW_WIDTH - (BORDER_LEFT + BORDER_RIGHT - 1); ++x) + kos_PutImage(tubeBodyImage, 1, Tube::width, x, 170); + WriteBorderedText(140, 185, 0x82, 0x000000, FAST_STRING, 0, 0xFFFFFF, 1); + + //Second button + for (int x = 100; x < WINDOW_WIDTH - (BORDER_LEFT + BORDER_RIGHT - 1); ++x) + kos_PutImage(tubeBodyImage, 1, Tube::width, x, 270); + WriteBorderedText(140, 285, 0x82, 0x000000, SLOW_STRING, 0, 0xFFFFFF, 1); + + kos_WindowRedrawStatus(2); +} + +inline bool checkCollision(Tube tube) +{ + return ((tube.x <= (bird.x + bird.sizeX) && tube.x + tube.width >= bird.x) && (bird.y <= tube.gapY || bird.y + bird.sizeY >= tube.gapY + tube.gapHeight)); +} + +inline bool checkAddScore(Tube tube) +{ + //int diff = bird.x - (tube.x + tube.width); + //return diff == 0 || diff == 1; + return ((bird.x - (tube.x + tube.width)) >> 1) == 0; +} + +void updateScoreString() +{ + int temp = score; + int index = 9; + do { + scoreString[index--] = temp % 10 + '0'; + temp /= 10; + } while (temp > 0); +} \ No newline at end of file diff --git a/programs/games/flappybird/flappybird.sln b/programs/games/flappybird/flappybird.sln new file mode 100644 index 0000000000..923726e2ee --- /dev/null +++ b/programs/games/flappybird/flappybird.sln @@ -0,0 +1,20 @@ +Microsoft Visual Studio Solution File, Format Version 11.00 +# Visual Studio 2010 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "flappybird", "flappybird.vcxproj", "{0556BA3E-9447-4000-8613-91AD1CD750D7}" + ProjectSection(ProjectDependencies) = postProject + {0D291390-1953-4E1F-BBE2-57F12AFF3214} = {0D291390-1953-4E1F-BBE2-57F12AFF3214} + EndProjectSection +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "smalllibc", "smalllibc\smalllibc.vcxproj", "{0D291390-1953-4E1F-BBE2-57F12AFF3214}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Release|Win32 = Release|Win32 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {0556BA3E-9447-4000-8613-91AD1CD750D7}.Release|Win32.ActiveCfg = Release|Win32 + {0556BA3E-9447-4000-8613-91AD1CD750D7}.Release|Win32.Build.0 = Release|Win32 + {0D291390-1953-4E1F-BBE2-57F12AFF3214}.Release|Win32.ActiveCfg = Release|Win32 + {0D291390-1953-4E1F-BBE2-57F12AFF3214}.Release|Win32.Build.0 = Release|Win32 + EndGlobalSection +EndGlobal diff --git a/programs/games/flappybird/flappybird.vcxproj b/programs/games/flappybird/flappybird.vcxproj new file mode 100644 index 0000000000..f24b1a35f4 --- /dev/null +++ b/programs/games/flappybird/flappybird.vcxproj @@ -0,0 +1,73 @@ + + + + + Release + Win32 + + + + {0556BA3E-9447-4000-8613-91AD1CD750D7} + flappybird + + + + Application + false + true + MultiByte + + + + + + + + + + false + false + + + + Level3 + MinSpace + true + true + false + MultiThreaded + smalllibc + true + Size + false + + + false + true + true + true + fakeEntry + Native + /merge:.data=.text /merge:.rdata=.text /merge:.1seg=.text /section:.bss,E %(AdditionalOptions) + true + + + set EXENAME=$(TargetPath) +"C:\kolibri\fasm\FASM.EXE" $(ProjectDir)smalllibc\doexe2.asm $(TargetDir)$(TargetName) + + + + + + + + {0D291390-1953-4E1F-BBE2-57F12AFF3214} + + + + + + + + + \ No newline at end of file diff --git a/programs/games/flappybird/flappybird.vcxproj.filters b/programs/games/flappybird/flappybird.vcxproj.filters new file mode 100644 index 0000000000..af11905d51 --- /dev/null +++ b/programs/games/flappybird/flappybird.vcxproj.filters @@ -0,0 +1,27 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Файлы исходного кода + + + + + Файлы исходного кода + + + \ No newline at end of file diff --git a/programs/games/flappybird/images.hpp b/programs/games/flappybird/images.hpp new file mode 100644 index 0000000000..2992929afe --- /dev/null +++ b/programs/games/flappybird/images.hpp @@ -0,0 +1,44 @@ +#pragma once + +#include + +static RGB birdImage[] = { +0x00FFFF, 0x00FFFF, 0x00FFFF, 0x00FFFF, 0x00FFFF, 0x00FFFF, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x00FFFF, 0x00FFFF, 0x00FFFF, 0x00FFFF, +0x00FFFF, 0x00FFFF, 0x00FFFF, 0x00FFFF, 0x000000, 0x000000, 0xFFFF00, 0xFFFF00, 0xFFFF00, 0xFFFF00, 0x000000, 0xFFFFFF, 0xFFFFFF, 0x000000, 0x00FFFF, 0x00FFFF, 0x00FFFF, +0x00FFFF, 0x00FFFF, 0x00FFFF, 0x000000, 0xFFFF00, 0xFFFF00, 0xFFFF00, 0xFFFF00, 0xFFFF00, 0x000000, 0xFFFFFF, 0xFFFFFF, 0xFFFFFF, 0xFFFFFF, 0x000000, 0x00FFFF, 0x00FFFF, +0x00FFFF, 0x000000, 0x000000, 0x000000, 0x000000, 0xFFFF00, 0xFFFF00, 0xFFFF00, 0xFFFF00, 0x000000, 0xFFFFFF, 0xFFFFFF, 0x000000, 0x000000, 0xFFFFFF, 0x000000, 0x00FFFF, +0x000000, 0xE0E080, 0xE0E080, 0xE0E080, 0xE0E080, 0x000000, 0xFFFF00, 0xFFFF00, 0xFFFF00, 0x000000, 0xFFFFFF, 0xFFFFFF, 0x000000, 0x000000, 0xFFFFFF, 0x000000, 0x00FFFF, +0x000000, 0xE0E080, 0xE0E080, 0xE0E080, 0xE0E080, 0xE0E080, 0x000000, 0xFFFF00, 0xFFFF00, 0xFFFF00, 0x000000, 0xFFFFFF, 0xFFFFFF, 0xFFFFFF, 0xFFFFFF, 0x000000, 0x00FFFF, +0x000000, 0xE0E080, 0xE0E080, 0xE0E080, 0xE0E080, 0xE0E080, 0x000000, 0xFFFF00, 0xFFFF00, 0xFFFF00, 0xFFFF00, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x00FFFF, +0x00FFFF, 0x000000, 0xE0E080, 0xE0E080, 0xE0E080, 0x000000, 0xFFFF00, 0xFFFF00, 0xFFFF00, 0xFFFF00, 0x000000, 0xE02000, 0xE02000, 0xE02000, 0xE02000, 0xE02000, 0x000000, +0x00FFFF, 0x00FFFF, 0x000000, 0x000000, 0x000000, 0xFFFF00, 0xFFFF00, 0xFFFF00, 0xFFFF00, 0x000000, 0xE02000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, +0x00FFFF, 0x00FFFF, 0x000000, 0xFFFF00, 0xFFFF00, 0xFFFF00, 0xFFFF00, 0xFFFF00, 0xFFFF00, 0xFFFF00, 0x000000, 0xE02000, 0xE02000, 0xE02000, 0xE02000, 0xE02000, 0x000000, +0x00FFFF, 0x00FFFF, 0x00FFFF, 0x000000, 0x000000, 0xFFFF00, 0xFFFF00, 0xFFFF00, 0xFFFF00, 0xFFFF00, 0xFFFF00, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x00FFFF, +0x00FFFF, 0x00FFFF, 0x00FFFF, 0x00FFFF, 0x00FFFF, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x00FFFF, 0x00FFFF, 0x00FFFF, 0x00FFFF, 0x00FFFF, 0x00FFFF +}; + +static RGB tubeBodyImage[] = { +0x00FFFF, 0x00FFFF, 0x000000, 0x608040, 0x80E040, 0xE0E080, 0xA0E040, 0xA0E040, 0x80C040, 0x80C040, 0x80C040, 0x80C040, 0x80E040, 0x60A000, 0x60A000, 0x80E040, 0x80E040, 0x60A000, 0x40A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x40A000, 0x60A000, 0x408000, 0x408000, 0x204000, 0x60A000, 0x408000, 0x408000, 0x204000, 0x206000, 0x204000, 0x200040, 0x00FFFF, 0x00FFFF +}; + +static RGB tubeHeadImage[] = { +0x200040, 0x200040, 0x200040, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x200040, 0x200040, 0x200040, 0x200040, 0x200040, 0x00FFFF, 0x00FFFF, +0x402000, 0x408000, 0x408000, 0x608040, 0x80A040, 0xC0C080, 0xC0C080, 0xC0C080, 0xC0C080, 0xC0C080, 0xC0C080, 0xC0C080, 0xC0C080, 0xC0C080, 0xC0C080, 0xC0C080, 0xC0C080, 0xC0C080, 0xC0C080, 0xC0C080, 0xC0C080, 0xC0C080, 0xC0C080, 0xC0C080, 0xC0C080, 0xC0C080, 0xC0C080, 0xC0C080, 0xA0C040, 0x60A040, 0x60A040, 0xA0C080, 0xA0C080, 0x60A040, 0x60A040, 0x80A040, 0x60A040, 0x60A040, 0x60A040, 0x60A040, 0x60A040, 0x80A040, 0x80A040, 0x80A040, 0x80A040, 0x608000, 0x406000, 0x204000, 0x204000, 0x402000, 0x00FFFF, 0x00FFFF, +0x402000, 0x60C000, 0x60C000, 0x80C040, 0xA0E040, 0xE0E080, 0xE0E080, 0xE0E080, 0xE0E080, 0xC0E080, 0xC0E080, 0xC0E080, 0xE0E080, 0xC0E080, 0xC0E080, 0xC0E080, 0xC0E080, 0xC0E080, 0xC0E080, 0xC0E080, 0xC0E080, 0xC0E080, 0xC0E080, 0xC0E080, 0xC0E080, 0xC0E080, 0xC0E080, 0xC0E080, 0xC0E080, 0x80C040, 0x80C040, 0xC0E080, 0xC0E080, 0x80C040, 0x80C040, 0x80E040, 0x80C040, 0x80C040, 0x80E040, 0x80E040, 0x80E040, 0x80C040, 0x80C040, 0x80C040, 0x80E040, 0x60A000, 0x408000, 0x206000, 0x206000, 0x402000, 0x00FFFF, 0x00FFFF, +0x402040, 0x80E040, 0x80E040, 0xC0E080, 0xE0E080, 0x80C040, 0x80C040, 0x80C040, 0x80C040, 0x40A000, 0x60A000, 0x60A000, 0x80E040, 0x40A000, 0x40A000, 0x40A000, 0x40A000, 0x40A000, 0x40A000, 0x40A000, 0x40A000, 0x40A000, 0x40A000, 0x40A000, 0x40A000, 0x40A000, 0x40A000, 0x40A000, 0x40A000, 0x40A000, 0x40A000, 0x40A000, 0x40A000, 0x40A000, 0x40A000, 0x40A000, 0x40A000, 0x40A000, 0x40A000, 0x40A000, 0x40A000, 0x204000, 0x406000, 0x406000, 0x60A000, 0x206000, 0x204000, 0x206000, 0x206000, 0x402000, 0x00FFFF, 0x00FFFF, +0x402040, 0x80E040, 0x80E040, 0xC0E080, 0xE0E080, 0x80C040, 0x80C040, 0x80C040, 0x80C040, 0x60A000, 0x60A000, 0x60A000, 0x80E040, 0x60A000, 0x40A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x206000, 0x406000, 0x406000, 0x60A000, 0x206000, 0x206000, 0x206000, 0x206000, 0x402000, 0x00FFFF, 0x00FFFF, +0x402040, 0x80E040, 0x80E040, 0xC0E080, 0xE0E080, 0x80C040, 0x80C040, 0x80C040, 0x80C040, 0x60A000, 0x60A000, 0x60A000, 0x80E040, 0x60A000, 0x40A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x206000, 0x406000, 0x406000, 0x60A000, 0x206000, 0x204000, 0x206000, 0x206000, 0x402000, 0x00FFFF, 0x00FFFF, +0x402040, 0x80E040, 0x80E040, 0xC0E080, 0xE0E080, 0x80C040, 0x80C040, 0x80C040, 0x80C040, 0x60A000, 0x60A000, 0x60A000, 0x80E040, 0x60A000, 0x40A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x206000, 0x406000, 0x406000, 0x60A000, 0x206000, 0x204000, 0x206000, 0x206000, 0x402000, 0x00FFFF, 0x00FFFF, +0x402040, 0x80E040, 0x80E040, 0xC0E080, 0xE0E080, 0x80C040, 0x80C040, 0x80C040, 0x80C040, 0x60A000, 0x60A000, 0x60A000, 0x80E040, 0x60A000, 0x40A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x206000, 0x406000, 0x406000, 0x60A000, 0x206000, 0x204000, 0x206000, 0x206000, 0x402000, 0x00FFFF, 0x00FFFF, +0x402040, 0x80E040, 0x80E040, 0xC0E080, 0xE0E080, 0x80C040, 0x80C040, 0x80C040, 0x80C040, 0x60A000, 0x60A000, 0x60A000, 0x80E040, 0x60A000, 0x40A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x206000, 0x406000, 0x406000, 0x60A000, 0x206000, 0x204000, 0x206000, 0x206000, 0x402000, 0x00FFFF, 0x00FFFF, +0x402040, 0x80E040, 0x80E040, 0xC0E080, 0xE0E080, 0x80C040, 0x80C040, 0x80C040, 0x80C040, 0x60A000, 0x60A000, 0x60A000, 0x80E040, 0x60A000, 0x40A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x206000, 0x406000, 0x406000, 0x60A000, 0x206000, 0x204000, 0x206000, 0x206000, 0x402000, 0x00FFFF, 0x00FFFF, +0x402040, 0x80E040, 0x80E040, 0xC0E080, 0xE0E080, 0x80C040, 0x80C040, 0x80C040, 0x80C040, 0x60A000, 0x60A000, 0x60A000, 0x80E040, 0x60A000, 0x40A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x206000, 0x406000, 0x406000, 0x60A000, 0x206000, 0x204000, 0x206000, 0x206000, 0x402000, 0x00FFFF, 0x00FFFF, +0x402040, 0x80E040, 0x80E040, 0xC0E080, 0xE0E080, 0x80C040, 0x80C040, 0x80C040, 0x80C040, 0x60A000, 0x60A000, 0x60A000, 0x80E040, 0x60A000, 0x40A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x206000, 0x406000, 0x406000, 0x60A000, 0x206000, 0x204000, 0x206000, 0x206000, 0x402000, 0x00FFFF, 0x00FFFF, +0x402040, 0x80E040, 0x80E040, 0xC0E080, 0xE0E080, 0x80C040, 0x80C040, 0x80C040, 0x80C040, 0x60A000, 0x60A000, 0x60A000, 0x80E040, 0x60A000, 0x40A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x206000, 0x406000, 0x406000, 0x60A000, 0x206000, 0x204000, 0x206000, 0x206000, 0x402000, 0x00FFFF, 0x00FFFF, +0x402040, 0x80E040, 0x80E040, 0xC0E080, 0xE0E080, 0x80C040, 0x80C040, 0x80C040, 0x80C040, 0x40A000, 0x60A000, 0x60A000, 0x80E040, 0x60A000, 0x40A000, 0x60A000, 0x60A000, 0x40A000, 0x40A000, 0x40A000, 0x40A000, 0x40A000, 0x40A000, 0x40A000, 0x40A000, 0x40A000, 0x40A000, 0x40A000, 0x40A000, 0x40A000, 0x40A000, 0x40A000, 0x40A000, 0x40A000, 0x40A000, 0x40A000, 0x40A000, 0x40A000, 0x40A000, 0x40A000, 0x60A000, 0x206000, 0x406000, 0x406000, 0x60A000, 0x206000, 0x204000, 0x206000, 0x206000, 0x402000, 0x00FFFF, 0x00FFFF, +0x402040, 0x80E040, 0x80E040, 0xC0E080, 0xE0E080, 0x80E040, 0x80E040, 0x80E040, 0x80E040, 0x60A000, 0x60C000, 0x60C000, 0xA0E040, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x60A000, 0x206000, 0x406000, 0x406000, 0x60A000, 0x206000, 0x204000, 0x206000, 0x206000, 0x402000, 0x00FFFF, 0x00FFFF, +0x402000, 0x408000, 0x408000, 0x608000, 0x608040, 0x408000, 0x408000, 0x408000, 0x408000, 0x406000, 0x408000, 0x408000, 0x408000, 0x406000, 0x406000, 0x406000, 0x406000, 0x406000, 0x406000, 0x406000, 0x406000, 0x406000, 0x406000, 0x406000, 0x406000, 0x406000, 0x406000, 0x406000, 0x406000, 0x406000, 0x406000, 0x406000, 0x406000, 0x406000, 0x406000, 0x406000, 0x406000, 0x406000, 0x406000, 0x406000, 0x406000, 0x206000, 0x206000, 0x206000, 0x406000, 0x206000, 0x206000, 0x206000, 0x206000, 0x402000, 0x00FFFF, 0x00FFFF, +0x402000, 0x204000, 0x204000, 0x204000, 0x204000, 0x204000, 0x204000, 0x204000, 0x204000, 0x204000, 0x204000, 0x204000, 0x204000, 0x204000, 0x204000, 0x204000, 0x204000, 0x204000, 0x204000, 0x204000, 0x204000, 0x204000, 0x204000, 0x204000, 0x204000, 0x204000, 0x204000, 0x204000, 0x204000, 0x204000, 0x204000, 0x204000, 0x204000, 0x204000, 0x204000, 0x204000, 0x204000, 0x204000, 0x204000, 0x204000, 0x204000, 0x204000, 0x204000, 0x204000, 0x204000, 0x204000, 0x204000, 0x206000, 0x206000, 0x402000, 0x00FFFF, 0x00FFFF, +0x200040, 0x200040, 0x200040, 0x200040, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x200040, 0x200040, 0x000000, 0x000000, 0x200040, 0x200040, 0x200040, 0x200040, 0x200040, 0x200040, 0x200040, 0x200040, 0x200040, 0x200040, 0x200040, 0x200040, 0x200040, 0x200040, 0x200040, 0x200040, 0x200040, 0x200040, 0x200040, 0x200040, 0x200040, 0x200040, 0x200040, 0x200040, 0x200040, 0x200040, 0x200040, 0x200040, 0x200040, 0x200040, 0x200040, 0x200040, 0x200040, 0x00FFFF, 0x00FFFF, + +}; \ No newline at end of file diff --git a/programs/games/flappybird/smalllibc/doexe2.asm b/programs/games/flappybird/smalllibc/doexe2.asm new file mode 100644 index 0000000000..969fa9cff3 --- /dev/null +++ b/programs/games/flappybird/smalllibc/doexe2.asm @@ -0,0 +1,68 @@ +filename equ '%EXENAME%' + +virtual at 0 +file filename:3Ch,4 +load pehea dword from 0 +file filename:pehea,0F8h+28h*3 +load NumberOfSections word from 4+6 +load SizeOfOptionalHeader word from 4+14h +if NumberOfSections<>3 +error Expected three sections, .text, .bss and .reloc +end if +if SizeOfOptionalHeader<>0E0h +error Nonstandard PE header +end if +load RelocsRVA dword from 4+0A0h +load RelocsSize dword from 4+0A4h +load ImageBase dword from 4+34h +load TextRVA dword from 4+0F8h+0Ch +load TextSize dword from 4+0F8h+8 +load TextOffs dword from 4+0F8h+14h +load BSSSize dword from 4+0F8h+28h+10h +load RelocRVA dword from 4+0F8h+28h*2+0Ch +load RelocOffs dword from 4+0F8h+28h*2+14h +if BSSSize +error Second section expected to be .bss +end if +if RelocRVA<>RelocsRVA +error Third section expected to be .reloc +end if +;file 'test.exe':pehea+0F8h,28h +;load physofs dword from 4+14h +;load mem dword from 4+8 +;file 'test.exe':physofs+16,4 +;load sz dword from $-4 +end virtual + +file filename:TextOffs,TextSize + +while RelocsSize>8 +virtual at 0 +file filename:RelocOffs,8 +load CurRelocPage dword from 0 +load CurRelocChunkSize dword from 4 +end virtual +RelocsSize=RelocsSize-CurRelocChunkSize +CurRelocChunkSize = CurRelocChunkSize-8 +RelocOffs=RelocOffs+8 +while CurRelocChunkSize +virtual at 0 +file filename:RelocOffs,2 +RelocOffs=RelocOffs+2 +CurRelocChunkSize=CurRelocChunkSize-2 +load s word from 0 +end virtual +CurRelocType = s shr 12 +RelocItem = CurRelocPage + (s and 0xFFF) +if CurRelocType=0 +else if CurRelocType=3 +load z dword from RelocItem-TextRVA +store dword z-(TextRVA+ImageBase) at RelocItem-TextRVA +else +error Unexpected relocation type +end if +end while +end while + +store dword TextSize at 10h +store dword RelocRVA-TextRVA at 14h diff --git a/programs/games/flappybird/smalllibc/func.cpp b/programs/games/flappybird/smalllibc/func.cpp new file mode 100644 index 0000000000..fb9b48a843 --- /dev/null +++ b/programs/games/flappybird/smalllibc/func.cpp @@ -0,0 +1,501 @@ + + +#include "func.h" + +int convert_error = 0; +int SysColor = 0; +char debuf[50] = ""; + + +// - +void kos_DrawLine( Word x1, Word y1, Word x2, Word y2, Dword colour, Dword invert ) +{ + Dword arg1, arg2, arg3; + + // + arg1 = ( x1 << 16 ) | x2; + arg2 = ( y1 << 16 ) | y2; + arg3 = (invert)?0x01000000:colour; + // + __asm{ + mov eax, 38 + mov ebx, arg1 + mov ecx, arg2 + mov edx, arg3 + int 0x40 + } +} + +// C-- +void DrawRegion(Dword x,Dword y,Dword width,Dword height,Dword color1) +{ + kos_DrawBar(x,y,width,1,color1); // + kos_DrawBar(x,y+height,width,1,color1); // + kos_DrawBar(x,y,1,height,color1); // + kos_DrawBar(x+width,y,1,height+1,color1); // +} + + +// , +int atoi(const char* string) +{ + int res=0; + int sign=0; + const char* ptr; + for (ptr=string; *ptr && *ptr<=' ';ptr++); + if (*ptr=='-') {sign=1;++ptr;} + while (*ptr >= '0' && *ptr <= '9') + { + res = res*10 + *ptr++ - '0'; + } + if (sign) res = -res; + return res; +} + +/*int abs(int n) +{ + return (n<0)?-n:n; +}*/ + + + + + +double fabs(double x) +{ + __asm fld x + __asm fabs +} +#define M_PI 3.14159265358979323846 +double cos(double x) +{ + __asm fld x + __asm fcos +} +double sin(double x) +{ + __asm fld x + __asm fsin +} + +bool isalpha(char c) +{ + return (c==' ' || c=='\n' || c=='\t' || c=='\r'); +} + +// - . . +double convert(char *s, int *len) +{ + + int i; + + + double sign,res, tail, div; + + convert_error = 0; + + res = 0.0; + + i=0; + while (s[i] && isalpha(s[i])) i++; + if (len) *len=i; + if (s[i] == '\0') + { + convert_error = ERROR_END; + return 0.0; + } + + sign=1.0; + if (s[i] == '-') + { + sign=-1.0; + i++; + } + while (s[i] && s[i] >= '0' && s[i] <= '9') + { + res *= 10.0; + res += id(s[i] - '0'); + i++; + } + if (len) *len=i; + if (!s[i] || isalpha(s[i])) + return sign*res; + if (s[i] != '.' && s[i] != ',') + { + convert_error = ERROR; + return 0; + } + i++; + if (len) *len=i; + if (!s[i]) + return sign*res; + + div = 1.0; + tail = 0.0; + while (s[i] && s[i] >= '0' && s[i] <= '9') + { + tail *= 10.0; + tail += id(s[i] - '0'); + div *= 10.0; + i++; + } + res += tail/div; + if (len) *len=i; + return sign*res; +} + +/* +#define PREC 2 + +double double_tab[]={1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10, 1e11, 1e12, 1e13, 1e14, 1e15}; + +// sprintf, __ (double) %f +void format( char *Str, int len, char* Format, ... ) +{ + int i, fmtlinesize, j, k, flag; + char c; + va_list arglist; + // + va_start(arglist, Format); + + // + fmtlinesize = strlen( Format ); + // + if( fmtlinesize == 0 ) return; + + for (i = 0; i < len; i++) + Str[i] = 0; + + // + for( i = 0, j = 0; i < fmtlinesize; i++ ) + { + // + c = Format[i]; + // + if( c != '%' ) + { + Str[j++] = c; + continue; + } + // + i++; + // + if( i >= fmtlinesize ) break; + + // + flag = 0; + // + c = Format[i]; + // + switch( c ) + { + // + case '%': + Str[j++] = c; + break; + // auaia aauanoaaiiiai ?enea + case 'f': + // ii?aaaeeou ?enei oeo? ai oi?ee + double val, w; + int p; + val = va_arg(arglist, double); + if (val < 0.0) + { + Str[j++] = '-'; + val = -val; + } + for (k = 0; k < 15; k++) + if (val < double_tab[k]) + break; + + if (val < 1.0) + { + Str[j++] = '0'; + } + + for (p = 1; p < k + 1; p++) + { + Str[j++] = '0' + di(val / double_tab[k - p] - 0.499) % 10; + } + Str[j++] = '.'; + w = 0.1; + for (p = 0; p < 2; p++) + { + val-=floor(val); + Str[j++] = '0' + di(val / w - 0.499) % 10; + w /= 10.0; + } + + // + default: + break; + } + } + // + Str[j] = 0; +} + +void *memcpy(void *dst, const void *src, unsigned size) +{ + while (size--) + *((char*)dst+size) = *((char*)src+size); + return dst; +} +*/ +int strcmp(const char *s1, const char *s2) +{ + int i; + + if (s1 == NULL) + if (s2 == NULL) + return 0; + else + return 1; + else + if (s2 == NULL) + return 1; + + for (i = 0;;i++) + { + if (s1[i] == '\0') + if (s2[i] == '\0') + return 0; + else + return 1; + else + if (s2[i] == '\0') + return 1; + else + { + if (s1[i] != s2[i]) + return 1; + } + } + return 0; +} + +kol_struct_import* kol_cofflib_load(char *name) +{ +//asm ("int $0x40"::"a"(68), "b"(19), "c"(name)); + __asm + { + mov eax, 68 + mov ebx, 19 + mov ecx, name + int 0x40 + } +} + + +void* kol_cofflib_procload (kol_struct_import *imp, char *name) +{ + +int i; +for (i=0;;i++) + if ( NULL == ((imp+i) -> name)) + break; + else + if ( 0 == strcmp(name, (imp+i)->name) ) + return (imp+i)->data; +return NULL; + +} + + +unsigned kol_cofflib_procnum (kol_struct_import *imp) +{ + +unsigned i, n; + +for (i=n=0;;i++) + if ( NULL == ((imp+i) -> name)) + break; + else + n++; + +return n; + +} + + +void kol_cofflib_procname (kol_struct_import *imp, char *name, unsigned n) +{ + +unsigned i; +*name = 0; + +for (i=0;;i++) + if ( NULL == ((imp+i) -> name)) + break; + else + if ( i == n ) + { + strcpy(name, ((imp+i)->name)); + break; + } + +} + + + +/* +end of system part +*/ + + +// ... +void line( int x1, int y1, int x2, int y2) +{ + kos_DrawLine(x1,y1,x2,y2,SysColor,0); +} + +void outtextxy( int x, int y, char *s, int len) +{ + kos_WriteTextToWindow(x,y,0,SysColor,s,len); +} + +double textwidth( char *s, int len) +{ + int i; + for (i = 0; i < len; i++) + if (s[i] == 0) + break; + return id(i * 6); +} + +double textheight( char *s, int len) +{ + return 8.0; +} + +void setcolor( DWORD color) +{ + SysColor = color; +} + +void rectangle( int x1, int y1, int x2, int y2) +{ + kos_DrawBar(x1,y1,x2-x1,y2-y1,SysColor); +} + + + +Dword kos_GetSkinHeight() +{ + __asm{ + mov eax, 48 + mov ebx, 4 + int 0x40 + } +} + +Dword kos_GetSpecialKeyState() +{ + __asm{ + mov eax, 66 + mov ebx, 3 + int 0x40 + } +} + + + +Dword kos_GetSlotByPID(Dword PID) +{ + __asm + { + push ebx + push ecx + mov eax, 18 + mov ebx, 21 + mov ecx, PID + int 0x40 + pop ecx + pop ebx + } +} + + +Dword kos_GetActiveSlot() +{ + __asm + { + push ebx + mov eax, 18 + mov ebx, 7 + int 0x40 + pop ebx + } +} + + + +void kos_GetScrollInfo(int &vert, int &hor) +{ + short v, h; + __asm + { + mov eax, 37 + mov ebx, 7 + int 0x40 + mov ebx, eax + and eax, 0xffff + mov v, ax + shr ebx, 16 + mov h, bx + } + vert = v; + hor = h; +} + + +// "" 37/1 +void kos_GetMouseStateWnd( Dword & buttons, int & cursorX, int & cursorY ) +{ + Dword mB; + Word curX; + Word curY; + sProcessInfo sPI; + + // + __asm{ + mov eax, 37 + mov ebx, 1 + int 0x40 + mov curY, ax + shr eax, 16 + mov curX, ax + mov eax, 37 + mov ebx, 2 + int 0x40 + mov mB, eax + } + // + kos_ProcessInfo( &sPI ); + // + buttons = mB; + cursorX = curX - sPI.processInfo.x_start; + cursorY = curY - sPI.processInfo.y_start; +} + +double atof(char *s) +{ + return convert(s, NULL); +} + + +int di(double x) +{ + int a; + __asm fld x + __asm fistp a + return a; +} + +double id(int x) +{ + double a; + __asm fild x + __asm fstp a + return a; +} diff --git a/programs/games/flappybird/smalllibc/func.h b/programs/games/flappybird/smalllibc/func.h new file mode 100644 index 0000000000..34c37e7b60 --- /dev/null +++ b/programs/games/flappybird/smalllibc/func.h @@ -0,0 +1,129 @@ + +#pragma once + +#include "kosSyst.h" +#include "kosFile.h" +#include "MCSMEMM.H" + +#include + + +#define min(a,b) (((a)<(b))?(a):(b)) +#define max(a,b) (((a)>(b))?(a):(b)) + + +#define ERROR -1 +#define ERROR_END -2 + +extern int convert_error; + +typedef int HDC; +typedef int DWORD; + +extern int SysColor; +extern char debuf[50]; + +typedef double (*function_t)(double); + +typedef struct +{ + double x, y; +} TCoord; + +struct kosBDVK +{ + Dword attrib; + Dword name_type; + Dword create_time; + Dword create_date; + Dword access_time; + Dword access_date; + Dword modify_time; + Dword modify_date; + Dword size_low; + Dword size_high; +}; + +Dword kos_GetSlotByPID(Dword PID); +Dword kos_GetActiveSlot(); +Dword kos_GetSkinHeight(); +Dword kos_GetSpecialKeyState(); +void kos_GetMouseStateWnd( Dword & buttons, int & cursorX, int & cursorY ); +void kos_DrawLine( Word x1, Word y1, Word x2, Word y2, Dword colour, Dword invert); +void DrawRegion(Dword x,Dword y,Dword width,Dword height,Dword color1); +int atoi(const char* string); +void kos_GetScrollInfo(int &vert, int &hor); + + +Dword kos_GetSlotByPID(Dword PID); +Dword kos_GetActiveSlot(); +Dword kos_GetSkinHeight(); +Dword kos_GetSpecialKeyState(); + + +double fabs(double x); +double cos(double x); +double sin(double x); +bool isalpha(char c); +double convert(char *s, int *len=NULL); +void format( char *Str, int len, char* Format, ... ); + +void line( int x1, int y1, int x2, int y2); + +void outtextxy( int x, int y, char *s, int len); +void settextstyle( int a1, int a2, int a3); + + +double textwidth( char *s, int len); +double textheight( char *s, int len); +void setcolor( DWORD color); +void unsetcolor(HDC hdc); +void rectangle( int x1, int y1, int x2, int y2); + +typedef struct +{ +unsigned p00 ; +unsigned p04 ; +unsigned p08 ; +unsigned p12 ; +unsigned p16 ; +char p20 ; +char *p21 ; +} kol_struct70 ; + + +typedef struct +{ +unsigned p00 ; +char p04 ; +char p05[3] ; +unsigned p08 ; +unsigned p12 ; +unsigned p16 ; +unsigned p20 ; +unsigned p24 ; +unsigned p28 ; +unsigned p32[2] ; +unsigned p40 ; +} kol_struct_BDVK ; + +typedef struct +{ +char *name ; +void *data ; +} kol_struct_import ; + + + +kol_struct_import* kol_cofflib_load(char *name); +void* kol_cofflib_procload (kol_struct_import *imp, char *name); +unsigned kol_cofflib_procnum (kol_struct_import *imp); +void kol_cofflib_procname (kol_struct_import *imp, char *name, unsigned n); +int strcmp(const char* string1, const char* string2); + +char *ftoa(double d); +double atof(char *s); + + +int di(double x); +double id(int x); diff --git a/programs/games/flappybird/smalllibc/init.asm b/programs/games/flappybird/smalllibc/init.asm new file mode 100644 index 0000000000..0bbef5bca7 --- /dev/null +++ b/programs/games/flappybird/smalllibc/init.asm @@ -0,0 +1,31 @@ +format MS COFF + +StackSize = 16384 + +; must be alphabetically first in the image +section '.1seg' data readable writable +extrn _crtStartUp ; real entry point +extrn _kosCmdLine +extrn _kosExePath +extrn _exeStack +public fakeEntry + +kos_header: + db 'MENUET01' ; header + dd 1 ; headerver + dd _crtStartUp ; entry + dd 0 ; i_end, filled by doexe2.asm + dd 0 ; memsize, filled by doexe2.asm + dd _exeStack + StackSize ; stack + dd _kosCmdLine ; params + dd _kosExePath ; icon +fakeEntry: ; only for linker, to force including this obj file + ; real entry is crtStartUp + +; initializers +section '.CRT$XCA' data readable writable +public ___xc_a +___xc_a: +section '.CRT$XCZ' data readable writable +public ___xc_z +___xc_z: diff --git a/programs/games/flappybird/smalllibc/kosFile.cpp b/programs/games/flappybird/smalllibc/kosFile.cpp new file mode 100644 index 0000000000..1718d47989 --- /dev/null +++ b/programs/games/flappybird/smalllibc/kosFile.cpp @@ -0,0 +1,132 @@ +#include "kosSyst.h" +#include "kosFile.h" +//#include "string.h" + + +CKosFile::CKosFile(char *fileName) +{ + // + this->fileInfo.bufferPtr = new Byte[FILE_BUFFER_SIZE]; + // + this->filePointer = 0; + this->bufferPointer = 0; + this->validBuffer = false; + // + strcpy( this->fileInfo.fileURL, fileName ); +} + + +CKosFile::~CKosFile(void) +{ + // + delete this->fileInfo.bufferPtr; +} + + +void CKosFile::ValidateBuffer() +{ + // + if ( this->validBuffer ) + { + // + if ( this->filePointer < this->bufferPointer + || this->filePointer >= (this->bufferPointer + FILE_BUFFER_SIZE) ) + { + // + this->validBuffer = false; + } + } +} + + +void CKosFile::UpdateBuffer(void) +{ + // + if ( ! this->validBuffer ) + { + // + this->fileInfo.OffsetLow = this->filePointer / OS_BLOCK_SIZE; + this->fileInfo.OffsetHigh = 0; + // + this->bufferPointer = this->fileInfo.OffsetLow * OS_BLOCK_SIZE; + // + this->fileInfo.dataCount = FILE_BUFFER_BLOCKS; + // + this->fileInfo.rwMode = 0; + // + Dword rr = kos_FileSystemAccess( &(this->fileInfo) ); + this->validBuffer = ( rr == 0 ); + } +} + + +int CKosFile::Seek(int seekFrom, int seekStep) +{ + // + switch ( seekFrom ) + { + // + case SEEK_SET: + // + this->filePointer = seekStep; + break; + // + case SEEK_CUR: + // + this->filePointer += seekStep; + break; + } + // + this->ValidateBuffer(); + // + return this->filePointer; +} + + +int CKosFile::Read(Byte *targetPtr, int readCount) +{ + int bufferLeast, result; + + // + result = 0; + // + do + { + // + this->UpdateBuffer(); + // + if ( ! this->validBuffer ) return result; + // + bufferLeast = FILE_BUFFER_SIZE - (this->filePointer - this->bufferPointer); + // + if ( bufferLeast > readCount ) bufferLeast = readCount; + // + if ( bufferLeast ) + { + // + memcpy( + targetPtr, + this->fileInfo.bufferPtr + (this->filePointer - this->bufferPointer), + bufferLeast + ); + // + targetPtr += bufferLeast; + readCount -= bufferLeast; + this->filePointer += bufferLeast; + // + result += bufferLeast; + } + // + this->ValidateBuffer(); + } + while ( readCount > 0 ); + // + return result; +} + + +int CKosFile::Write(Byte *sourcePtr, int writeCount) +{ + return 0; +} + diff --git a/programs/games/flappybird/smalllibc/kosFile.h b/programs/games/flappybird/smalllibc/kosFile.h new file mode 100644 index 0000000000..0c5e54affd --- /dev/null +++ b/programs/games/flappybird/smalllibc/kosFile.h @@ -0,0 +1,26 @@ +#pragma once + +#define SEEK_SET 0 +#define SEEK_CUR 1 + +#define FILE_BUFFER_SIZE 512 +#define OS_BLOCK_SIZE 1 +#define FILE_BUFFER_BLOCKS (FILE_BUFFER_SIZE / OS_BLOCK_SIZE) + + +class CKosFile +{ +public: + CKosFile(char *fileName); + ~CKosFile(void); + int Read(Byte *targetPtr, int readCount); + int Write(Byte *sourcePtr, int writeCount); + int Seek(int seekFrom, int seekStep); +protected: + int filePointer; + int bufferPointer; + bool validBuffer; + kosFileInfo fileInfo; + void ValidateBuffer(void); + void UpdateBuffer(void); +}; diff --git a/programs/games/flappybird/smalllibc/kosSyst.cpp b/programs/games/flappybird/smalllibc/kosSyst.cpp new file mode 100644 index 0000000000..0eeff63032 --- /dev/null +++ b/programs/games/flappybird/smalllibc/kosSyst.cpp @@ -0,0 +1,695 @@ +#include "kosSyst.h" +#include "func.h" +#include + +char kosCmdLine[257]; +char kosExePath[1024]; +extern "C" char exeStack[]; +char exeStack[16384]; + +#define atexitBufferSize 32 + +#ifndef SMALLLIBC_NO_ATEXIT +// +void (__cdecl *atExitList[atexitBufferSize])(); +int atExitFnNum = 0; +// +int __cdecl atexit( void (__cdecl *func )( void )) +{ + // + if ( atExitFnNum < atexitBufferSize ) + { + // + atExitList[atExitFnNum++] = func; + return 0; + } + else + { + return 1; + } +} +#endif + +// +Dword RandomSeed = 0; +// +void rtlSrand( Dword seed ) +{ + RandomSeed = seed; +} +// +Dword rtlRand( void ) +{ + // 0x80000776 + + Dword dwi, i; + + for ( i = 0; i < 32; i++ ) + { + + dwi = RandomSeed & 0x80000776; + + __asm{ + mov eax, dwi + mov edx, eax + bswap eax + xor eax, edx + xor al, ah + setpo al + movzx eax, al + mov dwi, eax + } + + RandomSeed = ( RandomSeed << 1 ) | ( dwi & 1 ); + } + + return RandomSeed; +} + +void* __cdecl memcpy( void *dst, const void *src, size_t bytesCount ) +{ + __asm{ + mov edi, dst + mov eax, dst + mov esi, src + mov ecx, bytesCount + rep movsb + } +} + +// +void memset( Byte *dst, Byte filler, Dword count ) +{ + // + __asm{ + mov edi, dst + mov al, filler + mov ecx, count + rep stosb + } +} + + +// +Dword rtlInterlockedExchange( Dword *target, Dword value ) +{ +// Dword result; + + // + __asm{ + mov eax, value + mov ebx, target + xchg eax, [ebx] +// mov result, eax + } + // +// return result; +} + + +////////////////////////////////////////////////////////////////////// +// +// +// + +char * __cdecl strcpy( char *target, const char *source ) +{ + char *result = target; + + while( target[0] = source[0] ) + { + target++; + source++; + } + + return result; +} + + +////////////////////////////////////////////////////////////////////// +// +// +// + +char * __cdecl strrchr( const char * string, int c ) +{ + char *cPtr; + + // + for ( cPtr = (char *)string + strlen( string ); cPtr >= string; cPtr-- ) + { + // + if ( *cPtr == c ) return cPtr; + } + // + return NULL; +} + + +////////////////////////////////////////////////////////////////////// +// +// +// + +int __cdecl strlen( const char *line ) +{ + int i; + + for( i=0; line[i] != 0; i++ ); + return i; +} + + + +////////////////////////////////////////////////////////////////////// +// +// +// + +unsigned int num2hex( unsigned int num ) +{ + if( num < 10 ) + return num + '0'; + return num - 10 + 'A'; +} + + +inline void __declspec(noreturn) kos_sysfuncm1(void) +{ + __asm or eax, -1 + __asm int 0x40 +} + +// -1 +void kos_ExitApp() +{ +#ifndef SMALLLIBC_NO_ATEXIT + int i; + + // + for ( i = atExitFnNum - 1; i >= 0; i-- ) + { + // + atExitList[i](); + } +#endif + // + kos_sysfuncm1(); +} + +static void __declspec(noinline) __fastcall kos_sysfunc0(Dword _ecx, Dword _edx, Dword _ebx, Dword _esi, Dword _edi) +{ + __asm xor eax, eax + __asm mov ebx, _ebx + __asm mov esi, _esi + __asm mov edi, _edi + __asm int 0x40 +} + +// 0 +void kos_DefineAndDrawWindow( + Word x, Word y, + Word sizeX, Word sizeY, + Byte mainAreaType, + Dword mainAreaColour, + Byte headerType, + Dword headerColour, + Dword borderColour + ) +{ + Dword arg1, arg2, arg3, arg4; + + // + arg1 = ( x << 16 ) + sizeX; + arg2 = ( y << 16 ) + sizeY; + arg3 = ( mainAreaType << 24 ) | mainAreaColour; + arg4 = ( headerType << 24 ) | headerColour; + // + kos_sysfunc0(arg2, arg3, arg1, arg4, borderColour); +} + + +// 1 +void kos_PutPixel( Dword x, Dword y, Dword colour ) +{ + // + __asm{ + push 1 + pop eax + mov ebx, x + mov ecx, y + mov edx, colour + int 0x40 + } +} + +inline Dword kos_sysfunc2(void) +{ + __asm push 2 + __asm pop eax + __asm int 0x40 +} + +// 2 +bool kos_GetKey( Byte &keyCode ) +{ + Dword result = kos_sysfunc2(); + // + keyCode = result >> 8; + // + return ( result & 0xFF ) == 0; +} + + +// 3 +Dword kos_GetSystemClock() +{ +// Dword result; + + // + __asm{ + push 3 + pop eax + int 0x40 +// mov result, eax + } + // +// return result; +} + +static void __declspec(noinline) __fastcall kos_sysfunc4(Dword _ecx, const char* _edx, Dword _ebx, Dword _esi) +{ + __asm push 4 + __asm pop eax + __asm mov ebx, [_ebx] + __asm mov esi, [_esi] + __asm int 0x40 +} + +// 4 +void kos_WriteTextToWindow( + Word x, + Word y, + Byte fontType, + Dword textColour, + const char *textPtr, + Dword textLen + ) +{ + Dword arg1, arg2; + + // + arg1 = ( x << 16 ) | y; + arg2 = ( fontType << 24 ) | textColour; + // + kos_sysfunc4(arg2, textPtr, arg1, textLen); +} + + +// 5 , +void kos_Pause( Dword value ) +{ + // + __asm{ + push 5 + pop eax + mov ebx, value + int 0x40 + } +} + + +// 7 +void kos_PutImage( RGB * imagePtr, Word sizeX, Word sizeY, Word x, Word y ) +{ + Dword arg1, arg2; + + // + arg1 = ( sizeX << 16 ) | sizeY; + arg2 = ( x << 16 ) | y; + // + __asm{ + push 7 + pop eax + mov ebx, imagePtr + mov ecx, arg1 + mov edx, arg2 + int 0x40 + } +} + + + +// 8 +void kos_DefineButton( Word x, Word y, Word sizeX, Word sizeY, Dword buttonID, Dword colour ) +{ + Dword arg1, arg2; + + // + arg1 = ( x << 16 ) | sizeX; + arg2 = ( y << 16 ) | sizeY; + // + __asm{ + push 8 + pop eax + mov ebx, arg1 + mov ecx, arg2 + mov edx, buttonID + mov esi, colour + int 0x40 + } +} + + +// 9 - +Dword kos_ProcessInfo( sProcessInfo *targetPtr, Dword processID ) +{ +// Dword result; + + // + __asm{ + push 9 + pop eax + mov ebx, targetPtr + mov ecx, processID + int 0x40 +// mov result, eax + } + // +// return result; +} + + +// 10 +Dword kos_WaitForEvent() +{ +// Dword result; + + __asm{ + push 10 + pop eax + int 0x40 +// mov result, eax + } + +// return result; +} + + +// 11 +Dword kos_CheckForEvent() +{ + Dword result; // + + __asm{ + push 11 + pop eax + int 0x40 + mov result, eax // + } + + return result; // +} + + +// 12 +void kos_WindowRedrawStatus( Dword status ) +{ + __asm{ + push 12 + pop eax + mov ebx, status + int 0x40 + } +} + + +// 13 +void kos_DrawBar( Word x, Word y, Word sizeX, Word sizeY, Dword colour ) +{ + Dword arg1, arg2; + + // + arg1 = ( x << 16 ) | sizeX; + arg2 = ( y << 16 ) | sizeY; + // + __asm{ + push 13 + pop eax + mov ebx, arg1 + mov ecx, arg2 + mov edx, colour + int 0x40 + } +} + + +// 17 +bool kos_GetButtonID( Dword &buttonID ) +{ + Dword result; + + // + __asm{ + push 17 + pop eax + int 0x40 + mov result, eax + } + // + buttonID = result >> 8; + // + return (result & 0xFF) == 0; +} + + +// 23 +Dword kos_WaitForEventTimeout( Dword timeOut ) +{ +// Dword result; + + __asm{ + push 23 + pop eax + mov ebx, timeOut + int 0x40 +// mov result, eax + } + +// return result; +} + + +// "" 37 +void kos_GetMouseState( Dword & buttons, int & cursorX, int & cursorY ) +{ + Dword mB; + Word curX; + Word curY; + sProcessInfo sPI; + + // + __asm{ + push 37 + pop eax + xor ebx, ebx + int 0x40 + mov curY, ax + shr eax, 16 + mov curX, ax + push 37 + pop eax + push 2 + pop ebx + int 0x40 + mov mB, eax + } + // + kos_ProcessInfo( &sPI ); + // + buttons = mB; + cursorX = curX - sPI.processInfo.x_start; + cursorY = curY - sPI.processInfo.y_start; +} + + +// 40 +void kos_SetMaskForEvents( Dword mask ) +{ + // + __asm{ + push 40 + pop eax + mov ebx, mask + int 0x40 + } +} + + +// 47 +void kos_DisplayNumberToWindow( + Dword value, + Dword digitsNum, + Word x, + Word y, + Dword colour, + eNumberBase nBase, + bool valueIsPointer + ) +{ + Dword arg1, arg2; + + // + arg1 = ( valueIsPointer ? 1 : 0 ) | + ( ((Byte)nBase) << 8 ) | + ( ( digitsNum & 0x1F ) << 16 ); + arg2 = ( x << 16 ) | y; + // + __asm{ + push 47 + pop eax + mov ebx, arg1 + mov ecx, value + mov edx, arg2 + mov esi, colour + int 0x40 + } +} + + +// 70 +Dword kos_FileSystemAccess( kosFileInfo *fileInfo ) +{ +// Dword result; + + // + __asm{ + push 70 + pop eax + mov ebx, fileInfo + int 0x40 +// mov result, eax + } + // +// return result; +} + + +// 63 +void kos_DebugOutChar( char ccc ) +{ + // + __asm{ + push 63 + pop eax + push 1 + pop ebx + mov cl, ccc + int 0x40 + } +} + + +// 66 +void kos_SetKeyboardDataMode( Dword mode ) +{ + // + __asm{ + push 66 + pop eax + push 1 + pop ebx + mov ecx, mode + int 0x40 + } +} + + +// +void rtlDebugOutString( char *str ) +{ + // + for ( ; str[0] != 0; str++ ) + { + kos_DebugOutChar( str[0] ); + } + // + kos_DebugOutChar( 13 ); + kos_DebugOutChar( 10 ); +} + + +// 64 , +bool kos_ApplicationMemoryResize( Dword targetSize ) +{ + Dword result; + + // + __asm{ + push 64 + pop eax + push 1 + pop ebx + mov ecx, targetSize + int 0x40 + mov result, eax + } + // + return result == 0; +} + + +// 67 , == -1 +void kos_ChangeWindow( Dword x, Dword y, Dword sizeX, Dword sizeY ) +{ + // + __asm{ + push 67 + pop eax + mov ebx, x + mov ecx, y + mov edx, sizeX + mov esi, sizeY + int 0x40 + } +} + +void kos_InitHeap() +{ + __asm{ + push 68 + pop eax + push 11 + pop ebx + int 0x40 + } +} + + + +// +typedef void (__cdecl *_PVFV)(void); +extern "C" _PVFV __xc_a[]; +extern "C" _PVFV __xc_z[]; +#pragma comment(linker, "/merge:.CRT=.rdata") +// +void __cdecl crtStartUp() +{ +#ifndef SMALLLIBC_NO_INIT + // + for ( _PVFV *pbegin = __xc_a; pbegin < __xc_z; pbegin++ ) + { + // + (**pbegin)(); + } +#endif + // + // , kos_Main() + //rtlSrand( kos_GetSystemClock() ); + // + kos_Main(); + // + kos_ExitApp(); +} + + diff --git a/programs/games/flappybird/smalllibc/kosSyst.h b/programs/games/flappybird/smalllibc/kosSyst.h new file mode 100644 index 0000000000..7a22524044 --- /dev/null +++ b/programs/games/flappybird/smalllibc/kosSyst.h @@ -0,0 +1,217 @@ + +#pragma once + +typedef unsigned __int32 Dword; +typedef unsigned __int16 Word; +typedef unsigned __int8 Byte; +//typedef unsigned __int32 size_t; + +extern "C" char kosCmdLine[]; // command line initialized by OS +extern "C" char kosExePath[]; // path to binary initialized by OS + +#define NULL 0 + +#define MAX_PATH 256 + +#define FO_READ 0 +#define FO_WRITE 2 + +#define EM_WINDOW_REDRAW 1 +#define EM_KEY_PRESS 2 +#define EM_BUTTON_CLICK 4 +#define EM_APP_CLOSE 8 +#define EM_DRAW_BACKGROUND 16 +#define EM_MOUSE_EVENT 32 +#define EM_IPC 64 +#define EM_NETWORK 256 + +#define KM_CHARS 0 +#define KM_SCANS 1 + +#define WRS_BEGIN 1 +#define WRS_END 2 + +#define PROCESS_ID_SELF -1 + +#define abs(a) (a<0?0-a:a) + +extern "C" double __cdecl acos(double x); +extern "C" double __cdecl asin(double x); +extern "C" double __cdecl floor(double x); +extern "C" double __cdecl round(double x); +#pragma function(acos,asin) +#if _MSC_VER > 1200 +#pragma function(floor) +#endif + + +struct kosFileInfo +{ + Dword rwMode; + Dword OffsetLow; + Dword OffsetHigh; + Dword dataCount; + Byte *bufferPtr; + char fileURL[MAX_PATH]; +}; + + +struct RGB +{ + Byte b; + Byte g; + Byte r; + // + RGB() {}; + // + RGB( Dword value ) + { + r = (Byte)(value >> 16); + g = (Byte)(value >> 8); + b = (Byte)value; + }; + // + bool operator != ( RGB &another ) + { + return this->b != another.b || this->g != another.g || this->r != another.r; + }; + // + bool operator == ( RGB &another ) + { + return this->b == another.b && this->g == another.g && this->r == another.r; + }; +}; + + +#pragma pack(push, 1) +union sProcessInfo +{ + Byte rawData[1024]; + struct + { + Dword cpu_usage; + Word window_stack_position; + Word window_stack_value; + Word reserved1; + char process_name[12]; + Dword memory_start; + Dword used_memory; + Dword PID; + Dword x_start; + Dword y_start; + Dword x_size; + Dword y_size; + Word slot_state; + } processInfo; +}; +#pragma pack(pop) + +// +extern "C" void __cdecl crtStartUp(); +// +int __cdecl _purecall(); +// +int __cdecl atexit( void (__cdecl *func )( void )); +// +void rtlSrand( Dword seed ); +Dword rtlRand( void ); +// +char * __cdecl strcpy( char *target, const char *source ); +int __cdecl strlen( const char *line ); +char * __cdecl strrchr( const char * string, int c ); + +#if _MSC_VER < 1400 +extern "C" void * __cdecl memcpy( void *dst, const void *src, size_t bytesCount ); +extern "C" void memset( Byte *dst, Byte filler, Dword count ); +//#pragma intrinsic(memcpy,memset) +#else +void * __cdecl memcpy( void *dst, const void *src, size_t bytesCount ); +void memset( Byte *dst, Byte filler, Dword count ); +#endif + +unsigned int num2hex( unsigned int num ); +void sprintf( char *Str, char* Format, ... ); +// +Dword rtlInterlockedExchange( Dword *target, Dword value ); +// -1 +void __declspec(noreturn) kos_ExitApp(); +// 0 +void kos_DefineAndDrawWindow( + Word x, Word y, + Word sizeX, Word sizeY, + Byte mainAreaType, Dword mainAreaColour, + Byte headerType, Dword headerColour, + Dword borderColour + ); +// 1 +void kos_PutPixel( Dword x, Dword y, Dword colour ); +// 2 +bool kos_GetKey( Byte &keyCode ); +// 3 +Dword kos_GetSystemClock(); +// 4 +void kos_WriteTextToWindow( + Word x, Word y, + Byte fontType, + Dword textColour, + const char *textPtr, + Dword textLen + ); +// 7 +void kos_PutImage( RGB * imagePtr, Word sizeX, Word sizeY, Word x, Word y ); +// 8 +void __declspec(noinline) kos_DefineButton( Word x, Word y, Word sizeX, Word sizeY, Dword buttonID, Dword colour ); +// 5 , +void kos_Pause( Dword value ); +// 9 - +Dword kos_ProcessInfo( sProcessInfo *targetPtr, Dword processID = PROCESS_ID_SELF ); +// 10 +Dword kos_WaitForEvent(); +// 11 +Dword kos_CheckForEvent(); +// 12 +void kos_WindowRedrawStatus( Dword status ); +// 13 +void __declspec(noinline) kos_DrawBar( Word x, Word y, Word sizeX, Word sizeY, Dword colour ); +// 17 +bool kos_GetButtonID( Dword &buttonID ); +// 23 +Dword kos_WaitForEventTimeout( Dword timeOut ); +// +enum eNumberBase +{ + nbDecimal = 0, + nbHex, + nbBin +}; +// "" 37 +void kos_GetMouseState( Dword & buttons, int & cursorX, int & cursorY ); +// 40 +void kos_SetMaskForEvents( Dword mask ); +// 47 +void kos_DisplayNumberToWindow( + Dword value, + Dword digitsNum, + Word x, + Word y, + Dword colour, + eNumberBase nBase = nbDecimal, + bool valueIsPointer = false + ); +// 58 +Dword kos_FileSystemAccess( kosFileInfo *fileInfo ); +// 63 +void kos_DebugOutChar( char ccc ); +// +void rtlDebugOutString( char *str ); +// 64 , == -1 +void kos_ChangeWindow( Dword x, Dword y, Dword sizeX, Dword sizeY ); +// 67 , +bool kos_ApplicationMemoryResize( Dword targetSize ); +// 66 +void kos_SetKeyboardDataMode( Dword mode ); + +void kos_InitHeap(); + +// +void kos_Main(); diff --git a/programs/games/flappybird/smalllibc/math2.cpp b/programs/games/flappybird/smalllibc/math2.cpp new file mode 100644 index 0000000000..93c43346fc --- /dev/null +++ b/programs/games/flappybird/smalllibc/math2.cpp @@ -0,0 +1,83 @@ +#include +#include "kosSyst.h" +extern "C" int _fltused = 0; +double __cdecl acos(double x) +{ + __asm { + fld qword ptr [esp+4] + fld1 + fadd st, st(1) + fld1 + fsub st, st(2) + fmulp st(1), st + fsqrt + fxch st(1) + fpatan + } +} +double __cdecl asin(double x) +{ + __asm { + fld qword ptr [esp+4] + fld1 + fadd st, st(1) + fld1 + fsub st, st(2) + fmulp st(1), st + fsqrt + fpatan + ret + } +} +#if _MSC_VER <= 1200 +extern "C" double _ftol(double x) +{ + __asm { + fld qword ptr [esp+4] + push 1F3Fh + fstcw word ptr [esp+2] + fldcw word ptr [esp] + frndint + fldcw word ptr [esp+2] + add esp, 4 + } +} +#endif +double __cdecl ceil(double x) +{ + __asm { + fld qword ptr [esp+4] + push 1B3Fh + fstcw word ptr [esp+2] + fldcw word ptr [esp] + frndint + fldcw word ptr [esp+2] + add esp, 4 + } +} + +double __cdecl floor(double x) +{ + __asm { + fld qword ptr [esp+4] + push 173Fh + fstcw word ptr [esp+2] + fldcw word ptr [esp] + frndint + fldcw word ptr [esp+2] + add esp, 4 + } +} + +double __cdecl round(double x) +{ + __asm { + fld qword ptr [esp+4] + push 133Fh + fstcw word ptr [esp+2] + fldcw word ptr [esp] + frndint + fldcw word ptr [esp+2] + add esp, 4 + } +} diff --git a/programs/games/flappybird/smalllibc/mcsmemm.cpp b/programs/games/flappybird/smalllibc/mcsmemm.cpp new file mode 100644 index 0000000000..fb7edbbdd4 --- /dev/null +++ b/programs/games/flappybird/smalllibc/mcsmemm.cpp @@ -0,0 +1,373 @@ +// memman.cpp : Defines the entry point for the console application. +// + +#include "kosSyst.h" +#include "mcsmemm.h" + + +void * __cdecl operator new ( size_t count, size_t element_size ) +{ + return allocmem( (Dword)(count * element_size) ); +} + +void * __cdecl operator new [] ( size_t amount ) +{ + return allocmem( (Dword)amount ); +} + +void * __cdecl operator new ( size_t amount ) +{ + return allocmem( (Dword)amount ); +} + +void __cdecl operator delete ( void *pointer ) +{ + if ( pointer != NULL ) freemem( pointer ); +} + +void __cdecl operator delete [] ( void *pointer ) +{ + if ( pointer != NULL ) freemem( pointer ); +} + +void __cdecl initHeap(void) +{ + __asm + { + push 68 + pop eax + push 11 + pop ebx + int 0x40 + } +} + +#pragma data_seg(".CRT$XCB") +__declspec(allocate(".CRT$XCB")) void (__cdecl *initHeapPtr)(void) = &initHeap; + +__declspec(noinline) Byte* __fastcall allocmem( Dword reqsize ) +{ + initHeapPtr; // force dependency + __asm + { + push 68 + pop eax + push 12 + pop ebx + int 0x40 + } + +} + +__declspec(noinline) void __fastcall freemem( void *vaddress ) +{ + initHeapPtr; // force dependency + __asm + { + push 68 + pop eax + push 13 + pop ebx + int 0x40 + } + +} +/* + +// +Dword mmMutex = FALSE; +MemBlock *rootfree = NULL; +MemBlock *rootuser = NULL; +bool mmInitialized = false; +Byte *mmHeapTop = NULL; + + +// +Byte * AllocMemFromSystem( Dword reqSize ) +{ + Byte *result; + sProcessInfo pInfo; + + // + if ( mmInitialized ) + { + result = mmHeapTop; + } + else + { + // + kos_ProcessInfo( &pInfo ); + // + result = (Byte *)(pInfo.processInfo.used_memory + 1); + // + mmInitialized = true; + } + // + if ( ! kos_ApplicationMemoryResize( ((Dword)result) + reqSize ) ) + { + result = NULL; + } + // + mmHeapTop = result + reqSize; + // + return result; +} + + +// +Byte *allocmem( Dword reqsize ) +{ + MemBlock *BlockForCheck; + MemBlock *LastKnownGood; + Dword tail; + Byte *address; + + // + if( ( tail = reqsize % SIZE_ALIGN ) != 0 ) + { + reqsize += SIZE_ALIGN - tail; + } + + LastKnownGood = NULL; + + // + while ( rtlInterlockedExchange( &mmMutex, TRUE ) ) + { + // + kos_Pause( 1 ); + } + + // + if( rootfree != NULL ) + { + for ( BlockForCheck = rootfree; ; BlockForCheck = BlockForCheck->Next ) + { + if ( BlockForCheck->Size >= reqsize ) + { + // + if ( LastKnownGood != NULL ) + { + if ( LastKnownGood->Size >= BlockForCheck->Size ) + LastKnownGood = BlockForCheck; + } + else + LastKnownGood = BlockForCheck; + if ( LastKnownGood->Size == reqsize ) + break; + } + if ( BlockForCheck->Next == NULL ) + break; + } + } + + if ( LastKnownGood != NULL ) + { + // + tail = LastKnownGood->Size - reqsize; + if ( tail >= ( sizeof(MemBlock) + SIZE_ALIGN ) ) + { + // + BlockForCheck = (MemBlock *)( ( (Byte *)LastKnownGood ) + tail ); + BlockForCheck->Size = reqsize; + // + if( rootuser != NULL ) + { + BlockForCheck->Next = rootuser; + rootuser->Previous = BlockForCheck; + BlockForCheck->Previous = NULL; + rootuser = BlockForCheck; + } + else + { + rootuser = BlockForCheck; + BlockForCheck->Next = NULL; + BlockForCheck->Previous = NULL; + } + + // + LastKnownGood->Size = tail - sizeof(MemBlock); + address = ( (Byte *)BlockForCheck ) + sizeof(MemBlock); + + // + rtlInterlockedExchange( &mmMutex, FALSE ); + + return address; + } + else + { + // + // + if ( LastKnownGood->Previous != NULL ) + { + LastKnownGood->Previous->Next = LastKnownGood->Next; + } + else + { + // + rootfree = LastKnownGood->Next; + } + if( LastKnownGood->Next != NULL ) + { + LastKnownGood->Next->Previous = LastKnownGood->Previous; + } + // + if( rootuser != NULL ) + { + LastKnownGood->Next = rootuser; + rootuser->Previous = LastKnownGood; + LastKnownGood->Previous = NULL; + rootuser = LastKnownGood; + } + else + { + rootuser = LastKnownGood; + LastKnownGood->Next = NULL; + LastKnownGood->Previous = NULL; + } + // + address = ( (Byte *)LastKnownGood ) + sizeof(MemBlock); + + // + rtlInterlockedExchange( &mmMutex, FALSE ); + + return address; + } + } + else + { + // + LastKnownGood = (MemBlock *)AllocMemFromSystem( reqsize + sizeof(MemBlock) ); + // + if( LastKnownGood != NULL ) + { + LastKnownGood->Size = reqsize; + // + if( rootuser != NULL ) + { + LastKnownGood->Next = rootuser; + rootuser->Previous = LastKnownGood; + LastKnownGood->Previous = NULL; + rootuser = LastKnownGood; + } + else + { + rootuser = LastKnownGood; + LastKnownGood->Next = NULL; + LastKnownGood->Previous = NULL; + } + address = ( (Byte *)LastKnownGood ) + sizeof(MemBlock); + + // + rtlInterlockedExchange( &mmMutex, FALSE ); + + return address; + } + } + + // + rtlInterlockedExchange( &mmMutex, FALSE ); + + // + rtlDebugOutString( "allocmem failed." ); + kos_ExitApp(); + // + return NULL; +} + +// +Dword freemem( void *vaddress ) +{ + Dword result; + + Byte *checknext, *address = (Byte *)vaddress; + + // + while ( rtlInterlockedExchange( &mmMutex, TRUE ) ) + { + // + kos_Pause( 1 ); + } + + MemBlock *released = (MemBlock *)( address - sizeof(MemBlock) ); + + result = released->Size; + + // + if ( released->Previous != NULL ) + { + released->Previous->Next = released->Next; + } + else + { + rootuser = released->Next; + } + if ( released->Next != NULL ) + { + released->Next->Previous = released->Previous; + } + // + released->Next = rootfree; + released->Previous = NULL; + rootfree = released; + if ( released->Next != NULL ) + { + released->Next->Previous = released; + } + + // + checknext = (Byte *)(rootfree) + ( rootfree->Size + sizeof(MemBlock) ); + // + for ( released = rootfree->Next; released != NULL; released = released->Next ) + { + if ( checknext == (Byte *)released ) + { + // + // + released->Previous->Next = released->Next; + if( released->Next != NULL ) + { + released->Next->Previous = released->Previous; + } + // + rootfree->Size += released->Size + sizeof(MemBlock); + break; + } + } + // , . + checknext = (Byte *)(rootfree); + // + if ( released == NULL ) + { + for ( released = rootfree->Next; released != NULL; released = released->Next ) + { + if ( checknext == (Byte *)released + ( released->Size + sizeof(MemBlock) ) ) + { + // + // + released->Size += rootfree->Size + sizeof(MemBlock); + // + released->Previous->Next = released->Next; + if ( released->Next != NULL ) + { + released->Next->Previous = released->Previous; + } + // + if ( rootfree->Next != NULL ) + { + rootfree->Next->Previous = released; + } + released->Next = rootfree->Next; + released->Previous = NULL; + rootfree = released; + break; + } + } + } + + // + rtlInterlockedExchange( &mmMutex, FALSE ); + + return result; +} + +*/ \ No newline at end of file diff --git a/programs/games/flappybird/smalllibc/mcsmemm.h b/programs/games/flappybird/smalllibc/mcsmemm.h new file mode 100644 index 0000000000..6d03b242b7 --- /dev/null +++ b/programs/games/flappybird/smalllibc/mcsmemm.h @@ -0,0 +1,28 @@ +// + +struct MemBlock +{ + Dword Size; + Dword Addr; + MemBlock *Next; + MemBlock *Previous; +}; + + +#define INITIALQUEUESIZE (32 * 4) + +#define FALSE 0 +#define TRUE -1 + +#define MB_FREE 0 +#define MB_USER 1 + +#define SIZE_ALIGN 4 + + + +Byte * __fastcall allocmem( Dword reqsize ); +void __fastcall freemem( void *vaddress ); + + + diff --git a/programs/games/flappybird/smalllibc/mymath.h b/programs/games/flappybird/smalllibc/mymath.h new file mode 100644 index 0000000000..74ac89bd03 --- /dev/null +++ b/programs/games/flappybird/smalllibc/mymath.h @@ -0,0 +1,95 @@ +/* Rocket Forces + * Filename: mymath.h + * Version 0.1 + * Copyright (c) Serial 2007 + */ + + +extern "C" int _fltused = 0; + +#define M_PI 3.14159265358979323846 + +inline double sin(double x) +{ + __asm fld x + __asm fsin +} + +inline double cos(double x) +{ + __asm fld x + __asm fcos +} + +inline double sqrt(double x) +{ + __asm fld x + __asm fsqrt +} + +inline double acos(double x) +{ + __asm fld x + __asm fld st(0) + __asm fmul st,st(1) + __asm fld1 + __asm fsubrp st(1),st(0) + __asm fsqrt + __asm fxch st(1) + __asm fpatan +} + +inline double atan(double x) +{ + double res = acos(1 / sqrt(1 + x * x)); + if (x < 0) + { + res *= -1; + } + return res; +} + +inline int round_int(double x) +{ + int i; + static const float round_to_nearest = 0.5f; + __asm + { + fld x + fadd st, st(0) + fadd round_to_nearest + fistp i + sar i, 1 + } + return i; +} + +inline int floor_int(double x) +{ + int i; + static const float round_toward_m_i = -0.5f; + __asm + { + fld x + fadd st, st (0) + fadd round_toward_m_i + fistp i + sar i, 1 + } + return i; +} + +inline int ceil_int(double x) +{ + int i; + static const float round_toward_p_i = -0.5f; + __asm + { + fld x + fadd st, st (0) + fsubr round_toward_p_i + fistp i + sar i, 1 + } + return (-i); +} diff --git a/programs/games/flappybird/smalllibc/purecall.cpp b/programs/games/flappybird/smalllibc/purecall.cpp new file mode 100644 index 0000000000..3047c6e805 --- /dev/null +++ b/programs/games/flappybird/smalllibc/purecall.cpp @@ -0,0 +1,11 @@ +#include "kosSyst.h" +static char pureCallMessage[] = "PURE function call!"; + +// +int __cdecl _purecall() +{ + rtlDebugOutString( pureCallMessage ); + kos_ExitApp(); + return 0; +} + diff --git a/programs/games/flappybird/smalllibc/smalllibc.vcxproj b/programs/games/flappybird/smalllibc/smalllibc.vcxproj new file mode 100644 index 0000000000..f635577938 --- /dev/null +++ b/programs/games/flappybird/smalllibc/smalllibc.vcxproj @@ -0,0 +1,72 @@ + + + + + Release + Win32 + + + + {0D291390-1953-4E1F-BBE2-57F12AFF3214} + Win32Proj + smalllibc + + + + StaticLibrary + false + true + Unicode + + + + + + + + + + + + Level3 + + + MinSpace + true + true + NDEBUG;_LIB;%(PreprocessorDefinitions) + MultiThreaded + false + true + Size + + + Windows + true + true + true + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/programs/games/flappybird/smalllibc/smalllibc.vcxproj.filters b/programs/games/flappybird/smalllibc/smalllibc.vcxproj.filters new file mode 100644 index 0000000000..7f531d6c39 --- /dev/null +++ b/programs/games/flappybird/smalllibc/smalllibc.vcxproj.filters @@ -0,0 +1,60 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Заголовочные файлы + + + Заголовочные файлы + + + Заголовочные файлы + + + Заголовочные файлы + + + Заголовочные файлы + + + + + Файлы исходного кода + + + Файлы исходного кода + + + Файлы исходного кода + + + Файлы исходного кода + + + Файлы исходного кода + + + Файлы исходного кода + + + Файлы исходного кода + + + + + + \ No newline at end of file diff --git a/programs/games/flappybird/smalllibc/sprintf.cpp b/programs/games/flappybird/smalllibc/sprintf.cpp new file mode 100644 index 0000000000..d85286a2c9 --- /dev/null +++ b/programs/games/flappybird/smalllibc/sprintf.cpp @@ -0,0 +1,228 @@ +#include "kosSyst.h" +#include "func.h" +#include + +////////////////////////////////////////////////////////////////////// +// +// . barsuk %f + +//#define PREC 2 +//#define HALF 0.499 +#define PREC 6 +#define HALF 0.4999999 + +static double double_tab[]={1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, +1e16, 1e17, 1e18, 1e19, 1e20, 1e21, 1e22, 1e23, 1e24, 1e25, 1e26, 1e27, 1e28, 1e29, 1e30}; + + +// + +static Dword dectab[] = { 1000000000, 100000000, 10000000, 1000000, 100000, + 10000, 1000, 100, 10, 0 }; + +// +void sprintf( char *Str, char* Format, ... ) +{ + int i, fmtlinesize, j, k, flag; + Dword head, tail; + char c; + va_list arglist; + // + va_start(arglist, Format); + + // + fmtlinesize = strlen( Format ); + // + if( fmtlinesize == 0 ) return; + + // + for( i = 0, j = 0; i < fmtlinesize; i++ ) + { + // + c = Format[i]; + // + if( c != '%' ) + { + Str[j++] = c; + continue; + } + // + i++; + // + if( i >= fmtlinesize ) break; + + // + flag = 0; + // + c = Format[i]; + // + switch( c ) + { + // + case '%': + Str[j++] = c; + break; + // + case 'S': + Byte* str; + str = va_arg(arglist, Byte*); + for( k = 0; ( c = str[k] ) != 0; k++ ) + { + Str[j++] = c; + } + break; + // + case 'B': + k = va_arg(arglist, int) & 0xFF; + Str[j++] = num2hex( ( k >> 4 ) & 0xF ); + Str[j++] = num2hex( k & 0xF ); + break; + // + case 'C': + Str[j++] = va_arg(arglist, int) & 0xFF; + break; + // + case 'X': + Dword val; + val = va_arg(arglist, Dword); + for( k = 7; k >= 0; k-- ) + { + // + c = num2hex ( ( val >> (k * 4) ) & 0xF ); + // + if( c == '0' ) + { + if( flag ) Str[j++] = c; + } + else + { + flag++; + Str[j++] = c; + } + } + // + if( flag == 0 ) Str[j++] = '0'; + break; + // + case 'U': + head = va_arg(arglist, Dword); + tail = 0; + for( k = 0; dectab[k] != 0; k++ ) + { + tail = head % dectab[k]; + head /= dectab[k]; + c = head + '0'; + if( c == '0' ) + { + if( flag ) Str[j++] = c; + } + else + { + flag++; + Str[j++] = c; + } + // + head = tail; + } + // + c = head + '0'; + Str[j++] = c; + break; + // 7.2 + case 'f': + case 'F': + case 'g': + case 'G': + { + double val, w; + int p; + val = va_arg(arglist, double); + if (val < 0.0) + { + Str[j++] = '-'; + val = -val; + } + for (k = 0; k < 30; k++) + if (val < double_tab[k]) + break; + + if (val < 1.0) + { + Str[j++] = '0'; + } + + for (p = 1; p < k + 1; p++) + { + int d = (int)di(val / double_tab[k - p] - HALF) % 10; + Str[j++] = '0' + d; + val -= d * double_tab[k - p]; + } + Str[j++] = '.'; + w = 0.1; + for (p = 0; p < PREC - 1; p++) + { + val-=floor(val); + Str[j++] = '0' + di(val / w - HALF) % 10; + w /= 10.0; + } + } + break; + + // 64- + case 'Q': + unsigned int low_dword, high_dword; + low_dword = va_arg(arglist, unsigned int); + high_dword = va_arg(arglist, unsigned int); + for( k = 7; k >= 0; k-- ) + { + // + c = num2hex ( ( ( high_dword + 1) >> (k * 4) ) & 0xF ); + // + if( c == '0' ) + { + if( flag ) Str[j++] = c; + } + else + { + flag++; + Str[j++] = c; + } + } + // + for( k=7; k >= 0; k-- ) + { + // + c = num2hex ( ( low_dword >> (k * 4) ) & 0xF ); + // + if( c == '0' ) + { + if( flag ) Str[j++] = c; + } + else + { + flag++; + Str[j++] = c; + } + } + // + if( flag == 0 ) Str[j++] = '0'; + // + break; + // + default: + break; + } + } + // + Str[j] = 0; +} + +char *ftoa(double d) +{ + char buffer[256], *p; + sprintf(buffer, "%f", d); + p = (char*)allocmem(strlen(buffer)+1); + strcpy(p, buffer); + return p; +} +