📄 cgame.cpp
字号:
}
}
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 1;
}
////////////////////////////////////////////////////////////////////////////////////////////
//初始化DirectX
////////////////////////////////////////////////////////////////////////////////////////////
BOOL CGame::InitDxlib(HINSTANCE hInstance)
{
WNDCLASS wc;
ZeroMemory(&wc,sizeof(wc));
wc.style=0;
wc.lpfnWndProc=WinProc;
wc.cbClsExtra=0;
wc.cbWndExtra=0;
wc.hInstance=hInstance;
wc.hIcon=NULL;
wc.hCursor=::LoadCursor(hInstance,MAKEINTRESOURCE(IDC_CURSOR));
wc.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
wc.lpszMenuName=NULL;
wc.lpszClassName="LOA";
RegisterClass(&wc);
m_hWnd=CreateWindow("LOA","LOA",WS_POPUP,0,0,
::GetSystemMetrics(SM_CXSCREEN),
::GetSystemMetrics(SM_CYSCREEN),
NULL,
NULL,
hInstance,
NULL);
if(!m_hWnd)
{
m_pLog->WriteMessage("Create Window Failed!\n");
return false;
}
m_pLog->WriteMessage("Create Window...OK!\n");
m_pDisplay=new CDisplay();
if(!m_pDisplay)
{
m_pLog->WriteMessage("New CDisplay Failed! Out of Memory!\n");
return false;
}
if(!CreateDisplayMode(false))
{
m_pLog->WriteMessage("Create DisplayMode Failed!\n");
return false;
}
m_pLog->WriteMessage("Create DisplayMode...OK!\n");
ShowWindow(m_hWnd,SW_SHOW);
UpdateWindow(m_hWnd);
DDPIXELFORMAT ddpf;
ZeroMemory(&ddpf,sizeof(ddpf));
ddpf.dwSize=sizeof(ddpf);
if(SUCCEEDED(m_pDisplay->GetFrontBuffer()->GetPixelFormat(&ddpf)))
{
if(ddpf.dwRGBBitCount!=16)
{
MessageBox(m_hWnd,"警告!您的桌面当前不是16位色!本程序无法在窗口模式下运行!\n"
"请在 桌面上-〉右键-〉属性-〉设置-〉颜色质量 中更改!\n"
"然后重新运行本程序\0","错误",MB_OK);
m_pLog->WriteMessage("当前桌面显示深度不是16位!程序不能在窗口模式下运行\n请在 桌面上-〉右键-〉属性-〉设置-〉颜色质量 中更改!然后重新运行本程序");
return false;
}
}
if(FAILED(m_pDisplay->CreateSurface(&m_pSurfScene,640,480)))
{
m_pLog->WriteMessage("Create Scene Surface Failed!\n");
return false;
}
m_pKeyBoard=new CDirectInputKeyBoard();
if(!m_pKeyBoard || FAILED(m_pKeyBoard->CreateKeyBoard(m_hWnd,false,true,true)))
{
m_pLog->WriteMessage("Create Input Device---Keyboard...Failed!\n");
return false;
}
m_pLog->WriteMessage("Create Input Device---Keyboard...OK!\n");
m_pMouse=new CDirectInputMouse();
if(!m_pMouse)
{
m_pLog->WriteMessage("New DirectInput Device---Mouse...Failed!\n");
return false;
}
if(FAILED(m_pMouse->CreateMouse(m_hWnd)))
{
m_pLog->WriteMessage("Create Input Device---Mouse...Failed!\n");
return false;
}
m_pLog->WriteMessage("Create Input Device---Mouse...OK!\n");
m_pSoundPlayer=new CSoundManager();
if(!m_pSoundPlayer || FAILED(m_pSoundPlayer->Initialize(m_hWnd,DSSCL_NORMAL,2,22050,16)))
{
m_pLog->WriteMessage("Init Direct Sound...Failed!\n");
return false;
}
m_pLog->WriteMessage("Init Direct Sound...OK!\n");
m_pDShow=KGDShow::KGDShowCreate();
if(!m_pDShow )
{
m_pLog->WriteMessage("Create Direct Show Failed!");
return false;
}
m_pLog->WriteMessage("Create Direct Show...OK!\n");
return true;
}
////////////////////////////////////////////////////////////////////////////////////////////
//显示一个Windows消息框
////////////////////////////////////////////////////////////////////////////////////////////
void CGame::MsgBox(char*str,...)
{
LOA_ASSERT(str!=NULL);
char msg[1024];
va_list vl;
va_start(vl,str);
wvsprintf(msg,str,vl);
va_end(vl);
MessageBox(m_hWnd,msg,"Message",MB_OK);
}
////////////////////////////////////////////////////////////////////////////////////////////
//读取地图数据库中标识为ID的地图
////////////////////////////////////////////////////////////////////////////////////////////
BOOL CGame::LoadMap(int ID)
{
STChangeMapData *cmd;
if(!m_pdbChangeMap->FindData(ID,&cmd))
{
m_pLog->WriteMessage("Mapping Map ID(%d) to MapName failed!",ID);
return false;
}
return LoadMap(cmd->szName);
}
////////////////////////////////////////////////////////////////////////////////////////////
//屏幕变黑
////////////////////////////////////////////////////////////////////////////////////////////
void CGame::FadeIn()
{
//创建临时表面
CSurface*pSurfTemp=NULL;
if(FAILED(m_pDisplay->CreateSurface(&pSurfTemp,640,480)))
return;
pSurfTemp->BltFast(0,0,m_pDisplay->GetBackBuffer(),NULL,false);
RECT rt;
::SetRect(&rt,0,0,640,480);
int i;
for(i=32;i>=0;i-=2) //淡入----变黑
{
m_pDisplay->Blt(0,0,pSurfTemp);
m_pDisplay->ColorAlphaBlt(RGB(0,0,0),&rt,i);
m_pDisplay->Present();
}
delete pSurfTemp;
}
////////////////////////////////////////////////////////////////////////////////////////////
//屏幕从黑恢复到正常
////////////////////////////////////////////////////////////////////////////////////////////
void CGame::FadeInRestore()
{
CSurface*pSurfTemp=NULL;
if(FAILED(m_pDisplay->CreateSurface(&pSurfTemp,640,480)))
return;
RECT rt;
::SetRect(&rt,0,0,640,480);
pSurfTemp->BltFast(0,0,m_pDisplay->GetBackBuffer(),NULL,false);
for(int i=0;i<32;i+=2) //恢复
{
m_pDisplay->Blt(0,0,pSurfTemp);
m_pDisplay->ColorAlphaBlt(RGB(0,0,0),&rt,i);
m_pDisplay->Present();
}
delete pSurfTemp;
}
////////////////////////////////////////////////////////////////////////////////////////////
//屏幕变白
////////////////////////////////////////////////////////////////////////////////////////////
void CGame::FadeOut()
{
CSurface*pSurfTemp=NULL;
if(FAILED(m_pDisplay->CreateSurface(&pSurfTemp,640,480)))
return;
pSurfTemp->BltFast(0,0,m_pDisplay->GetBackBuffer(),NULL,false);
RECT rt;
::SetRect(&rt,0,0,640,480);
int i;
for(i=32;i>=0;i-=2) //淡出----变白
{
m_pDisplay->Blt(0,0,pSurfTemp);
m_pDisplay->ColorAlphaBlt(RGB(255,255,255),&rt,i);
m_pDisplay->Present();
}
delete pSurfTemp;
}
////////////////////////////////////////////////////////////////////////////////////////////
//屏幕从白恢复到正常
////////////////////////////////////////////////////////////////////////////////////////////
void CGame::FadeOutRestore()
{
CSurface*pSurfTemp=NULL;
if(FAILED(m_pDisplay->CreateSurface(&pSurfTemp,640,480)))
return;
RECT rt;
::SetRect(&rt,0,0,640,480);
pSurfTemp->BltFast(0,0,m_pDisplay->GetBackBuffer(),NULL,false);
for(int i=0;i<32;i+=2) //恢复
{
m_pDisplay->Blt(0,0,pSurfTemp);
m_pDisplay->ColorAlphaBlt(RGB(255,255,255),&rt,i);
m_pDisplay->Present();
}
delete pSurfTemp;
}
void CGame::ClearScreen()
{
m_pDisplay->Clear();
}
////////////////////////////////////////////////////////////////////////////////////////////
//读取地图
////////////////////////////////////////////////////////////////////////////////////////////
BOOL CGame::LoadMap(char*szFile)
{
LOA_ASSERT(szFile!=NULL);
m_nBattleFrequency=5;
m_bRandomBattle=false;
FadeIn();
m_pNpcList->ResetToHead();
STRenderRole*pNpc=NULL;
while(m_pNpcList->GetAndMovePointToNext(pNpc)) //清除旧的npc
{
//debug
pNpc->pRole->Path.~STPath();
pNpc->pRole=NULL;
//end debug
delete pNpc;
}
m_pNpcList->Clear();
CRole*pPlayerData=NULL;
if(m_pPlayer) //清除旧的主角
{
pPlayerData=m_pPlayer->pRole; //保存数据
m_pPlayer->pRole=NULL;
delete m_pPlayer;
}
if(!m_pMap->LoadMap(szFile)) //读取地图
{
MsgBox("LoadMap %s Failed!","",MB_OK);
return false;
}
if(m_pMap->m_head.bRandomBattle!=1)
m_bRandomBattle=false;
else
m_bRandomBattle=true;
char szFileTemp[128];
strcpy(szFileTemp,szFile);
memset(this->m_szCurrentMapName,0,128);
strcpy(this->m_szCurrentMapName,szFileTemp); //确保参数szFile是自身的时候也能正确运行
if(!m_pRenderMap->InitRender(m_pDisplay,m_pMapSurface,m_pMap))
{
MessageBox(m_hWnd,"RenderMap:Init Render Failed!","",MB_OK);
return false;
}
char str[256];
memset(str,0,256);
wsprintf(str,"%s.lsr",szFile);
ExecuteScript(str); //执行地图脚本
if(pPlayerData)
{
if(m_pPlayer->pRole->nKinds!=0xffffff)
{
strcpy(pPlayerData->szName,m_pPlayer->pRole->szName);
strcpy(pPlayerData->szSegmentName,m_pPlayer->pRole->szSegmentName);
pPlayerData->bShow=m_pPlayer->pRole->bShow;
pPlayerData->bOffsiteOk=m_pPlayer->pRole->bOffsiteOk;
pPlayerData->Speed=m_pPlayer->pRole->Speed;
pPlayerData->dwFramsDelay=m_pPlayer->pRole->dwFramsDelay;
pPlayerData->x=m_pPlayer->pRole->x;
pPlayerData->y=m_pPlayer->pRole->y;
delete m_pPlayer->pRole;
m_pPlayer->pRole=pPlayerData; //恢复旧的数据,避免读取一次地图玩家数据就没有了
}
else
{
delete pPlayerData;
}
}
m_pMapRole=m_pPlayer;
ComputeMapStartPos();
SortAndShowRole();
m_pRenderRole=new CRenderRole();
m_pRenderRole->CreateTask(true,true,8); //创建一个自动释放的任务
m_pRenderRole->InitRender(m_pDisplay,m_pMap,m_pMapSurface,m_pRenderMap,m_pRoleQueue);//更新渲染队列
m_pRender->AddTask(m_pRenderRole); //添加任务
if(strcmp(m_pMap->m_head.szMapName,"无名地图")!=0)
{
CRenderBattleText*pText=new CRenderBattleText;
pText->InitRender(this,280,200,RGB(128,0,0),0,5000,m_pMap->m_head.szMapName);
pText->CreateTask();
m_pRender->AddTask(pText);
}
m_pRender->Render();
FadeInRestore();
memset(str,0,256);
wsprintf(str,"%s.Auto.lsr",szFile);
if(-1==ExecuteScript(str))
ShowBeginMenu(); //执行地图脚本,(此脚本可以不存在)
return true;
}
////////////////////////////////////////////////////////////////////////////////////////////
//初始化角色图形数据
////////////////////////////////////////////////////////////////////////////////////////////
BOOL CGame::InitRoleGraphData(char*szFile)
{
LOA_ASSERT(szFile!=NULL);
char strTemp[128];
strcpy(strTemp,szFile);
strcpy(this->m_szCurrentRoleIniName,strTemp);
if(!m_pRoleGraph->InitRoleData(m_pDisplay,this->m_szCurrentRoleIniName))
{
MsgBox("初始化角色图形数据库失败!配置文件%s可能有问题",szFile);
return false;
}
return true;
}
////////////////////////////////////////////////////////////////////////////////////////////
//删除一个NPC
////////////////////////////////////////////////////////////////////////////////////////////
BOOL CGame::DelNpc(int nIDInGame)
{
STRenderRole*pNpc=NULL;
if(!m_pNpcList->FindData(nIDInGame,pNpc))
return false;
//debug
// pNpc->pRole->Path.~STPath();
// pNpc->pRole=NULL;
//end debug
delete pNpc;
return m_pNpcList->Delete(nIDInGame);
return true;
}
////////////////////////////////////////////////////////////////////////////////////////////
//添加一个NPC
////////////////////////////////////////////////////////////////////////////////////////////
BOOL CGame::AddNpc(CRole*pRole)
{
LOA_ASSERT(pRole);
STRenderRole*pNpc;
if(!LoadRole(this->m_szCurrentRoleIniName,pRole->szSegmentName,&pNpc))
{
MsgBox("读取文件%s中的角色<%s>信息失败!",this->m_szCurrentRoleIniName,pRole->szName);
return false;
}
delete pNpc->pRole;
pNpc->pRole=pRole;
return m_pNpcList->Insert(pNpc,pNpc->pRole->nID);
}
////////////////////////////////////////////////////////////////////////////////////////////
//从文件中添加一个NPC
////////////////////////////////////////////////////////////////////////////////////////////
BOOL CGame::AddNpc(char*szRoleName,char*szNameInGame,int nIDInGame,int x,int y)
{
LOA_ASSERT(szRoleName!=NULL);
LOA_ASSERT(szNameInGame);
STRenderRole*pNpc;
if(!LoadRole(this->m_szCurrentRoleIniName,szRoleName,&pNpc))
{
MsgBox("读取文件%s中的角色<%s>信息失败!",this->m_szCurrentRoleIniName,szRoleName);
return false;
}
pNpc->pRole->nID=nIDInGame;
strcpy(pNpc->pRole->szName,szNameInGame);
if(x<0)
x=0;
if(x>=m_pMap->m_head.Width)
x=m_pMap->m_head.Width-1;
if(y<0)
y=0;
if(y>=m_pMap->m_head.Height)
y=m_pMap->m_head.Height-1;
pNpc->pRole->x=x<<5;
pNpc->pRole->y=y<<5;
return m_pNpcList->Insert(pNpc,nIDInGame);
}
////////////////////////////////////////////////////////////////////////////////////////////
//添加主角(使用文件中的状态数据)
////////////////////////////////////////////////////////////////////////////////////////////
BOOL CGame::AddPlayerWithIniData(char*szFile,char*szSection)
{
LOA_ASSERT(szFile);
LOA_ASSERT(szSection);
STRenderRole*pPlayer;
if(!LoadRole(szFile,szSection,&pPlayer))
{
MsgBox("读取文件%s中的角色<%s>信息失败!",szFile,szSection);
return false;
}
m_pPlayer=pPlayer;
m_pPlayer->pRole->nKinds=0xffffff;
return true;
}
////////////////////////////////////////////////////////////////////////////////////////////
//添加主角(保存旧数据)
////////////////////////////////////////////////////////////////////////////////////////////
BOOL CGame::AddPlayer(CRole*pRole)
{
LOA_ASSERT(pRole);
STRenderRole*pPlayer;
if(!LoadRole(this->m_szCurrentRoleIniName,pRole->szSegmentName,&pPlayer))
{
MsgBox("读取文件%s中的角色<%s>信息失败!",this->m_szCurrentRoleIniName,pRole->szName);
return false;
}
delete pPlayer->pRole;
pPlayer->pRole=pRole;
m_pPlayer=pPlayer;
m_pPlayer->pRole->nKinds=0;
return true;
}
////////////////////////////////////////////////////////////////////////////////////////////
//添加主角到x,y格子处
////////////////////////////////////////////////////////////////////////////////////////////
BOOL CGame::AddPlayer(char*szName,int x,int y)
{
LOA_ASSERT(szName!=NULL);
STRenderRole*pPlayer;
if(!LoadRole(this->m_szCurrentRoleIniName,szName,&pPlayer))
{
MsgBox("读取文件%s中的角色<%s>信息失败!",this->m_szCurrentRoleIniName,szName);
return false;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -