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

📄 main.cpp

📁 <B>很多DirectX 9.0游戏编程源码例子</B>
💻 CPP
📖 第 1 页 / 共 3 页
字号:
		vDrawInterfaceObject( 512, 0, 256.0f, 256.0f, 2 );
		vDrawInterfaceObject( 0, 256, 256.0f, 256.0f, 3 );
		vDrawInterfaceObject( 256, 256, 256.0f, 256.0f, 4 );
		vDrawInterfaceObject( 512, 256, 256.0f, 256.0f, 5 );
		// Input box
		vDrawInterfaceObject( 192, 64, 256.0f, 256.0f, 14 );

		// Draw cursor if input active
		if( g_bTextInputActive ) {
			// Update cursor flash status
			if( timeGetTime() > g_dwTextInputTimer ) {
				if( g_bTextInputCursorFlash ) {
					g_bTextInputCursorFlash = 0;
					g_dwTextInputTimer = timeGetTime()+250;
				}
				else {
					g_bTextInputCursorFlash = 1;
					g_dwTextInputTimer = timeGetTime()+250;
				}
			}
			// Draw the cursor if flash on
			if( g_bTextInputCursorFlash ) {
				vDrawInterfaceObject(	g_shTextInputXPos 
										+ g_shTextInputPosition * 8, 
										g_shTextInputYPos, 
										4.0f, 16.0f, 15 );
			}
		}
		// Create the text rectangle
		RECT rectText = {	g_shTextInputXPos, 
							g_shTextInputYPos, 
							g_shTextInputXPos+
							( g_shTextMaxSize * 8 ), 
							g_shTextInputYPos+20 };

		// Draw the text
		pD3DXFont->DrawText(	g_szTextInputBuffer, -1, &rectText, 
								DT_LEFT, 
								D3DCOLOR_RGBA(255, 255, 255, 255));
	}

	//
	// IN-GAME SCREEN
	//
	else if( g_iCurrentScreen == 5 ) {
		// Draw the options menu
		vDrawInterfaceObject( 0, 0, 256.0f, 256.0f, 0 );
		vDrawInterfaceObject( 256, 0, 256.0f, 256.0f, 1 );
		vDrawInterfaceObject( 512, 0, 256.0f, 256.0f, 2 );
		vDrawInterfaceObject( 0, 256, 256.0f, 256.0f, 3 );
		vDrawInterfaceObject( 256, 256, 256.0f, 256.0f, 4 );
		vDrawInterfaceObject( 512, 256, 256.0f, 256.0f, 5 );
		// Options
		vDrawInterfaceObject( 192, 64, 256.0f, 256.0f, 16 );
	}

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

	// Present the backbuffer contents to the display
	g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
}

