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

📄 console.cpp

📁 Visual C++ 游戏开发与设计实例 源代码(所有)
💻 CPP
📖 第 1 页 / 共 3 页
字号:

#include "global.h"
#include "console.h"
#include "network.h"
#include "dxutil.h"
#include "ddutil.h"
#include "dsutil.h"


#define DEFTURNSTEP			8
#define DEFPROTECTTIME		600		// 20*30fps
#define WAITTIMEOUT			5000	// second

#define BULLET_REBOUND
#define	_TK_DEBUG

//-----------------------------------------------------------------------------
// Global variables
//-----------------------------------------------------------------------------

bool				bRecvCmd = false;
TANKCMD				m_pCmd[20];


CDisplay				Display;
CSurface				*pResource, *pMapGrass, *pMapSolid, *pRes2;
CSurface				*pText;
LPDIRECTDRAWSURFACE7	pddsSolid, pddsGrass, pddsRes, pddsRes2;

CSoundManager			SoundManager;
CSound					*pFire, *pBoom;

char		*szFireWave = "Wave/bfire.wav";
char		*szBoomWave = "Wave/lboom.wav";

char		fps[20] = "FPS: ";

int			g_FrameBig = 640, g_FrameSmall = 32, g_FrameLeft = 608,
			g_FrameWidth = 640, g_FrameHeight = 640, g_FrameBasis = 16;

//-----------------------------------------------------------------------------
// Variables defined in other files
//-----------------------------------------------------------------------------
extern CMsgList			NetList;
extern HWND				hMainWnd, hChildWnd;
extern unsigned int		uiCurrentCmd;
extern bool				bFired;	

unsigned int	cmdbuf;
bool			firebuf;
bool			bFresh = true;

//-----------------------------------------------------------------------------
// 
//-----------------------------------------------------------------------------
#ifdef _TK_DEBUG

#include <time.h>
#include <sys/timeb.h>
#include <stdio.h>

#define MAXDEBUG		128

HANDLE				hfile;
char				szDebugFile[20];

void DebugOutput( int num, ... ) {
	char	*s = new char[MAXDEBUG];
	s[0] = 0;

	// add time stamp
	struct _timeb tstruct;
	_ftime( &tstruct );
	_strtime( s );
	lstrcat(s, ":");
	sprintf( s+strlen(s), "%u", tstruct.millitm );
	lstrcat(s, ": ");

	// add messages
	va_list		vl;
    va_start( vl, num );
    for( int i = 0; i<num ; i++ )
        lstrcat( s, va_arg( vl, char * ) );
    va_end( vl );
	lstrcat( s, "\r\n" );

	// output msg
	if ( hfile ) {
		DWORD ret;
		WriteFile( hfile, s, strlen(s)+1, &ret, NULL );
	}
}

#else
#define DebugOutput

#endif

//-----------------------------------------------------------------------------
// Functions defined in other files
//-----------------------------------------------------------------------------
bool SendMsg(int msg, LPVOID param, int size);


//-----------------------------------------------------------------------------
// Name: 
// Desc: 
//-----------------------------------------------------------------------------
bool SendCmdMsg( TANKCMD * pcmds, int num, int local ) {
	int size = sizeof(int)*2 + num * sizeof(TANKCMD);
	char *param = new char[size];
	int *ibuf = (int *)param;
	ibuf[0] = local;
	ibuf[1] = num;
	memcpy( ibuf+2, pcmds, num*sizeof(TANKCMD));
	SendMsg( NETMSGTK_CMDINFO, param, size );
	
	delete[] param;
	return true;
}

//-----------------------------------------------------------------------------
// Name: 
// Desc: 
//-----------------------------------------------------------------------------
Console::Console() {
	con_version = ASIDER_GAME_TANK_VER_ALPHA3;
	err_code = 0;
}


//-----------------------------------------------------------------------------
// Name: 
// Desc: 
//-----------------------------------------------------------------------------
Console::~Console() {

#ifdef _TK_DEBUG
	CloseHandle( hfile );
#endif

}


//-----------------------------------------------------------------------------
// Name: 
// Desc: 
//-----------------------------------------------------------------------------
void Console::ErrorTrigger( unsigned int error ) {
	if ( error != 0 )
		MessageBox( NULL, "Something Error :)", "Error", MB_ICONERROR );
}



