【VC开源代码栏目提醒】:文章导读:在新的一年中,各位网友都进入紧张的学习或是工作阶段。网学会员整理了VC开源代码-GAMEMANAGER.CPP的相关内容供大家参考,祝大家在新的一年里工作和学习顺利!
/*****************************************************************************
*
* GameManager.cpp
*
* Electrical Engineering Faculty - Software Lab
* Spring semester 1998
*
* Tanks game
*
* Module description: The core of the game - implements the main game loop.
* Holds the list of objects participating in the game.
* In every game iteration, all the objects on the list
* are refreshed, and the game display is updated.
* More details on the algorithms in use are described below.
*
* Authors: Eran Yariv - 28484475
* Moshe Zur - 24070856
*
*
* Date: 23/09/98
*
******************************************************************************/
#include "stdafx.h"
#include <afxmt.h>
#include "tanks.h"
#include "DIB.h"
#include "GameManager.h"
#include "ImageManager.h"
#include "MsgQueue.h"
#include "Bonus.h"
#include "GameBoard.h"
#include "TankObj.h"
#include "Shell.h"
#include "Bullet.h"
#include "Mine.h"
#include "Bomber.h"
#include "Message.h"
#include "GameOver.h"
CGameManager::CGameManager (UINT uFreq) :
m_Timer(TANKS_APP->m_gTimer),
m_ImageManager(TANKS_APP->m_gImageManager),
m_MsgQueue(TANKS_APP->m_gIncomingMsgQueue),
m_CommManager(TANKS_APP->m_gCommManager),
m_dwBonusTimeout (0)
{
VERIFY (SetFrequency (uFreq));
}
BOOL CGameManager::SetFrequency (UINT uFreq)
{
if (uFreq < MIN_RENDER_FREQ || uFreq > MAX_RENDER_FREQ)
return FALSE;
m_uFreq = uFreq;
m_uMilliSleep = UINT (double(1000.0) / double(m_uFreq));
return TRUE;
}
UINT CGameManager::ThreadEntry (LPVOID /*lpParam*/)
{
SetPriority (GAME_MANAGER_THREAD_PRIORITY);
m_MapHWnd = TANKS_APP->m_gHwndMap; // Get handle to map (Windows handle)
ASSERT (NULL != m_MapHWnd);
m_dwChkSumSignal = 0; // Don't send check sum right away
m_iNumTanks = 0; // Initially, no tanks.
m_iLocalTankID = -1; // Initially, no local tank
HDC dc = ::GetDC (m_MapHWnd);
m_BackBuffer.CreateEmpty (MAP_WIDTH, MAP_HEIGHT);// Create back (off-screen) buffer
m_BackBuffer.GetPaletteFromResourceBitmap (IDB_BULLET);
TANKS_APP->m_gDrawDIB.SetPalette (*m_BackBuffer.m_pPalette);
TANKS_APP->m_gDrawDIB.Realize (dc, FALSE);
::ReleaseDC (m_MapHWnd, dc);
MultiRectGameTechnique (); // Do the game main loop
DestroyObjects (); // Kill list of objects
return 0;
}
void CGameManager::DestroyObjects ()
{
m_GameObjsList.KillList();
m_TanksCS.Lock();
for (int i=0; i<MAX_TANKS; i++)
m_pTanks[i] = NULL;
m_TanksCS.Unlock();
m_BonusCS.Lock();
m_pBonus = NULL;
m_BonusCS.Unlock();
m_iNumTanks = 0;
}
#pragma warning (disable : 4701)
/* warning C4701: local variable 'CurObjHImg' may be used without having been initialized
God damn it, trust me on this one - I know what I'm doing here....
*/
/*------------------------------------------------------------------------------
Function: MultiRectGameTechnique
Purpose: Main game loop. Every iteration of the loop, game objects recalc
their status, and the display is refreshed. Every Iteration lasts a
constant period of time, to allow a steady frame rate of the display,
using the sleep system call, to stay low on CPU use.
Input: None.
Output: None.
Remarks: The algorithm in use is explained in details in the Programmers's
manual section in the game's help files.
------------------------------------------------------------------------------*/
void
CGameManager::MultiRectGameTechnique ()
{
#ifdef GATHER_RENDERING_STATS
DWORD dwLoopCount=0;
DWORD dwStartTime = GetTickCount();
int lTotalTimeLeft = 0, // For statistics only !
lMaxTimeLeft = 0, // For statistics only !
lMinTimeLeft = LONG(m_uMilliSleep), // For statistics only !
iMaxObjs = 0; // For statistics only !
#endif // GATHER_RENDERING_STATS
BOOL b