//--------------------------------------------------------------------------------------
//
// Function to initialize the global interface object vertex buffer
//
// Uses the following global variables:
//
//		g_pd3dDevice			The Direct3D Device
//		g_pVBInterface			The Vertex buffer for the interface objects
//		CUSTOMVERTEX			The Vertex structure
//		D3DFVF_CUSTOMVERTEX		The Vertex format
//		g_pTexture				The Texture array to hold textures
//
//--------------------------------------------------------------------------------------
void vInitInterfaceObjects( void )
{
	CUSTOMVERTEX *pVertices;

	// Create the interface object buffer
	if( FAILED( g_pd3dDevice->CreateVertexBuffer( 4*sizeof(CUSTOMVERTEX),
		0, D3DFVF_CUSTOMVERTEX,
		D3DPOOL_DEFAULT, &g_pVBInterface, NULL ) ) )
	{
		return;
	}

	// Lock the buffer for editing
	if( FAILED( g_pVBInterface->Lock( 0, 0, (void**)&pVertices, 0 ) ) )
		return;

	//
	// Create a quad made up of 3 vertices (triangle strips)
	//
	// 1          3
	//  X--------X
	//  |x       |
	//  | x      |
	//  |  x     |
	//  |   x    |
	//  |    x   |
	//  |     x  |
	//  |      x |
	//  |       x|
	//  X--------X
	// 0          2
	//
	pVertices[0].position = D3DXVECTOR3( 0.0f, 0.0f, 0.0f );
	pVertices[0].tu       = 0.0f;
	pVertices[0].tv       = 1.0f;
	pVertices[0].tu2       = 0.0f;
	pVertices[0].tv2       = 1.0f;
	pVertices[0].vecNorm = D3DXVECTOR3(0.0f,0.0f,1.0f);

	pVertices[1].position = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
	pVertices[1].tu       = 0.0f;
	pVertices[1].tv       = 0.0f;
	pVertices[1].tu2       = 0.0f;
	pVertices[1].tv2       = 0.0f;
	pVertices[1].vecNorm = D3DXVECTOR3(0.0f,0.0f,1.0f);

	pVertices[2].position = D3DXVECTOR3( 1.0f, 0.0f, 0.0f );
	pVertices[2].tu       = 1.0f;
	pVertices[2].tv       = 1.0f;
	pVertices[2].tu2       = 1.0f;
	pVertices[2].tv2       = 1.0f;
	pVertices[2].vecNorm = D3DXVECTOR3(0.0f,0.0f,1.0f);

	pVertices[3].position = D3DXVECTOR3( 1.0f, 1.0f, 0.0f );
	pVertices[3].tu       = 1.0f;
	pVertices[3].tv       = 0.0f;
	pVertices[3].tu2       = 1.0f;
	pVertices[3].tv2       = 0.0f;
	pVertices[3].vecNorm = D3DXVECTOR3(0.0f,0.0f,1.0f);

	// Done editing, unlock the buffer
	g_pVBInterface->Unlock();

	//---------------------------------------------
	//      LOAD TEXTURES
	//---------------------------------------------

	// Load the texture for the interface
	if( FAILED( D3DXCreateTextureFromFile( g_pd3dDevice, "ba_interfacewindow_0.tga", &g_pTexture[ 0 ] ) ) ) {
		return;
	}
	if( FAILED( D3DXCreateTextureFromFile( g_pd3dDevice, "ba_interfacewindow_1.tga", &g_pTexture[ 1 ] ) ) ) {
		return;
	}
	if( FAILED( D3DXCreateTextureFromFile( g_pd3dDevice, "ba_interfacewindow_2.tga", &g_pTexture[ 2 ] ) ) ) {
		return;
	}
	if( FAILED( D3DXCreateTextureFromFile( g_pd3dDevice, "ba_interfacewindow_3.tga", &g_pTexture[ 3 ] ) ) ) {
		return;
	}
	if( FAILED( D3DXCreateTextureFromFile( g_pd3dDevice, "ba_interfacewindow_4.tga", &g_pTexture[ 4 ] ) ) ) {
		return;
	}
	if( FAILED( D3DXCreateTextureFromFile( g_pd3dDevice, "ba_interfacewindow_5.tga", &g_pTexture[ 5 ] ) ) ) {
		return;
	}
	if( FAILED( D3DXCreateTextureFromFile( g_pd3dDevice, "ba_logo.tga", &g_pTexture[ 6 ] ) ) ) {
		return;
	}
	if( FAILED( D3DXCreateTextureFromFile( g_pd3dDevice, "ba_mainmenu.tga", &g_pTexture[ 7 ] ) ) ) {
		return;
	}
	if( FAILED( D3DXCreateTextureFromFile( g_pd3dDevice, "ba_exitscreen.tga", &g_pTexture[ 8 ] ) ) ) {
		return;
	}
	if( FAILED( D3DXCreateTextureFromFile( g_pd3dDevice, "ba_optionsmenu.tga", &g_pTexture[ 9 ] ) ) ) {
		return;
	}
	if( FAILED( D3DXCreateTextureFromFile( g_pd3dDevice, "ba_mainmenu_newgame.tga", &g_pTexture[ 10 ] ) ) ) {
		return;
	}
	if( FAILED( D3DXCreateTextureFromFile( g_pd3dDevice, "ba_mainmenu_loadgame.tga", &g_pTexture[ 11 ] ) ) ) {
		return;
	}
	if( FAILED( D3DXCreateTextureFromFile( g_pd3dDevice, "ba_mainmenu_savegame.tga", &g_pTexture[ 12 ] ) ) ) {
		return;
	}
	if( FAILED( D3DXCreateTextureFromFile( g_pd3dDevice, "ba_mainmenu_options.tga", &g_pTexture[ 13 ] ) ) ) {
		return;
	}
	if( FAILED( D3DXCreateTextureFromFile( g_pd3dDevice, "ba_inputblock.tga", &g_pTexture[ 14 ] ) ) ) {
		return;
	}
	if( FAILED( D3DXCreateTextureFromFile( g_pd3dDevice, "ba_inputcursor.tga", &g_pTexture[ 15 ] ) ) ) {
		return;
	}
	if( FAILED( D3DXCreateTextureFromFile( g_pd3dDevice, "ba_gamestart.tga", &g_pTexture[ 16 ] ) ) ) {
		return;
	}

	// Text Font
	hFont = CreateFont( 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, PROOF_QUALITY, 0, "fixedsys" ); 
	D3DXCreateFont( g_pd3dDevice, hFont, &pD3DXFont ); 
};
//--------------------------------------------------------------------------------------
//
// Function to draw a 3D object in 2D space
//
// iXPos    = x-position in screen-space of the top-left corner of the object
// iYPos    = y-position in screen-space of the top-left corner of the object
// fXSize   = Width of the object in screen-space (pixels)
// fYSize   = Height of the object in screen-space (pixels)
// iTexture = Index into the texture array of object to draw
//
//--------------------------------------------------------------------------------------
void vDrawInterfaceObject( int iXPos, int iYPos, float fXSize, float fYSize, int iTexture )
{
	D3DXMATRIX	matWorld, matRotation, matTranslation, matScale;
	float		fXPos, fYPos;

	// Set default position,scale,rotation
	D3DXMatrixIdentity( &matTranslation );
	// Scale the sprite
	D3DXMatrixScaling( &matScale, fXSize, fYSize, 1.0f );
	D3DXMatrixMultiply( &matTranslation, &matTranslation, &matScale );
	// Rotate the sprite
	D3DXMatrixRotationZ( &matRotation, 0.0f );
	D3DXMatrixMultiply( &matWorld, &matTranslation, &matRotation );

	// Calculate the position in screen-space
	fXPos = (float)(-(g_iWindowWidth/2)+iXPos);
	fYPos = (g_iWindowHeight/2)-fYSize-iYPos;

	// Move the sprite
	matWorld._41 = fXPos;	// X
	matWorld._42 = fYPos;	// Y

	// Set matrix
	g_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
	g_pd3dDevice->SetTexture( 0, g_pTexture[ iTexture ] );
	g_pd3dDevice->SetStreamSource( 0, g_pVBInterface, 0, sizeof( CUSTOMVERTEX ) );
	g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
	g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2 );

	// Dereference texture
	g_pd3dDevice->SetTexture(0, NULL);
}

//--------------------------------------------------------------------------------------
//
// Function to process mouse input
//
//--------------------------------------------------------------------------------------
void vCheckInput( void )
{
	bool		bRet;
	char		szZoneHit[ 64 ];
	POINT		Point;
	RECT		rcWindowRect;
	int			iMouseX;
	int			iMouseY;
	
	// Check the window offsets
	GetWindowRect( g_hWnd, &rcWindowRect );

	// Update the mouse position
	GetCursorPos( &Point );

	// Calculate real mouse coordinates
	iMouseX = Point.x-g_iXOffset-rcWindowRect.left;
	iMouseY = Point.y-g_iYOffset-rcWindowRect.top;
	
	// Check for mouse hits
	bRet = MZones.bCheckZones( (short)iMouseX, (short)iMouseY, szZoneHit, g_bLeftButton, g_bRightButton );
	if( bRet ) {
		//
		// TITLE SCREEN LOGIC
		//
		if( g_iCurrentScreen == 0 ) {
			// Go to the main menu
			if( !stricmp( szZoneHit, "TITLE_SCREEN" ) ) {
				// Set menu to main menu
				g_iCurrentScreen = 1;
				// Setup the mouse zones
				vSetupMouseZones( 1 );
			}
			// Go to the exit splash screen
			else if( !stricmp( szZoneHit, "EXIT_BUTTON" ) ) {
				// Set current screen to exit screen
				g_iCurrentScreen = 2;
				// Setup the mouse zones
				vSetupMouseZones( 2 );
			}
		}
		//
		// MAIN MENU LOGIC
		//
		else if( g_iCurrentScreen == 1 ) {
			// Turn off all highlights
			g_bMainMenu_NewGame_Highlight = 0;
			g_bMainMenu_LoadGame_Highlight = 0;
			g_bMainMenu_SaveGame_Highlight = 0;
			g_bMainMenu_Options_Highlight = 0;
			
			// Go to the title screen
			if( !stricmp( szZoneHit, "EXIT_BUTTON" ) ) {
				// Set current screen to exit screen
				g_iCurrentScreen = 2;
				// Setup the mouse zones
				vSetupMouseZones( 2 );
			}
			else if( !stricmp( szZoneHit, "MAINMENU_NEWGAME" ) ) {
				// Set current screen to new game screen
				g_iCurrentScreen = 4;
				// Setup the mouse zones
				vSetupMouseZones( 4 );
			}
			else if( !stricmp( szZoneHit, "MAINMENU_LOADGAME" ) ) {
				// Add load game logic here
			}
			else if( !stricmp( szZoneHit, "MAINMENU_SAVEGAME" ) ) {
				// Add save game logic here
			}
			else if( !stricmp( szZoneHit, "MAINMENU_OPTIONS" ) ) {
				// Set current screen to options menu
				g_iCurrentScreen = 7;
				// Setup the mouse zones
				vSetupMouseZones( 7 );
			}
			//
			// Check for mouse-overs
			//
			else if( !stricmp( szZoneHit, "MAINMENU_NEWGAME_H" ) ) {
				// Activate highlight
				g_bMainMenu_NewGame_Highlight = 1;
			}
			else if( !stricmp( szZoneHit, "MAINMENU_LOADGAME_H" ) ) {
				// Activate highlight
				g_bMainMenu_LoadGame_Highlight = 1;
			}
			else if( !stricmp( szZoneHit, "MAINMENU_SAVEGAME_H" ) ) {
				// Activate highlight
				g_bMainMenu_SaveGame_Highlight = 1;
			}
			else if( !stricmp( szZoneHit, "MAINMENU_OPTIONS_H" ) ) {
				// Activate highlight
				g_bMainMenu_Options_Highlight = 1;
			}
		}
		//
		// EXIT SCREEN LOGIC
		//
		else if( g_iCurrentScreen == 2 ) {
			// Exit the program if the user clicks anything
			if( !stricmp( szZoneHit, "EXIT_SCREEN" ) ) {
				// Flag WinMain() to exit program
				g_iCurrentScreen = 3;
			}
		}
		//
		// NEW GAME LOGIC
		//
		else if( g_iCurrentScreen == 4 ) {
			// Go to the title screen
			if( !stricmp( szZoneHit, "EXIT_BUTTON" ) ) {
				// Set current screen to exit screen
				g_iCurrentScreen = 2;
				// Setup the mouse zones
				vSetupMouseZones( 2 );
			}
		}
		//
		// IN-GAME
		//
		else if( g_iCurrentScreen == 5 ) {
			// Go to the title screen

⌨️ 快捷键说明

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