//-----------------------------------------------------------------------------
// Name: 
// Desc: 
//-----------------------------------------------------------------------------
ConsoleNet::ConsoleNet() {
	m_dwStatus		= CONSTAT_NOTHING;
	m_nPlayers		= 0;
	m_nComputers	= 0;
	m_nComPerTeam	= 0;
	m_nLocal		= 0;
	m_bHomeTeam		= true;

	m_pProCounter[0] = -1;
	m_pProCounter[1] = -1;
	m_nMsgCounter = 0;

	m_pRiver = NULL;
	m_pFood = NULL;

	ZeroMemory( m_ppTanks, sizeof(LPVOID)*DEFTANKNUM );
	ZeroMemory( m_ppBullets, sizeof(LPVOID)*DEFTANKNUM );
	ZeroMemory( m_ppBases, sizeof(LPVOID)*2);
}


//-----------------------------------------------------------------------------
// Name: 
// Desc: 
//-----------------------------------------------------------------------------
ConsoleNet::~ConsoleNet() {
	DELETE_ARRAY( m_pRiver );
	DELETE_PTR( m_pFood );
}


//-----------------------------------------------------------------------------
// Name: 
// Desc: 
//-----------------------------------------------------------------------------
void ConsoleNet::ErrorTrigger( unsigned int error ) {
	Console::ErrorTrigger( error );
}

//-----------------------------------------------------------------------------
// Name: 
// Desc: 
//-----------------------------------------------------------------------------
bool ConsoleNet::LoadMap( const char *mapname ) {
	return MapInfo.LoadMap( mapname );
}

//-----------------------------------------------------------------------------
// Name: 
// Desc: 
//-----------------------------------------------------------------------------
bool ConsoleNet::RenderMap(  bool hometeam ) {
	if ( !hometeam )
		MapInfo.CenterRotate180();

	// Set class properties
	m_bHomeTeam = hometeam;
	m_dwStatus = CONSTAT_WAITBEGIN;
	m_nNumTanks = MapInfo.GetObjectNum( OBJTYPE_TANK );
	m_nNumRiver = MapInfo.BlockCounter( LAND_RIVER, NULL );
	m_pRiver = new CObjBlock[m_nNumRiver];
	MapInfo.BlockCounter( LAND_RIVER, m_pRiver );

	// Create bases
	m_ppBases[0] = new CObjBase();
	m_ppBases[1] = new CObjBase();
	m_ppBases[0]->CreateBase( MapInfo.m_ptBase[0].x, MapInfo.m_ptBase[0].y, true);
	m_ppBases[1]->CreateBase( MapInfo.m_ptBase[1].x, MapInfo.m_ptBase[1].y, false);

	// Blt blocks
	int		x, y, xl, yl, level;
	RECT	*prc = new RECT, *prcTrans = new RECT;
	SetRect( prcTrans, 0, 32, 32, 64 );

	for ( y = 0, yl = 0; y < MapInfo.m_cyBlocks-1; y++, yl += g_FrameSmall )
		for ( x = 0, xl = 0; x < MapInfo.m_cxBlocks-1; x++, xl += g_FrameSmall ) {
			MapInfo.BlockLocator( x, y, prc, level );
			if ( level == LAYER_GROUND ) {
				if ( FAILED( pddsSolid->BltFast( xl, yl, pddsRes, prc, 0 ) ) )
					return false;
				if ( FAILED( pddsGrass->BltFast( xl, yl, pddsRes, prcTrans, 0 ) ) )
					return false;
			}else if ( level == LAYER_TREE ) {
				if ( FAILED( pddsSolid->BltFast( xl, yl, pddsRes, prcTrans, 0 ) ) )
					return false;
				if ( FAILED( pddsGrass->BltFast( xl, yl, pddsRes, prc, 0 ) ) )
					return false;
			}
		}
	return true;
}

//-----------------------------------------------------------------------------
// Name: 
// Desc: 
//-----------------------------------------------------------------------------
bool ConsoleNet::InitDisplay( HWND hwnd ) {
	if ( !hwnd )
		return false;

	// Create display
	if ( FAILED( Display.CreateWindowedDisplay( hwnd, g_FrameBig, g_FrameBig ) ) )
		return false;
	
	// Create surfaces
	pResource = new CSurface();
	if ( FAILED( Display.CreateSurfaceFromBitmap( &pResource, 
		"Res/res3.bmp", 512, 640 ) ) )
		return false;

	pRes2 = new CSurface();
	if ( FAILED( Display.CreateSurfaceFromBitmap( &pRes2, 
		"Res/res2.bmp", 640, 480 ) ) )
		return false;

	pMapSolid = new CSurface();
	if ( FAILED( Display.CreateSurface( &pMapSolid, g_FrameBig, g_FrameBig ) ) )
		return false;
	
	pMapGrass = new CSurface();
	if ( FAILED( Display.CreateSurface( &pMapGrass, g_FrameBig, g_FrameBig ) ) )
		return false;

	pText = new CSurface();
	if ( FAILED( Display.CreateSurface( &pText, 80, 30 ) ) )
		return false;

	// Set color key
	pMapSolid->SetColorKey( 0 );
	pMapGrass->SetColorKey( 0 );
	pResource->SetColorKey( 0 );
	pRes2->SetColorKey( 0 );

	// Render the scene according to the map data
	pddsSolid = pMapSolid->GetDDrawSurface();
	pddsGrass = pMapGrass->GetDDrawSurface();
	pddsRes	  = pResource->GetDDrawSurface();
	pddsRes2  = pRes2->GetDDrawSurface();

	return true;
}

