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

📄 racex.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 <algorithm>


// This GUID value is used by DirectPlay. Each direct play game needs a GUID
// so that we can find sessions of a game in the network
#ifndef APP_GUID
	#define APP_GUID
static const GUID g_guidApp = 
{ 0xce1aeec6, 0x8291, 0x4b9e, { 0xa1, 0x2, 0x16, 0xd4, 0x19, 0xc1, 0x87, 0x48 } };
#endif

cRaceXApp* GetRaceXApp()
{
	return (cRaceXApp*)GetMainApp();
}

int string_compare(string s1, string s2)
{
    //return strcmp(s1,s2);
	return s1 < s2 ? 1 : 0;
}

cApplication* CreateApplication()
{
	cRaceXApp*	theApp;
	
	theApp = new cRaceXApp();

	return  (cApplication*)theApp;
}

void cRaceXApp::DoIdle()
{
	
	// In this function is where the game logic starts
	// Each time we don磘 have a system message to process, the cApplication base
	// class will call the DoIdle virtual function
	
	DDBLTFX ddbltfx;

	char buffer[255];
    
	// This static variables are used to control some game timings
	static long lLastIdle = 0;
	static long lLastOption = 0;
	static long lLastSprite = 0;
	static long lLastCaret = 0;

	static BOOL	bShowCaret = FALSE;	
	int			iCaretPos;

	cRaceCar*	pTmpCar;
	string		sTmp;


	long lStart;

	int i = 0, j = 0;

	// The frames are processed in a 30 miliseconds rate
	// If we didn磘 reach the 30 milisecond mark, we磍l not flip 
	// the primary surface
	if(GetTickCount() - lLastIdle < 30)
	{		
		PreventFlip();
		return;
	}

	// Update the LastIdle variable
	lLastIdle = GetTickCount();

	// Clear the BackBuffer with black color
	ddbltfx.dwSize      = sizeof(ddbltfx);
    ddbltfx.dwFillColor = RGB(0,0,0);
	GetMainApp()->m_pBackBuffer->Blt(NULL, NULL, NULL, DDBLT_COLORFILL,  &ddbltfx );

	// Based on the game state we磍l show each game screen
	switch(m_iState)
	{
	case GS_HELP:
		// Help screen
		m_txDigital.WriteText(m_pBackBuffer, "RACING WITH RACEX", 200, 40);

		m_txDigital.WriteText(m_pBackBuffer, "UP ARROW", 50, 140);
		m_txDigital.WriteText(m_pBackBuffer, "ACCELERATE THE CAR", 270, 140);

		m_txDigital.WriteText(m_pBackBuffer, "RIGHT ARROW", 50, 180);
		m_txDigital.WriteText(m_pBackBuffer, "TURN THE CAR CLOCKWISE", 270, 180);

		m_txDigital.WriteText(m_pBackBuffer, "LEFT ARROW", 50, 220);
		m_txDigital.WriteText(m_pBackBuffer, "TURN THE CAR UNCLOCKIWISE", 270, 220);

		m_txDigital.WriteText(m_pBackBuffer, "DOWN ARROW", 50, 260);
		m_txDigital.WriteText(m_pBackBuffer, "BREAK THE CAR", 270, 260);

		m_txDigital.WriteText(m_pBackBuffer, "PRESS ENTER TO CONTINUE", 160, 360);

		// If the user hit enter os space or escape, go back to the main screen
		m_Keyboard.Process();
		if(GetTickCount() - lLastOption > 200)
		{
			if(m_Keyboard.CheckKey(DIK_RETURN) ||
			   m_Keyboard.CheckKey(DIK_NUMPADENTER) ||
			   m_Keyboard.CheckKey(DIK_SPACE) ||
			   m_Keyboard.CheckKey(DIK_ESCAPE))
			{
				m_iState = GS_MAINSCREEN;
				iStart = 0;
				lLastOption = GetTickCount();			
			}
		}
		break;
	case GS_WAITCOMPETITION:
		
		// This game state is used the multiplayer game.
		// When we磖e in the competiton mode, we磍l show this screen
		// when we already saw the competition status screen but the
		// other network player didn磘 saw it yet.
		if(iStart = 0)
		{
			lLastSprite = 0;
		}

		if(GetTickCount() - lLastSprite > 500)
		{
			bShowCaret = !bShowCaret;
			lLastSprite = GetTickCount();
		}

		if(bShowCaret)
		{
			m_txDigital.WriteText(m_pBackBuffer, "WAITING OTHER PLAYERS", 180, 120);
			m_txDigital.WriteText(m_pBackBuffer, "LOOK AT COMPETITION RESULTS", 125, 180);
			m_txDigital.WriteText(m_pBackBuffer, "PLEASE WAIT...", 225, 240);
		}

		// Check if all the players are ready (saw the competition status)
		// If so, go to the next race
		if(m_pCompetition.AllPlayersReady() && m_pMultiPlayer.IsHosting())
		{
			strcpy(buffer, pRaceTrack.GetFileName());
			i = strlen(buffer);
			m_pMultiPlayer.SendTo(0, MSG_START_RACE, (unsigned char *) &buffer[0], i, DPNSEND_SYNC | DPNSEND_NOLOOPBACK | DPNSEND_GUARANTEED);
			m_iState = GS_RACE;
			iStart = 0;
			lLastOption = GetTickCount();
		}

		break;
	case GS_WAIT:
		// This state is used in the multiplayer game
		// If we need to start a race and the other players didn磘 finish their
		// configuration yet (doesn磘 inform the name and car color) we
		// need to wait.
		if(iStart = 0)
		{
			lLastSprite = 0;
		}

		if(GetTickCount() - lLastSprite > 500)
		{
			bShowCaret = !bShowCaret;
			lLastSprite = GetTickCount();
		}

		if(bShowCaret)
		{
			m_txDigital.WriteText(m_pBackBuffer, "WAITING OTHER PLAYERS", 180, 150);
			m_txDigital.WriteText(m_pBackBuffer, "PLEASE WAIT...", 240, 200);
		}
		
		// Check if all the players are ready
		if(m_pCompetition.AllPlayersReady() && m_pMultiPlayer.IsHosting())
		{
			// If we磖e in the competition mode, we go to the competition status
			// screen. If we磖e in single track mode, start the race
			if(m_pCompetition.GetCompetitionMode() == TRUE)
			{
				m_pCompetition.ResetReadyState();
				m_iState = GS_COMPETITIONSTATUS;
				iStart = 0;
				m_sndSelect.Play();
				lLastOption = GetTickCount();
				m_pMultiPlayer.SendTo(0, MSG_COMPETITIONSTATUS, NULL, 0, DPNSEND_SYNC | DPNSEND_NOLOOPBACK | DPNSEND_GUARANTEED);
			}
			else
			{
				strcpy(buffer, pRaceTrack.GetFileName());
				i = strlen(buffer);
				m_pMultiPlayer.SendTo(0, MSG_START_RACE, (unsigned char *) &buffer[0], i, DPNSEND_SYNC | DPNSEND_NOLOOPBACK | DPNSEND_GUARANTEED);
				m_iState = GS_RACE;
				iStart = 0;
				lLastOption = GetTickCount();
			}
		}
		break;
	case GS_CREDITS:
		// This state show the credits of the game
		// **********************************************************
		// *                                                        *
		// * PLEASE !! RESPECT THE COPYRIGHTS AND DON碩 CHANGE IT ! *
		// *                                                        *
		// **********************************************************

		#define LINE_STEP 40

		lStart = 480;

		iStart+=1;
		
		if(lStart-iStart > 15 && lStart-iStart < 465)
		{
			m_txDigital.WriteText(m_pBackBuffer, "GAME DESIGN", 120, lStart-iStart);
			m_txDigital.WriteText(m_pBackBuffer, "MAURICIO RITTER", 340, lStart-iStart);
		}

		
		lStart+=40;
		if(lStart-iStart > 15 && lStart-iStart < 465)
		{
			m_txDigital.WriteText(m_pBackBuffer, "3D MODELING", 120, lStart-iStart);
			m_txDigital.WriteText(m_pBackBuffer, "MAURICIO RITTER", 340, lStart-iStart);
		}

		lStart+=40;
		if(lStart-iStart > 15 && lStart-iStart < 465)
		{
			m_txDigital.WriteText(m_pBackBuffer, "TEXTURES", 120, lStart-iStart);
			m_txDigital.WriteText(m_pBackBuffer, "MAURICIO RITTER", 340, lStart-iStart);
		}

		lStart+=40;
		if(lStart-iStart > 15 && lStart-iStart < 465)
		{
			m_txDigital.WriteText(m_pBackBuffer, "2D ART", 120, lStart-iStart);
			m_txDigital.WriteText(m_pBackBuffer, "MAURICIO RITTER", 340, lStart-iStart);
		}

		lStart+=40;
		if(lStart-iStart > 15 && lStart-iStart < 465)
		{
			m_txDigital.WriteText(m_pBackBuffer, "LEVEL DESIGN", 120, lStart-iStart);
			m_txDigital.WriteText(m_pBackBuffer, "MAURICIO RITTER", 340, lStart-iStart);
		}

		lStart+=40;
		if(lStart-iStart > 15 && lStart-iStart < 465)
		{
			m_txDigital.WriteText(m_pBackBuffer, "PROGRAMMING", 120, lStart-iStart);
			m_txDigital.WriteText(m_pBackBuffer, "MAURICIO RITTER", 340, lStart-iStart);
		}

		lStart+=40;
		if(lStart-iStart > 15 && lStart-iStart < 465)
		{
			m_txDigital.WriteText(m_pBackBuffer, "SOUND EDITING", 120, lStart-iStart);
			m_txDigital.WriteText(m_pBackBuffer, "MAURICIO RITTER", 340, lStart-iStart);
		}

		lStart+=40;
		if(lStart-iStart > 15 && lStart-iStart < 465)
		{
			m_txDigital.WriteText(m_pBackBuffer, "MUSIC", 120, lStart-iStart);
			m_txDigital.WriteText(m_pBackBuffer, "MAURICIO RITTER", 340, lStart-iStart);
		}


		lStart+=40;
		if(lStart-iStart > 15 && lStart-iStart < 465)
		{
			m_txDigital.WriteText(m_pBackBuffer, "SPECIAL THANKS TO ALL MY BETA TESTERS", 50, lStart-iStart);
		}

		lStart+=30;
		if(lStart-iStart > 15 && lStart-iStart < 465)
		{
			m_txDigital.WriteText(m_pBackBuffer, "IN SPECIAL TO", 120, lStart-iStart);
			m_txDigital.WriteText(m_pBackBuffer, "COLIN DAVIES", 340, lStart-iStart);
		}

		lStart+=20;
		if(lStart-iStart > 15 && lStart-iStart < 465)
		{
			m_txDigital.WriteText(m_pBackBuffer, "ISAAC SASSON", 340, lStart-iStart);
		}

		lStart+=20;
		if(lStart-iStart > 15 && lStart-iStart < 465)
		{
			m_txDigital.WriteText(m_pBackBuffer, "JAMES T JOHNSON", 340, lStart-iStart);
		}


		lStart+=20;
		if(lStart-iStart > 15 && lStart-iStart < 465)
		{
			m_txDigital.WriteText(m_pBackBuffer, "NISHANT SIVAKUMAR", 340, lStart-iStart);
		}

		lStart+=20;
		if(lStart-iStart > 15 && lStart-iStart < 465)
		{
			m_txDigital.WriteText(m_pBackBuffer, "NNAMDI ONYEYIRI", 340, lStart-iStart);
		}

		lStart+=20;
		if(lStart-iStart > 15 && lStart-iStart < 465)
		{
			m_txDigital.WriteText(m_pBackBuffer, "SMITHA", 340, lStart-iStart);
		}

		lStart+=40;
		if(lStart-iStart > 15 && lStart-iStart < 465)
		{
			m_txDigital.WriteText(m_pBackBuffer, "THIS GAME IS TOTALLY FREEWARE", 120, lStart-iStart);
		}

		lStart+=40;
		if(lStart-iStart > 15 && lStart-iStart < 465)
		{
			m_txDigital.WriteText(m_pBackBuffer, "IF YOU WANT TO CONTACT ME ABOUT THE GAME", 20, lStart-iStart);
		}

		lStart+=40;
		if(lStart-iStart > 15 && lStart-iStart < 465)
		{
			m_txDigital.WriteText(m_pBackBuffer, "JUST SEND AN EMAIL TO", 160, lStart-iStart);
		}

		lStart+=40;
		if(lStart-iStart > 15 && lStart-iStart < 465)
		{
			m_txDigital.WriteText(m_pBackBuffer, "MAURICIORITTER@HOTMAIL.COM", 130, lStart-iStart);
		}

		//lStart+=480;

		if(iStart > lStart)
		{
			iStart = 0;
		}

		// If the user hit enter os space or escape, go back to the main screen
		m_Keyboard.Process();
		if(GetTickCount() - lLastOption > 200)
		{
			if(m_Keyboard.CheckKey(DIK_RETURN) ||
			   m_Keyboard.CheckKey(DIK_NUMPADENTER) ||
			   m_Keyboard.CheckKey(DIK_SPACE) ||
			   m_Keyboard.CheckKey(DIK_ESCAPE))
			{
				m_iState = GS_MAINSCREEN;
				iStart = 0;
				lLastOption = GetTickCount();			
			}
		}
		break;
	case GS_NETWORKSTATUS:

		// This state is used the multiplayer game
		// It shows the current multiplayer game status (number of connected,
		// players, their name and car color)
		if(iStart == 0)
		{
			iStart = 1;
			m_surfTitle.Destroy();

			for(i=0;i<4;i++)
			{
				m_surfBigCars[i].Create(150, 140, RGB(0,0,0));
				m_surfBigCars[i].LoadBitmap(GetInstHandle(), IDB_BIGCAR_BLUE + i);
			}

			m_surfCarPannel.Create(320,110, RGB(0,0,0));
			m_surfCarPannel.LoadBitmap(GetInstHandle(), IDB_CAR_PANNEL);

			m_surfHelmet.Create(640,60, RGB(0,0,0));
			m_surfHelmet.LoadBitmap(GetInstHandle(), IDB_CURRENTCONNECTED_PANEL);
			
			m_surfTitle.Create(520,40, RGB(0,0,0));
			m_surfTitle.LoadBitmap(GetInstHandle(), IDB_MULTIPLAYERSTATUS_TITLE);

			m_surfPositions.Create(120,40, RGB(0,0,0));
			m_surfPositions.LoadBitmap(GetInstHandle(), IDB_POSITIONS);
		}

		// If the number of players is different from the last iteration,
		// a new player connected to the game session. Add this player to the
		// competition or race.
		if(m_pMultiPlayer.GetNumConnected() != m_pCompetition.GetNumberOfCars()-1)
		{
			for(i=0;i<m_pMultiPlayer.GetNumConnected();i++)
			{
				if(m_pCompetition.DPNIDExists(m_pMultiPlayer.GetPlayerInfo(i)->dpnidPlayer) == FALSE)
				{
					m_pCompetition.AddPlayer("UNKNOWN", -1, CTRL_NETWORK_REMOTE, i+1, m_pMultiPlayer.GetPlayerInfo(i)->dpnidPlayer);
				}
			}
		}

		m_surfTitle.Draw(m_pBackBuffer, 60,0);
		m_surfHelmet.Draw(m_pBackBuffer, 0,80);


		// Show each one of the connected players
		for(i=0;i<m_pCompetition.GetNumberOfCars();i++)
		{

			switch(i)
			{
			case 0:
				if(m_pCompetition.GetColor(i) != -1)
					m_surfBigCars[m_pCompetition.GetColor(i) - IDB_CAR_BLUE].Draw(m_pBackBuffer, 10, 140, 20, 14, 130, 106);
				m_surfCarPannel.Draw(m_pBackBuffer, 0, 140);
				m_surfPositions.Draw(m_pBackBuffer, 130, 2 + 140, 0, 0, 30, 40);
				sTmp = m_pCompetition.GetPlayerName(m_pCompetition.GetPlayerID(0));
				m_txDigitalSmall.WriteText(m_pBackBuffer, &sTmp[0], 145, 78 + 140);
				break;
			case 1:
				if(m_pCompetition.GetColor(i) != -1)
					m_surfBigCars[m_pCompetition.GetColor(i) - IDB_CAR_BLUE].Draw(m_pBackBuffer, 330, 140, 20, 14, 130, 106);
				m_surfCarPannel.Draw(m_pBackBuffer, 320, 140);
				m_surfPositions.Draw(m_pBackBuffer, 450, 2 + 140, 30, 0, 30, 40);
				sTmp = m_pCompetition.GetPlayerName(m_pCompetition.GetPlayerID(1));
				m_txDigitalSmall.WriteText(m_pBackBuffer, &sTmp[0], 145 + 310, 78 + 140);
				break;
			case 2:
				if(m_pCompetition.GetColor(i) != -1)
					m_surfBigCars[m_pCompetition.GetColor(i) - IDB_CAR_BLUE].Draw(m_pBackBuffer, 10, 250, 20, 14, 130, 106);
				m_surfCarPannel.Draw(m_pBackBuffer, 0, 250);
				m_surfPositions.Draw(m_pBackBuffer, 130, 2 + 250, 60, 0, 30, 40);
				sTmp = m_pCompetition.GetPlayerName(m_pCompetition.GetPlayerID(0));
				m_txDigitalSmall.WriteText(m_pBackBuffer, &sTmp[0], 145, 78 + 250);

				break;
			case 3:
				if(m_pCompetition.GetColor(i) != -1)
					m_surfBigCars[m_pCompetition.GetColor(i) - IDB_CAR_BLUE].Draw(m_pBackBuffer, 330, 250, 20, 14, 130, 106);
				m_surfCarPannel.Draw(m_pBackBuffer, 320, 250);
				m_surfPositions.Draw(m_pBackBuffer, 450, 2 + 250, 90, 0, 30, 40);
				sTmp = m_pCompetition.GetPlayerName(m_pCompetition.GetPlayerID(1));
				m_txDigitalSmall.WriteText(m_pBackBuffer, &sTmp[0], 145 + 310, 78 + 250);

				break;

			}

		}
	
		// If the user is hosting the session, he need to press enter to start
		// the race with the currently connected players
		// If its a peer, he need to wait the Start command from the host

⌨️ 快捷键说明

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