main.cpp
来自「<B>很多DirectX 9.0游戏编程源码例子</B>」· C++ 代码 · 共 836 行 · 第 1/2 页
CPP
836 行
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;
}
};
//--------------------------------------------------------------------------------------
//
// 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" ) ) {
g_SoundSys.hrPlaySound( g_sndButtonOver );
// 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" ) ) {
g_SoundSys.hrPlaySound( g_sndButton );
// 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
g_SoundSys.hrPlaySound( g_sndButtonOver );
}
else if( !stricmp( szZoneHit, "MAINMENU_LOADGAME" ) ) {
// Add load game logic here
g_SoundSys.hrPlaySound( g_sndButtonOver );
}
else if( !stricmp( szZoneHit, "MAINMENU_SAVEGAME" ) ) {
// Add save game logic here
g_SoundSys.hrPlaySound( g_sndButtonOver );
}
else if( !stricmp( szZoneHit, "MAINMENU_OPTIONS" ) ) {
g_SoundSys.hrPlaySound( g_sndButtonOver );
// 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" ) ) {
g_SoundSys.hrPlaySound( g_sndButton );
// 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" ) ) {
g_SoundSys.hrPlaySound( g_sndButton );
// 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" ) ) {
g_SoundSys.hrPlaySound( g_sndButtonOver );
// 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( 10 );
MZones.iAddZone( "BACKGROUND", 0, 0, 640, 480, 3 );
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 );
// Highlight zones (for graphic looks only)
MZones.iAddZone( "MAINMENU_NEWGAME_H", 192, 64, 256, 64, 3 );
MZones.iAddZone( "MAINMENU_LOADGAME_H", 192, 128, 256, 64, 3 );
MZones.iAddZone( "MAINMENU_SAVEGAME_H", 192, 192, 256, 64, 3 );
MZones.iAddZone( "MAINMENU_OPTIONS_H", 192, 256, 256, 64, 3 );
}
// 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( 6 );
MZones.iAddZone( "BACKGROUND", 0, 0, 640, 480, 3 );
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 );
}
}
//--------------------------------------------------------------------------------------
//
// Function to initialize the sound system and load the various
// sounds needed for the game.
//
//--------------------------------------------------------------------------------------
bool bInitializeSoundSystem( void )
{
HRESULT hr;
//-------------------------------------------------------
// Allocate memory for the game sound(s)
//-------------------------------------------------------
g_sndButton = new GameSound;
g_sndButtonOver = new GameSound;
//-------------------------------------------------------
// Initialize the sound system
//-------------------------------------------------------
g_SoundSys.hrInitSoundSystem();
//-------------------------------------------------------
// Load the game sound(s)
//-------------------------------------------------------
hr = g_SoundSys.hrLoadSound( "button.wav", g_sndButton );
if( hr == SOUNDERROR_LOAD ) {
return( 0 );
}
hr = g_SoundSys.hrLoadSound( "button_over.wav", g_sndButtonOver );
if( hr == SOUNDERROR_LOAD ) {
return( 0 );
}
// Return success
return( 1 );
}
//--------------------------------------------------------------------------------------
//
// Function to initialize the the sound graph and load
// the mp3 file
//
//--------------------------------------------------------------------------------------
bool bPlayTitleMusic( void )
{
HRESULT hr;
// Initialize COM
CoInitialize( NULL );
// Create the graph
CoCreateInstance( CLSID_FilterGraph,
NULL,
CLSCTX_INPROC_SERVER,
IID_IGraphBuilder,
(void **)&g_pGraph );
// Query for interface objects
g_pGraph->QueryInterface( IID_IMediaControl, (void **)&g_pMediaControl );
g_pGraph->QueryInterface( IID_IMediaEvent, (void **)&g_pEvent );
g_pGraph->QueryInterface( IID_IMediaSeeking, (void **)&g_pSeeking );
// Load the song (insert your own file name below)
hr = g_pGraph->RenderFile(L"c:\\dxsdk\\samples\\media\\track3.mp3", NULL);
if( hr != S_OK ) {
return( 0 );
}
// Set the playback rate
g_pSeeking->SetRate( 1.25 );
// Play the song
g_pMediaControl->Run();
// Set background music active flag
g_bBackgroundMusicActive = 1;
return( 1 );
}
//--------------------------------------------------------------------------------------
//
// Function to clean up the graph and allocated memory
//
//--------------------------------------------------------------------------------------
void vStopTitleMusic( void )
{
// Stop the song if playing
g_pMediaControl->Stop();
// Clean up
g_pMediaControl->Release();
g_pEvent->Release();
g_pGraph->Release();
}
//--------------------------------------------------------------------------------------
//
// Function to check the status of the music to see if it is done and
// needs to be restarted
//
//--------------------------------------------------------------------------------------
void vCheckMusicStatus( void )
{
long evCode;
// Check the event code
g_pEvent->WaitForCompletion( 0, &evCode );
// If the music is done, restart it
if( evCode == EC_COMPLETE ) {
// Set the starting position to 0
LONGLONG lStartPos = 0;
// Stop the music
g_pMediaControl->Stop();
// Set the positions
g_pSeeking->SetPositions( &lStartPos, AM_SEEKING_AbsolutePositioning, NULL, AM_SEEKING_NoPositioning );
// Run the music
g_pMediaControl->Run();
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?