⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 game.cpp

📁 一个3D的保龄球的源代码
💻 CPP
📖 第 1 页 / 共 5 页
字号:
// Game.cpp: implementation of the CGame class.
//
//////////////////////////////////////////////////////////////////////

#include "Game.h"
#include "..\System\resfile.h"
#include "..\System\Gfx.h"
#include "..\System\simplememory.h"
#include "..\System\Sysdef.h"
#include "..\System\bufreader.h"
#include "..\System\keyinput.h"
#include "..\System\imath.h"
#include "..\System\sound.h"
#include "..\3d\Vector.h"
#include "..\3d\Mesh.h"
#include "..\3d\Texture.h"
#include "..\3d\Graphics3D.h"
#include "..\System\font.h"
#include "..\System\Network.h"

#include "def.h"
#include "CScene.h"
#include "CPlayer.h"
#include "CTenpin.h"
#include "CCameraControl.h"
#include "Menu.h"
#include "Cid.h"



#define HEAPMALLOC(ret, length, type) \
{ ret = (type)m_pEngine->GetMem()->HeapMalloc(length);\
	SYS_ASSERT(NULL != ret);\
CSimpleMemory::Fill(ret, 0, length);}

#define GLOBALMALLOC(ret, length, type) \
{ ret = (type)m_pEngine->GetMem()->GlobalMalloc(length);\
	SYS_ASSERT(NULL != ret);\
CSimpleMemory::Fill(ret, 0, length);}

//#define DEFAULT_PINMODE 1023

CGame::CGame(CEngine *pEngine)
{
	
	m_pause=false;
	//	m_pMatch->m_smIsStoryMode=false;
	
	
	m_pEngine = pEngine;
	
	m_pDataManager = new CDataManager();
	m_pMenu = new CMenu(pEngine,m_pDataManager);
	m_pMatch = new CMatch(pEngine);
	m_pScene = new CScene(pEngine, m_pMatch, m_pDataManager);
	m_pUI = new CUI(pEngine);
	
	m_pDataManager->SetRender(&m_pScene->m_render3D);
	m_pDataManager->LoadResourceInfo(pEngine);
	m_pDataManager->LoadSounds(m_pEngine->GetSound());  
	m_pScene->Load();
	m_pMenu->Init();
	m_pMenu->SetUI(m_pUI);

	m_pUI->FirstInit(m_pEngine->GetGfx(),m_pEngine->GetFont());
	m_pUI->LoadRes(m_pEngine);

	m_fpsStart = m_LastTick = m_TimeTick = 0;
	m_iRollingTime[0] = 0;
	m_iRollingTime[1] = 0;
	m_bSetSceneStage = FALSE;
	m_bIsShowHint = TRUE;
	m_iTitleType = TITLE_FIRST_ANI;

	m_saveFlag=SAVE_NONE;
	m_bContinueStory=false;



	m_isSuspend=0;

	m_UnlockCoinLimit[0]=100;
	m_UnlockCoinLimit[1]=100;
	m_UnlockCoinLimit[2]=150;
	m_UnlockCoinLimit[3]=150;
	m_UnlockCoinLimit[4]=200;
	m_UnlockCoinLimit[5]=200;

	m_pMatch->m_cnMatchCount=0;
	
	// money system init
	moneyInit();

	if(!loadGeneralRecord())// load record from save file
	{
		initGeneralRecord();	// reset record
		m_saveFlag=saveGeneralRecord(SAVE_GENERAL,1);
	}
	else
	{
		m_saveFlag=saveGeneralRecord(SAVE_OPTION,2);//TEST THE SAVE FUNCTION	
	}
	m_chcode = false;

	//from INIT	
	m_GameState=GS_MENU;
	m_pMenu->ResetState();
	m_pMenu->SetState(STATE_PREGAME);
	m_pMenu->m_WindDur=GETUPTIMEMS();

	m_iDrawCount = 0;
	m_iCurSound = -1;//ConvertSoundID(ID_SOUND_CLUB_1);
}



CGame::~CGame()
{

	if(m_pMatch) 
	{
		delete m_pMatch;
		m_pMatch = NULL;
	}
	
	if(m_pMenu)
	{
		delete m_pMenu;
		m_pMenu=NULL;
	}
	
	if(m_pScene)
	{
		delete m_pScene;
		m_pScene=NULL;
	}
	
	if(m_pUI)
	{
		delete m_pUI;
		m_pUI=NULL;
	}
	
	if(m_pDataManager)
	{
		delete m_pDataManager;
		m_pDataManager=NULL;
	}
}

