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

📄 winmain.cpp

📁 这是一个服务端/客户端模式的小型网络游戏
💻 CPP
📖 第 1 页 / 共 3 页
字号:
	switch(uMsg) {
    case WM_INITDIALOG:
		// Get adapter list window handle
		hAdapters = GetDlgItem(hWnd, IDC_ADAPTERS);
		
		// Add adapter names to list
		for(i=0;i<g_Adapters->GetNumAdapters();i++) {
			g_Adapters->GetName(i, Text);
			SendMessage(hAdapters, CB_INSERTSTRING,i,(LPARAM)Text);
		}
		
		// Select first adapter in list
		SendMessage(hAdapters, CB_SETCURSEL, 0, 0);
		
		// Clear fields
		SetWindowText(GetDlgItem(hWnd, IDC_HOSTIP), "127.0.0.1");
		SetWindowText(GetDlgItem(hWnd, IDC_NAME), "");
		
		return TRUE;
		
    case WM_COMMAND:
		switch(LOWORD(wParam)) {
			
        case IDC_OK:
			// Make sure an adapter was selected
			if((Selection = SendMessage(hAdapters,              \
				CB_GETCURSEL,           \
				0, 0)) == LB_ERR)
				break;
			
			// Make sure there's an IP entered
			GetWindowText(GetDlgItem(hWnd, IDC_HOSTIP), IP, 16);
			if(!strlen(IP))
				break;
			
			// Make sure there's a name entered
			GetWindowText(GetDlgItem(hWnd, IDC_NAME), Name, 32);
			if(!strlen(Name))
				break;
			
			// Assign Adapter, IP, and Name
			if(g_Application != NULL)
				g_Application->SetInfo(                           \
				g_Adapters->GetGUID(Selection), IP, Name);
			
			// Quit dialog
			EndDialog(hWnd, TRUE);
			return TRUE;
			
        case IDC_CANCEL:
			EndDialog(hWnd, FALSE);
			return TRUE;
		}
		
	}
	return FALSE;
}

void cApp::SetInfo(GUID *Adapter, char *IP, char *Name)
{
	m_guidAdapter = Adapter;
	strcpy(m_HostIP, IP);
	strcpy(m_Name, Name);
}

///////////////////////////////////////////////////////////
// Misc. application functions
///////////////////////////////////////////////////////////
sCharacter* cApp::GetCharacterPoint(DPNID dpnidPlayer)
{
	sCharacter *CharPtr;
	// Return success if no characters to update
	if((CharPtr = m_CharController.GetParentCharacter()) == NULL)
		return NULL;
	
	// Loop through all characters
	while(CharPtr != NULL) {
		if(CharPtr->dpnidPlayer == dpnidPlayer){
			return CharPtr;
		}
		// go to next character    
		CharPtr = CharPtr->Next;
	}
	
	
	return NULL;  // Not found in list
}

///////////////////////////////////////////////////////////
// Network send and receive functions
///////////////////////////////////////////////////////////
BOOL cApp::SendNetworkMessage(void *Msg, long SendFlags)
{
	sMessageHeader *mh = (sMessageHeader*)Msg;
	unsigned long Size;
	
	// Get size of message to send
	if((Size = mh->Size) == 0)
		return FALSE;
	
	// Send the mesage
	return m_Client.Send(Msg, Size, SendFlags);
}

BOOL cApp::Receive(DPNMSG_RECEIVE *Msg)
{
	sMessage *MsgPtr;
	
	// Get pointer to received data
	MsgPtr = (sMessage*)Msg->pReceiveData;
	
	// Handle packets by type
	switch(MsgPtr->Header.Type) {
    case MSG_ASSIGNID:       // Assign local player ID
		AssignID(MsgPtr);
		break;
		
    case MSG_PLAYER_INFO:    // Add a player to list
    case MSG_CREATE_PLAYER:
		CreatePlayer(MsgPtr);
		break;
		
    case MSG_DESTROY_PLAYER: // Remove a player from list
		DestroyPlayer(MsgPtr);
		break;
		
    case MSG_STATE_CHANGE:   // Change state of player
		ChangeState(MsgPtr);
		break;
		
	case MSG_ROUTINE_CHANGE:
		RoutineChange(MsgPtr);
		break;
		
    case MSG_SPELL_CHANGE:   // Change state of player
		ChangeSpell(MsgPtr);
		break;
		
	case MSG_ATTACK_CHANGE:
		ChangeAttack(MsgPtr);
		break;
		
    case MSG_ALTER_EFFECT:
		AlterEffect(MsgPtr);
		break;
		
    case MSG_INIT_FILE:
		FileHandle(MsgPtr);
		break;
		
	case MSG_RELIVE_CHANGE:
		ReliveChange(MsgPtr);
		break;
	}
	
	return TRUE;
}

