Subversion Repositories Kolibri OS

Rev

Rev 4484 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

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