📄 winmain.cpp
字号:
// Set player as disconnected
m_CharController.m_Messages.push_back((sMessage &)rcpm);
// Leave critical section
LeaveCriticalSection(&m_CharController.m_UpdateCS);
}
void cApp::ChangeAttack(sMessage *Msg)
{
sAttackChangeMessage *acm;
sCharacter *Character, *CharacterVictim;
// Get pointer to message data
acm = (sAttackChangeMessage*)Msg;
// Get player number in list
if((Character = GetCharacterPoint(acm->Header.PlayerID)) == NULL)
return;
sAttackChangeProcMessage acpm;
acpm.Header.Type = acm->Header.Type;
acpm.Header.Size = sizeof(sAttackChangeProcMessage);
acpm.Header.PlayerID = acm->Header.PlayerID;
// Store new sytate info
CharacterVictim = GetCharacterPoint(acm->VictimDpnid);
if(CharacterVictim == NULL)
acpm.VictimID = -1;
else acpm.VictimID = CharacterVictim->ID;
// Adjust time based on latency
acpm.Time = timeGetTime() -m_CharController.Latency;
// Enter critical section
EnterCriticalSection(&Character->m_UpdateCS);
Character->m_Messages.push_back((sMessage &)acpm);
// Leave critical section
LeaveCriticalSection(&Character->m_UpdateCS);
return;
}
void cApp::AlterEffect(sMessage *Msg)
{
sAlterEffectMessage *aem;
sCharacter *Character;
// Get pointer to message data
aem = (sAlterEffectMessage*)Msg;
sAlterEffectProcMessage aepm;
aepm.Header.Type = aem->Header.Type;
aepm.Header.Size = sizeof(sAlterEffectProcMessage);
Character = GetCharacterPoint(aem->Header.PlayerID);
if(Character == NULL)
aepm.Header.ID = -1;
else aepm.Header.ID = Character->ID;
aepm.EffectType = aem->EffectType;
aepm.HealthPoints = aem->HealthPoints;
aepm.ManaPoints = aem->ManaPoints;
aepm.AlterHPAmount = aem->AlterHPAmount;
// Store new sytate info
Character = GetCharacterPoint(aem->VictimDpnid);
if(Character == NULL)
aepm.VictimID = -1;
else aepm.VictimID = Character->ID;
// Adjust time based on latency
// Get player number in list
if((Character = GetCharacterPoint(aem->Header.PlayerID)) == NULL)
return;
// Enter critical section
EnterCriticalSection(&m_CharController.m_UpdateCS);
aepm.Time = timeGetTime() -m_CharController.Latency;
m_CharController.m_Messages.push_back((sMessage &)aepm);
// Leave critical section
LeaveCriticalSection(&m_CharController.m_UpdateCS);
return;
}
///////////////////////////////////////////////////////////
// App initialization functions
///////////////////////////////////////////////////////////
BOOL cApp::SelectAdapter()
{
int Result;
// Hide main window
ShowWindow(GethWnd(), SW_HIDE);
// Build a list of network adapters
m_Adapters.Init();
// Open connection dialog
Result=DialogBox(GethInst(), MAKEINTRESOURCE(IDD_CONNECT), \
GethWnd(), ConnectDialogProc);
// Don't continue if quit selected
if(Result == FALSE)
return FALSE;
// Show main window
ShowWindow(GethWnd(), SW_SHOW);
// Continue if user selected OK
return TRUE;
}
BOOL cApp::InitializeGame()
{
FILE *fp;
long i;
SYSTEMTIME st;
GetSystemTime (&st);
wsprintf(sTemp, "%d-%02d-%02d %02d:%02d:%02d.%003d", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);
// 1实例化一个layout 对象
log4cpp::Layout* layout =
new log4cpp::BasicLayout();
// 2. 初始化一个appender 对象
log4cpp::Appender* appender = new
log4cpp::FileAppender("FileAppender",
"./Debug_log4cpp.log");
// 3. 把layout对象附着在appender对象上
appender->setLayout(layout);
// 5. 设置additivity为false,替换已有的appender
warn_log.setAdditivity(false);
// 5. 把appender对象附到category上
warn_log.setAppender(appender);
// 6. 设置category的优先级,低于此优先级的日志不被记录
warn_log.setPriority(log4cpp::Priority::DEBUG);
// 记录一些日志
strcat(sTemp, "-------------------------------------------------------------------------------");
warn_log.debug(sTemp);
// Initialize the graphics device and set display mode
m_Graphics.Init();
m_Graphics.SetMode(GethWnd(), TRUE, TRUE);
m_Graphics.SetPerspective(D3DX_PI/4, 1.3333f, 1.0f, 20000.0f);
ShowMouse(FALSE);
// Enable lighting and setup light
m_Graphics.EnableLighting(TRUE);
m_Graphics.SetAmbientLight(48,48,48);
m_Graphics.EnableLight(0, TRUE);
m_Light.SetAttenuation0(0.5f);
m_Light.SetRange(1000.0f);
// Get a font
m_Font.Create(&m_Graphics, "Arial", 16);
// Initialize input and input devices
m_Input.Init(GethWnd(), GethInst());
m_Keyboard.Create(&m_Input, KEYBOARD);
m_Mouse.Create(&m_Input, MOUSE, TRUE);
// Load the mesh and create an NodeTree mesh from it
m_TerrainMesh.Load(&m_Graphics, "..\\Data\\Arena.x", \
"..\\Data\\");
m_NodeTreeMesh.Create(&m_Graphics, &m_TerrainMesh, QUADTREE);
// Load the master item list
for(i=0;i<MAX_MIL;i++)
ZeroMemory(&m_MIL[i], sizeof(sItem));
if((fp=fopen("..\\Data\\Default.mil", "rb")) != NULL) {
for(i=0;i<MAX_MIL;i++)
fread(&m_MIL[i], 1, sizeof(sItem), fp);
fclose(fp);
}
// Initialize the character controller
m_CharController.SetData(this);
m_CharController.Init(&m_Graphics, &m_Font, \
"..\\Data\\Default.mcl", (sItem*)&m_MIL, \
m_SpellController.GetSpell(0), \
sizeof(g_CharMeshNames)/sizeof(char*), g_CharMeshNames, \
"..\\Data\\", "..\\Data\\", \
sizeof(g_CharAnimations) / sizeof(sCharAnimationInfo), \
(sCharAnimationInfo*)&g_CharAnimations, \
&m_SpellController);
// Initialize the spell controller
m_SpellController.Init(&m_Graphics, \
"..\\Data\\Default.msl", \
sizeof(g_SpellMeshNames)/sizeof(char*),g_SpellMeshNames, \
"..\\Data\\", &m_CharController);
// Process the startup script
m_Script.SetData(this, &m_Graphics, &m_Font, &m_Keyboard, \
&m_CharController);
// Get the options bitmap
m_Options.Load(&m_Graphics, "..\\Data\\Force.bmp");
// Load character ICS
MapICS = new cMapICS();
MapICS->Init(&m_Graphics, (sItem*)&m_MIL);
MapICS->Load("..\\Data\\MapItems.mi");
// Add the character player
m_CharController.Add(0, NULL, 0, CHAR_PC, CHAR_STAND, \
0.0f, 0.0f, 0.0f, 3.14f, CHAR_IDLE, m_Name);
m_CamXPos = (float)cos(m_CamAngle) * cos(m_YCamAngle) * 300.0f;
m_CamYPos = (float)sin(m_YCamAngle) * 300.0f;
m_CamZPos = (float)sin(m_CamAngle) * cos(m_YCamAngle) * 300.0f;
return TRUE;
}
BOOL cApp::JoinGame()
{
// Initialize network and try to connect to host
m_Client.Init();
if(m_Client.Connect(m_guidAdapter, m_HostIP, 9123, \
m_Name, "RPGGAME", NULL) == FALSE)
return FALSE;
m_Client.SetData(g_Application);
UpdateLatency();
return TRUE; // Return success
}
///////////////////////////////////////////////////////////
// Game logic code
///////////////////////////////////////////////////////////
void cApp::UpdateCamera()
{
float m_TempCamXPos, m_TempCamYPos, m_TempCamZPos, Dist, Height;
float m_DestCamXPos, m_DestCamYPos, m_DestCamZPos, m_CamAngleTemp;
sCharacter *Character;
float radius;
Character =m_CharController.GetCharacter(0);
m_DestCamXPos = Character->XPos + (float)cos(m_CamAngle) * cos(m_YCamAngle) * 300.0f;
m_DestCamYPos = Character->YPos + (float)sin(m_YCamAngle) * 300.0f;
m_DestCamZPos = Character->ZPos + (float)sin(m_CamAngle) * cos(m_YCamAngle) * 300.0f;
// Check for movement collisions -
// can't walk past anything blocking path
if(m_NodeTreeMesh.CheckIntersect(m_CamXPos, m_CamYPos, m_CamZPos,
m_DestCamXPos, m_DestCamYPos, m_DestCamZPos,
&Dist) == TRUE) {
m_DestCamXPos = m_CamXPos;
m_DestCamYPos = m_CamYPos;
m_DestCamZPos = m_CamZPos;
radius = sqrt(pow(m_DestCamXPos-Character->XPos,2.0f)+pow(m_DestCamYPos-Character->YPos,2.0f)+pow(m_DestCamZPos-Character->ZPos,2.0f));
m_YCamAngle = (float)asin((m_DestCamYPos -Character->YPos)/radius);
m_CamAngleTemp = (float)acos((m_DestCamXPos -Character->XPos)/(cos(m_YCamAngle)*radius));
if(m_CamAngle < 0.0f) m_CamAngle = -m_CamAngleTemp;
else m_CamAngle =m_CamAngleTemp;
if ( radius> 305.0f ){
m_TempCamXPos = Character->XPos + (float)cos(m_CamAngle) * cos(m_YCamAngle) * 300.0f;
m_TempCamYPos = Character->YPos + (float)sin(m_YCamAngle) * 300.0f;
m_TempCamZPos = Character->ZPos + (float)sin(m_CamAngle) * cos(m_YCamAngle) * 300.0f;
// Check for height changes (can step up to 64 units)
Height = m_NodeTreeMesh.GetHeightBelow(m_TempCamXPos, m_TempCamYPos + 64.0f, m_TempCamZPos);
if(m_TempCamYPos < Height) {
// Dropping
m_TempCamYPos = Height + 1.0f;
}
m_DestCamXPos = m_TempCamXPos;
m_DestCamYPos = m_TempCamYPos;
m_DestCamZPos = m_TempCamZPos;
radius = sqrt(pow(m_DestCamXPos-Character->XPos,2.0f)+pow(m_DestCamYPos-Character->YPos,2.0f)+pow(m_DestCamZPos-Character->ZPos,2.0f));
m_YCamAngle = (float)asin((m_DestCamYPos -Character->YPos)/radius);
m_CamAngleTemp = (float)acos((m_DestCamXPos -Character->XPos)/(cos(m_YCamAngle)*radius));
if(m_CamAngle < 0.0f) m_CamAngle = -m_CamAngleTemp;
else m_CamAngle =m_CamAngleTemp;
}
}
m_CamXPos = m_DestCamXPos;
m_CamYPos = m_DestCamYPos;
m_CamZPos = m_DestCamZPos;
// Center camera on player using CamAngle
m_Camera.Point(m_CamXPos, m_CamYPos, m_CamZPos, \
Character->XPos, Character->YPos, Character->ZPos);
// Set camera and construct frustum
m_Graphics.SetCamera(&m_Camera);
// Position light
m_Light.Move(Character->XPos, Character->YPos+60.0f, Character->ZPos);
m_Graphics.SetLight(0, &m_Light);
}
void cApp::RenderScene()
{
cFrustum Frustum;
Frustum.Construct(&m_Graphics);
// Render scene
m_Graphics.Clear();
if(m_Graphics.BeginScene() == TRUE) {
// Draw the terrain
m_Graphics.EnableZBuffer(TRUE);
m_NodeTreeMesh.Render(&Frustum);
// Render all characters
m_CharController.Render();
// Render spells
m_SpellController.Render();
MapICS->Render();
if(ScripteHandle.Renderflag == TRUE) {
sCharacter *Character;
sCharacter *CharPtr;
Character = m_CharController.GetCharacter(0);
if(Character != NULL){
if(pow(Character->XPos-ScripteHandle.last_XPos,2)+pow(Character->YPos-ScripteHandle.last_YPos,2)+pow(Character->ZPos-ScripteHandle.last_ZPos,2)>400.0f){
ScripteHandle.Renderflag = FALSE;
}else {
CharPtr = m_CharController.GetCharacter(ScripteHandle.ScriptCharID);
if( CharPtr != NULL){
m_Script.Execute(CharPtr->ScriptFilename);
}
}
}
}
if(m_Status == MSG_ITEMS) m_CharController.Render2D();
else if(m_Status == MSG_STATUS) m_CharController.RenderStatus();
// m_Font.Print(status, 2, 2);
// Display stats
/* char status[1024];
if(m_CharController.GetCharacter(2)!=NULL){
sprintf(status, "dpnidPlayer: %ld, ID: %ld, Type: %ld, Enabled: %ld",
m_CharController.GetCharacter(2)->dpnidPlayer, m_CharController.GetCharacter(2)->ID, \
m_CharController.GetCharacter(2)->Type, m_CharController.GetCharacter(2)->Enabled);
m_Font.Print(status, 2, 2);
}
if(m_CharController.GetCharacter(6)!=NULL){
sprintf(status, "dpnidPlayer: %ld, ID: %ld, Type: %ld, Enabled: %ld",
m_CharController.GetCharacter(6)->dpnidPlayer, m_CharController.GetCharacter(6)->ID,\
m_CharController.GetCharacter(6)->Type, m_CharController.GetCharacter(6)->Enabled);
m_Font.Print(status, 2, 20);
}*/
m_Graphics.EndScene();
}
m_Graphics.Display(); // Display the scene
}
BOOL cApp::CheckIntersect( \
float XStart, float YStart, float ZStart, \
float XEnd, float YEnd, float ZEnd)
{
sMesh *MeshPtr;
BOOL Hit;
float u, v, Dist;
float XDiff, YDiff, ZDiff, Size;
DWORD FaceIndex;
D3DXVECTOR3 vecDir;
// Start with parent mesh
if((MeshPtr = m_TerrainMesh.GetParentMesh()) == NULL)
return FALSE; // No polygons hit
// Calculate ray
XDiff = XEnd - XStart;
YDiff = YEnd - YStart;
ZDiff = ZEnd - ZStart;
Size = (float)sqrt(XDiff*XDiff+YDiff*YDiff+ZDiff*ZDiff);
D3DXVec3Normalize(&vecDir, &D3DXVECTOR3(XDiff, YDiff, ZDiff));
// Go through each mesh looking for intersection
while(MeshPtr != NULL) {
D3DXIntersect(MeshPtr->m_Mesh, \
&D3DXVECTOR3(XStart,YStart,ZStart), &vecDir, \
&Hit, &FaceIndex, &u, &v, &Dist);
if(Hit == TRUE) {
if(Dist <= Size)
return TRUE; // Hit a polygon
}
MeshPtr = MeshPtr->m_Next;
}
return FALSE; // No polygons hit
}
void cApp::UpdateLatency()
{
DPN_CONNECTION_INFO dpci;
HRESULT hr;
// Request player connection settings
hr = m_Client.GetClientCOM()->GetConnectionInfo(&dpci, 0);
if(SUCCEEDED(hr)) {
m_CharController.Latency = dpci.dwRoundTripLatencyMS / 2;
// Bounds latency to 1 second
if(m_CharController.Latency > 1000)
m_CharController.Latency = 1000;
} else {
m_CharController.Latency = 0;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -