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

📄 cracetrack.cpp

📁 Visual C++ 游戏开发与设计实例 源代码(所有)
💻 CPP
📖 第 1 页 / 共 5 页
字号:
// RACE X
//
// Written by Mauricio Teichmann Ritter
//
// Copyright (C) 2002, Brazil. All rights reserved.
// 
//

#include "stdafx.h"
#include "RaceX.h"
#include "cRaceTrack.h"
#include ".\applib\capplication.h"

#include <stdio.h>
#include <search.h>
#include <math.h>

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

cRaceTrack::cRaceTrack()
{
	m_iCols = 0;
	m_iRows = 0;

	m_iViewX = 0;
	m_iViewY = 0;

	m_iNumCars = 0;
	
	m_pRaceCars[0] = NULL;
	m_pRaceCars[1] = NULL;
	m_pRaceCars[2] = NULL;
	m_pRaceCars[3] = NULL;

	m_iMaxCheckPoint = 0;

	m_sTrackName = NULL;

	m_iLaps = 0;

}

cRaceTrack::~cRaceTrack()
{

}

void cRaceTrack::Create(int nWidth, int nHeight)
{
	
	m_iState = 0;
	m_lStartTime = 0;


	// 创建拥有赛道贴片数据的数组
	m_RoadMap.Create(nWidth,nHeight);

	// 创建碰撞检测矩形区域
	m_hcRoadMap.CreateRectBound(nWidth * 40, nHeight * 40);

	// 初始化宽度和高度变量
	m_iCols = nWidth;
	m_iRows = nHeight;

	static BOOL IsLoaded = FALSE;
	if(!IsLoaded)
	{
		// 初始化信号灯声音
		m_sndSemaphore.Create(MAKEINTRESOURCE(IDR_SEMAPHORE), DSBCAPS_CTRL3D, DS3DALG_NO_VIRTUALIZATION);
		m_sndSemaphore.m_pSoundBuffer->SetCurrentPosition( 0 );
		IsLoaded = TRUE;
	}
}

void cRaceTrack::SetTile(UINT iTile)
{
	//载入碰撞贴片到曲面:草贴片或者沙漠贴片
	m_surfTile.Create(200,200);
	switch(iTile)
	{
		case 1:
			m_surfTile.LoadBitmap(GetMainApp()->m_hInst, IDB_TILE_GRASS, 0, 0, 200, 200);
			break;
		case 2:
			m_surfTile.LoadBitmap(GetMainApp()->m_hInst, IDB_TILE_SAND, 0, 0, 200, 200);
			break;
	}
}

