📄 main.cpp
字号:
//-----------------------------------------------------------------------------
// Name: D3DFrame_TitleScreen
//
// Description: Example code showing how to create a simple application
// that starts out by displaying a title screen.
//
// File Function: Main body of the function (main.cpp)
//
// Code:
// Copyright (c) 2002 LostLogic Corporation. All rights reserved.
//
// Libraries Required:
// d3d9.lib dxguid.lib d3dx9dt.lib d3dxof.lib comctl32.lib winmm.lib
//
// DX Files Required:
// d3dapp.cpp
// d3denumeration.cpp
// d3dfont.cpp
// d3dsettings.cpp
// d3dutil.cpp
// dxutil.cpp
//
// Local Files Required:
// resourcedata.rc
// main.cpp
// main.h
//
// Original D3D Application Shell Code:
// Copyright (c) 1997-2001 Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#include "main.h"
int g_iNumTextures = 7;
//-----------------------------------------------------------------------------
// Desc: Entry point to the program. Initializes everything, and goes into a
// message-processing loop. Idle time is used to render the scene.
//-----------------------------------------------------------------------------
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
{
// Instance of the framework application
CD3DFramework d3dApp;
InitCommonControls();
// Create the application environment
// ----------------------------------
// This initializes the display and loads all appropriate data.
//
if( FAILED( d3dApp.Create( hInst ) ) )
return 0;
// Start up the application
// ------------------------
// Starts the program running by activating the message loop
// and render functions.
//
return d3dApp.Run();
}
//-----------------------------------------------------------------------------
// Desc: Application constructor. Sets attributes for the app.
//-----------------------------------------------------------------------------
CD3DFramework::CD3DFramework()
{
m_strWindowTitle = _T("Title Screen Demo");
m_pStatsFont = NULL;
m_shWindowWidth = 640;
m_shWindowHeight = 461;
m_dwCreationWidth = 640;
m_dwCreationHeight = 461;
}
//-----------------------------------------------------------------------------
// Desc: Called during initial app startup, this function performs all the
// permanent initialization.
//-----------------------------------------------------------------------------
HRESULT CD3DFramework::OneTimeSceneInit()
{
m_pStatsFont = new CD3DFont( _T("Arial"), 8, D3DFONT_BOLD );
if( m_pStatsFont == NULL )
return E_FAIL;
return S_OK;
}
//-----------------------------------------------------------------------------
// Desc: Called once per frame, the call is the entry point for 3d
// rendering. This function sets up render states, clears the
// viewport, and renders the scene.
//-----------------------------------------------------------------------------
HRESULT CD3DFramework::Render()
{
RECT rectDest;
// Clear the viewport
m_pd3dDevice->Clear( 0L, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0), 1.0f, 0L );
// Begin the scene
if( SUCCEEDED( m_pd3dDevice->BeginScene() ) ) {
// Start Drawing using the sprite device
pd3dxSprite->Begin();
// Upper left
rectDest.left = 0;
rectDest.top = 0;
BltSprite( &rectDest, m_pTexture[ 0 ], NULL );
// Upper middle
rectDest.left = 256;
rectDest.top = 0;
BltSprite( &rectDest, m_pTexture[ 1 ], NULL );
// Upper right
rectDest.left = 512;
rectDest.top = 0;
BltSprite( &rectDest, m_pTexture[ 2 ], NULL );
// Lower left
rectDest.left = 0;
rectDest.top = 256;
BltSprite( &rectDest, m_pTexture[ 3 ], NULL );
// Lower middle
rectDest.left = 256;
rectDest.top = 256;
BltSprite( &rectDest, m_pTexture[ 4 ], NULL );
// Lower right
rectDest.left = 512;
rectDest.top = 256;
BltSprite( &rectDest, m_pTexture[ 5 ], NULL );
// Logo - in the center
rectDest.left = 192;
rectDest.top = 64;
BltSprite( &rectDest, m_pTexture[ 6 ], NULL );
// End the sprite device usage
pd3dxSprite->End();
// Show frame rate
m_pStatsFont->DrawText( 2, 30, D3DCOLOR_ARGB(255,255,255,0), m_strFrameStats );
// Show video device information
m_pStatsFont->DrawText( 2, 50, D3DCOLOR_ARGB(255,255,255,0), m_strDeviceStats );
// End the scene
m_pd3dDevice->EndScene();
}
return S_OK;
}
//-----------------------------------------------------------------------------
// Desc: Initialize scene objects.
//-----------------------------------------------------------------------------
HRESULT CD3DFramework::InitDeviceObjects()
{
HRESULT hr;
if( FAILED( hr = m_pStatsFont->InitDeviceObjects( m_pd3dDevice ) ) )
return hr;
return S_OK;
}
//-----------------------------------------------------------------------------
// Desc: Initialize scene objects.
//-----------------------------------------------------------------------------
HRESULT CD3DFramework::RestoreDeviceObjects()
{
D3DXMATRIX matproj, matview, Identity;
// Restore the fonts
m_pStatsFont->RestoreDeviceObjects();
// Create the sprite interface
D3DXCreateSprite( m_pd3dDevice, &pd3dxSprite );
// Set up the textures
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE );
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1 );
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
// Set miscellaneous render states
m_pd3dDevice->SetRenderState( D3DRS_DITHERENABLE, FALSE );
m_pd3dDevice->SetRenderState( D3DRS_SPECULARENABLE, FALSE );
m_pd3dDevice->SetRenderState( D3DRS_ZENABLE, FALSE );
m_pd3dDevice->SetRenderState( D3DRS_AMBIENT, 0x00000000 );
// Set alpha blending states
// This is used to provide transparency/translucency
m_pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE );
m_pd3dDevice->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_SRCALPHA );
m_pd3dDevice->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA );
// Setup the 3D View
D3DXMatrixIdentity( &matview );
m_pd3dDevice->SetTransform( D3DTS_VIEW, &matview );
D3DXMatrixOrthoLH( &matproj, (float)m_shWindowWidth, (float)m_shWindowHeight, 0.0f, 1.0f );
m_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matproj );
m_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );
m_pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE );
m_pd3dDevice->SetRenderState( D3DRS_ZENABLE, FALSE );
m_pd3dDevice->SetRenderState( D3DRS_ZWRITEENABLE, FALSE );
// Setup a material
D3DMATERIAL9 mtrl;
D3DUtil_InitMaterial( mtrl, 1.0f, 1.0f, 1.0f, 1.0f );
m_pd3dDevice->SetMaterial( &mtrl );
//---------------------------------------------
// LOAD TEXTURES
//---------------------------------------------
// Load the texture for the interface
if( FAILED( D3DXCreateTextureFromFile( m_pd3dDevice, "ba_interfacewindow_0.tga", &m_pTexture[ 0 ] ) ) ) {
return S_OK;
}
if( FAILED( D3DXCreateTextureFromFile( m_pd3dDevice, "ba_interfacewindow_1.tga", &m_pTexture[ 1 ] ) ) ) {
return S_OK;
}
if( FAILED( D3DXCreateTextureFromFile( m_pd3dDevice, "ba_interfacewindow_2.tga", &m_pTexture[ 2 ] ) ) ) {
return S_OK;
}
if( FAILED( D3DXCreateTextureFromFile( m_pd3dDevice, "ba_interfacewindow_3.tga", &m_pTexture[ 3 ] ) ) ) {
return S_OK;
}
if( FAILED( D3DXCreateTextureFromFile( m_pd3dDevice, "ba_interfacewindow_4.tga", &m_pTexture[ 4 ] ) ) ) {
return S_OK;
}
if( FAILED( D3DXCreateTextureFromFile( m_pd3dDevice, "ba_interfacewindow_5.tga", &m_pTexture[ 5 ] ) ) ) {
return S_OK;
}
if( FAILED( D3DXCreateTextureFromFile( m_pd3dDevice, "ba_logo.tga", &m_pTexture[ 6 ] ) ) ) {
return S_OK;
}
return S_OK;
}
//-----------------------------------------------------------------------------
// Desc: Called when the app is exiting, or the device is being changed,
// this function deletes any device dependent objects.
//-----------------------------------------------------------------------------
HRESULT CD3DFramework::InvalidateDeviceObjects()
{
// Invalidate the font
m_pStatsFont->InvalidateDeviceObjects();
// Release textures
for( int i = 0; i < g_iNumTextures; i++ ) {
SAFE_RELEASE( m_pTexture[ i ] );
}
// Release the sprite device
SAFE_RELEASE( pd3dxSprite );
return S_OK;
}
//-----------------------------------------------------------------------------
// Desc: Called when the app is exiting, or the device is being changed,
// this function deletes any device dependent objects.
//-----------------------------------------------------------------------------
HRESULT CD3DFramework::DeleteDeviceObjects()
{
// Delete the font
m_pStatsFont->DeleteDeviceObjects();
// Release textures
for( int i = 0; i < g_iNumTextures; i++ ) {
SAFE_RELEASE( m_pTexture[ i ] );
}
// Release the sprite device
SAFE_RELEASE( pd3dxSprite );
return S_OK;
}
//-----------------------------------------------------------------------------
// Desc: Called before the app exits, this function gives the app the chance
// to cleanup after itself.
//-----------------------------------------------------------------------------
HRESULT CD3DFramework::FinalCleanup()
{
// Delete the font
SAFE_DELETE( m_pStatsFont );
// Delete textures
for( int i = 0; i < g_iNumTextures; i++ ) {
SAFE_DELETE( m_pTexture[ i ] );
}
// Delete the sprite device
SAFE_DELETE( pd3dxSprite );
return S_OK;
}
//-----------------------------------------------------------------------------
// Desc: Message proc function to handle key and menu input
//-----------------------------------------------------------------------------
LRESULT CD3DFramework::MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
return CD3DApplication::MsgProc( hWnd, uMsg, wParam, lParam );
}
//-----------------------------------------------------------------------------
// Desc: Rendering function to render a sprite
//-----------------------------------------------------------------------------
HRESULT CD3DFramework::BltSprite( RECT *pDestRect, LPDIRECT3DTEXTURE9 pSrcTexture, RECT *pSrcRect )
{
D3DXVECTOR2 vecScaling(1,1); // Scaling
D3DXVECTOR2 vecRotCenter(0,0); // Rotation center
D3DXVECTOR2 vecTrans(0,0); // Translation
// Set the destination location
vecTrans.x = (float) pDestRect->left;
vecTrans.y = (float) pDestRect->top;
// Display the sprite on-screen
pd3dxSprite->Draw( pSrcTexture, NULL, &vecScaling, &vecRotCenter, 0, &vecTrans, 0xFFFFFFFF );
return S_OK;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -