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

📄 main.cpp

📁 <B>很多DirectX 9.0游戏编程源码例子</B>
💻 CPP
📖 第 1 页 / 共 2 页
字号:
		vDrawInterfaceObject( 192, 64, 256.0f, 256.0f, 9 );
	}

	// 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;
	}
};

//--------------------------------------------------------------------------------------
//
// 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 = (float)(-(g_iWindowHeight/2)-iYPos+fYSize-g_iYOffset);

	// 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 ) {
			// 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" ) ) {
				// Add new game logic here
			}
			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 );
			}
		}
		//
		// 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;
			}
		}
		//
		// OPTIONS MENU LOGIC
		//
		else if( g_iCurrentScreen == 7 ) {
			// 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 );
			}
			// Go back to main menu
			else if( !stricmp( szZoneHit, "OPTIONS_BACK" ) ) {
				// Set current screen to main menu
				g_iCurrentScreen = 1;
				// Setup the mouse zones
				vSetupMouseZones( 1 );
			}
		}
	}
}

//--------------------------------------------------------------------------------------
//
// Function to setup mouse zones for the various screens and menus
// in the program.
//
//--------------------------------------------------------------------------------------
void vSetupMouseZones( int iMenu )
{
	// Title screen
	if( iMenu == 0 ) {
		MZones.vFreeZones();
		MZones.vInitialize( 2 );
		MZones.iAddZone( "TITLE_SCREEN", 0, 0, 640, 480, 2 );	// Left or right activated
		MZones.iAddZone( "EXIT_BUTTON", 587, 0, 53, 24, 0 );	// Left activated
	}
	// Main menu
	else if( iMenu == 1 ) {
		MZones.vFreeZones();
		MZones.vInitialize( 5 );
		MZones.iAddZone( "EXIT_BUTTON", 587, 0, 53, 24, 0 );
		MZones.iAddZone( "MAINMENU_NEWGAME", 192, 64, 256, 64, 0 );
		MZones.iAddZone( "MAINMENU_LOADGAME", 192, 128, 256, 64, 0 );
		MZones.iAddZone( "MAINMENU_SAVEGAME", 192, 192, 256, 64, 0 );
		MZones.iAddZone( "MAINMENU_OPTIONS", 192, 256, 256, 64, 0 );
	}
	// Exit splash screen
	else if( iMenu == 2 ) {
		MZones.vFreeZones();
		MZones.vInitialize( 1 );
		MZones.iAddZone( "EXIT_SCREEN", 0, 0, 640, 480, 2 );
	}
	// Options menu
	else if( iMenu == 7 ) {
		MZones.vFreeZones();
		MZones.vInitialize( 5 );
		MZones.iAddZone( "EXIT_BUTTON", 587, 0, 53, 24, 0 );
		MZones.iAddZone( "OPTIONS_AUDIO", 192, 64, 256, 64, 0 );
		MZones.iAddZone( "OPTIONS_VIDEO", 192, 128, 256, 64, 0 );
		MZones.iAddZone( "OPTIONS_DIFF", 192, 192, 256, 64, 0 );
		MZones.iAddZone( "OPTIONS_BACK", 192, 256, 256, 64, 0 );
	}
}

⌨️ 快捷键说明

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