void cRaceTrack::Destroy(BOOL bLastDestroy)
{
	// Clean up everything
	m_surfTile.Destroy();

	m_surfDiagonalQ0.Destroy();
	m_surfDiagonalQ1.Destroy();
	m_surfDiagonalQ2.Destroy();
	m_surfDiagonalQ3.Destroy();

	m_surfMidDiagonalQ0.Destroy();
	m_surfMidDiagonalQ1.Destroy();
	m_surfMidDiagonalQ2.Destroy();
	m_surfMidDiagonalQ3.Destroy();

	m_surfBlackPointQ0.Destroy();
	m_surfBlackPointQ1.Destroy();
	m_surfBlackPointQ2.Destroy();
	m_surfBlackPointQ3.Destroy();

	m_surf_HZ_StartDiagonalQ0.Destroy();
	m_surf_HZ_StartDiagonalQ1.Destroy();
	m_surf_HZ_StartDiagonalQ2.Destroy();
	m_surf_HZ_StartDiagonalQ3.Destroy();

	m_surf_HZ_EndDiagonalQ0.Destroy();
	m_surf_HZ_EndDiagonalQ1.Destroy();
	m_surf_HZ_EndDiagonalQ2.Destroy();
	m_surf_HZ_EndDiagonalQ3.Destroy();

	m_surf_VR_StartDiagonalQ0.Destroy();
	m_surf_VR_StartDiagonalQ1.Destroy();
	m_surf_VR_StartDiagonalQ2.Destroy();
	m_surf_VR_StartDiagonalQ3.Destroy();

	m_surf_VR_EndDiagonalQ0.Destroy();
	m_surf_VR_EndDiagonalQ1.Destroy();
	m_surf_VR_EndDiagonalQ2.Destroy();
	m_surf_VR_EndDiagonalQ3.Destroy();

	m_surfSRaceRoadQ0.Destroy();
	m_surfSRaceRoadQ1.Destroy();
	m_surfSRaceRoadQ2.Destroy();
	m_surfSRaceRoadQ3.Destroy();

	m_surfHalfRoadQ0.Destroy();
	m_surfHalfRoadQ1.Destroy();
	m_surfHalfRoadQ2.Destroy();
	m_surfHalfRoadQ3.Destroy();

	m_surfCurveQ0.Destroy();
	m_surfCurveQ1.Destroy();
	m_surfCurveQ2.Destroy();
	m_surfCurveQ3.Destroy();

	m_surfEndCurveQ0.Destroy();
	m_surfEndCurveQ1.Destroy();
	m_surfEndCurveQ2.Destroy();
	m_surfEndCurveQ3.Destroy();

	m_surfFullRoadQ0.Destroy();
	m_surfFullRoadQ1.Destroy();
	m_surfFullRoadQ2.Destroy();
	m_surfFullRoadQ3.Destroy();
	m_surfFullRoadQ4.Destroy();
	m_surfFullRoadQ5.Destroy();
	m_surfFullRoadQ6.Destroy();
	m_surfFullRoadQ7.Destroy();
	m_surfFullRoadQ8.Destroy();
	m_surfFullRoadQ9.Destroy();
	m_surfFullRoadQ10.Destroy();
	m_surfFullRoadQ11.Destroy();
	m_surfFullRoadQ12.Destroy();
	m_surfFullRoadQ13.Destroy();
	m_surfFullRoadQ14.Destroy();
	m_surfFullRoadQ15.Destroy();
	m_surfFullRoadQ16.Destroy();
	m_surfFullRoadQ17.Destroy();


	m_surfMediumCurveQ0P1.Destroy();
	m_surfMediumCurveQ1P1.Destroy();
	m_surfMediumCurveQ2P1.Destroy();
	m_surfMediumCurveQ3P1.Destroy();

	m_surfMediumCurveQ0P2.Destroy();
	m_surfMediumCurveQ1P2.Destroy();
	m_surfMediumCurveQ2P2.Destroy();
	m_surfMediumCurveQ3P2.Destroy();

	m_surfMediumCurveQ0P3.Destroy();
	m_surfMediumCurveQ1P3.Destroy();
	m_surfMediumCurveQ2P3.Destroy();
	m_surfMediumCurveQ3P3.Destroy();

	m_surfEndMediumCurveQ0P1.Destroy();
	m_surfEndMediumCurveQ1P1.Destroy();
	m_surfEndMediumCurveQ2P1.Destroy();
	m_surfEndMediumCurveQ3P1.Destroy();

	m_surfEndMediumCurveQ0P2.Destroy();
	m_surfEndMediumCurveQ1P2.Destroy();
	m_surfEndMediumCurveQ2P2.Destroy();
	m_surfEndMediumCurveQ3P2.Destroy();

	m_surfEndMediumCurveQ0P3.Destroy();
	m_surfEndMediumCurveQ1P3.Destroy();
	m_surfEndMediumCurveQ2P3.Destroy();
	m_surfEndMediumCurveQ3P3.Destroy();


	//m_surfMap.Destroy();
	m_RoadMap.Destroy();

	m_sptrWait.Destroy();

	m_hcRoadMap.Destroy();

	m_surfRaceCompleted.Destroy();

	// Destroy all the cars also
	for(int i=0;i<m_iNumCars;i++)
	{
		m_pRaceCars[i]->Destroy();
	}

	if(m_sTrackName != NULL)
	{
		free(m_sTrackName);
		m_sTrackName = NULL;
	}

	m_iCols = 0;
	m_iRows = 0;

	m_iViewX = 0;
	m_iViewY = 0;

	m_iNumCars = 0;
	
	m_pRaceCars[0] = NULL;
	m_pRaceCars[1] = NULL;
	m_pRaceCars[2] = NULL;
	m_pRaceCars[3] = NULL;

	m_iMaxCheckPoint = 0;

	m_sTrackName = NULL;

	m_iLaps = 0;

	if(bLastDestroy)
		m_sndSemaphore.Destroy();

}

