2016-01-27 00:50:42 chip
Page 1545
📢 PUBLIC
WinMain.c:
/*************************************************************************/
/** Game.cpp: Manages game objects. **/
/** (C)2016 nlited systems, Chip Doran **/
/*************************************************************************/
#include <Windows.h>
#include "Util.h"
#include "Globals.h"
#define ITEM_MAX 100 //Limit the number of INITIAL game objects.
class Game {
public:
static Game *Ptr(HGAME hGame);
static int Create(HGAME *phGame);
int Destroy(void);
int SetImage(HIMAGE hImg);
int SetArena(const RECT *pR);
int SetControl(UINT PlayerID, UINT Key, UINT OnOff);
int GetControl(UINT PlayerID, UINT *pCtrlState);
int Update(void);
int Event(UINT EventID);
//Data
UINT32 Signature;
private:
Game(void);
~Game(void);
int Create2(void);
int CreateItems(void);
int CreateItem(UINT ItemID);
void Sounds(void);
//Data
HITEMS hItems;
HIMAGE hImg;
RECT rArena;
UINT CtrlState;
HSOUND hSndFire;
HSOUND hSndThrust;
HSOUND hSndRockDestroyed;
};
/*************************************************************************/
/** Public interface **/
/*************************************************************************/
int GameCreate(HGAME *phGame) {
if(IsBadPtr(phGame,sizeof(*phGame),BADPTR_WRITE))
return(ERR_BADPTR);
return(Game::Create(phGame));
}
int GameDestroy(HGAME hGame) {
Game *pGame= Game::Ptr(hGame);
if(!pGame)
return(ERR_BADHANDLE);
return(pGame->Destroy());
}
int GameSetImage(HGAME hGame, HIMAGE hImg) {
Game *pGame= Game::Ptr(hGame);
if(!pGame)
return(ERR_BADHANDLE);
return(pGame->SetImage(hImg));
}
int GameSetArena(HGAME hGame, const RECT *pR) {
Game *pGame= Game::Ptr(hGame);
if(!pGame)
return(ERR_BADHANDLE);
if(IsBadPtr(pR,sizeof(*pR),BADPTR_READ))
return(ERR_BADPTR);
return(pGame->SetArena(pR));
}
int GameSetControl(HGAME hGame, UINT PlayerID, UINT Key, UINT OnOff) {
Game *pGame= Game::Ptr(hGame);
if(!pGame)
return(ERR_BADHANDLE);
return(pGame->SetControl(PlayerID,Key,OnOff));
}
int GameGetControl(HGAME hGame, UINT PlayerID, UINT *pCtrlState) {
Game *pGame= Game::Ptr(hGame);
if(!pGame)
return(ERR_BADHANDLE);
if(pCtrlState && IsBadPtr(pCtrlState,sizeof(*pCtrlState),BADPTR_WRITE))
return(ERR_BADPTR);
return(pGame->GetControl(PlayerID,pCtrlState));
}
int GameUpdate(HGAME hGame) {
Game *pGame= Game::Ptr(hGame);
if(!pGame)
return(ERR_BADHANDLE);
return(pGame->Update());
}
int GameEvent(HGAME hGame, UINT EventID) {
Game *pGame= Game::Ptr(hGame);
if(!pGame)
return(ERR_BADHANDLE);
return(pGame->Event(EventID));
}
/*************************************************************************/
/** Public internals **/
/*************************************************************************/
Game *Game::Ptr(HGAME hGame) {
Game *pGame= (Game*)hGame;
if(!hGame || IsBadPtr(pGame,sizeof(*pGame),BADPTR_RW) || pGame->Signature!=SIGNATURE_GAME)
pGame= 0;
return(pGame);
}
int Game::Create(HGAME *phGame) {
int Err= ERR_OK;
Game *pGame= new Game;
if(!pGame) {
Err= Error(ERR_NOMEM,__FUNCTION__": Unable to alloc %d.",sizeof(*pGame));
} else if(IsErr(Err= pGame->Create2())) {
delete pGame;
} else {
*phGame= (HGAME)pGame;
}
return(Err);
}
int Game::Destroy(void) {
int Err= ERR_OK;
SoundDestroy(hSndFire);
SoundDestroy(hSndThrust);
SoundDestroy(hSndRockDestroyed);
delete this;
return(Err);
}
int Game::SetImage(HIMAGE hImg) {
int Err= ERR_OK;
this->hImg= hImg;
return(Err);
}
int Game::SetArena(const RECT *pR) {
int Err= ERR_OK;
rArena= *pR;
ItemsSetArena(hItems,&rArena);
return(Err);
}
#define SETBIT(Var,Bit,OnOff) { if(OnOff) { Var|= Bit; } else { Var&= ~Bit; } }
int Game::SetControl(UINT PlayerID, UINT Key, UINT OnOff) {
int Err= ERR_OK;
switch(Key) {
case VK_LEFT: SETBIT(CtrlState,CTRL_LEFT,OnOff); break;
case VK_RIGHT: SETBIT(CtrlState,CTRL_RIGHT,OnOff); break;
case VK_DOWN: SETBIT(CtrlState,CTRL_THRUST,OnOff); break;
case VK_SPACE: SETBIT(CtrlState,CTRL_FIRE,OnOff); break;
}
return(Err);
}
int Game::GetControl(UINT PlayerID, UINT *pCtrlState) {
int Err= ERR_OK;
if(pCtrlState)
*pCtrlState= CtrlState;
else
Err= CtrlState;
return(Err);
}
int Game::Update(void) {
int Err= ERR_OK;
ItemsUpdate(hItems,GetTickCount());
Sounds();
ImgBegin(hImg);
ItemsDraw(hItems,hImg);
ImgPaint(hImg);
ImgEnd(hImg);
return(Err);
}
int Game::Event(UINT EventID) {
int Err= ERR_OK;
switch(EventID) {
case EVENT_ROCK_DESTROYED: SoundPlay(hSndRockDestroyed,0); break;
default: Err= ERR_BADARG2;
}
return(Err);
}
/*************************************************************************/
/** Private internals **/
/*************************************************************************/
Game::Game(void) {
memset(this,0,sizeof(*this));
Signature= SIGNATURE_GAME;
SetRect(&rArena,0,0,1000,800);
}
Game::~Game(void) {
Signature|= SIGNATURE_INVALID;
ImgDestroy(hImg);
}
int Game::Create2(void) {
int Err2,Err= ERR_OK;
if(IsErr(Err2= SoundCreate(&hSndFire,"Resource\\Fire.wav")))
Warn(Err2,"Game:Create2: Unable to create Fire sound.");
if(IsErr(Err2= SoundCreate(&hSndThrust,"Resource\\Thrust.wav")))
Warn(Err2,"Game:Create2: Unable to create thrust sound.");
if(IsErr(Err2= SoundCreate(&hSndRockDestroyed,"Resource\\RockDestroyed.wav")))
Warn(Err2,"Game:Create2: Unable to create RockDestroyed sound.");
if(IsErr(Err= ItemsCreate(&hItems,(HGAME)this))) {
Err= Error(Err,__FUNCTION__": Unable to create Items.");
} else if(IsErr(Err= CreateItems())) {
Err= Error(Err,__FUNCTION__": Unable to create starting items.");
}
return(Err);
}
int Game::CreateItems(void) {
int Err= ERR_OK;
UINT n1;
if(IsErr(Err= CreateItem(ITEM_ROCKET))) {
Err= Error(Err,__FUNCTION__": Unable to create rocket.");
} else {
for(n1=0;n1<ITEM_MAX;n1++) {
if(IsErr(Err= CreateItem(ITEM_ROCK))) {
Err= Warn(Err,__FUNCTION__": Unable to add Rock #%d",n1);
break;
}
}
}
return(Err);
}
int Game::CreateItem(UINT ItemID) {
int Err= ERR_OK;
HITEM hItem;
if(IsErr(Err= ItemCreate(&hItem,ItemID,0))) {
Err= Error(Err,__FUNCTION__": Unable to create ItemID %d",ItemID);
} else if(IsErr(Err= ItemsAdd(hItems,hItem))) {
Err= Error(Err,__FUNCTION__": Unable to add ItemID %d",ItemID);
}
return(Err);
}
void Game::Sounds(void) {
if(CtrlState & CTRL_FIRE)
SoundPlay(hSndFire,0);
if(CtrlState & CTRL_THRUST)
SoundPlay(hSndThrust,SNDPLAY_LOOP);
else
SoundStop(hSndThrust);
}
WebV7 (C)2018 nlited | Rendered by tikope in 83.862ms | 3.135.204.43