///////////////////////////////////////////////////////////
// Message parsing functions
///////////////////////////////////////////////////////////
void cApp::AssignID(sMessage *Msg)
{
	sAssignPlayerIDMessage *apidm;
	// Error checking
	if((m_CharController.GetCharacter(0)->dpnidPlayer))
		return;
	
	// Get pointer to message data
	apidm = (sAssignPlayerIDMessage*)Msg;
	
	sAssignPlayerIDProcMessage apidpm;
	apidpm.Header.Type = apidm->Header.Type;
	apidpm.Header.Size = sizeof(sAssignPlayerIDProcMessage);
	apidpm.Header.PlayerID      = apidm->Header.PlayerID;
	
    // Enter critical section
	EnterCriticalSection(&m_CharController.m_UpdateCS);
	m_CharController.m_Messages.push_back((sMessage &)apidpm);
	
	LeaveCriticalSection(&m_CharController.m_UpdateCS);
}

void cApp::CreatePlayer(sMessage *Msg)
{
	sCreatePlayerMessage *cpm;
	sCharacter *CharPtr;
	
	
	// Error checking
	if(!m_CharController.GetCharacter(0) || !m_CharController.GetCharacter(0)->dpnidPlayer)
		return;
	
	// Get pointer to message data
	cpm = (sCreatePlayerMessage*)Msg;
	// Don't add local player to list
	if(cpm->Header.PlayerID == m_CharController.GetCharacter(0)->dpnidPlayer)
		return;
	
	deque<sMessage>::iterator it = find_if(m_CharController.m_Messages.begin(), m_CharController.m_Messages.end(), evenDpnidPlayer(cpm->Header.PlayerID));
	if(it!=m_CharController.m_Messages.end()){
		return;
	}
	
	// Return success if no characters to update
	if((CharPtr = m_CharController.GetParentCharacter()) == NULL)
		return;

	// Loop through all characters
	while(CharPtr != NULL) {
		if(CharPtr->dpnidPlayer == cpm->Header.PlayerID) {
			return;
		}
		// go to next character    
		CharPtr = CharPtr->Next;
	}
	
	sCreatePlayerProcMessage cppm;
	cppm.Header.Type = cpm->Header.Type;
	cppm.Header.Size = sizeof(sCreatePlayerProcMessage);
	cppm.Header.PlayerID		   = cpm->Header.PlayerID;
	cppm.Definition      = cpm->Definition;
	cppm.Type            = cpm->Type;
	cppm.AI              = cpm->AI;
	cppm.XPos            = cpm->XPos;
	cppm.YPos            = cpm->YPos;
	cppm.ZPos            = cpm->ZPos;
	cppm.Direction       = cpm->Direction;
	cppm.Action		   = cpm->Action;
	strcpy(cppm.Name, cpm->Name);
    // Enter critical section
	EnterCriticalSection(&m_CharController.m_UpdateCS);
	m_CharController.m_Messages.push_back((sMessage &)cppm);
	
	// Leave critical section
	LeaveCriticalSection(&m_CharController.m_UpdateCS);
}

void cApp::DestroyPlayer(sMessage *Msg)
{
	sDestroyPlayerMessage *dpm;
	sCharacter *Character;
	// Error checking
	if(!m_CharController.GetCharacter(0)->dpnidPlayer)
		return;
	
	// Get pointer to message data
	dpm = (sDestroyPlayerMessage*)Msg;
	
	// Don't remove local player from list
	if(dpm->Header.PlayerID == m_CharController.GetCharacter(0)->dpnidPlayer)
		return;
	
	// Get player number in list
	if((Character = GetCharacterPoint(dpm->Header.PlayerID)) == NULL)
		return;
	
	sDestroyPlayerProcMessage dppm;
	dppm.Header.Type = dpm->Header.Type;
	dppm.Header.Size = sizeof(sDestroyPlayerProcMessage);
	
	dppm.Header.ID = Character->ID;

	// Enter critical section
	EnterCriticalSection(&m_CharController.m_UpdateCS);
	
	// Set player as disconnected
	m_CharController.m_Messages.push_back((sMessage &)dppm);
	
	// Leave critical section
	LeaveCriticalSection(&m_CharController.m_UpdateCS);
	
}

void cApp::ChangeState(sMessage *Msg)
{
	sStateChangeMessage *scm;
	sRequestPlayerInfoMessage rpim;
	sCharacter *Character;
	
	// Error checking
	if(!m_CharController.GetCharacter(0) || !m_CharController.GetCharacter(0)->dpnidPlayer)
		return;
	
	// Get pointer to message data
	scm = (sStateChangeMessage*)Msg;
	
	if((Character = GetCharacterPoint(scm->Header.PlayerID)) == NULL) {
		
		rpim.Header.Type = MSG_PLAYER_INFO;
		rpim.Header.Size = sizeof(sRequestPlayerInfoMessage);
		rpim.Header.PlayerID = m_CharController.GetCharacter(0)->dpnidPlayer;
		rpim.PlayerID = scm->Header.PlayerID;
		
		// Send message to server
		SendNetworkMessage(&rpim, DPNSEND_NOLOOPBACK);
		
		return;
	}
	
	sMessage pm;
	
	pm.Header.Type = scm->Header.Type;
	pm.Header.PlayerID = scm->Header.PlayerID;
	
	sStateChangeProcMessage *scpm;
	scpm = (sStateChangeProcMessage *)&pm;
	scpm->Header.Size = sizeof(sStateChangeProcMessage);
	scpm->Action = scm->Action;
	if(scm->Action == CHAR_MOVE){
		scpm->Direction = scm->Direction;
	} else if(scm->Action == CHAR_DIE){
		scpm->AttackerID = GetCharacterPoint(scm->AttackerDpnid)->ID;
	}
	
	// Adjust time based on latency
	scpm->Time = timeGetTime() - m_CharController.Latency;
	// Enter critical section
	EnterCriticalSection(&Character->m_UpdateCS);
	Character->m_Messages.push_back(pm);
	// Leave critical section
	LeaveCriticalSection(&Character->m_UpdateCS);
	
}