//DEL void CGame::fillScreen(int color, bool viewOnly) 
//DEL {
//DEL 	CGfx *g = m_pEngine->GetGfx();
//DEL 	
//DEL 	if(viewOnly) {    // fill only in viewport
//DEL 		g->FillRec(VIEW_LEFT, VIEW_TOP, VIEW_WIDTH, VIEW_HEIGHT, g->GetNativeColor(color));
//DEL 	}
//DEL 	else {
//DEL 		g->FillRec(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, g->GetNativeColor(color));
//DEL 	}
//DEL }


void CGame::drawRoundDelivery()
{
	m_pScene->DrawRoundDelivery(m_MatchState,  m_iRollingTime[1], m_SaveQMFinishTime,
							     m_bIsShowHint,m_pUI, m_StoryModeState);


	// only for money system
	if(m_MatchState == MS_MONEY_RESULT)// draw result normal game(except bonus mode)
	{
		int type=0;
		int id=0;
		const char *p;
		
		//get unlocked item type and id
		getFirstUnlock(m_money,type,id);
		
		//get the name of the unlocked item
		if(SHIFT_LOCK_BALL==type)
			p=m_pDataManager->m_pBallName[id];
		else if (SHIFT_LOCK_CHARACTER==type)
			p=m_pDataManager->m_pPlayer[id].name;
		else if(SHIFT_LOCK_SUIT==type)
			p=m_pDataManager->m_pPlayer[id].name;
		
		switch(m_moneyState) {
		case STATE_MONEY_RESULT:
		case STATE_MONEY_RESULT_DONE:
//			m_pEngine->GetSound()->playMIDI(ConvertSoundID(ID_SOUND_REPUTATION),0);
			GetUI()->RepResult(m_money,m_counterForMoney, m_pMatch->GetPlayerCount()==2 , m_money.winBonus!=0);// show result
			break;
		case STATE_MONEY_UNLOCK:
			GetUI()->Unlock(type,p,1);// show unlock item
			break;
		case STATE_MONEY_UNLOCK_DONE:	
			if(m_money.newUnlock)
				GetUI()->Unlock(type,p,1);// show result
			else
				GetUI()->RepResult(m_money,m_counterForMoney,m_pMatch->GetPlayerCount()==2 , m_money.winBonus!=0);// show unlock item
			break;
		case STATE_MONEY_ASK_RETRY:
			m_pEngine->GetGfx()->DrawColorMask(MAKERGB(128, 128, 128), 0, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
			GetUI()->TVScreen();
			GetUI()->DrawMenuString(45,70,"ANOTHER GAME?",0,1);
			if( bRetryState )
			{
				GetUI()->DrawMenuString(52,100,"YES",0,0);
				GetUI()->DrawMenuString(105,100,"NO",0,1);
			}
			else{
				GetUI()->DrawMenuString(52,100,"YES",0,1);
				GetUI()->DrawMenuString(105,100,"NO",0,0);
			}
			break;
		}
		
	}
	if (m_MatchState==MS_BONUS_MONEY_BET)// begin to draw bet
	{
		int i=0;
		int min=0;
		for(i=0;(m_pMatch->m_BonusId)>BET_MONEY_LEVEL_lIMIT_MAPPING[i];++i);
		
		GetUI()->Bet(min,BET_MONEY_lIMIT[i],m_money.currBet,m_money.totalMoney-m_money.currBet);
	}


	if (MS_BONUS_WATING==m_MatchState)// display bonus result
	{
		int money=m_money.currBet;
		int type=0;
		int id=0;
		int i=0;
		for(i=0;(m_pMatch->m_BonusId)>BET_MONEY_LEVEL_lIMIT_MAPPING[i];++i);
		const char *p;
		
		switch(m_moneyState) {
		case STATE_MONEY_RESULT:
		case STATE_MONEY_RESULT_DONE:
			GetUI()->BetResult(money,BET_MONEY_INCREMENT[i],m_money.totalMoney,m_counterForMoney,m_money.currSpareCount);			
			break;
		case STATE_MONEY_UNLOCK:
		case STATE_MONEY_UNLOCK_DONE:	
		
		getFirstUnlock(m_money,type,id);

		if(SHIFT_LOCK_BALL==type)
			p=m_pDataManager->m_pBallName[id];
		else if (SHIFT_LOCK_CHARACTER==type)
			p=m_pDataManager->m_pPlayer[id].name;
		else if(SHIFT_LOCK_SUIT==type)
			p=m_pDataManager->m_pPlayer[id].name;
		
		GetUI()->Unlock(type,p,(m_money.currSpareCount!=0));// reputation
			
			break;
	
		}
	}

}




// release memory from stack and heap
void CGame::ReleaseLocalResource()
{
	m_pEngine->GetMem()->ResetHeap();
	m_pEngine->GetMem()->ResetStack();
	m_pScene->m_render3D.ReleaseModel();
	m_pScene->m_render3D.GetG3d()->ReleaseTextures();
	m_pScene->m_ball.m_Texture.m_pData = NULL;
}

// load save record from save file
bool CGame::loadGeneralRecord(int LoadStory)
{
	//RecordStore record = null;
	int bytenum = 6+BALL_COUNT+BONUS_COUNT+PLAYER_COUNT;
	bytenum+=PLAYER_COUNT*CLOTH_COUNT+BALLICON_COUNT+LANE_COUNT*2;
	bytenum+=23+sizeof(ScoreInfo)*2+40;
	bytenum+=12+24+1;
	bytenum+=sizeof(long)+(TOTAL_BALL+TOTAL_PLAYER+TOTAL_SUIT*TOTAL_PLAYER)*sizeof(boolean);// for money system
	bytenum+=2;
	
	//char data[SAVE_DATA_LENGTH];
	unsigned char * data;
	data = (unsigned char *)m_pEngine->GetMem()->StackMalloc(bytenum);

	DBGPRINTF("began to load data.");	
	int counter = 0;
	int i,j;
	IFile * pIFile = NULL;
	pIFile = IFILEMGR_OpenFile ( (IFileMgr*)m_pEngine->pFileMgr, SAVEFILE_NAME1, _OFM_READ);
	
	if(pIFile == NULL)
	{
		DBGPRINTF("the file can not open. line %d.",__LINE__);
		return false;
		
	}
	
	   IFILE_Seek( pIFile, _SEEK_START, 0);
	   if (NULL != pIFile)
	   {
		   unsigned int count = IFILE_Read(pIFile, data,bytenum);
		   if (count==0)// error, no data is readed
		   {
			   IFILE_Release( pIFile );
			   DBGPRINTF("the file can not open. line %d.",__LINE__);
			   return false;
		   }
		   DBGPRINTF("file loaded, %d bytes read.",count);
		   //LOAD START
		   //m_pEngine->GetSound()->soundOn = data[0];
		   
		   m_pEngine->GetSound()->SetVibOn(data[1] );
		   m_bIsShowHint = (data[2]!=0);
		   m_pEngine->GetSound()->playerSoundOn = data[3];
		   //////The tutorial stage will be set after getting the m_cnMatchCount
		   //m_pScene->SetTutorialStage(m_cnMatchCount == 0 ? 0 : data[4]);
		   int tmp =data[4]%2 == 1 ? data[4] :data[4]-1;
		   m_pScene->SetTutorialStage(tmp < 0 ? 0:tmp); 
		   m_pEngine->GetSound()->SetVolume(data[5]);
		   counter+=6;	
		   
		   for(i =0; i< BALL_COUNT; ++i)
		   {
			   m_SaveBallCanSel[i] = data[counter+i]!=0;
		   }
		   counter+=BALL_COUNT;	
		   
		   for(i =0; i< BONUS_COUNT; ++i)
		   {
			   m_SaveBonusCanSel[i] = data[counter+i]!=0;
		   }
		   counter+=BONUS_COUNT;
		   
		   for(i =0; i< PLAYER_COUNT; ++i)
		   {
			   m_SavePlayerCanSel[i] = data[counter+i]!=0;
		   }
		   counter+=PLAYER_COUNT;	



//////////////////////////////////
		// player cloth select
		for(i=0; i<PLAYER_COUNT; i++)
			for(j=0;j<CLOTH_COUNT;j++)
			{				
				m_SaveClothCanSel[i][j]=data[counter+i*CLOTH_COUNT+j]!=0;
			}
		counter+=PLAYER_COUNT*CLOTH_COUNT;


		// ball icon select
		for(i=0;i<BALLICON_COUNT;i++)
		{
			m_SaveBallIconCanSel[i]=data[counter+i]!=0;
		}
		counter+=BALL_COUNT;


		// lane select
		for(i=0;i<LANE_COUNT;i++)
		{
			m_SaveLaneCanSel[i]=data[counter+i]!=0;
		}
		counter+=LANE_COUNT;


		// lane, camera view
		for(i=0;i<LANE_COUNT;i++)
		{
			m_SaveLaneCanView[i]=data[counter+i]!=0;
		}
		counter+=LANE_COUNT;

		

		m_SaveStoryPassed=data[counter]!=0;
	
		m_SavePlayerId=data[counter+1];
		m_SaveStageIndex=data[counter+2];
		m_SaveCurPlayer=data[counter+3];
		m_SavePlayerRound[0]=data[counter+4];                //current round of the match
		m_SavePlayerRound[1]=data[counter+5];                //current round of the match
		m_SavePlayerBall[0]=data[counter+6];
		m_SavePlayerBall[1]=data[counter+7];
		m_SavePlayerBallIcon[0]=data[counter+8];
		m_SavePlayerBallIcon[1]=data[counter+9];

		m_SavePlayerPinMode[0]=(short)((short)data[counter+10]&0x00ff)+(short)((short)(data[counter+11]&0x00ff)<<8);
		m_SavePlayerPinMode[1]=(short)((short)data[counter+12]&0x00ff)+(short)((short)(data[counter+13]&0x00ff)<<8);

		m_SavePlayerClothesID[0]=+data[counter+14];
		m_SavePlayerClothesID[1]=+data[counter+15];	

		m_SaveCanContinue=data[counter+16]!=0;
		m_SaveQMFinishTime=data[counter+17];
		m_SaveCanCamView=data[counter+18]!=0;
		
		m_SavesmCoinPassed=data[counter+19]!=0;

		m_SaveIsDraw=data[counter+20]!=0;
		
		m_SaveGameFinish=data[counter+21]!=0;

		m_SaveDrawCount=data[counter+22];
		counter+=23;

		int t=sizeof(ScoreInfo);
		MEMCPY(&m_SaveScores[0],&data[counter],t);
		MEMCPY(&m_SaveScores[1],&data[counter]+t,t);
		
		counter+=t*2;

		for(i=0;i<10;i++)
			for(j=0;j<4;j++)
				m_SaveCoinPassed[i][j]=data[counter+i*4+j]!=0;

		counter+=40;//i*j;


		
		
		//// for coin system <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
		m_pMatch->m_cnMatchCount = data[counter];
		m_pMatch->m_cnNextUCoinMatch = data[counter+1];
		m_pMatch->m_cnNextFCoinMatch = data[counter+2];
		m_pMatch->m_hasUnlockCoin = data[counter+3]!=0;
		m_pMatch->m_hasFuncCoin = data[counter+4]!=0;
		m_pMatch->m_uCoinRound = data[counter+5];
		m_pMatch->m_uCoinPassed = data[counter+6]!=0;
		m_pMatch->m_uCoinType = data[counter+7];
		m_pMatch->m_uCoinInfo = data[counter+8];
		m_pMatch->m_fCoinRound = data[counter+9];
		m_pMatch->m_fCoinPassed = data[counter+10]!=0;
		m_pMatch->m_fCoinType = data[counter+11];
		
		counter+=12;
		int temp;
		MEMCPY(&temp, &data[counter],4);
		m_pMatch->m_uCoinPos.x=temp;
		MEMCPY(&temp, &data[counter+4],4);
		m_pMatch->m_uCoinPos.y=temp;
		MEMCPY(&temp, &data[counter+8],4);
		m_pMatch->m_uCoinPos.z=temp;
		MEMCPY(&temp, &data[counter+12],4);
		m_pMatch->m_fCoinPos.x=temp;
		MEMCPY(&temp, &data[counter+16],4);
		m_pMatch->m_fCoinPos.y=temp;
		MEMCPY(&temp, &data[counter+20],4);
		m_pMatch->m_fCoinPos.z=temp;
		
		counter+=24;
		//// for coin system >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
		
		//// for BUG 3854 store isNextDeliveryDone
		//m_bNextDeliDone = data[counter];//(data[counter]!=0);
		++counter;
	   }
		
	   {
		  int size=0;
		size=sizeof(long);
		MEMCPY(&(m_money.totalMoney),data+counter,size);
		counter+=size;
		
		size=TOTAL_BALL*sizeof(boolean);
		MEMCPY(m_money.unlockedBall,data+counter,size);
		counter+=size;

		size=TOTAL_PLAYER*sizeof(boolean);
		MEMCPY(m_money.unlockedCharacters,data+counter,size);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -