main.cpp

来自「<B>很多DirectX 9.0游戏编程源码例子</B>」· C++ 代码 · 共 1,469 行 · 第 1/3 页

CPP
1,469
字号
	//

	// 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_iCurLayer ] 
						= 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, 40000, sizeof(int), fp );

	// Close the tile file
	fclose( fp );

	// Play sound to indicate action
	PlaySound("button.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, 40000, 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-140, 
		100, 100, 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 - only use base layer
			iCurTile = g_iTileMap[ iBufferPos ][ 0 ];
			// Display the tile
			vDrawInterfaceObject(	( iX ), 
									( iY ), 
									(float)1, 
									(float)1, 
									iCurTile );
		}
	}

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


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

	//
	// Output the scene
	//

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

	// 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 ] ][ 0 ] = 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 ] ][ 0 ] = 17;
			}
		}
	}

	// Render minimap
	vRenderMinimap();

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


//-----------------------------------------------------------------------------
//
// Activate layer on the tile-map
//
//-----------------------------------------------------------------------------
void vChangeLayer( int iLayer )
{
	//
	// Turn off all layer buttons
	//
	DestroyWindow( hBUTTON_LAYER1 );
	DestroyWindow( hBUTTON_LAYER2 );
	DestroyWindow( hBUTTON_LAYER3 );
	DestroyWindow( hBUTTON_LAYER4 );

	// Set buttons to all default
	hBUTTON_LAYER1 = CreateWindow(
		"BUTTON", "1",
		WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
		3, 275, 20, 20, hWndToolBar, (HMENU)ID_BUTTON_LAYER1, 
		g_hInstance, NULL);
	hBUTTON_LAYER2 = CreateWindow(
		"BUTTON", "2",
		WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
		25, 275, 20, 20, hWndToolBar, (HMENU)ID_BUTTON_LAYER2, 
		g_hInstance, NULL);
	hBUTTON_LAYER3 = CreateWindow(
		"BUTTON", "3",
		WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
		48, 275, 20, 20, hWndToolBar, (HMENU)ID_BUTTON_LAYER3, 
		g_hInstance, NULL);
	hBUTTON_LAYER4 = CreateWindow(
		"BUTTON", "4",
		WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
		71, 275, 20, 20, hWndToolBar, (HMENU)ID_BUTTON_LAYER4, 
		g_hInstance, NULL);
	
	// Activate proper button
	if( iLayer == 1 ) {
		DestroyWindow( hBUTTON_LAYER1 );
		hBUTTON_LAYER1 = CreateWindow(
			"BUTTON", "1",
			WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON,
			3, 275, 20, 20, hWndToolBar, (HMENU)ID_BUTTON_LAYER1, 
			g_hInstance, NULL);
	}
	else if( iLayer == 2 ) {
		DestroyWindow( hBUTTON_LAYER2 );
		hBUTTON_LAYER2 = CreateWindow(
			"BUTTON", "2",
			WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON,
			25, 275, 20, 20, hWndToolBar, (HMENU)ID_BUTTON_LAYER2, 
			g_hInstance, NULL);
	}
	else if( iLayer == 3 ) {
		DestroyWindow( hBUTTON_LAYER3 );
		hBUTTON_LAYER3 = CreateWindow(
			"BUTTON", "3",
			WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON,
			48, 275, 20, 20, hWndToolBar, (HMENU)ID_BUTTON_LAYER3, 
			g_hInstance, NULL);
	}
	else if( iLayer == 4 ) {
		DestroyWindow( hBUTTON_LAYER4 );
		hBUTTON_LAYER4 = CreateWindow(
			"BUTTON", "4",
			WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON,
			71, 275, 20, 20, hWndToolBar, (HMENU)ID_BUTTON_LAYER4, 
			g_hInstance, NULL);
	}

	// Set current layer
	g_iCurLayer = (iLayer-1);

	PlaySound("button.wav",NULL,SND_FILENAME|SND_ASYNC);
}

⌨️ 快捷键说明

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