game.c
资源名称:eluosi方块.rar [点击查看]
上传用户:lwxs689
上传日期:2010-02-24
资源大小:130k
文件大小:35k
源码类别:
BREW编程
开发平台:
C/C++
- /*===========================================================================
- FILE: Game.c
- /*===============================================================================
- INCLUDES AND VARIABLE DEFINITIONS
- =============================================================================== */
- #include "AEEModGen.h"// Module interface definitions
- #include "AEEAppGen.h"// Applet interface definitions
- #include "AEEShell.h"// Shell interface definitions
- #include "game.bid" // Applet-specific header that contains class ID
- #include "game_h.bid"
- #include "AEEStdLib.h"
- #include "game_res.h"
- #define MAP_WIDTH 11
- #define MAP_HEIGHT 20
- #ifdef COLOR_DISPLAY
- #define RED_COLOR MAKE_RGB(0xC0, 0x00, 0x00)
- #define BLUE_COLOR MAKE_RGB(0, 0, 255)
- #define GREEN_COLOR MAKE_RGB(0x00, 0x60, 0x00)
- #define LIGHT_RED_COLOR MAKE_RGB(255, 0, 0)
- #define LIGHT_BLUE_COLOR MAKE_RGB(128, 255, 255)
- #define DARK_GREEN_COLOR MAKE_RGB(0, 128, 64)
- #define ORANGE_COLOR MAKE_RGB(255, 128, 0)
- #define PURPLE_COLOR MAKE_RGB(179, 21, 206)
- #define YELLOW_GREEN_COLOR MAKE_RGB(158, 152, 69)
- #define GREY_COLOR MAKE_RGB(80, 80, 80)
- #endif
- #define noblack FALSE
- #define black TRUE
- /*----------------------------------------------
- Structrue definition
- ------------------------------------------------*/
- typedef struct _CGametype{
- AEEApplet a;
- AEERect m_Rect;
- unsigned char block_width;
- unsigned char block_height;
- RGBVAL frame_color;
- int difficult_level;
- int a0;
- int dels;
- int next_block;
- int block[4][4];
- int map[MAP_WIDTH][MAP_HEIGHT];
- int map_line;
- int map_col;
- int x_size;
- int y_size;
- int block_x;
- int block_y;
- int current_block;
- int gameover;
- word score;
- boolean pause;
- RGBVAL color_table[10];
- int32 r_color;
- int32 g_color;
- int32 b_color;
- }CGametype;
- /*-------------------------------------------------------------------
- Static function prototypes
- -------------------------------------------------------------------*/
- static boolean game_HandleEvent(CGametype * pme, AEEEvent eCode,uint16 wParam, uint32 dwParam);
- static boolean game_InitApp(CGametype * pMe);
- void DeleteRow(CGametype * pMe);
- int CanPut(CGametype * pMe);
- void RotateBlock(CGametype * pMe);
- void PutBlock(CGametype * pMe);
- void ClearBlock(CGametype * pMe);
- /*-----------------------------------------------------------------
- static variable definition
- ----------------------------------------------------------------*/
- /*translate an integer into decimal sting*/
- unsigned int up_Itoa(AECHAR * buffer, int eger)
- {
- int len, ueger;
- unsigned char minusP;
- minusP = (eger < 0);
- if(minusP)
- {
- if(buffer)
- {
- *(buffer++) = '-';
- }
- ueger = (int)(0 - eger);
- }
- else
- {
- ueger = (unsigned int)eger;
- }
- {
- unsigned int tmp;
- tmp = ueger;
- len = 1;
- while ( (tmp /= 10) ) len += 1;
- }
- {
- unsigned int pt;
- pt = len;
- if (buffer) buffer[pt] = 0;
- while(pt--)
- {
- if (buffer)
- {
- buffer[pt] = (char)('0' + (ueger % 10));
- }
- ueger /= 10;
- }
- }
- return (len + 1 + minusP);
- }
- //=====================================================================
- // the following code for Russia frame
- //Function: clearblock
- //Description: clear a block
- //Side effect: none
- //=====================================================================
- void ClearBlock(CGametype * pMe) /*clear the data within the block*/
- {int i,j;
- for (i=0; i<4; i++) for (j=0; j<4; j++) pMe->block[i][j] = 0;
- }
- //====================================================================
- // Function: write_video
- // Description: draw a frame
- // input value position(x,y)
- // Side effect: play the bitmap_buf
- //====================================================================
- void write_video
- (
- CGametype * pMe,
- int x,
- int y,
- RGBVAL color,
- unsigned char full
- )
- {
- AEERect rect;
- rect.x=x;
- rect.y=y;
- rect.dx=pMe->block_width;
- rect.dy=pMe->block_width;
- if(full)
- {
- IDISPLAY_DrawRect(pMe->a.m_pIDisplay,
- &rect,
- RGB_BLACK,// RGBVAL
- color,// RGBVAL
- IDF_RECT_FRAME | IDF_RECT_FILL);
- SETAEERECT(&rect,x+1,y+1,pMe->block_width-2,1);
- IDISPLAY_FillRect(pMe->a.m_pIDisplay,&rect,RGB_WHITE);
- SETAEERECT(&rect,x+1,y+1,1,pMe->block_width-2);
- IDISPLAY_FillRect(pMe->a.m_pIDisplay,&rect,RGB_WHITE);
- }
- else
- {
- IDISPLAY_DrawRect(pMe->a.m_pIDisplay,
- &rect,
- RGB_WHITE,// RGBVAL
- RGB_WHITE,// RGBVAL
- IDF_RECT_FRAME | IDF_RECT_FILL);
- }
- }
- /*===================================================================
- Function: generateblock
- Description: generate a block
- input: block class
- Side effect: none
- ====================================================================*/
- void GenerateBlock(CGametype * pMe,int kind) /*generate a block*/
- {
- int val;
- val=kind%10;
- ClearBlock(pMe);
- switch (kind) {
- case 1: pMe->block[1][0] = pMe->block[2][0] = pMe->block[0][1] = pMe->block[1][1] = val; break;
- case 11: pMe->block[0][0] = pMe->block[0][1] = pMe->block[1][1] = pMe->block[1][2] = val; break;
- case 21: pMe->block[1][0] = pMe->block[2][0] = pMe->block[0][1] = pMe->block[1][1] = val; break;
- case 31: pMe->block[0][0] = pMe->block[0][1] = pMe->block[1][1] = pMe->block[1][2] = val; break;
- case 2: pMe->block[0][0] = pMe->block[1][0] = pMe->block[1][1] = pMe->block[2][1] = val; break;
- case 12: pMe->block[1][0] = pMe->block[1][1] = pMe->block[0][1] = pMe->block[0][2] = val; break;
- case 22: pMe->block[0][0] = pMe->block[1][0] = pMe->block[1][1] = pMe->block[2][1] = val; break;
- case 32: pMe->block[1][0] = pMe->block[1][1] = pMe->block[0][1] = pMe->block[0][2] = val; break;
- case 3: pMe->block[0][0] = pMe->block[0][1] = pMe->block[1][0] = pMe->block[1][1] = val; break;
- case 13: pMe->block[0][0] = pMe->block[0][1] = pMe->block[1][0] = pMe->block[1][1] = val; break;
- case 23: pMe->block[0][0] = pMe->block[0][1] = pMe->block[1][0] = pMe->block[1][1] = val; break;
- case 33: pMe->block[0][0] = pMe->block[0][1] = pMe->block[1][0] = pMe->block[1][1] = val; break;
- case 4: pMe->block[1][0] = pMe->block[0][1] = pMe->block[1][1] = pMe->block[2][1] = val; break;
- case 14: pMe->block[0][0] = pMe->block[0][1] = pMe->block[0][2] = pMe->block[1][1] = val; break;
- case 24: pMe->block[0][0] = pMe->block[1][0] = pMe->block[2][0] = pMe->block[1][1] = val; break;
- case 34: pMe->block[0][1] = pMe->block[1][0] = pMe->block[1][1] = pMe->block[1][2] = val; break;
- case 5: pMe->block[2][0] = pMe->block[0][1] = pMe->block[1][1] = pMe->block[2][1] = val; break;
- case 15: pMe->block[0][0] = pMe->block[0][1] = pMe->block[0][2] = pMe->block[1][2] = val; break;
- case 25: pMe->block[0][0] = pMe->block[1][0] = pMe->block[2][0] = pMe->block[0][1] = val; break;
- case 35: pMe->block[0][0] = pMe->block[1][0] = pMe->block[1][1] = pMe->block[1][2] = val; break;
- case 6: pMe->block[0][0] = pMe->block[0][1] = pMe->block[1][1] = pMe->block[2][1] = val; break;
- case 16: pMe->block[0][0] = pMe->block[1][0] = pMe->block[0][1] = pMe->block[0][2] = val; break;
- case 26: pMe->block[0][0] = pMe->block[1][0] = pMe->block[2][0] = pMe->block[2][1] = val; break;
- case 36: pMe->block[1][0] = pMe->block[1][1] = pMe->block[1][2] = pMe->block[0][2] = val; break;
- case 7: pMe->block[0][0] = pMe->block[1][0] = pMe->block[2][0] = pMe->block[3][0] = val; break;
- case 17: pMe->block[0][0] = pMe->block[0][1] = pMe->block[0][2] = pMe->block[0][3] = val; break;
- case 27: pMe->block[0][0] = pMe->block[1][0] = pMe->block[2][0] = pMe->block[3][0] = val; break;
- case 37: pMe->block[0][0] = pMe->block[0][1] = pMe->block[0][2] = pMe->block[0][3] = val; break;
- case 8: pMe->block[0][1] = pMe->block[1][0] = pMe->block[1][1] = pMe->block[1][2] = pMe->block[2][1] = val; break;
- case 18: pMe->block[0][1] = pMe->block[1][0] = pMe->block[1][1] = pMe->block[1][2] = pMe->block[2][1] = val; break;
- case 28: pMe->block[0][1] = pMe->block[1][0] = pMe->block[1][1] = pMe->block[1][2] = pMe->block[2][1] = val; break;
- case 38: pMe->block[0][1] = pMe->block[1][0] = pMe->block[1][1] = pMe->block[1][2] = pMe->block[2][1] = val; break;
- case 9: pMe->block[0][0] = pMe->block[0][1] = pMe->block[1][1] = pMe->block[2][0] = pMe->block[2][1] = val; break;
- case 19: pMe->block[0][0] = pMe->block[0][2] = pMe->block[1][0] = pMe->block[1][1] = pMe->block[1][2] = val; break;
- case 29: pMe->block[0][0] = pMe->block[0][1] = pMe->block[1][0] = pMe->block[2][0] = pMe->block[2][1] = val; break;
- case 39: pMe->block[0][0] = pMe->block[0][1] = pMe->block[0][2] = pMe->block[1][0] = pMe->block[1][2] = val; break;
- default: break;
- }
- if ((kind == 7) || (kind == 27)) {pMe->x_size = 4; pMe->y_size = 1; }
- else if ((kind == 17) || (kind == 37)) {pMe->x_size = 1; pMe->y_size = 4;}
- else if (kind % 10 == 3) {pMe->x_size = 2; pMe->y_size = 2; }
- else if (kind % 10 == 8) {pMe->x_size = 3; pMe->y_size = 3; }
- else if ((kind / 10 == 0) || (kind / 10 == 2)) {pMe->x_size = 3; pMe->y_size = 2;}
- else if ((kind / 10 == 1) || (kind / 10 == 3)) {pMe->x_size = 2; pMe->y_size = 3;}
- pMe->current_block = kind;
- }
- //================================================================
- //Funciton initialmemory
- //Description: initial memory
- //Side effect: none
- //================================================================
- void initialmemory(CGametype * pMe)
- {
- int i,j;
- AEERect rect;
- AECHAR szText[] ={'N','e','x','t',' ','S','c','o','r','e',' '},game_title[30]={0};
- uint32 dwTime = GET_UPTIMEMS();
- ISHELL_LoadResString(pMe->a.m_pIShell, GAME_RES_FILE, IDS_GAME_TITLE, game_title, sizeof(game_title));
- rect.x=0;
- rect.y=0;
- rect.dx=pMe->m_Rect.dx; //screen width
- rect.dy=pMe->block_height;
- IDISPLAY_DrawRect(pMe->a.m_pIDisplay,
- &rect,
- pMe->frame_color,// RGBVAL
- pMe->frame_color,// RGBVAL
- IDF_RECT_FRAME | IDF_RECT_FILL);
- rect.x=0;
- rect.y=MAP_HEIGHT*pMe->block_height+pMe->block_height;
- rect.dx=pMe->m_Rect.dx;//screen width
- rect.dy=pMe->block_height;
- IDISPLAY_DrawRect(pMe->a.m_pIDisplay,
- &rect,
- pMe->frame_color,// RGBVAL
- pMe->frame_color,// RGBVAL
- IDF_RECT_FRAME | IDF_RECT_FILL);
- rect.x=MAP_WIDTH*pMe->block_width+pMe->block_width;
- rect.y=pMe->block_height;
- rect.dx=pMe->block_width;
- rect.dy=MAP_HEIGHT*pMe->block_height;
- IDISPLAY_DrawRect(pMe->a.m_pIDisplay,
- &rect,
- pMe->frame_color,
- pMe->frame_color,
- IDF_RECT_FRAME | IDF_RECT_FILL);
- rect.x=0;
- rect.y=pMe->block_height;
- rect.dx=pMe->block_width;
- rect.dy=MAP_HEIGHT*pMe->block_height;
- IDISPLAY_DrawRect(pMe->a.m_pIDisplay,
- &rect,
- pMe->frame_color,// RGBVAL
- pMe->frame_color,// RGBVAL
- IDF_RECT_FRAME | IDF_RECT_FILL);
- rect.x=pMe->m_Rect.dx-pMe->block_width;
- rect.y=pMe->block_height;
- rect.dx=pMe->block_width;
- rect.dy=MAP_HEIGHT*pMe->block_height;
- IDISPLAY_DrawRect(pMe->a.m_pIDisplay,
- &rect,
- pMe->frame_color,// RGBVAL
- pMe->frame_color,// RGBVAL
- IDF_RECT_FRAME | IDF_RECT_FILL);
- IDISPLAY_DrawText(pMe->a.m_pIDisplay, // Display instance
- AEE_FONT_BOLD, // Use BOLD font
- szText, // Text - Normally comes from resource
- -1, // -1 = Use full string length
- MAP_WIDTH*pMe->block_width+1+pMe->block_width+pMe->block_width, // x-cordinate
- pMe->block_height+8, // y-cordinate
- NULL, // No clipping
- 0);//IDF_ALIGN_CENTER | IDF_ALIGN_MIDDLE);
- IDISPLAY_DrawText(pMe->a.m_pIDisplay, // Display instance
- AEE_FONT_BOLD, // Use BOLD font
- szText+5, // Text - Normally comes from resource
- -1, // -1 = Use full string length
- MAP_WIDTH*pMe->block_width+1+pMe->block_width+pMe->block_width, // x-cordinate
- MAP_HEIGHT*pMe->block_height-32+pMe->block_height, // y-cordinate
- NULL, // No clipping
- 0);//IDF_ALIGN_CENTER | IDF_ALIGN_MIDDLE);
- IDISPLAY_DrawText(pMe->a.m_pIDisplay, // Display instance
- AEE_FONT_BOLD, // Use BOLD font
- game_title, // Text - Normally comes from resource
- -1, // -1 = Use full string length
- 0, // x-cordinate
- MAP_HEIGHT*pMe->block_height+pMe->block_height+pMe->block_height, // y-cordinate
- NULL, // No clipping
- IDF_ALIGN_CENTER );
- for (i=0; i<pMe->map_line; i++)
- for (j=0; j<pMe->map_col; j++)
- pMe->map[i][j]=0;
- ClearBlock(pMe);
- pMe->a0=0;
- //map_line=24;
- //map_col=12;
- pMe->block_x=0;
- pMe->block_y=20;
- pMe->gameover=0;
- pMe->x_size=0;
- pMe->y_size=0;
- pMe->score=0;
- pMe->dels=0;
- pMe->pause=FALSE;
- pMe->next_block=pMe->x_size+pMe->y_size+pMe->block_x+pMe->block_y+dwTime;
- pMe->next_block=pMe->next_block%pMe->difficult_level+pMe->a0;
- pMe->next_block=pMe->next_block%pMe->difficult_level+1;
- }
- //================================================================
- //Function: restorememory
- //Description: get over the memory
- //Side effect: none
- //================================================================
- void restorememory(CGametype * pMe)
- {
- int i,j;
- AEERect rect;
- int temp = pMe->current_block;
- AECHAR szText[] ={'N','e','x','t',' ','S','c','o','r','e',' '},game_title[30]={0};
- AECHAR game_score[8]={0};
- ISHELL_LoadResString(pMe->a.m_pIShell,GAME_RES_FILE, IDS_GAME_TITLE, game_title, sizeof(game_title));
- rect.x=0;
- rect.y=0;
- rect.dx=120; //screen width
- rect.dy=pMe->block_height;
- IDISPLAY_DrawRect(pMe->a.m_pIDisplay,
- &rect,
- pMe->frame_color,// RGBVAL
- pMe->frame_color,// RGBVAL
- IDF_RECT_FRAME | IDF_RECT_FILL);
- rect.x=0;
- rect.y=MAP_HEIGHT*pMe->block_height+pMe->block_height;
- rect.dx=pMe->m_Rect.dx;//screen width
- rect.dy=pMe->block_height;
- IDISPLAY_DrawRect(pMe->a.m_pIDisplay,
- &rect,
- pMe->frame_color,// RGBVAL
- pMe->frame_color,// RGBVAL
- IDF_RECT_FRAME | IDF_RECT_FILL);
- rect.x=MAP_WIDTH*pMe->block_width+pMe->block_width;
- rect.y=pMe->block_height;
- rect.dx=pMe->block_width;
- rect.dy=MAP_HEIGHT*pMe->block_height;
- IDISPLAY_DrawRect(pMe->a.m_pIDisplay,
- &rect,
- pMe->frame_color,
- pMe->frame_color,
- IDF_RECT_FRAME | IDF_RECT_FILL);
- rect.x=0;
- rect.y=pMe->block_height;
- rect.dx=pMe->block_width;
- rect.dy=MAP_HEIGHT*pMe->block_height;
- IDISPLAY_DrawRect(pMe->a.m_pIDisplay,
- &rect,
- pMe->frame_color,// RGBVAL
- pMe->frame_color,// RGBVAL
- IDF_RECT_FRAME | IDF_RECT_FILL);
- rect.x=pMe->m_Rect.dx-pMe->block_width;
- rect.y=pMe->block_height;
- rect.dx=pMe->block_width;
- rect.dy=MAP_HEIGHT*pMe->block_height;
- IDISPLAY_DrawRect(pMe->a.m_pIDisplay,
- &rect,
- pMe->frame_color,// RGBVAL
- pMe->frame_color,// RGBVAL
- IDF_RECT_FRAME | IDF_RECT_FILL);
- IDISPLAY_DrawText(pMe->a.m_pIDisplay, // Display instance
- AEE_FONT_BOLD, // Use BOLD font
- szText, // Text - Normally comes from resource
- -1, // -1 = Use full string length
- MAP_WIDTH*pMe->block_width+1+pMe->block_width+pMe->block_width, // x-cordinate
- pMe->block_height+8, // y-cordinate
- NULL, // No clipping
- 0);//IDF_ALIGN_CENTER | IDF_ALIGN_MIDDLE);
- IDISPLAY_DrawText(pMe->a.m_pIDisplay, // Display instance
- AEE_FONT_BOLD, // Use BOLD font
- szText+5, // Text - Normally comes from resource
- -1, // -1 = Use full string length
- MAP_WIDTH*pMe->block_width+1+pMe->block_width+pMe->block_width, // x-cordinate
- MAP_HEIGHT*pMe->block_height-32+pMe->block_height, // y-cordinate
- NULL, // No clipping
- 0);//IDF_ALIGN_CENTER | IDF_ALIGN_MIDDLE);
- IDISPLAY_DrawText(pMe->a.m_pIDisplay, // Display instance
- AEE_FONT_BOLD, // Use BOLD font
- game_title, // Text - Normally comes from resource
- -1, // -1 = Use full string length
- 0, // x-cordinate
- MAP_HEIGHT*pMe->block_height+pMe->block_height+pMe->block_height, // y-cordinate
- NULL, // No clipping
- IDF_ALIGN_CENTER );
- up_Itoa(game_score,(int)pMe->score);
- IDISPLAY_DrawText(pMe->a.m_pIDisplay, // Display instance
- AEE_FONT_BOLD, // Use BOLD font
- game_score, // Text - Normally comes from resource
- -1, // -1 = Use full string length
- MAP_WIDTH*pMe->block_width+1+pMe->block_width, // x-cordinate
- MAP_HEIGHT*pMe->block_height-16+pMe->block_height, // y-cordinate
- NULL, // No clipping
- 0);//IDF_ALIGN_CENTER | IDF_ALIGN_MIDDLE);
- GenerateBlock(pMe,pMe->next_block);
- for (i=0; i<4; i++) {
- for (j=0; j<4; j++) {
- if (pMe->block[i][j] !=0)
- 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);
- else
- {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);}
- }
- }
- GenerateBlock(pMe,temp);
- }
- //=================================================================
- //Function: generatenew
- //Description: generate a new block
- //Side effect: none
- //=================================================================
- void GenerateNew(CGametype * pMe) /*generate a new block*/
- {int i,j;
- int temp = pMe->next_block;
- uint32 dwTime = GET_UPTIMEMS();
- pMe->next_block=pMe->x_size+pMe->y_size+pMe->block_x+dwTime;
- pMe->next_block=pMe->next_block%pMe->difficult_level+pMe->a0;
- pMe->next_block=pMe->next_block%pMe->difficult_level+1;
- pMe->a0=0;
- GenerateBlock(pMe,pMe->next_block);
- // for(i=0;i<4;i++)
- // for(j=0;j<4;j++)
- // write_video(pMe,(map_line+i)*pMe->block_width,(map_col+j)*pMe->block_width,noblack);
- for (i=0; i<4; i++) {
- for (j=0; j<4; j++) {
- if (pMe->block[i][j] !=0)
- 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);
- else
- {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);}
- }
- }
- GenerateBlock(pMe,temp);
- pMe->block_x = 4;
- pMe->block_y = 0;
- if (!CanPut(pMe)) {
- pMe->gameover=1;
- }
- }
- //=====================================================================
- //Function: putblock
- //Description: put a block into map
- //Side effect: none
- //=====================================================================
- void PutBlock(CGametype * pMe) /* put block data into map*/
- {int i,j;
- for (i=0; i<4; i++) {
- for (j=0; j<4; j++) {
- if (pMe->block[i][j] !=0)
- pMe->map[pMe->block_x+i][pMe->block_y+j] = pMe->block[i][j];
- }
- }
- DeleteRow(pMe);
- }
- //======================================================================
- //Function: rotateblock
- //Description: rotate the current block at the clockwise
- //Side effect: none
- //======================================================================
- void RotateBlock(CGametype * pMe) /*rotate the block*/
- {
- int old_block = pMe->current_block;
- pMe->current_block += 10;
- if (pMe->current_block > 40) pMe->current_block -= 40;
- GenerateBlock(pMe,pMe->current_block);
- if (!CanPut(pMe)) {
- pMe->current_block = old_block;
- GenerateBlock(pMe,pMe->current_block);
- }
- }
- //======================================================================
- //Function canput
- //Description: determine whether block is out of the map and go down the bottom of map
- //Side effect: none
- //======================================================================
- int CanPut(CGametype * pMe) /*check if block have been out of the range*/
- {int i,j;
- for (i=0; i<4; i++) {
- for (j=0; j<4; j++) {
- if (pMe->block[i][j] !=0) {
- if (pMe->block_x + i > pMe->map_line-1) return 0;
- if (pMe->block_x + i < 0) return 0;
- if (pMe->block_y + j > pMe->map_col-1) return 0;
- if (pMe->block_y + j < 0) return 0;
- if (pMe->map[pMe->block_x+i][pMe->block_y+j] != 0) return 0;
- }
- }
- }
- return 1;
- }
- //===============================================================
- //Function: drawmap
- //Description: refresh the whole map
- //Side effect: play with the display
- //===============================================================
- void DrawMap(CGametype * pMe) /*draw map*/
- {int i,j;
- for (i=0; i<pMe->map_line; i++)
- {
- for (j=0; j<pMe->map_col; j++)
- {
- if (pMe->map[i][j] !=0)
- {
- 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);
- }
- else
- write_video(pMe,(i)*pMe->block_width+pMe->block_width,(j)*pMe->block_width+pMe->block_height,RGB_NONE,noblack);
- }
- }
- for (i=0; i<pMe->x_size; i++)
- {
- for (j=0; j<pMe->y_size; j++)
- {
- if (pMe->block[i][j] != 0)
- 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);
- }
- }
- }
- //===================================================================
- //Function: deleterow
- //Description: delete one more rows
- //Side effect: play with the display
- //===================================================================
- void DeleteRow(CGametype * pMe) /*delete a line meet the requirement*/
- {
- int sum, i, j,k,del = 0;
- int a[]={0,1,3,5,7};
- AECHAR game_score[8];
- for (i=pMe->map_col-1; i>=0; i--)
- {
- sum = 0;
- for (j=0; j<pMe->map_line; j++) sum += pMe->map[j][i]?1:0;
- if (sum == pMe->map_line)
- {
- for (j=i; j>=1; j--) for (k=0; k<pMe->map_line; k++) pMe->map[k][j] = pMe->map[k][j-1];
- i++;
- del++;
- }
- }
- if(del!=0)
- {
- //ISHELL_Beep(pMe->a.m_pIShell, BEEP_ERROR, TRUE);
- pMe->dels += del;
- pMe->score+=del*100+(del-1)*25+(pMe->dels/10)*50;
- up_Itoa(game_score,(int)pMe->score);
- IDISPLAY_DrawText(pMe->a.m_pIDisplay, // Display instance
- AEE_FONT_BOLD, // Use BOLD font
- game_score, // Text - Normally comes from resource
- -1, // -1 = Use full string length
- MAP_WIDTH*pMe->block_width+1+pMe->block_width+pMe->block_width, // x-cordinate
- MAP_HEIGHT*pMe->block_height-16+pMe->block_height, // y-cordinate
- NULL, // No clipping
- 0);//IDF_ALIGN_CENTER | IDF_ALIGN_MIDDLE);
- }
- }
- //game over
- void Gameover(CGametype * pMe,uint32 r_color,uint32 g_color, uint32 b_color) /*draw map*/
- {int i,j;
- AECHAR game_over[] ={'G','a','m','e',' ','O','v','e','r','!',' '};
- for (i=0; i<pMe->map_line; i++)
- {
- for (j=0; j<pMe->map_col; j++)
- {
- 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);
- }
- }
- IDISPLAY_DrawText(pMe->a.m_pIDisplay, // Display instance
- AEE_FONT_BOLD, // Use BOLD font
- game_over, // Text - Normally comes from resource
- -1, // -1 = Use full string length
- pMe->block_height, // x-cordinate
- MAP_HEIGHT*pMe->block_height/2,// y-cordinate
- NULL, // No clipping
- IDF_ALIGN_CENTER | IDF_ALIGN_MIDDLE);
- }
- static void update_map(CGametype * pMe)
- {
- int timer=100;
- if(!pMe->gameover)
- {
- pMe->block_y++;
- if(CanPut(pMe)){
- DrawMap(pMe);
- }
- else
- {
- pMe->block_y--;
- PutBlock(pMe);
- GenerateNew(pMe);
- DrawMap(pMe);
- }
- timer=300-pMe->dels*4;
- timer=timer>100?timer:100;
- ISHELL_SetTimer(pMe->a.m_pIShell, 300, (PFNNOTIFY) update_map, (CGametype *) pMe);
- }
- else
- {
- Gameover(pMe,pMe->r_color,pMe->g_color,pMe->b_color);
- if(pMe->b_color==0&&pMe->g_color==0&&pMe->r_color==0)
- {
- ISHELL_CancelTimer(pMe->a.m_pIShell, (PFNNOTIFY) update_map, (CGametype *) pMe);
- }
- else
- {
- pMe->r_color=pMe->r_color*4/5;
- pMe->g_color=pMe->g_color*4/5;
- pMe->b_color=pMe->b_color*4/5;
- ISHELL_SetTimer(pMe->a.m_pIShell, 200, (PFNNOTIFY) update_map, (CGametype *) pMe);
- }
- }
- IDISPLAY_Update (pMe->a.m_pIDisplay);
- }
- //=======================================
- /*===========================================================================
- FUNCTION: AEEClsCreateInstance
- DESCRIPTION
- This function is invoked while the app is being loaded. All Modules must provide this
- function. Ensure to retain the same name and parameters for this function.
- In here, the module must verify the ClassID and then invoke the CGametype_New() function
- that has been provided in AEEAppGen.c.
- After invoking AEEApplet_New(), this function can do app specific initialization. In this
- example, a generic structure is provided so that app developers need not change app specific
- initialization section every time except for a call to InitAppData(). This is done as follows:
- InitAppData() is called to initialize AppletData instance. It is app developers
- responsibility to fill-in app data initialization code of InitAppData(). App developer
- is also responsible to release memory allocated for data contained in AppletData -- this can be
- done in FreeAppData().
- PROTOTYPE:
- int AEEAppCreateInstance(AEECLSID clsID, IShell* pIShell, IModule* pIModule,IApplet** ppApplet)
- PARAMETERS:
- clsID: [in]: Specifies the ClassID of the applet which is being loaded
- pIShell: [in]: Contains pointer to the IShell interface.
- pIModule: pin]: Contains pointer to the IModule interface to the current module to which
- this app belongs
- ppApplet: [out]: On return, *ppApplet must point to a valid AEEApplet structure. Allocation
- of memory for this structure and initializing the base data members is done by AEEApplet_New().
- DEPENDENCIES
- none
- RETURN VALUE
- SUCCESS: If the app needs to be loaded and if AEEApplet_New() invocation was successful
- EFAILED: If the app does not need to be loaded or if errors occurred in AEEApplet_New().
- If this function returns FALSE, the app will not be loaded.
- SIDE EFFECTS
- none
- ===========================================================================*/
- int AEEClsCreateInstance(AEECLSID ClsId,IShell * pIShell,IModule * pMod,void ** ppObj)
- {
- CGametype *pMe;
- *ppObj = NULL;
- if(AEEApplet_New( sizeof(CGametype), // Size of our private class
- ClsId, // Our class ID
- pIShell, // Shell interface
- pMod, // Module instance
- (IApplet**)ppObj, // Return object
- (AEEHANDLER)game_HandleEvent, // Our event handler
- NULL)) // No special "cleanup" function
- {
- pMe=*ppObj;
- if(game_InitApp(pMe))
- return(AEE_SUCCESS);
- }
- return (EFAILED);
- }
- /*===========================================================================
- FUNCTION game_HandleEvent
- DESCRIPTION
- This is the EventHandler for this app. All events to this app are handled in this
- function. All APPs must supply an Event Handler.
- Note - The switch statement in the routine is to demonstrate how event handlers are
- generally structured. However, realizing the simplicity of the example, the code could
- have been reduced as follows:
- if(eCode == EVT_APP_START){
- IDISPLAY_DrawText();
- IDISPLAY_Update();
- return(TRUE);
- }
- return(FALSE);
- However, while doing so would have demonstrated how BREW apps can be written in about 8
- lines of code (including the app creation function), it might have confused those who wanted
- a bit more practical example.
- Also note that the use of "szText" below is provided only for demonstration purposes. As
- indicated in the documentation, a more practical approach is to load text resources
- from the applicaton's resource file.
- Finally, note that the ONLY event that an applet must process is EVT_APP_START. Failure
- to return TRUE from this event will indicate that the app cannot be started and BREW
- will close the applet.
- PROTOTYPE:
- boolean game_HandleEvent(IApplet * pi, AEEEvent eCode, uint16 wParam, uint32 dwParam)
- PARAMETERS:
- pi: Pointer to the AEEApplet structure. This structure contains information specific
- to this applet. It was initialized during the AppCreateInstance() function.
- ecode: Specifies the Event sent to this applet
- wParam, dwParam: Event specific data.
- DEPENDENCIES
- none
- RETURN VALUE
- TRUE: If the app has processed the event
- FALSE: If the app did not process the event
- SIDE EFFECTS
- none
- ===========================================================================*/
- static boolean game_HandleEvent(CGametype * pMe, AEEEvent eCode, uint16 wParam, uint32 dwParam)
- {
- switch (eCode){
- case EVT_APP_START:
- initialmemory(pMe);
- GenerateNew(pMe);
- DrawMap(pMe);
- IDISPLAY_Update (pMe->a.m_pIDisplay);
- ISHELL_SetTimer(pMe->a.m_pIShell, 300, (PFNNOTIFY) update_map, (CGametype *) pMe);
- return(TRUE);
- case EVT_APP_RESUME:
- ISHELL_SetTimer(pMe->a.m_pIShell, 300, (PFNNOTIFY) update_map, (CGametype *) pMe);
- restorememory(pMe);
- return(TRUE);
- case EVT_APP_SUSPEND:
- case EVT_APP_STOP:
- ISHELL_CancelTimer(pMe->a.m_pIShell, (PFNNOTIFY) update_map, (CGametype *) pMe);
- return(TRUE);
- case EVT_KEY:
- switch (wParam)
- {
- case AVK_CLR:
- ISHELL_CancelTimer(pMe->a.m_pIShell, (PFNNOTIFY) update_map, (CGametype *) pMe);
- return(FALSE);
- case AVK_UP:
- if(!pMe->gameover)
- {
- pMe->a0++;
- RotateBlock(pMe);
- DrawMap(pMe);
- }
- break;
- case AVK_DOWN:
- if(pMe->gameover)
- break;
- else
- {
- while (CanPut(pMe))
- {
- //DrawMap(pMe);
- pMe->block_y++;
- //IDISPLAY_Update (pMe->a.m_pIDisplay);
- };
- pMe->block_y--;
- PutBlock(pMe);
- GenerateNew(pMe);
- DrawMap(pMe);
- }
- break;
- case AVK_LEFT:
- if(!pMe->gameover)
- {
- pMe->a0++;
- pMe->block_x--;
- if (!CanPut(pMe)) pMe->block_x++;
- DrawMap(pMe);
- }
- break;
- case AVK_RIGHT:
- if(!pMe->gameover)
- {
- pMe->a0++;
- pMe->block_x++;
- if (!CanPut(pMe)) pMe->block_x--;
- DrawMap(pMe);
- }
- break;
- }
- IDISPLAY_Update (pMe->a.m_pIDisplay);
- return(TRUE);
- default:
- break;
- }
- return(FALSE);
- }
- static boolean game_InitApp(CGametype * pMe)
- {
- AEEDeviceInfo devInfo;
- ISHELL_GetDeviceInfo(pMe->a.m_pIShell,&devInfo);
- pMe->block_width=devInfo.cxScreen/(MAP_WIDTH+8);
- pMe->block_height=devInfo.cyScreen/(MAP_HEIGHT+4);
- pMe->block_width=pMe->block_height=pMe->block_width>pMe->block_height?pMe->block_height:pMe->block_width;
- if(devInfo.nColorDepth>2)
- #ifdef COLOR_DISPLAY
- pMe->frame_color=GREY_COLOR;
- #else
- pMe->frame_color=RGB_BLACK;
- #endif
- pMe->difficult_level=7;
- SETAEERECT(&pMe->m_Rect,0,0,devInfo.cxScreen,devInfo.cyScreen);
- pMe->a0=0;
- pMe->dels=0;
- pMe->next_block=1;
- pMe->map_line=MAP_WIDTH;
- pMe->map_col=MAP_HEIGHT;
- pMe->block_x=0;
- pMe->block_y=5;
- pMe->gameover=0;
- pMe->score=0;
- pMe->pause=FALSE;
- #ifdef COLOR_DISPLAY
- pMe->color_table[0]=RGB_NONE;
- pMe->color_table[1]=RED_COLOR;
- pMe->color_table[2]=BLUE_COLOR;
- pMe->color_table[3]=GREEN_COLOR;
- pMe->color_table[4]=LIGHT_RED_COLOR;
- pMe->color_table[5]=LIGHT_BLUE_COLOR;
- pMe->color_table[6]=DARK_GREEN_COLOR;
- pMe->color_table[7]=ORANGE_COLOR;
- pMe->color_table[8]=PURPLE_COLOR;
- pMe->color_table[9]=YELLOW_GREEN_COLOR;
- pMe->r_color=0;
- pMe->g_color=128;
- pMe->b_color=255;
- #else
- pMe->color_table[0]=RGB_BLACK;
- pMe->color_table[1]=RGB_BLACK;
- pMe->color_table[2]=RGB_BLACK;
- pMe->color_table[3]=RGB_BLACK;
- pMe->color_table[4]=RGB_BLACK;
- pMe->color_table[5]=RGB_BLACK;
- pMe->color_table[6]=RGB_BLACK;
- pMe->color_table[7]=RGB_BLACK;
- pMe->color_table[8]=RGB_BLACK;
- pMe->color_table[9]=RGB_BLACK;
- pMe->r_color=225;
- pMe->g_color=225;
- pMe->b_color=225;
- #endif
- return TRUE;
- }