#include "cGame.h"
#include "Game.h"

cGame::cGame(){
	pPlayer = 0;
}

cGame::~cGame(){

}

BOOL cGame::Open(){
	pPlayer = new cGamePlayer;
	pPlayer->SetX(100);
	pPlayer->SetY(400);
	if(!OpenPictures())
		return FALSE;
	return TRUE;
}

void cGame::Close(){
	if(pPlayer){
		delete pPlayer;
		pPlayer = 0;
	}

}

BOOL cGame::OpenPictures(){
	struct sPictureInfo{
		char *FileName;
		int xAdjust;
		int yAdjust;
	} PicInfo[PicCount] = {
		"p0.bmp",  -42, -174,
		"p1.bmp",  -56, -167,
		"p2.bmp",  -42, -172,
		"p3.bmp", -119,  -45,
		"p4.bmp",  -42, -157,
		"p5.bmp",  -70, -161,
		"p6.bmp",  -48, -163,
		"p7.bmp",  -36, -174,

	};
	int i;

	for(i = 0; i < PicCount; i ++){
		if(!Pics[i].Create(PicInfo[i].FileName))
			return FALSE;
		Pics[i].SetAdjust(PicInfo[i].xAdjust,
			PicInfo[i].yAdjust);
	}
	return TRUE;	
}

void cGame::ClosePictures(){
	int i;

	i = PicCount;
	while(i --)
		Pics[i].Destroy();
}

eGameRunReturn cGame::Run(){
	pPlayer->Act();
	return GRR_CONTINUE;
}

void cGame::Draw(int xOrg, int yOrg){
	cPicture *pBackPic = GetBackPic();
	HDC hdc;

	if(!pBackPic->GetDC(&hdc)) return;
	Rectangle(hdc, xOrg, yOrg, xOrg + 640, yOrg + 480);
	pBackPic->ReleaseDC(hdc);

	pBackPic->BltAdjust(
		xOrg + pPlayer->GetX(),
		yOrg + pPlayer->GetY(),
		&Pics[pPlayer->GetPicNo()],
		DDBLT_WAIT | DDBLT_KEYSRC);

	GetFrontPic()->Blt(NULL, pBackPic, NULL);

}

