game.c
上传用户:lwxs689
上传日期:2010-02-24
资源大小:130k
文件大小:35k
源码类别:

BREW编程

开发平台:

C/C++

  1. /*===========================================================================
  2. FILE: Game.c
  3.   
  4. /*===============================================================================
  5. INCLUDES AND VARIABLE DEFINITIONS
  6. =============================================================================== */
  7. #include "AEEModGen.h"// Module interface definitions
  8. #include "AEEAppGen.h"// Applet interface definitions
  9. #include "AEEShell.h"// Shell interface definitions
  10. #include "game.bid" // Applet-specific header that contains class ID
  11. #include "game_h.bid"
  12. #include "AEEStdLib.h" 
  13. #include "game_res.h"
  14. #define MAP_WIDTH  11
  15. #define MAP_HEIGHT 20
  16. #ifdef COLOR_DISPLAY
  17. #define RED_COLOR MAKE_RGB(0xC0, 0x00, 0x00)
  18. #define BLUE_COLOR MAKE_RGB(0, 0, 255)
  19. #define GREEN_COLOR MAKE_RGB(0x00, 0x60, 0x00)
  20. #define LIGHT_RED_COLOR MAKE_RGB(255, 0, 0)
  21. #define LIGHT_BLUE_COLOR MAKE_RGB(128, 255, 255)
  22. #define DARK_GREEN_COLOR MAKE_RGB(0, 128, 64)
  23. #define ORANGE_COLOR MAKE_RGB(255, 128, 0)
  24. #define PURPLE_COLOR MAKE_RGB(179, 21, 206)
  25. #define YELLOW_GREEN_COLOR MAKE_RGB(158, 152, 69)
  26. #define GREY_COLOR MAKE_RGB(80, 80, 80)
  27. #endif
  28. #define noblack FALSE
  29. #define black TRUE
  30. /*----------------------------------------------
  31.  Structrue definition
  32. ------------------------------------------------*/
  33. typedef struct _CGametype{
  34. AEEApplet a;
  35. AEERect   m_Rect;
  36. unsigned char block_width;
  37. unsigned char block_height;
  38. RGBVAL frame_color;
  39. int difficult_level;
  40. int a0;
  41. int dels;
  42. int next_block;
  43. int block[4][4];
  44. int map[MAP_WIDTH][MAP_HEIGHT];
  45. int map_line;
  46. int map_col;
  47. int x_size;
  48. int y_size;
  49. int block_x;
  50. int block_y;
  51. int current_block;
  52. int gameover;
  53. word score;
  54. boolean pause;
  55. RGBVAL color_table[10];
  56. int32 r_color;
  57. int32 g_color;
  58. int32 b_color;
  59. }CGametype;
  60. /*-------------------------------------------------------------------
  61. Static function prototypes
  62. -------------------------------------------------------------------*/
  63. static boolean game_HandleEvent(CGametype * pme, AEEEvent eCode,uint16 wParam, uint32 dwParam);
  64. static boolean game_InitApp(CGametype * pMe);
  65. void DeleteRow(CGametype * pMe); 
  66. int CanPut(CGametype * pMe);   
  67. void RotateBlock(CGametype * pMe); 
  68. void PutBlock(CGametype * pMe);  
  69. void ClearBlock(CGametype * pMe);
  70. /*-----------------------------------------------------------------
  71.  static variable definition
  72. ----------------------------------------------------------------*/
  73. /*translate an integer into decimal sting*/
  74. unsigned int up_Itoa(AECHAR * buffer, int eger)
  75. {
  76. int len, ueger;
  77. unsigned char minusP;
  78. minusP = (eger < 0);
  79. if(minusP)
  80. {
  81. if(buffer) 
  82. {
  83. *(buffer++) = '-';
  84. }
  85. ueger = (int)(0 - eger);
  86. }
  87. else
  88. {
  89. ueger = (unsigned int)eger;
  90. }
  91. {
  92. unsigned int tmp;
  93. tmp = ueger;
  94. len = 1;
  95. while ( (tmp /= 10) ) len += 1;
  96. }
  97. {
  98. unsigned int pt;
  99. pt = len;
  100. if (buffer) buffer[pt] = 0;
  101. while(pt--) 
  102. {
  103. if (buffer)
  104. {
  105. buffer[pt] = (char)('0' + (ueger % 10));
  106. }
  107. ueger /= 10;
  108. }
  109. }
  110. return (len + 1 + minusP);
  111. }
  112. //=====================================================================
  113. // the following code for Russia frame
  114. //Function: clearblock
  115. //Description: clear a block
  116. //Side effect: none
  117. //=====================================================================
  118. void ClearBlock(CGametype * pMe) /*clear the data within the block*/
  119. {int i,j;
  120.   for (i=0; i<4; i++)  for (j=0; j<4; j++) pMe->block[i][j] = 0;
  121. }
  122. //====================================================================
  123. // Function: write_video
  124. // Description: draw a frame
  125. // input value position(x,y)
  126. // Side effect: play the bitmap_buf
  127. //====================================================================
  128. void write_video
  129. (
  130.       CGametype * pMe, 
  131.       int x,
  132.       int y,
  133.       RGBVAL color,
  134.       unsigned char full
  135.  )
  136. {
  137.      AEERect rect;
  138.      rect.x=x;
  139.      rect.y=y;
  140.      rect.dx=pMe->block_width;
  141.      rect.dy=pMe->block_width;
  142.      if(full)
  143.  {
  144.       IDISPLAY_DrawRect(pMe->a.m_pIDisplay,
  145.                            &rect,
  146.                            RGB_BLACK,// RGBVAL 
  147.                            color,// RGBVAL 
  148.                            IDF_RECT_FRAME | IDF_RECT_FILL);
  149. SETAEERECT(&rect,x+1,y+1,pMe->block_width-2,1);
  150. IDISPLAY_FillRect(pMe->a.m_pIDisplay,&rect,RGB_WHITE);
  151. SETAEERECT(&rect,x+1,y+1,1,pMe->block_width-2);
  152. IDISPLAY_FillRect(pMe->a.m_pIDisplay,&rect,RGB_WHITE);
  153.      }
  154.    else
  155.  {
  156.     IDISPLAY_DrawRect(pMe->a.m_pIDisplay,
  157.                            &rect,
  158.                            RGB_WHITE,// RGBVAL 
  159.                            RGB_WHITE,// RGBVAL 
  160.                            IDF_RECT_FRAME | IDF_RECT_FILL);
  161.  }
  162. }
  163. /*===================================================================
  164. Function: generateblock
  165. Description: generate a block
  166. input: block class
  167. Side effect: none
  168. ====================================================================*/
  169. void GenerateBlock(CGametype * pMe,int kind)   /*generate a block*/
  170. {
  171.       int val;
  172.   val=kind%10;
  173.   ClearBlock(pMe);
  174. switch (kind) {
  175. case  1: pMe->block[1][0] = pMe->block[2][0] = pMe->block[0][1] = pMe->block[1][1] = val; break;
  176.    case 11: pMe->block[0][0] = pMe->block[0][1] = pMe->block[1][1] = pMe->block[1][2] = val; break;
  177.    case 21: pMe->block[1][0] = pMe->block[2][0] = pMe->block[0][1] = pMe->block[1][1] = val; break;
  178.    case 31: pMe->block[0][0] = pMe->block[0][1] = pMe->block[1][1] = pMe->block[1][2] = val; break;
  179.    case  2: pMe->block[0][0] = pMe->block[1][0] = pMe->block[1][1] = pMe->block[2][1] = val; break;
  180. case 12: pMe->block[1][0] = pMe->block[1][1] = pMe->block[0][1] = pMe->block[0][2] = val; break;
  181. case 22: pMe->block[0][0] = pMe->block[1][0] = pMe->block[1][1] = pMe->block[2][1] = val; break;
  182. case 32: pMe->block[1][0] = pMe->block[1][1] = pMe->block[0][1] = pMe->block[0][2] = val; break;
  183. case  3: pMe->block[0][0] = pMe->block[0][1] = pMe->block[1][0] = pMe->block[1][1] = val; break;
  184. case 13: pMe->block[0][0] = pMe->block[0][1] = pMe->block[1][0] = pMe->block[1][1] = val; break;
  185. case 23: pMe->block[0][0] = pMe->block[0][1] = pMe->block[1][0] = pMe->block[1][1] = val; break;
  186. case 33: pMe->block[0][0] = pMe->block[0][1] = pMe->block[1][0] = pMe->block[1][1] = val; break;
  187. case  4: pMe->block[1][0] = pMe->block[0][1] = pMe->block[1][1] = pMe->block[2][1] = val; break;
  188. case 14: pMe->block[0][0] = pMe->block[0][1] = pMe->block[0][2] = pMe->block[1][1] = val; break;
  189.    case 24: pMe->block[0][0] = pMe->block[1][0] = pMe->block[2][0] = pMe->block[1][1] = val; break;
  190.    case 34: pMe->block[0][1] = pMe->block[1][0] = pMe->block[1][1] = pMe->block[1][2] = val; break;
  191.    case  5: pMe->block[2][0] = pMe->block[0][1] = pMe->block[1][1] = pMe->block[2][1] = val; break;
  192.    case 15: pMe->block[0][0] = pMe->block[0][1] = pMe->block[0][2] = pMe->block[1][2] = val; break;
  193.    case 25: pMe->block[0][0] = pMe->block[1][0] = pMe->block[2][0] = pMe->block[0][1] = val; break;
  194. case 35: pMe->block[0][0] = pMe->block[1][0] = pMe->block[1][1] = pMe->block[1][2] = val; break;
  195. case  6: pMe->block[0][0] = pMe->block[0][1] = pMe->block[1][1] = pMe->block[2][1] = val; break;
  196. case 16: pMe->block[0][0] = pMe->block[1][0] = pMe->block[0][1] = pMe->block[0][2] = val; break;
  197.    case 26: pMe->block[0][0] = pMe->block[1][0] = pMe->block[2][0] = pMe->block[2][1] = val; break;
  198.    case 36: pMe->block[1][0] = pMe->block[1][1] = pMe->block[1][2] = pMe->block[0][2] = val; break;
  199.    case  7: pMe->block[0][0] = pMe->block[1][0] = pMe->block[2][0] = pMe->block[3][0] = val; break;
  200. case 17: pMe->block[0][0] = pMe->block[0][1] = pMe->block[0][2] = pMe->block[0][3] = val; break;
  201.    case 27: pMe->block[0][0] = pMe->block[1][0] = pMe->block[2][0] = pMe->block[3][0] = val; break;
  202. case 37: pMe->block[0][0] = pMe->block[0][1] = pMe->block[0][2] = pMe->block[0][3] = val; break;
  203. case  8: pMe->block[0][1] = pMe->block[1][0] = pMe->block[1][1] = pMe->block[1][2] = pMe->block[2][1] = val; break;
  204. case 18: pMe->block[0][1] = pMe->block[1][0] = pMe->block[1][1] = pMe->block[1][2] = pMe->block[2][1] = val; break;
  205.     case 28: pMe->block[0][1] = pMe->block[1][0] = pMe->block[1][1] = pMe->block[1][2] = pMe->block[2][1] = val; break;
  206. case 38: pMe->block[0][1] = pMe->block[1][0] = pMe->block[1][1] = pMe->block[1][2] = pMe->block[2][1] = val; break;
  207. case  9: pMe->block[0][0] = pMe->block[0][1] = pMe->block[1][1] = pMe->block[2][0] = pMe->block[2][1] = val; break;
  208. case 19: pMe->block[0][0] = pMe->block[0][2] = pMe->block[1][0] = pMe->block[1][1] = pMe->block[1][2] = val; break;
  209.     case 29: pMe->block[0][0] = pMe->block[0][1] = pMe->block[1][0] = pMe->block[2][0] = pMe->block[2][1] = val; break;
  210. case 39: pMe->block[0][0] = pMe->block[0][1] = pMe->block[0][2] = pMe->block[1][0] = pMe->block[1][2] = val; break;
  211. default:  break;
  212. }
  213.    if ((kind == 7) || (kind == 27)) {pMe->x_size = 4; pMe->y_size = 1; }
  214. else if ((kind == 17) || (kind == 37)) {pMe->x_size = 1; pMe->y_size = 4;}
  215. else if (kind % 10 == 3) {pMe->x_size = 2; pMe->y_size = 2; }
  216. else if (kind % 10 == 8) {pMe->x_size = 3; pMe->y_size = 3; }
  217. else if ((kind / 10 == 0) || (kind / 10 == 2)) {pMe->x_size = 3; pMe->y_size = 2;}
  218. else if ((kind / 10 == 1) || (kind / 10 == 3)) {pMe->x_size = 2; pMe->y_size = 3;}
  219.    pMe->current_block = kind;
  220. }
  221. //================================================================
  222. //Funciton initialmemory
  223. //Description: initial memory
  224. //Side effect: none
  225. //================================================================
  226. void initialmemory(CGametype * pMe)
  227. {
  228. int i,j;
  229. AEERect rect;
  230. AECHAR szText[] ={'N','e','x','t','','S','c','o','r','e',''},game_title[30]={0};
  231. uint32   dwTime = GET_UPTIMEMS();
  232. ISHELL_LoadResString(pMe->a.m_pIShell, GAME_RES_FILE, IDS_GAME_TITLE, game_title, sizeof(game_title)); 
  233.      rect.x=0;
  234.      rect.y=0;
  235.      rect.dx=pMe->m_Rect.dx; //screen width
  236.      rect.dy=pMe->block_height;
  237.    IDISPLAY_DrawRect(pMe->a.m_pIDisplay,
  238.                            &rect,
  239.                            pMe->frame_color,// RGBVAL 
  240.                            pMe->frame_color,// RGBVAL 
  241.                            IDF_RECT_FRAME | IDF_RECT_FILL);
  242.      rect.x=0;
  243.      rect.y=MAP_HEIGHT*pMe->block_height+pMe->block_height;
  244.      rect.dx=pMe->m_Rect.dx;//screen width
  245.      rect.dy=pMe->block_height;
  246.    IDISPLAY_DrawRect(pMe->a.m_pIDisplay,
  247.                            &rect,
  248.                            pMe->frame_color,// RGBVAL 
  249.                            pMe->frame_color,// RGBVAL 
  250.                            IDF_RECT_FRAME | IDF_RECT_FILL);
  251.      rect.x=MAP_WIDTH*pMe->block_width+pMe->block_width;
  252.      rect.y=pMe->block_height;
  253.      rect.dx=pMe->block_width;
  254.      rect.dy=MAP_HEIGHT*pMe->block_height;
  255.    IDISPLAY_DrawRect(pMe->a.m_pIDisplay,
  256.                            &rect,
  257.                            pMe->frame_color,
  258.                            pMe->frame_color,
  259.                            IDF_RECT_FRAME | IDF_RECT_FILL);
  260.      rect.x=0;
  261.      rect.y=pMe->block_height;
  262.      rect.dx=pMe->block_width;
  263.      rect.dy=MAP_HEIGHT*pMe->block_height;
  264.    IDISPLAY_DrawRect(pMe->a.m_pIDisplay,
  265.                            &rect,
  266.                            pMe->frame_color,// RGBVAL 
  267.                            pMe->frame_color,// RGBVAL 
  268.                            IDF_RECT_FRAME | IDF_RECT_FILL);
  269.      rect.x=pMe->m_Rect.dx-pMe->block_width;
  270.      rect.y=pMe->block_height;
  271.      rect.dx=pMe->block_width;
  272.      rect.dy=MAP_HEIGHT*pMe->block_height;
  273.    IDISPLAY_DrawRect(pMe->a.m_pIDisplay,
  274.                            &rect,
  275.                            pMe->frame_color,// RGBVAL 
  276.                            pMe->frame_color,// RGBVAL 
  277.                            IDF_RECT_FRAME | IDF_RECT_FILL);
  278.       IDISPLAY_DrawText(pMe->a.m_pIDisplay,    // Display instance
  279.                            AEE_FONT_BOLD,       // Use BOLD font
  280.                            szText,              // Text - Normally comes from resource
  281.                            -1,                  // -1 = Use full string length
  282.                            MAP_WIDTH*pMe->block_width+1+pMe->block_width+pMe->block_width,   // x-cordinate
  283.                            pMe->block_height+8,                   // y-cordinate
  284.                            NULL,                // No clipping
  285.                            0);//IDF_ALIGN_CENTER | IDF_ALIGN_MIDDLE);
  286.       IDISPLAY_DrawText(pMe->a.m_pIDisplay,    // Display instance
  287.                            AEE_FONT_BOLD,       // Use BOLD font
  288.                            szText+5,              // Text - Normally comes from resource
  289.                            -1,                  // -1 = Use full string length
  290.                            MAP_WIDTH*pMe->block_width+1+pMe->block_width+pMe->block_width,  // x-cordinate
  291.                            MAP_HEIGHT*pMe->block_height-32+pMe->block_height,                   // y-cordinate
  292.                            NULL,                // No clipping
  293.                            0);//IDF_ALIGN_CENTER | IDF_ALIGN_MIDDLE);
  294.       IDISPLAY_DrawText(pMe->a.m_pIDisplay,    // Display instance
  295.                            AEE_FONT_BOLD,       // Use BOLD font
  296.                            game_title,              // Text - Normally comes from resource
  297.                            -1,                  // -1 = Use full string length
  298.                            0,   // x-cordinate
  299.                            MAP_HEIGHT*pMe->block_height+pMe->block_height+pMe->block_height,                   // y-cordinate
  300.                            NULL,                // No clipping
  301.                            IDF_ALIGN_CENTER );
  302.    
  303.     for (i=0; i<pMe->map_line; i++) 
  304. for (j=0; j<pMe->map_col; j++)
  305. pMe->map[i][j]=0;
  306. ClearBlock(pMe);
  307. pMe->a0=0;
  308.  
  309.     //map_line=24;
  310.     //map_col=12;
  311.     pMe->block_x=0;
  312.     pMe->block_y=20;
  313.     pMe->gameover=0;
  314.     pMe->x_size=0;
  315.     pMe->y_size=0;
  316.     pMe->score=0;
  317.     pMe->dels=0;
  318.     pMe->pause=FALSE;
  319.   pMe->next_block=pMe->x_size+pMe->y_size+pMe->block_x+pMe->block_y+dwTime;
  320.    pMe->next_block=pMe->next_block%pMe->difficult_level+pMe->a0;
  321.    pMe->next_block=pMe->next_block%pMe->difficult_level+1;
  322. }
  323. //================================================================
  324. //Function: restorememory
  325. //Description: get over the memory
  326. //Side effect: none
  327. //================================================================
  328. void restorememory(CGametype * pMe)
  329. {
  330.     int i,j;
  331. AEERect rect;
  332. int temp = pMe->current_block;
  333. AECHAR szText[] ={'N','e','x','t','','S','c','o','r','e',''},game_title[30]={0};
  334.     AECHAR game_score[8]={0};
  335.     ISHELL_LoadResString(pMe->a.m_pIShell,GAME_RES_FILE, IDS_GAME_TITLE, game_title, sizeof(game_title)); 
  336.      rect.x=0;
  337.      rect.y=0;
  338.      rect.dx=120; //screen width
  339.      rect.dy=pMe->block_height;
  340.    IDISPLAY_DrawRect(pMe->a.m_pIDisplay,
  341.                            &rect,
  342.                            pMe->frame_color,// RGBVAL 
  343.                            pMe->frame_color,// RGBVAL 
  344.                            IDF_RECT_FRAME | IDF_RECT_FILL);
  345.      rect.x=0;
  346.      rect.y=MAP_HEIGHT*pMe->block_height+pMe->block_height;
  347.      rect.dx=pMe->m_Rect.dx;//screen width
  348.      rect.dy=pMe->block_height;
  349.    IDISPLAY_DrawRect(pMe->a.m_pIDisplay,
  350.                            &rect,
  351.                            pMe->frame_color,// RGBVAL 
  352.                            pMe->frame_color,// RGBVAL 
  353.                            IDF_RECT_FRAME | IDF_RECT_FILL);
  354.      rect.x=MAP_WIDTH*pMe->block_width+pMe->block_width;
  355.      rect.y=pMe->block_height;
  356.      rect.dx=pMe->block_width;
  357.      rect.dy=MAP_HEIGHT*pMe->block_height;
  358.    IDISPLAY_DrawRect(pMe->a.m_pIDisplay,
  359.                            &rect,
  360.                            pMe->frame_color,
  361.                            pMe->frame_color,
  362.                            IDF_RECT_FRAME | IDF_RECT_FILL);
  363.      rect.x=0;
  364.      rect.y=pMe->block_height;
  365.      rect.dx=pMe->block_width;
  366.      rect.dy=MAP_HEIGHT*pMe->block_height;
  367.    IDISPLAY_DrawRect(pMe->a.m_pIDisplay,
  368.                            &rect,
  369.                            pMe->frame_color,// RGBVAL 
  370.                            pMe->frame_color,// RGBVAL 
  371.                            IDF_RECT_FRAME | IDF_RECT_FILL);
  372.      rect.x=pMe->m_Rect.dx-pMe->block_width;
  373.      rect.y=pMe->block_height;
  374.      rect.dx=pMe->block_width;
  375.      rect.dy=MAP_HEIGHT*pMe->block_height;
  376.    IDISPLAY_DrawRect(pMe->a.m_pIDisplay,
  377.                            &rect,
  378.                            pMe->frame_color,// RGBVAL 
  379.                            pMe->frame_color,// RGBVAL 
  380.                            IDF_RECT_FRAME | IDF_RECT_FILL);
  381.       IDISPLAY_DrawText(pMe->a.m_pIDisplay,    // Display instance
  382.                            AEE_FONT_BOLD,       // Use BOLD font
  383.                            szText,              // Text - Normally comes from resource
  384.                            -1,                  // -1 = Use full string length
  385.                            MAP_WIDTH*pMe->block_width+1+pMe->block_width+pMe->block_width,   // x-cordinate
  386.                            pMe->block_height+8,                   // y-cordinate
  387.                            NULL,                // No clipping
  388.                            0);//IDF_ALIGN_CENTER | IDF_ALIGN_MIDDLE);
  389.       IDISPLAY_DrawText(pMe->a.m_pIDisplay,    // Display instance
  390.                            AEE_FONT_BOLD,       // Use BOLD font
  391.                            szText+5,              // Text - Normally comes from resource
  392.                            -1,                  // -1 = Use full string length
  393.                            MAP_WIDTH*pMe->block_width+1+pMe->block_width+pMe->block_width,  // x-cordinate
  394.                            MAP_HEIGHT*pMe->block_height-32+pMe->block_height,                   // y-cordinate
  395.                            NULL,                // No clipping
  396.                            0);//IDF_ALIGN_CENTER | IDF_ALIGN_MIDDLE);
  397.       IDISPLAY_DrawText(pMe->a.m_pIDisplay,    // Display instance
  398.                            AEE_FONT_BOLD,       // Use BOLD font
  399.                            game_title,              // Text - Normally comes from resource
  400.                            -1,                  // -1 = Use full string length
  401.                            0,   // x-cordinate
  402.                            MAP_HEIGHT*pMe->block_height+pMe->block_height+pMe->block_height,                   // y-cordinate
  403.                            NULL,                // No clipping
  404.                            IDF_ALIGN_CENTER );
  405.   up_Itoa(game_score,(int)pMe->score);
  406.       IDISPLAY_DrawText(pMe->a.m_pIDisplay,    // Display instance
  407.                            AEE_FONT_BOLD,       // Use BOLD font
  408.                            game_score,              // Text - Normally comes from resource
  409.                            -1,                  // -1 = Use full string length
  410.                            MAP_WIDTH*pMe->block_width+1+pMe->block_width,   // x-cordinate
  411.                            MAP_HEIGHT*pMe->block_height-16+pMe->block_height,                   // y-cordinate
  412.                            NULL,                // No clipping
  413.                            0);//IDF_ALIGN_CENTER | IDF_ALIGN_MIDDLE);
  414. GenerateBlock(pMe,pMe->next_block);
  415. for (i=0; i<4; i++) {
  416.     for (j=0; j<4; j++) {
  417. if (pMe->block[i][j] !=0)
  418. write_video(pMe,(pMe->map_line+2+i)*pMe->block_width+pMe->block_width,(j)*pMe->block_height+pMe->block_height+24,pMe->color_table[pMe->block[i][j]],black);
  419. else 
  420. {write_video(pMe,(pMe->map_line+2+i)*pMe->block_width+pMe->block_width,(j)*pMe->block_height+pMe->block_height+24,RGB_NONE,noblack);}
  421. }
  422.   }
  423. GenerateBlock(pMe,temp);
  424. }
  425. //=================================================================
  426. //Function: generatenew
  427. //Description: generate a new block
  428. //Side effect: none
  429. //=================================================================
  430. void GenerateNew(CGametype * pMe)       /*generate a new block*/
  431. {int i,j;
  432.  int temp = pMe->next_block;
  433.  uint32   dwTime = GET_UPTIMEMS();
  434.    pMe->next_block=pMe->x_size+pMe->y_size+pMe->block_x+dwTime;
  435.    pMe->next_block=pMe->next_block%pMe->difficult_level+pMe->a0;
  436.    pMe->next_block=pMe->next_block%pMe->difficult_level+1;
  437.    pMe->a0=0;
  438.    
  439.    GenerateBlock(pMe,pMe->next_block);
  440.   // for(i=0;i<4;i++)
  441.   // for(j=0;j<4;j++)
  442.   // write_video(pMe,(map_line+i)*pMe->block_width,(map_col+j)*pMe->block_width,noblack);
  443. for (i=0; i<4; i++) {
  444.     for (j=0; j<4; j++) {
  445. if (pMe->block[i][j] !=0)
  446. write_video(pMe,(pMe->map_line+2+i)*pMe->block_width+pMe->block_width,(j)*pMe->block_height+pMe->block_height+24,pMe->color_table[pMe->block[i][j]],black);
  447. else 
  448. {write_video(pMe,(pMe->map_line+2+i)*pMe->block_width+pMe->block_width,(j)*pMe->block_height+pMe->block_height+24,RGB_NONE,noblack);}
  449. }
  450.   }
  451. GenerateBlock(pMe,temp);
  452.    pMe->block_x = 4;
  453.    pMe->block_y = 0;
  454.    if (!CanPut(pMe)) {
  455.       pMe->gameover=1;
  456.    }
  457. }
  458. //=====================================================================
  459. //Function: putblock
  460. //Description: put a block into map
  461. //Side effect: none
  462. //=====================================================================
  463. void PutBlock(CGametype * pMe)    /* put block data into map*/
  464. {int i,j;
  465.    for (i=0; i<4; i++) {
  466.       for (j=0; j<4; j++) {
  467.          if (pMe->block[i][j] !=0)
  468.          pMe->map[pMe->block_x+i][pMe->block_y+j] = pMe->block[i][j];
  469.       }
  470.    }
  471. DeleteRow(pMe);
  472. }
  473. //======================================================================
  474. //Function: rotateblock
  475. //Description: rotate the current block at the clockwise
  476. //Side effect: none
  477. //======================================================================
  478. void RotateBlock(CGametype * pMe) /*rotate the block*/
  479. {
  480.   int old_block = pMe->current_block;
  481.   pMe->current_block += 10;
  482.    if (pMe->current_block > 40) pMe->current_block -= 40;
  483.    GenerateBlock(pMe,pMe->current_block);
  484.    if (!CanPut(pMe))  {
  485.       pMe->current_block = old_block;
  486.       GenerateBlock(pMe,pMe->current_block);
  487.    }
  488. }
  489. //======================================================================
  490. //Function canput
  491. //Description: determine whether block is out of the map and go down the bottom of map
  492. //Side effect: none
  493. //======================================================================
  494. int CanPut(CGametype * pMe)     /*check if block have been out of the range*/
  495. {int i,j;
  496. for (i=0; i<4; i++) {
  497. for (j=0; j<4; j++) {
  498. if (pMe->block[i][j] !=0) {
  499.    if (pMe->block_x + i > pMe->map_line-1)  return 0;
  500.        if (pMe->block_x + i < 0)          return 0;
  501.    if (pMe->block_y + j > pMe->map_col-1)  return 0;
  502.        if (pMe->block_y + j < 0)          return 0;
  503.        if (pMe->map[pMe->block_x+i][pMe->block_y+j] != 0) return 0;
  504.          }
  505. }
  506.    }
  507. return 1;
  508. }
  509. //===============================================================
  510. //Function: drawmap
  511. //Description: refresh the whole map
  512. //Side effect: play with the display
  513. //===============================================================
  514. void DrawMap(CGametype * pMe)  /*draw map*/
  515. {int i,j;
  516. for (i=0; i<pMe->map_line; i++) 
  517. {
  518. for (j=0; j<pMe->map_col; j++)
  519. {
  520.         if (pMe->map[i][j] !=0)
  521.             {
  522.                   write_video(pMe,(i)*pMe->block_width+pMe->block_width,(j)*pMe->block_width+pMe->block_height,pMe->color_table[pMe->map[i][j]],black);
  523.           }
  524.         else
  525.            write_video(pMe,(i)*pMe->block_width+pMe->block_width,(j)*pMe->block_width+pMe->block_height,RGB_NONE,noblack);
  526.        }
  527.    }
  528. for (i=0; i<pMe->x_size; i++)
  529. {
  530.       for (j=0; j<pMe->y_size; j++)
  531.        {
  532.         if (pMe->block[i][j] != 0)
  533.                 write_video(pMe,(pMe->block_x+i)*pMe->block_width+pMe->block_width,(pMe->block_y+j)*pMe->block_width+pMe->block_height,pMe->color_table[pMe->block[i][j]],black);  
  534.     }
  535.     }
  536. }
  537. //===================================================================
  538. //Function: deleterow
  539. //Description: delete one more rows
  540. //Side effect: play with the display
  541. //===================================================================
  542. void DeleteRow(CGametype * pMe)  /*delete a line meet the requirement*/
  543. {
  544. int sum, i, j,k,del = 0;
  545. int a[]={0,1,3,5,7};
  546. AECHAR game_score[8];
  547. for (i=pMe->map_col-1; i>=0; i--) 
  548. {
  549.       sum = 0;
  550.   for (j=0; j<pMe->map_line; j++) sum += pMe->map[j][i]?1:0;
  551. if (sum == pMe->map_line)
  552. {
  553. for (j=i; j>=1; j--) for (k=0; k<pMe->map_line; k++) pMe->map[k][j] = pMe->map[k][j-1];
  554.   i++;
  555.           del++;
  556. }
  557.     }
  558. if(del!=0) 
  559.    {
  560.    //ISHELL_Beep(pMe->a.m_pIShell,  BEEP_ERROR, TRUE);
  561.    pMe->dels += del;
  562.    pMe->score+=del*100+(del-1)*25+(pMe->dels/10)*50;
  563.    up_Itoa(game_score,(int)pMe->score);
  564.    IDISPLAY_DrawText(pMe->a.m_pIDisplay,    // Display instance
  565.                            AEE_FONT_BOLD,       // Use BOLD font
  566.                            game_score,              // Text - Normally comes from resource
  567.                            -1,                  // -1 = Use full string length
  568.                            MAP_WIDTH*pMe->block_width+1+pMe->block_width+pMe->block_width,   // x-cordinate
  569.                            MAP_HEIGHT*pMe->block_height-16+pMe->block_height,                   // y-cordinate
  570.                            NULL,                // No clipping
  571.                            0);//IDF_ALIGN_CENTER | IDF_ALIGN_MIDDLE);
  572. }
  573. }
  574. //game over
  575. void Gameover(CGametype * pMe,uint32 r_color,uint32 g_color, uint32 b_color)  /*draw map*/
  576. {int i,j;
  577. AECHAR game_over[] ={'G','a','m','e',' ','O','v','e','r','!',''};
  578. for (i=0; i<pMe->map_line; i++) 
  579. {
  580. for (j=0; j<pMe->map_col; j++)
  581. {
  582.            write_video(pMe,(i)*pMe->block_width+pMe->block_width,(j)*pMe->block_width+pMe->block_height,MAKE_RGB(r_color, g_color, b_color),black);
  583.        }
  584.    }
  585. IDISPLAY_DrawText(pMe->a.m_pIDisplay,    // Display instance
  586.                            AEE_FONT_BOLD,       // Use BOLD font
  587.                            game_over,              // Text - Normally comes from resource
  588.                            -1,                  // -1 = Use full string length
  589.                            pMe->block_height,   // x-cordinate
  590.                            MAP_HEIGHT*pMe->block_height/2,// y-cordinate
  591.                            NULL,                // No clipping
  592.                            IDF_ALIGN_CENTER | IDF_ALIGN_MIDDLE);
  593. }
  594. static void update_map(CGametype * pMe)
  595. {
  596.  int timer=100;
  597.  
  598.   if(!pMe->gameover)
  599.   {
  600.    pMe->block_y++;
  601.           if(CanPut(pMe)){
  602.             DrawMap(pMe);
  603.          
  604.        }
  605.     else
  606.     {
  607.      pMe->block_y--;
  608.      PutBlock(pMe); 
  609. GenerateNew(pMe);
  610. DrawMap(pMe);
  611.     }
  612. timer=300-pMe->dels*4;
  613. timer=timer>100?timer:100;
  614. ISHELL_SetTimer(pMe->a.m_pIShell, 300, (PFNNOTIFY) update_map, (CGametype *) pMe);
  615. }
  616. else
  617. {
  618. Gameover(pMe,pMe->r_color,pMe->g_color,pMe->b_color);
  619. if(pMe->b_color==0&&pMe->g_color==0&&pMe->r_color==0)  
  620. {
  621. ISHELL_CancelTimer(pMe->a.m_pIShell, (PFNNOTIFY) update_map, (CGametype *) pMe);
  622. }
  623. else
  624. {
  625. pMe->r_color=pMe->r_color*4/5;
  626. pMe->g_color=pMe->g_color*4/5;
  627. pMe->b_color=pMe->b_color*4/5;
  628. ISHELL_SetTimer(pMe->a.m_pIShell, 200, (PFNNOTIFY) update_map, (CGametype *) pMe);
  629. }
  630. }
  631. IDISPLAY_Update (pMe->a.m_pIDisplay);
  632. }
  633. //=======================================
  634. /*===========================================================================
  635. FUNCTION: AEEClsCreateInstance
  636. DESCRIPTION
  637. This function is invoked while the app is being loaded. All Modules must provide this 
  638. function. Ensure to retain the same name and parameters for this function.
  639. In here, the module must verify the ClassID and then invoke the CGametype_New() function
  640. that has been provided in AEEAppGen.c. 
  641.    After invoking AEEApplet_New(), this function can do app specific initialization. In this
  642.    example, a generic structure is provided so that app developers need not change app specific
  643.    initialization section every time except for a call to InitAppData(). This is done as follows:
  644.    InitAppData() is called to initialize AppletData instance. It is app developers 
  645.    responsibility to fill-in app data initialization code of InitAppData(). App developer
  646.    is also responsible to release memory allocated for data contained in AppletData -- this can be
  647.    done in FreeAppData().
  648. PROTOTYPE:
  649. int AEEAppCreateInstance(AEECLSID clsID, IShell* pIShell, IModule* pIModule,IApplet** ppApplet)
  650. PARAMETERS:
  651. clsID: [in]: Specifies the ClassID of the applet which is being loaded
  652. pIShell: [in]: Contains pointer to the IShell interface. 
  653. pIModule: pin]: Contains pointer to the IModule interface to the current module to which
  654. this app belongs
  655. ppApplet: [out]: On return, *ppApplet must point to a valid AEEApplet structure. Allocation
  656. of memory for this structure and initializing the base data members is done by AEEApplet_New().
  657. DEPENDENCIES
  658.   none
  659. RETURN VALUE
  660.   SUCCESS: If the app needs to be loaded and if AEEApplet_New() invocation was successful
  661.   EFAILED: If the app does not need to be loaded or if errors occurred in AEEApplet_New().
  662.   If this function returns FALSE, the app will not be loaded.
  663. SIDE EFFECTS
  664.   none
  665. ===========================================================================*/
  666. int AEEClsCreateInstance(AEECLSID ClsId,IShell * pIShell,IModule * pMod,void ** ppObj)
  667. {
  668.    CGametype *pMe;
  669. *ppObj = NULL;
  670.    if(AEEApplet_New( sizeof(CGametype),                  // Size of our private class
  671.                      ClsId,                              // Our class ID
  672.                      pIShell,                            // Shell interface
  673.                      pMod,                               // Module instance
  674.                      (IApplet**)ppObj,                   // Return object
  675.                      (AEEHANDLER)game_HandleEvent, // Our event handler
  676.                      NULL))                              // No special "cleanup" function
  677.    {
  678.    pMe=*ppObj;
  679.    if(game_InitApp(pMe))
  680.    return(AEE_SUCCESS);
  681.    }
  682.       
  683. return (EFAILED);
  684. }
  685. /*===========================================================================
  686. FUNCTION game_HandleEvent
  687.   
  688. DESCRIPTION
  689.    This is the EventHandler for this app. All events to this app are handled in this
  690.    function. All APPs must supply an Event Handler.  
  691.    Note - The switch statement in the routine is to demonstrate how event handlers are 
  692.    generally structured.  However, realizing the simplicity of the example, the code could
  693.    have been reduced as follows:
  694.    if(eCode == EVT_APP_START){
  695.       IDISPLAY_DrawText();
  696.       IDISPLAY_Update();
  697.       return(TRUE);
  698.    }
  699.    return(FALSE);
  700.    However, while doing so would have demonstrated how BREW apps can be written in about 8
  701.    lines of code (including the app creation function), it might have confused those who wanted 
  702.    a bit more practical example.
  703.    Also note that the use of "szText" below is provided only for demonstration purposes.  As 
  704.    indicated in the documentation, a more practical approach is to load text resources
  705.    from the applicaton's resource file.
  706.    Finally, note that the ONLY event that an applet must process is EVT_APP_START.  Failure
  707.    to return TRUE from this event will indicate that the app cannot be started and BREW
  708.    will close the applet.
  709.     
  710. PROTOTYPE:
  711.    boolean game_HandleEvent(IApplet * pi, AEEEvent eCode, uint16 wParam, uint32 dwParam)
  712.       
  713. PARAMETERS:
  714.    pi: Pointer to the AEEApplet structure. This structure contains information specific
  715.    to this applet. It was initialized during the AppCreateInstance() function.
  716.         
  717.    ecode: Specifies the Event sent to this applet
  718.           
  719.    wParam, dwParam: Event specific data.
  720.             
  721. DEPENDENCIES
  722.    none
  723.               
  724. RETURN VALUE
  725.    TRUE: If the app has processed the event
  726.    FALSE: If the app did not process the event
  727.                 
  728. SIDE EFFECTS
  729.    none
  730. ===========================================================================*/
  731. static boolean game_HandleEvent(CGametype * pMe, AEEEvent eCode, uint16 wParam, uint32 dwParam)
  732. {  
  733.    switch (eCode){
  734.       case EVT_APP_START:  
  735.      
  736.    initialmemory(pMe);
  737. GenerateNew(pMe);
  738.         DrawMap(pMe);
  739.         IDISPLAY_Update (pMe->a.m_pIDisplay);
  740.        
  741.         ISHELL_SetTimer(pMe->a.m_pIShell, 300, (PFNNOTIFY) update_map, (CGametype *) pMe);
  742. return(TRUE);
  743.   case EVT_APP_RESUME:
  744. ISHELL_SetTimer(pMe->a.m_pIShell, 300, (PFNNOTIFY) update_map, (CGametype *) pMe);
  745.             restorememory(pMe);
  746. return(TRUE);
  747.   case EVT_APP_SUSPEND:
  748.       case EVT_APP_STOP:
  749.   ISHELL_CancelTimer(pMe->a.m_pIShell, (PFNNOTIFY) update_map, (CGametype *) pMe);
  750.          return(TRUE);
  751.   case EVT_KEY:
  752. switch (wParam)
  753.          {
  754. case AVK_CLR:
  755.        ISHELL_CancelTimer(pMe->a.m_pIShell, (PFNNOTIFY) update_map, (CGametype *) pMe);
  756.                return(FALSE);
  757. case AVK_UP:
  758. if(!pMe->gameover) 
  759. {
  760. pMe->a0++;
  761.              RotateBlock(pMe);
  762.            DrawMap(pMe);
  763. }
  764. break;
  765. case AVK_DOWN:
  766.          if(pMe->gameover)
  767.               break;
  768.                else
  769.                 {
  770.                   while (CanPut(pMe)) 
  771.                    {
  772.         //DrawMap(pMe);
  773. pMe->block_y++;
  774. //IDISPLAY_Update (pMe->a.m_pIDisplay);
  775. };
  776.        pMe->block_y--; 
  777.        PutBlock(pMe); 
  778. GenerateNew(pMe);
  779. DrawMap(pMe);
  780.                 }
  781. break;
  782. case AVK_LEFT:
  783.  if(!pMe->gameover)
  784.   {
  785.   pMe->a0++;
  786.            pMe->block_x--; 
  787. if (!CanPut(pMe)) pMe->block_x++;
  788. DrawMap(pMe);
  789. }
  790. break;
  791. case AVK_RIGHT:
  792. if(!pMe->gameover)
  793. {
  794. pMe->a0++;
  795.            pMe->block_x++; 
  796. if (!CanPut(pMe)) pMe->block_x--;
  797.            DrawMap(pMe);   
  798. }    
  799. break;
  800. }
  801.          IDISPLAY_Update (pMe->a.m_pIDisplay);
  802.  return(TRUE);
  803.   
  804.       default:
  805.          break;
  806.    }
  807.    return(FALSE);
  808. }
  809. static boolean game_InitApp(CGametype * pMe)
  810. {
  811. AEEDeviceInfo devInfo;
  812. ISHELL_GetDeviceInfo(pMe->a.m_pIShell,&devInfo);
  813. pMe->block_width=devInfo.cxScreen/(MAP_WIDTH+8);
  814. pMe->block_height=devInfo.cyScreen/(MAP_HEIGHT+4);
  815. pMe->block_width=pMe->block_height=pMe->block_width>pMe->block_height?pMe->block_height:pMe->block_width;
  816. if(devInfo.nColorDepth>2) 
  817. #ifdef COLOR_DISPLAY
  818. pMe->frame_color=GREY_COLOR;
  819. #else
  820. pMe->frame_color=RGB_BLACK;
  821. #endif
  822.   pMe->difficult_level=7;
  823. SETAEERECT(&pMe->m_Rect,0,0,devInfo.cxScreen,devInfo.cyScreen);
  824. pMe->a0=0;
  825. pMe->dels=0;
  826. pMe->next_block=1;
  827. pMe->map_line=MAP_WIDTH;
  828.     pMe->map_col=MAP_HEIGHT;
  829. pMe->block_x=0;
  830. pMe->block_y=5;
  831. pMe->gameover=0;
  832. pMe->score=0;
  833. pMe->pause=FALSE;
  834. #ifdef COLOR_DISPLAY
  835. pMe->color_table[0]=RGB_NONE;
  836. pMe->color_table[1]=RED_COLOR;
  837. pMe->color_table[2]=BLUE_COLOR;
  838. pMe->color_table[3]=GREEN_COLOR;
  839. pMe->color_table[4]=LIGHT_RED_COLOR;
  840. pMe->color_table[5]=LIGHT_BLUE_COLOR;
  841. pMe->color_table[6]=DARK_GREEN_COLOR;
  842. pMe->color_table[7]=ORANGE_COLOR;
  843. pMe->color_table[8]=PURPLE_COLOR;
  844. pMe->color_table[9]=YELLOW_GREEN_COLOR;
  845.     pMe->r_color=0;
  846. pMe->g_color=128;
  847. pMe->b_color=255;
  848. #else
  849. pMe->color_table[0]=RGB_BLACK;
  850. pMe->color_table[1]=RGB_BLACK;
  851. pMe->color_table[2]=RGB_BLACK;
  852. pMe->color_table[3]=RGB_BLACK;
  853. pMe->color_table[4]=RGB_BLACK;
  854. pMe->color_table[5]=RGB_BLACK;
  855. pMe->color_table[6]=RGB_BLACK;
  856. pMe->color_table[7]=RGB_BLACK;
  857. pMe->color_table[8]=RGB_BLACK;
  858. pMe->color_table[9]=RGB_BLACK;
  859.     pMe->r_color=225;
  860. pMe->g_color=225;
  861. pMe->b_color=225;
  862. #endif
  863. return TRUE;
  864. }