📄 ex_directx.cpp
字号:
//-----------------------------------------------------------------------------
// File: Ex_DirectX.cpp
//
// Desc: DirectX window application created by the DirectX AppWizard
//-----------------------------------------------------------------------------
#define STRICT
#include <windows.h>
#include <commctrl.h>
#include <commdlg.h>
#include <basetsd.h>
#include <math.h>
#include <stdio.h>
#include <d3dx9.h>
#include <dxerr9.h>
#include <tchar.h>
#include "DXUtil.h"
#include "D3DEnumeration.h"
#include "D3DSettings.h"
#include "D3DApp.h"
#include "D3DUtil.h"
#include "resource.h"
#include "Ex_DirectX.h"
//-----------------------------------------------------------------------------
// Global access to the app (needed for the global WndProc())
//-----------------------------------------------------------------------------
CMyD3DApplication* g_pApp = NULL;
HINSTANCE g_hInst = NULL;
//-----------------------------------------------------------------------------
// Name: WinMain()
// 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 )
{
CMyD3DApplication d3dApp;
g_pApp = &d3dApp;
g_hInst = hInst;
InitCommonControls();
if( FAILED( d3dApp.Create( hInst ) ) )
return 0;
return d3dApp.Run();
}
//-----------------------------------------------------------------------------
// Name: CMyD3DApplication()
// Desc: Application constructor. Paired with ~CMyD3DApplication()
// Member variables should be initialized to a known state here.
// The application window has not yet been created and no Direct3D device
// has been created, so any initialization that depends on a window or
// Direct3D should be deferred to a later stage.
//-----------------------------------------------------------------------------
CMyD3DApplication::CMyD3DApplication()
{
m_dwCreationWidth = 500;
m_dwCreationHeight = 375;
m_strWindowTitle = TEXT( "Ex_DirectX" );
m_d3dEnumeration.AppUsesDepthBuffer = TRUE;
m_bStartFullscreen = false;
m_bShowCursorWhenFullscreen = false;
m_pD3DXFont = NULL;
m_bLoadingApp = TRUE;
m_pVB = NULL;
ZeroMemory( &m_UserInput, sizeof(m_UserInput) );
m_fWorldRotX = 0.0f;
m_fWorldRotY = 0.0f;
// Read settings from registry
ReadSettings();
}
//-----------------------------------------------------------------------------
// Name: ~CMyD3DApplication()
// Desc: Application destructor. Paired with CMyD3DApplication()
//-----------------------------------------------------------------------------
CMyD3DApplication::~CMyD3DApplication()
{
}
//-----------------------------------------------------------------------------
// Name: OneTimeSceneInit()
// Desc: Paired with FinalCleanup().
// The window has been created and the IDirect3D9 interface has been
// created, but the device has not been created yet. Here you can
// perform application-related initialization and cleanup that does
// not depend on a device.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::OneTimeSceneInit()
{
// TODO: perform one time initialization
// Drawing loading status message until app finishes loading
SendMessage( m_hWnd, WM_PAINT, 0, 0 );
m_bLoadingApp = FALSE;
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: ReadSettings()
// Desc: Read the app settings from the registry
//-----------------------------------------------------------------------------
VOID CMyD3DApplication::ReadSettings()
{
HKEY hkey;
if( ERROR_SUCCESS == RegCreateKeyEx( HKEY_CURRENT_USER, DXAPP_KEY,
0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, NULL ) )
{
// TODO: change as needed
// Read the stored window width/height. This is just an example,
// of how to use DXUtil_Read*() functions.
DXUtil_ReadIntRegKey( hkey, TEXT("Width"), &m_dwCreationWidth, m_dwCreationWidth );
DXUtil_ReadIntRegKey( hkey, TEXT("Height"), &m_dwCreationHeight, m_dwCreationHeight );
RegCloseKey( hkey );
}
}
//-----------------------------------------------------------------------------
// Name: WriteSettings()
// Desc: Write the app settings to the registry
//-----------------------------------------------------------------------------
VOID CMyD3DApplication::WriteSettings()
{
HKEY hkey;
if( ERROR_SUCCESS == RegCreateKeyEx( HKEY_CURRENT_USER, DXAPP_KEY,
0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, NULL ) )
{
// TODO: change as needed
// Write the window width/height. This is just an example,
// of how to use DXUtil_Write*() functions.
DXUtil_WriteIntRegKey( hkey, TEXT("Width"), m_rcWindowClient.right );
DXUtil_WriteIntRegKey( hkey, TEXT("Height"), m_rcWindowClient.bottom );
RegCloseKey( hkey );
}
}
//-----------------------------------------------------------------------------
// Name: ConfirmDevice()
// Desc: Called during device initialization, this code checks the display device
// for some minimum set of capabilities
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::ConfirmDevice( D3DCAPS9* pCaps, DWORD dwBehavior,
D3DFORMAT Format )
{
UNREFERENCED_PARAMETER( Format );
UNREFERENCED_PARAMETER( dwBehavior );
UNREFERENCED_PARAMETER( pCaps );
BOOL bCapsAcceptable;
// TODO: Perform checks to see if these display caps are acceptable.
bCapsAcceptable = TRUE;
if( bCapsAcceptable )
return S_OK;
else
return E_FAIL;
}
//-----------------------------------------------------------------------------
// Name: InitDeviceObjects()
// Desc: Paired with DeleteDeviceObjects()
// The device has been created. Resources that are not lost on
// Reset() can be created here -- resources in D3DPOOL_MANAGED,
// D3DPOOL_SCRATCH, or D3DPOOL_SYSTEMMEM. Image surfaces created via
// CreateImageSurface are never lost and can be created here. Vertex
// shaders and pixel shaders can also be created here as they are not
// lost on Reset().
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::InitDeviceObjects()
{
// TODO: create device objects
HRESULT hr;
// Create the vertex buffer
if( FAILED( hr = m_pd3dDevice->CreateVertexBuffer( 3*2*sizeof(CUSTOMVERTEX),
0, D3DFVF_CUSTOMVERTEX,
D3DPOOL_MANAGED, &m_pVB, NULL ) ) )
return DXTRACE_ERR( "CreateVertexBuffer", hr );
// Fill the vertex buffer with 2 triangles
CUSTOMVERTEX* pVertices;
if( FAILED( hr = m_pVB->Lock( 0, 0, (VOID**)&pVertices, 0 ) ) )
return DXTRACE_ERR( "Lock", hr );
// Front triangle
pVertices[0].position = D3DXVECTOR3( -1.0f, -1.0f, 0.0f );
pVertices[0].normal = D3DXVECTOR3( 0.0f, 0.0f, -1.0f );
pVertices[1].position = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
pVertices[1].normal = D3DXVECTOR3( 0.0f, 0.0f, -1.0f );
pVertices[2].position = D3DXVECTOR3( 1.0f, -1.0f, 0.0f );
pVertices[2].normal = D3DXVECTOR3( 0.0f, 0.0f, -1.0f );
// Back triangle
pVertices[3].position = D3DXVECTOR3( -1.0f, -1.0f, 0.0f );
pVertices[3].normal = D3DXVECTOR3( 0.0f, 0.0f, 1.0f );
pVertices[4].position = D3DXVECTOR3( 1.0f, -1.0f, 0.0f );
pVertices[4].normal = D3DXVECTOR3( 0.0f, 0.0f, 1.0f );
pVertices[5].position = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
pVertices[5].normal = D3DXVECTOR3( 0.0f, 0.0f, 1.0f );
m_pVB->Unlock();
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: RestoreDeviceObjects()
// Desc: Paired with InvalidateDeviceObjects()
// The device exists, but may have just been Reset(). Resources in
// D3DPOOL_DEFAULT and any other device state that persists during
// rendering should be set here. Render states, matrices, textures,
// etc., that don't change during rendering can be set once here to
// avoid redundant state setting during Render() or FrameMove().
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::RestoreDeviceObjects()
{
// TODO: setup render states
HRESULT hr;
// Setup a material
D3DMATERIAL9 mtrl;
D3DUtil_InitMaterial( mtrl, 1.0f, 0.0f, 0.0f );
m_pd3dDevice->SetMaterial( &mtrl );
// Set up the textures
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE );
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_MODULATE );
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -