📄 flarbopt.c
字号:
pApp->pIStatic = NULL;
}
ISHELL_CancelTimer(pApp->a.m_pIShell, NULL, NULL);
}
void FlarbOpt_ResumeGame(myapp_t* pApp)
{
if (pApp->pIMenu)
{
IMENUCTL_SetActive(pApp->pIMenu,TRUE);
IMENUCTL_Redraw(pApp->pIMenu);
}
else if (pApp->nGameMode == GAME_MODE_SCORES)
{
FlarbOpt_BuildScoreScreen(pApp);
ISTATIC_Redraw(pApp->pIStatic);
}
else if (pApp->nGameMode == GAME_MODE_PLAY)
{
FlarbOpt_DrawScreen(pApp, TRUE);
//set up timer
ISHELL_SetTimer(pApp->a.m_pIShell, TIMER_PERIOD, (PFNNOTIFY)FlarbOpt_TimerCallback, pApp);
}
}
void FlarbOpt_SetupGame(myapp_t* pApp)
{
pApp->bGameOver = FALSE;
pApp->bBullet = FALSE;
//the player starts at the bottom, centered
pApp->sprPlayer.nX = pApp->di.cxScreen / 2 - (pApp->sprPlayer.nWidth / 2);
pApp->sprPlayer.nY = pApp->di.cyScreen - pApp->sprPlayer.nHeight;
//the alien starts near the top
pApp->sprAlien.nX = 0;
pApp->sprAlien.nY = pApp->sprAlien.nHeight;
//Set the main game loop timer up
ISHELL_SetTimer(pApp->a.m_pIShell, TIMER_PERIOD, (PFNNOTIFY)FlarbOpt_TimerCallback, pApp);
}
void FlarbOpt_DrawScreen(myapp_t* pApp, boolean bForce)
{
AEERect rect;
AECHAR szTempBuf[] = {'%','d','\0'};
//clear background
if (bForce)
IDISPLAY_ClearScreen(pApp->a.m_pIDisplay);
//clear screen area underneath score
rect.x = 0;
rect.y = 0;
rect.dx = pApp->di.cxScreen;
rect.dy = 10;
IGRAPHICS_DrawRect(pApp->pIGraphics, &rect);
//draw score and number of lives
WSPRINTF(pApp->szBuf, (MAX_STRING_LENGTH * sizeof(AECHAR)), szTempBuf, pApp->nScore);
IDISPLAY_DrawText(pApp->a.m_pIDisplay, AEE_FONT_NORMAL, pApp->szBuf, -1, 5, 0, NULL, IDF_TEXT_TRANSPARENT);
FlarbOpt_DrawSprite(pApp, &pApp->sprPlayer, pApp->bForce);
FlarbOpt_DrawSprite(pApp, &pApp->sprAlien, pApp->bForce);
if (pApp->bBullet)
FlarbOpt_DrawSprite(pApp, &pApp->sprBullet, pApp->bForce);
//draw game over text
if (pApp->bGameOver)
{
ISHELL_LoadResString(pApp->a.m_pIShell, RES_FILE, STR_GAMEOVER, pApp->szBuf, sizeof(pApp->szBuf));
IDISPLAY_DrawText(pApp->a.m_pIDisplay, AEE_FONT_NORMAL, pApp->szBuf, -1, 0, 0, NULL, IDF_TEXT_TRANSPARENT | IDF_ALIGN_CENTER | IDF_ALIGN_MIDDLE);
}
pApp->bForce = FALSE;
//refresh screen
IDISPLAY_Update(pApp->a.m_pIDisplay);
}
void FlarbOpt_DrawSprite(myapp_t* pApp, sprite_t* pSprite, boolean bForce)
{
AEERect rect;
if (!pSprite)
return;
//if it's not dirty, and we aren't forcing a redraw, bail
if ( (!(pSprite->nFlags & SPRITEFLAG_DIRTY)) && !bForce)
return;
//make clearing rectangle
rect.x = pSprite->nOldX;
rect.y = pSprite->nOldY;
rect.dx = pSprite->nWidth;
rect.dy = pSprite->nHeight;
IGRAPHICS_DrawRect(pApp->pIGraphics, &rect);
//now draw sprite in new locaiton
IDISPLAY_BitBlt(pApp->a.m_pIDisplay, pSprite->nX, pSprite->nY, -1, -1, pApp->pRawImagePtrs[pSprite->nImage], 0, 0, AEE_RO_COPY);
pSprite->nFlags &= ~SPRITEFLAG_DIRTY;
}
void FlarbOpt_HideBullet(myapp_t* pApp)
{
AEERect rect;
pApp->bBullet = FALSE;
//clear the bullet's current position
rect.x = pApp->sprBullet.nX;
rect.y = pApp->sprBullet.nY;
rect.dx = pApp->sprBullet.nWidth;
rect.dy = pApp->sprBullet.nHeight + (pApp->sprBullet.nOldY - pApp->sprBullet.nY);
IGRAPHICS_DrawRect(pApp->pIGraphics, &rect);
}
void FlarbOpt_LoadResources(myapp_t* pApp)
{
//Load each image and fill sprite structure accordingly
FlarbOpt_LoadImage(pApp, IMAGE_SHIP, SPRITE_SHIP, &pApp->sprAlien);
FlarbOpt_LoadImage(pApp, IMAGE_PLAYER, SPRITE_PLAYER, &pApp->sprPlayer);
FlarbOpt_LoadImage(pApp, IMAGE_BULLET, SPRITE_BULLET, &pApp->sprBullet);
}
void FlarbOpt_ClearGUI(myapp_t* pApp)
{
//Release all valid GUI controls
if (pApp->pIMenu)
{
IMENUCTL_Release(pApp->pIMenu);
pApp->pIMenu = NULL;
}
if (pApp->pIStatic)
{
ISTATIC_Release(pApp->pIStatic);
pApp->pIStatic = NULL;
}
}
void FlarbOpt_SetGameMode(myapp_t* pApp, int nMode)
{
//Kill timer clear gui for all state changes
ISHELL_CancelTimer(pApp->a.m_pIShell, NULL, NULL);
FlarbOpt_ClearGUI(pApp);
switch (nMode)
{
case GAME_MODE_MENU:
FlarbOpt_BuildMainMenu(pApp);
break;
case GAME_MODE_PLAY:
pApp->bForce = TRUE;
pApp->nScore = 0;
pApp->nSpeed = START_SPEED;
FlarbOpt_SetupGame(pApp);
break;
case GAME_MODE_SCORES:
FlarbOpt_BuildScoreScreen(pApp);
break;
}
//This is now our current game mode
pApp->nGameMode = nMode;
}
void FlarbOpt_TimerCallback(myapp_t* pApp)
{
if (pApp->bPaused)
return;
if (pApp->bSuspended)
return;
if (pApp->bGameOver)
return;
//move objects
FlarbOpt_MoveEnemy(pApp);
FlarbOpt_MoveBullet(pApp);
//perform collision check if we have a bullet in play
if (pApp->bBullet)
FlarbOpt_DoCollision(pApp);
//redraw screen
FlarbOpt_DrawScreen(pApp, pApp->bForce);
//Set the timer up again--time for another go
ISHELL_SetTimer(pApp->a.m_pIShell, TIMER_PERIOD, (PFNNOTIFY)FlarbOpt_TimerCallback, pApp);
}
void FlarbOpt_Cleanup(myapp_t* pApp)
{
int i;
//remove GUI components, this will release
//memory tha they have allocated
FlarbOpt_ClearGUI(pApp);
for (i = 0; i < MAX_IMAGES; i++)
{
//release all bitmap resources
if (pApp->pBMPImagePtrs[i])
{
ISHELL_FreeResData(pApp->a.m_pIShell, pApp->pBMPImagePtrs[i]);
pApp->pBMPImagePtrs[i] = NULL;
}
//if we have allocated memory in the conversion, free that as well
if (pApp->pAllocFlags[i])
{
if (pApp->pRawImagePtrs[i])
{
SYSFREE(pApp->pRawImagePtrs[i]);
pApp->pRawImagePtrs[i] = NULL;
pApp->pAllocFlags[i] = 0;
}
}
}
//release graphics interface
if (pApp->pIGraphics)
{
IGRAPHICS_Release(pApp->pIGraphics);
pApp->pIGraphics = NULL;
}
}
void* FlarbOpt_LoadImage(myapp_t* pApp, int nResID, int nIndex, sprite_t* pSprite)
{
AEEBmp pbmSource;
AEEImageInfo imageInfo;
byte* pDataBytes;
boolean bVal = FALSE;
//pull the BMP from the resource file
pApp->pBMPImagePtrs[nIndex] = pbmSource = ISHELL_LoadResData(pApp->a.m_pIShell, RES_FILE, (short)nResID, RESTYPE_IMAGE);
if (pApp->pBMPImagePtrs[nIndex] == NULL)
return(NULL);
pDataBytes = (byte *)pbmSource + *((byte *)pbmSource);
pApp->pRawImagePtrs[nIndex] = CONVERTBMP(pDataBytes, &imageInfo, &bVal);
//if we have allocated memory in the conversion, signify
if (bVal)
{
pApp->pAllocFlags[nIndex] = 1;
}
else
pApp->pAllocFlags[nIndex] = 0;
//if we want to retain the sprite info, fill it in
if (pSprite)
{
pSprite->nWidth = (unsigned char)imageInfo.cx;
pSprite->nHeight = (unsigned char)imageInfo.cy;
pSprite->nImage = nIndex;
}
return(pApp->pRawImagePtrs[nIndex]);
}
void FlarbOpt_GameInput(myapp_t* pApp, uint16 wParam)
{
unsigned char nTempPos = pApp->sprPlayer.nX;
pApp->sprPlayer.nOldX = pApp->sprPlayer.nX;
pApp->sprPlayer.nOldY = pApp->sprPlayer.nY;
//if the game is over, return to the main menu
//instead of controlling the player
if (pApp->bGameOver)
{
FlarbOpt_SetGameMode(pApp, GAME_MODE_MENU);
return;
}
//move the ship or fire a bullet based on key input
switch (wParam)
{
case AVK_LEFT:
pApp->sprPlayer.nFlags |= SPRITEFLAG_DIRTY;
pApp->sprPlayer.nX -=1;
//check if we've hit the edge
if (pApp->sprPlayer.nX < pApp->sprPlayer.nWidth)
pApp->sprPlayer.nX = nTempPos;
break;
case AVK_RIGHT:
pApp->sprPlayer.nFlags |= SPRITEFLAG_DIRTY;
pApp->sprPlayer.nX += 1;
//check if we've hit the edge
if (pApp->sprPlayer.nX > (pApp->di.cxScreen - pApp->sprPlayer.nWidth))
pApp->sprPlayer.nX = nTempPos;
break;
case AVK_SELECT:
if (!pApp->bBullet)
{
//launch bullet
pApp->sprBullet.nX = (pApp->sprPlayer.nX + (pApp->sprPlayer.nWidth / 2) - (pApp->sprBullet.nWidth / 2));
pApp->sprBullet.nY = pApp->sprPlayer.nY - pApp->sprPlayer.nHeight + 12;
pApp->bBullet = TRUE;
}
break;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -