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

📄 main.cpp

📁 <B>很多DirectX 9.0游戏编程源码例子</B>
💻 CPP
📖 第 1 页 / 共 3 页
字号:
				vDrawInterfaceObject(	
					iX*g_iTileSize, 
					iY*g_iTileSize, 
					(float)g_iTileSize, 
					(float)g_iTileSize, 
					iTile );
			}

			// Draw overlay to show which
			// tile is selected
			if( iTile == g_iCurTile ) {
				vDrawInterfaceObject(	
					iX*g_iTileSize, 
					iY*g_iTileSize, 
					(float)g_iTileSize, 
					(float)g_iTileSize, 
					18 );
			}
		}
	}

	// Display a red box around the 
	// current tile selected
	vDrawInterfaceObject(	
		32, 
		32*7, 
		(float)g_iTileSize, 
		(float)g_iTileSize, 
		g_iCurTile );
	
	// Stop Rendering
	g_pd3dDevice->EndScene();
	
	//
	// Output the scene
	//

	// Source rectangle
	rectSrc.top = 0;		
	rectSrc.bottom = g_iTileSize*8;
	rectSrc.left = 0;
	rectSrc.right = g_iTileSize*3;
	
	// Destination rectangle
	rectDest.top = 0;
	rectDest.bottom = (g_iTileSize*8);
	rectDest.left = 0;
	rectDest.right = (g_iTileSize*3);

	// Present the results
	g_pd3dDevice->Present( &rectSrc, &rectDest, hWndToolBar, NULL );
}

void vCheckMouse( void )
{
	RECT		rcWindow;
	POINT		Point;
	int			iMouseX;
	int			iMouseY;
	int			iTileX;
	int			iTileY;

	// Get mouse coordinates
	GetCursorPos( &Point );
	iMouseX = Point.x;
	iMouseY = Point.y;
	
	// Figure out the toolbar work area
	GetWindowRect( hWndToolBar, &rcWindow );

	// Check if mouse within tool-bar window
	if( iMouseX > rcWindow.left &&
		iMouseX < rcWindow.right && 
		iMouseY > rcWindow.top &&
		iMouseY < rcWindow.bottom ) 
	{
		// Adjust mouse coords to be local to toolbar
		iMouseX -= rcWindow.right;
		iMouseY -= rcWindow.top;

		// Figure out tile coordinates
		iTileX = iMouseX/g_iTileSize;
		iTileY = iMouseY/g_iTileSize;

		// Figure out picked tile
		g_iCurTile = (g_iCurTileSet*21)+(iTileX+(iTileY*3))-1;

		// Make sure tile is valid
		if( g_iCurTile < 0 || g_iCurTile >= g_iTotalTiles ) {
			g_iCurTile = 0;		
		}

		vRenderTileSet();
	}

	// Check if mouse within edit window
	else {
		GetWindowRect( g_hWnd, &rcWindow );

		if( iMouseX > rcWindow.left &&
			iMouseX < rcWindow.right && 
			iMouseY > rcWindow.top &&
			iMouseY < rcWindow.bottom ) 
		{
			// Adjust mouse coords to be local to edit window
			iMouseX -= rcWindow.left+g_iXOffset;
			iMouseY -= rcWindow.top+g_iYOffset;

			// Figure out tile coordinates
			iTileX = iMouseX/g_iTileSize;
			iTileY = iMouseY/g_iTileSize;

			g_iTileMap[ iTileX+g_iXPos+
						( (iTileY + g_iYPos) 
						* g_iMapWidth ) ] = g_iCurTile;

			// Render updated minimap
			vRenderMinimap();
		}
	}
}

//-----------------------------------------------------------------------------
//
// Saves the tile map to a binary file.
//
//-----------------------------------------------------------------------------
void vSaveMap( void )
{
	FILE			*fp;
	int				iRet;
	OPENFILENAME	fileStruct;
	char			szFileName[ 512 ];
	char			szFilter[ 32 ];
	char			szExtension[ 32 ];

	// Clear buffer to receive file name
	memset( szFileName, 0x00, 512 );

	// Create file filter
	memset( szFilter, 0x00, 32 );
	strcpy( szFilter, "*.dat" );

	// Create file extension
	memset( szExtension, 0x00, 32 );
	strcpy( szExtension, "dat" );

	//
	// Setup the file dialog box
	//

	// Clear file dialog structure
	memset( &fileStruct, 0x00, sizeof( OPENFILENAME ) );

	// Initialize structure
	fileStruct.hInstance = g_hInstance;
	fileStruct.hwndOwner = g_hWnd;
	fileStruct.lpstrDefExt = szExtension;
	fileStruct.lpstrFileTitle = szFileName;
	fileStruct.lpstrFilter = szFilter;
	fileStruct.nMaxFileTitle = 512;
	fileStruct.lStructSize = sizeof( OPENFILENAME );
	
	// Retrieve the filename
	iRet = GetSaveFileName( &fileStruct );

	// Exit out on failure
	if( !iRet ) {
		return;
	}

	// Open the tile file
	fp = fopen( szFileName, "wb" );

	// Return if open failed
	if( fp == NULL ) {
		return;
	}

	// Save the tile buffer
	fwrite( g_iTileMap, 10000, sizeof(int), fp );

	// Close the tile file
	fclose( fp );

	// Play sound to indicate action
	PlaySound("bleep.wav",NULL,SND_FILENAME|SND_ASYNC);
}

//-----------------------------------------------------------------------------
//
// Loads the tile map from a binary file.
//
//-----------------------------------------------------------------------------
void vLoadMap( void )
{
	FILE			*fp;
	OPENFILENAME	fileStruct;
	char			szFileName[ 512 ];
	char			szFilter[ 32 ];
	char			szExtension[ 32 ];
	int				iRet;

	// Clear buffer to receive file name
	memset( szFileName, 0x00, 512 );

	// Create file filter
	memset( szFilter, 0x00, 32 );
	strcpy( szFilter, "*.dat" );

	// Create file extension
	memset( szExtension, 0x00, 32 );
	strcpy( szExtension, "dat" );

	//
	// Setup the file dialog box
	//

	// Clear file dialog structure
	memset( &fileStruct, 0x00, sizeof( OPENFILENAME ) );

	// Initialize structure
	fileStruct.hInstance = g_hInstance;
	fileStruct.hwndOwner = g_hWnd;
	fileStruct.lpstrDefExt = szExtension;
	fileStruct.lpstrFileTitle = szFileName;
	fileStruct.lpstrFilter = szFilter;
	fileStruct.nMaxFileTitle = 512;
	fileStruct.lStructSize = sizeof( OPENFILENAME );

	// Retrieve the filename
	iRet = GetOpenFileName( &fileStruct );

	// Exit out on failure
	if( !iRet ) {
		return;
	}

	// Open the tile file
	fp = fopen( szFileName, "rb" );

	// Return if no file to load
	if( fp == NULL ) {
		return;
	}
	
	// Read the tile buffer
	fread( g_iTileMap, 10000, sizeof(int), fp );

	// Close the tile file
	fclose( fp );
	
	// Play sound to indicate action
	PlaySound("button.wav",NULL,SND_FILENAME|SND_ASYNC);
}

//-----------------------------------------------------------------------------
//
// Create the minimap window
//
//-----------------------------------------------------------------------------
void vCreateMinimap(HWND hwnd, HINSTANCE hinst)
{
	WNDCLASSEX	wcMinimap;
	RECT		rcWindow;

	// Set up and register toolbar window class
	wcMinimap.cbSize		= sizeof(wcMinimap);
	wcMinimap.style			= CS_HREDRAW | CS_VREDRAW;
	wcMinimap.lpfnWndProc	= fnMessageProcessor;
	wcMinimap.cbClsExtra	= 0;
	wcMinimap.cbWndExtra	= 0;
	wcMinimap.hInstance		= hinst;
	wcMinimap.hIcon			= LoadIcon( NULL, IDI_APPLICATION );
	wcMinimap.hCursor		= LoadCursor( NULL, IDC_ARROW );
	wcMinimap.hbrBackground	= (HBRUSH) GetStockObject (COLOR_BACKGROUND);
	wcMinimap.lpszMenuName	= NULL;
	wcMinimap.lpszClassName	= "Minimap";	// Registered Class Name
	wcMinimap.hIconSm		= LoadIcon( NULL, IDI_APPLICATION );
    RegisterClassEx(&wcMinimap);
    
	// Figure out position
	GetWindowRect( g_hWnd, &rcWindow );

	// Create mini-map window
	hWndMinimap = CreateWindowEx ( 
		WS_EX_LEFT|WS_EX_TOPMOST|WS_EX_TOOLWINDOW,
		"Minimap", "Minimap",
		WS_BORDER | WS_VISIBLE | WS_MINIMIZEBOX,	
		rcWindow.left+10, rcWindow.bottom+g_iYOffset-240, 
		200, 200, hwnd, NULL, hinst, NULL);

	// Activate edit area
	SetActiveWindow( g_hWnd );
}

//-----------------------------------------------------------------------------
//
// Render the minimap
//
//-----------------------------------------------------------------------------
void vRenderMinimap( void )
{
	RECT	rectSrc;
	RECT	rectDest;
	int		iX;
	int		iY;
	int		iCurTile;
	int		iBufferPos;

	// Clear the backbuffer to a blue color
	g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0), 1.0f, 0 );
	// Begin the scene
	g_pd3dDevice->BeginScene();

	//---------------------
	//
	// RENDER THE MINIMAP
	//
	//---------------------

	// Top to bottom
	for( iY = 0; iY < g_iMapHeight; iY++ ) {
		// Left to right
		for( iX = 0; iX < g_iMapWidth; iX++ ) {
			// Calculate buffer offset
			iBufferPos = iX+( iY * g_iMapWidth );
			// Get the proper tile
			iCurTile = g_iTileMap[ iBufferPos ];
			// Display the tile
			vDrawInterfaceObject(	( iX*2 ), 
									( iY*2 ), 
									(float)2, 
									(float)2, 
									iCurTile );
		}
	}

	// Render the current viewing area
	vDrawInterfaceObject(	( g_iXPos*2 ), 
							( g_iYPos*2 ), 
							(float)32, 
							(float)32, 
							18 );


	// End the scene
	g_pd3dDevice->EndScene();

	//
	// Output the scene
	//

	// Source rectangle
	rectSrc.top = 0;		
	rectSrc.bottom = g_iMapHeight*2;
	rectSrc.left = 0;
	rectSrc.right = g_iMapWidth*2;
	
	// Destination rectangle
	rectDest.top = 0;
	rectDest.bottom = g_iMapHeight*2;
	rectDest.left = 0;
	rectDest.right = g_iMapWidth*2;

	// Present the results
	g_pd3dDevice->Present( &rectSrc, &rectDest, hWndMinimap, NULL );
}

//-----------------------------------------------------------------------------
//
// Generate a random map
//
//-----------------------------------------------------------------------------
void vGenerateMap( int iType )
{
	int iRandDirection;
	int iSeedPos[ 32 ];
	int i, j;
	int iNumSeeds	= 32;
	int iNumUpdates = 800;
	
	//
	// -- TYPE 0 --
	// Random seeds
	//
	if( iType == 0 ) {
		// Clear the map
		vInitMap();

		//
		// Randomly create starting seeds
		//
		for( i = 0; i < iNumSeeds; i++ ) {
			// Set seed starting position
			iSeedPos[ i ] = rand()%(g_iMapHeight*g_iMapWidth);
			
			// Place the chunk of grass around it
			g_iTileMap[ iSeedPos [ i ] ] = 17;
		}

		//
		// Move seeds around
		//
		for( i = 0; i < iNumUpdates; i++ ) {
			for( j = 0; j < iNumSeeds; j++ ) {
				iRandDirection = rand()%4;

				// Move seed up a "line"
				if( iRandDirection == 0 ) {
					iSeedPos[ j ] -= g_iMapWidth;
				}
				// Move seed right
				else if( iRandDirection == 1 ) {
					iSeedPos[ j ] ++;
				}
				// Move seed down a "line"
				else if( iRandDirection == 2 ) {
					iSeedPos[ j ] += g_iMapWidth;
				}
				// Move seed left
				else if( iRandDirection == 3 ) {
					iSeedPos[ j ] --;
				}

				// If seed in invalid area, move it to a
				// random location
				if( iSeedPos[ j ] < 0 || 
					iSeedPos[ j ] >= (g_iMapHeight*g_iMapWidth) ) 
				{
					iSeedPos[ j ] = rand()%(g_iMapHeight*g_iMapWidth);
				}

				// Place the chunk of grass around the seed
				g_iTileMap[ iSeedPos [ j ] ] = 17;
			}
		}
	}

	// Render minimap
	vRenderMinimap();

	// Play sound to indicate action
	PlaySound("bleep.wav",NULL,SND_FILENAME|SND_ASYNC);
}

⌨️ 快捷键说明

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