// 这个函数用来在屏幕中绘制赛道
void cRaceTrack::Draw(int iDestX, int iDestY, int nX, int nY, int nWidth, int nHeight)
{
	// First we create to reference variables, to know in which position we磖e
	// gonna draw the track

	// If we receive the nX and nY parameters we磍l draw the track using these
	// values (this is used in the select track screen)
	// If we don磘 inform this values, we磍l use the current view of the player
	// that are stored in the m_iView variables
	int iRefX, iRefY;

	if(nX == -1)
	{
		iRefX = m_iViewX;
		iRefY = m_iViewY;
	}
	else
	{
		iRefX = nX;
		iRefY = nY;
	}

	#define TILE_SIZE 200
	#define ITEM_SIZE 40
	int iSizeX, iSizeY;

	int iTmpX = iDestX, iTmpY = iDestY;

	// First we磍l start drawing the tile
	// We need to draw the tile based on the current view of the iRef variables
	while(iTmpX < iDestX+nWidth)
	{
		while(iTmpY < iDestY+nHeight)
		{
			if(iTmpX+TILE_SIZE > iDestX+nWidth)
				iSizeX = TILE_SIZE - ((iTmpX+TILE_SIZE)-(iDestX+nWidth));
			else
				iSizeX = TILE_SIZE;

			if(iTmpY+TILE_SIZE > iDestY+nHeight)
				iSizeY = TILE_SIZE - ((iTmpY+TILE_SIZE)-(iDestY+nHeight));
			else
				iSizeY = TILE_SIZE;

			m_surfTile.Draw(GetMainApp()->m_pBackBuffer, iTmpX, iTmpY, iRefX % TILE_SIZE, iRefY % TILE_SIZE, iSizeX-(iRefX % TILE_SIZE), iSizeY-(iRefY % TILE_SIZE));

			iTmpY += TILE_SIZE - iRefY % TILE_SIZE;
			iRefY = 0;
		}
		iTmpX += TILE_SIZE - iRefX % TILE_SIZE;
		iTmpY = iDestY;
		if(nX == -1)
		{
			iRefY = m_iViewY;
		}
		else
		{
			iRefY = nY;
		}
		iRefX = 0;
	}
	
	int iItemX, iItemY;

	iTmpX = iDestX;
	iTmpY = iDestY;

	// Reset the iRef variables (since we use it in the loop to draw the tiles)
	if(nX == -1)
	{
		iRefX = m_iViewX;
		iRefY = m_iViewY;
	}
	else
	{
		iRefX = nX;
		iRefY = nY;
	}

	// Based on the tile matrix, we磍l identify which tiles are visible to the
	// currently view
	// The ItemX and ItemY will hold the Column and Row of the first visible
	// tile
	iItemX = iRefX / ITEM_SIZE;
	iItemY = iRefY / ITEM_SIZE;

	// The iMaxCol and iMaxRow will hold the maximum column and row that we need
	// to show to the user
	int iMaxCol = ceil((double)iRefX / ITEM_SIZE)+ceil((double)nWidth/(double)ITEM_SIZE);
	int iMaxRow = ceil((double)iRefY / ITEM_SIZE)+ceil((double)nHeight/(double)ITEM_SIZE);

	if(iMaxCol > m_iCols)
		iMaxCol = m_iCols;

	if(iMaxRow > m_iRows)
		iMaxRow = m_iRows;

	// Now we磍l loop based on the first visible Column and Row (iItemX and iItemY)
	// and based on the iMaxCol and iMaxRow
	for(int iCol = iItemX;iCol<iMaxCol;iCol++)
	{
		for(int iRow = iItemY;iRow<iMaxRow;iRow++)
		{
			// If we have a tile associated with the current position
			// of the iteration, draw it.
			if(m_RoadMap.GetValue(iCol,iRow) != 0)

⌨️ 快捷键说明

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