void cApp::RoutineChange(sMessage *Msg)
{
	sRoutineChangeMessage *rcm;
	sRequestPlayerInfoMessage rpim;
	sCharacter *Character;
	
	// Error checking
	if(!m_CharController.GetCharacter(0) || !m_CharController.GetCharacter(0)->dpnidPlayer)
		return;
	
	// Get pointer to message data
	rcm = (sRoutineChangeMessage*)Msg;
	
	if((Character = GetCharacterPoint(rcm->Header.PlayerID)) == NULL) {
		
		rpim.Header.Type = MSG_PLAYER_INFO;
		rpim.Header.Size = sizeof(sRequestPlayerInfoMessage);
		rpim.Header.PlayerID = m_CharController.GetCharacter(0)->dpnidPlayer;
		rpim.PlayerID = rcm->Header.PlayerID;
		
		// Send message to server
		SendNetworkMessage(&rpim, DPNSEND_NOLOOPBACK);
		
		return;
	}
	
	
	sMessage pm;
	
	pm.Header.Type = rcm->Header.Type;
	pm.Header.PlayerID = rcm->Header.PlayerID;
	
	sRoutineChangeProcMessage *rcpm;
	rcpm = (sRoutineChangeProcMessage *)&pm;
	rcpm->Header.Size = sizeof(sRoutineChangeProcMessage);
	
	rcpm->Direction		= rcm->Direction;
	rcpm->XPos			= rcm->XPos;
	rcpm->YPos			= rcm->YPos;
	rcpm->ZPos			= rcm->ZPos;
	// Enter critical section
	EnterCriticalSection(&Character->m_UpdateCS);
	Character->m_Messages.push_back(pm);
	// Leave critical section
	LeaveCriticalSection(&Character->m_UpdateCS);
}

void cApp::ChangeSpell(sMessage *Msg)
{
	sSpellChangeMessage *spcm;
	sRequestPlayerInfoMessage rpim;
	sCharacter *Character;
	
	// Error checking
	if(!m_CharController.GetCharacter(0) || !m_CharController.GetCharacter(0)->dpnidPlayer)
		return;
	
	// Get pointer to message data
	spcm = (sSpellChangeMessage*)Msg;
	
	// Get player number in list
	if((Character = GetCharacterPoint(spcm->Header.PlayerID)) == NULL) {

		// Construct message
		rpim.Header.Type = MSG_PLAYER_INFO;
		rpim.Header.Size = sizeof(sRequestPlayerInfoMessage);
		rpim.Header.PlayerID = m_CharController.GetCharacter(0)->dpnidPlayer;
		rpim.PlayerID = spcm->Header.PlayerID;
		
		// Send message to server
		SendNetworkMessage(&rpim, DPNSEND_NOLOOPBACK);
		
		return;
	}

    if(Character!=NULL){
		sSpellChangeProcMessage spcpm;
		
		spcpm.Header.Type = spcm->Header.Type;
		spcpm.Header.Size = sizeof(sSpellChangeProcMessage);		
		// Store new sytate info
		spcpm.SpellNum      = spcm->SpellNum;
		spcpm.SpellTarget      = spcm->SpellTarget;
		spcpm.TargetX = spcm->TargetX;
		spcpm.TargetY   = spcm->TargetY;
		spcpm.TargetZ   = spcm->TargetZ;
		
		// Adjust time based on latency
		spcpm.Time = timeGetTime() -m_CharController.Latency;
		// Enter critical section
		EnterCriticalSection(&Character->m_UpdateCS);
		Character->m_Messages.push_back((sMessage &)spcpm);		
		// Leave critical section
		LeaveCriticalSection(&Character->m_UpdateCS);
	}
}



void cApp::FileHandle(sMessage *Msg)
{
	
	sFileProcMessage fpm;
	memcpy(&fpm, Msg, sizeof(sFileMessage));
	
	EnterCriticalSection(&m_CharController.m_UpdateCS);
	
	// Set player as disconnected
	m_CharController.m_Messages.push_back((sMessage &)fpm);
	
	// Leave critical section
	LeaveCriticalSection(&m_CharController.m_UpdateCS);
}


void cApp::ReliveChange(sMessage *Msg)
{
	
	sReliveChangeProcMessage rcpm;
	memcpy(&rcpm, Msg, sizeof(sReliveChangeMessage));
	
	EnterCriticalSection(&m_CharController.m_UpdateCS);
	

⌨️ 快捷键说明

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