//-----------------------------------------------------------------------------
// Name: 
// Desc: 
//-----------------------------------------------------------------------------
bool ConsoleNet::InitSound( HWND hwnd ) {
	if ( FAILED(SoundManager.Initialize( hwnd, DSSCL_PRIORITY, 1, 22050, 8 )) )
		return false;

	if ( FAILED(SoundManager.Create( &pFire, szFireWave )) )
		return false;

	if ( FAILED(SoundManager.Create( &pBoom, szBoomWave )) )
		return false;

	return true;
}

//-----------------------------------------------------------------------------
// Name: 
// Desc: 
//-----------------------------------------------------------------------------
bool ConsoleNet::InitObjects() {
	int		i;
	for ( i=0; i<DEFTANKNUM/2; i++ ) {		// !TODO: because of the map setting position
		m_ppTanks[i] = new CObjTank();
		m_ppTanks[i]->CreateTank(MapInfo.m_pTankInfo[DEFTANKNUM-1-i].posLT.x,
			MapInfo.m_pTankInfo[DEFTANKNUM-1-i].posLT.y,true);
		m_ppBullets[i] = new CObjBullet();
		m_ppBullets[i]->CreateBullet( NULL );
		m_ppTanks[i]->m_uiDirection = DIR_UP;
	}

	for ( ; i<DEFTANKNUM; i++ ) {
		m_ppTanks[i] = new CObjTank();
		m_ppTanks[i]->CreateTank(MapInfo.m_pTankInfo[DEFTANKNUM-1-i].posLT.x,
			MapInfo.m_pTankInfo[DEFTANKNUM-1-i].posLT.y,false);
		m_ppBullets[i] = new CObjBullet();
		m_ppBullets[i]->CreateBullet( NULL );
		m_ppTanks[i]->m_uiDirection = DIR_DOWN;
	}

	if ( !m_bHomeTeam ) {
		for ( i=0; i<DEFTANKNUM; i++ ) {
			MapInfo.ConvertCoord( m_ppTanks[i] );
			m_ppTanks[i]->m_uiDirection = 5 - m_ppTanks[i]->m_uiDirection;
		}
	}
	
	// Initialize AI command generator
	RECT		scope;
	SetRect( &scope, DEFTRACKSCOPE, DEFTRACKSCOPE, DEFTRACKSCOPE, DEFTRACKSCOPE );
	CObjTank	**pptk = m_ppTanks+m_nLocal+1;
	for ( i=0; i<DEFAINUM; i++ ) {
		m_ppAiGen[i].CreateGenerator( *(pptk+i), scope );
		m_ppAiGen[i].SetAim( m_ppBases[m_bHomeTeam], -1 );
		m_ppAiGen[i].SetAvoid( m_ppBases[1-m_bHomeTeam]->GetSurroundRect() );
	}

	// food
	m_pFood = new CObjFood();

	return true;
}

//-----------------------------------------------------------------------------
// Name: 
// Desc: 
//-----------------------------------------------------------------------------
bool ConsoleNet::CreateConsole( HWND hwnd, const char *mapname, bool hometeam ) {
	if ( !LoadMap( mapname ) || !InitDisplay( hwnd ) ||
		!InitSound( hwnd ) || !RenderMap( hometeam ) )
		return false;

#ifdef _TK_DEBUG
	lstrcpy(szDebugFile, "debug0.txt");
	hfile = CreateFile( szDebugFile, GENERIC_WRITE,
						FILE_SHARE_READ, NULL, CREATE_ALWAYS,
						FILE_ATTRIBUTE_NORMAL, NULL );
	if ( hfile == INVALID_HANDLE_VALUE ) {
		lstrcpy(szDebugFile, "debug1.txt");
		hfile = CreateFile( szDebugFile, GENERIC_WRITE,
						FILE_SHARE_READ, NULL, CREATE_ALWAYS,
						FILE_ATTRIBUTE_NORMAL, NULL );
		if ( hfile == INVALID_HANDLE_VALUE ) {

⌨️ 快捷键说明

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