📄 basic.cpp
字号:
//-----------------------------------------------------------------------------
// File: basic.cpp
//
// Desc: Example code showing how to use a vertex structure with a vertex buffer
// and how to map a texture. Additionally it shows how to use indexed
// triangles
//
// Last modification: December 24th, 2002
//
// Copyright (c) Wolfgang F. Engel (wolf@direct3d.net)
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#define STRICT
#include <Windows.h>
#include <commctrl.h>
#include <stdio.h>
#include <math.h>
#include <D3DX9.h>
#include "DXUtil.h"
#include "D3DEnumeration.h"
#include "D3DSettings.h"
#include "D3DApp.h"
#include "D3DFile.h"
#include "D3DFont.h"
#include "D3DUtil.h"
#include "resource.h"
// A structure for our custom vertex type
struct VERTEX
{
FLOAT x, y, z, rhw; // The transformed position for the vertex
FLOAT tu, tv;
};
// Fixed-Function Vertex structure
const DWORD FVF = (D3DFVF_XYZRHW | D3DFVF_TEX1);
//-----------------------------------------------------------------------------
// Name: class CMyD3DApplication
// Desc: Application class. The base class (CD3DApplication) provides the
// generic functionality needed in all Direct3D samples. CMyD3DApplication
// adds functionality specific to this sample program.
//-----------------------------------------------------------------------------
class CMyD3DApplication : public CD3DApplication
{
CD3DFont* m_pFont;
LPDIRECT3DVERTEXBUFFER9 m_pVB; // Buffer to hold vertices
DWORD m_dwSizeofVertices;
LPDIRECT3DINDEXBUFFER9 m_pIB;
DWORD m_wSizeofIndices;
LPDIRECT3DTEXTURE9 m_pBackgroundTexture;
protected:
HRESULT OneTimeSceneInit();
HRESULT InitDeviceObjects();
HRESULT RestoreDeviceObjects();
HRESULT InvalidateDeviceObjects();
HRESULT DeleteDeviceObjects();
HRESULT FinalCleanup();
HRESULT Render();
HRESULT FrameMove();
HRESULT ConfirmDevice( D3DCAPS9* pCaps, DWORD dwBehavior,
D3DFORMAT adapterFormat, D3DFORMAT backBufferFormat );
public:
CMyD3DApplication();
};
//-----------------------------------------------------------------------------
// 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;
InitCommonControls();
if( FAILED( d3dApp.Create( hInst ) ) )
return 0;
return d3dApp.Run();
}
//-----------------------------------------------------------------------------
// Name: CMyD3DApplication()
// Desc: Application constructor. Sets attributes for the app.
//-----------------------------------------------------------------------------
CMyD3DApplication::CMyD3DApplication()
{
m_strWindowTitle = _T("Basic");
m_d3dEnumeration.AppUsesDepthBuffer = FALSE;
m_dwCreationWidth = 800; // Width used to create window
m_dwCreationHeight = 600;
m_pVB = NULL;
m_pIB = NULL;
m_pFont = new CD3DFont( _T("Arial"), 12, D3DFONT_BOLD );
}
//-----------------------------------------------------------------------------
// Name: OneTimeSceneInit()
// Desc: Called during initial app startup, this function performs all the
// permanent initialization.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::OneTimeSceneInit()
{
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: FrameMove()
// Desc: Called once per frame, the call is the entry point for animating
// the scene.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::FrameMove()
{
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: Render()
// 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 CMyD3DApplication::Render()
{
// Begin the scene
if( SUCCEEDED( m_pd3dDevice->BeginScene() ) )
{
// Set up texture stage states for the background
m_pd3dDevice->SetTexture( 0, m_pBackgroundTexture );
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1 );
// Passing an FVF to IDirect3DDevice9::SetFVF specifies a legacy FVF with stream 0.
m_pd3dDevice->SetFVF(FVF );
m_pd3dDevice->SetStreamSource( 0, m_pVB, 0, sizeof(VERTEX) );
m_pd3dDevice->SetIndices( m_pIB ); // new in DX9 only takes one parameter
// second parameter new in DX9
m_pd3dDevice->DrawIndexedPrimitive( D3DPT_TRIANGLESTRIP,
0,
0,
4, // number of vertices
0,
2); // number of primitives
// Output statistics
m_pFont->DrawText( 2, 0, D3DCOLOR_ARGB(255,255,255,0), m_strFrameStats );
m_pFont->DrawText( 2, 20, D3DCOLOR_ARGB(255,255,255,0), m_strDeviceStats );
// End the scene.
m_pd3dDevice->EndScene();
}
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: InitDeviceObjects()
// Desc: Initialize scene objects.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::InitDeviceObjects()
{
m_pFont->InitDeviceObjects( m_pd3dDevice );
// Load the texture for the background image
if( FAILED( D3DUtil_CreateTexture( m_pd3dDevice, _T("Lake.bmp"),
&m_pBackgroundTexture) ) )
return D3DAPPERR_MEDIANOTFOUND;
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: RestoreDeviceObjects()
// Desc: Initialize scene objects.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::RestoreDeviceObjects()
{
// we don`t need any transformation -> position will be delivered in screencoordinates
m_pFont->RestoreDeviceObjects();
// fill the vertex buffer with the new data
// Initialize to render a quad
VERTEX Vertices[] =
{
{ 0.0f, 0.0f, 0.5f, 1.0f, 0.0f, 0.0f, },
{ (float)m_d3dsdBackBuffer.Width, 0.0f, 0.5f, 1.0f, 1.0f,0.0f, },
{ 0.0f, (float)m_d3dsdBackBuffer.Height, 0.5f, 1.0f, 0.0f, 1.0f, }, // x, y, z, rhw, tu, tv
{ (float)m_d3dsdBackBuffer.Width, (float)m_d3dsdBackBuffer.Height, 0.5f, 1.0f, 1.0f,1.0f, },
};
m_dwSizeofVertices = sizeof(Vertices);
// sixth parameter is new in DX9
// Create a square for rendering the background
if( FAILED( m_pd3dDevice->CreateVertexBuffer( m_dwSizeofVertices,
D3DUSAGE_WRITEONLY, FVF,
D3DPOOL_MANAGED, &m_pVB, NULL ) ) )
return E_FAIL;
VERTEX* pVertices;
// the third parameter changed from BYTE** to VOID** in DX9
if( FAILED( m_pVB->Lock( 0, m_dwSizeofVertices, (VOID**)&pVertices, 0 ) ) )
return E_FAIL;
memcpy( pVertices, Vertices, m_dwSizeofVertices);
m_pVB->Unlock();
// Initialize the Index buffer
WORD wIndices[] = {0, 1, 2, 3, 2, 0};
m_wSizeofIndices = sizeof(wIndices);
// Create the index buffer
// six parameter new in DX9
if( FAILED( m_pd3dDevice->CreateIndexBuffer( m_wSizeofIndices,
0, D3DFMT_INDEX16,
D3DPOOL_MANAGED, &m_pIB, NULL ) ) )
return E_FAIL;
VOID* pIndices;
// DX9 third parameter changed from BYTE** to VOID **
if( FAILED( m_pIB->Lock( 0, m_wSizeofIndices, (VOID**)&pIndices, 0 ) ) )
return E_FAIL;
memcpy( pIndices, wIndices, sizeof(wIndices) );
m_pIB->Unlock();
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: InvalidateDeviceObjects()
// Desc:
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::InvalidateDeviceObjects()
{
m_pFont->InvalidateDeviceObjects();
SAFE_RELEASE( m_pVB );
SAFE_RELEASE( m_pIB );
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: DeleteDeviceObjects()
// Desc: Called when the app is exiting, or the device is being changed,
// this function deletes any device dependent objects.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::DeleteDeviceObjects()
{
m_pFont->DeleteDeviceObjects();
SAFE_RELEASE( m_pBackgroundTexture );
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: FinalCleanup()
// Desc: Called before the app exits, this function gives the app the chance
// to cleanup after itself.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::FinalCleanup()
{
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: ConfirmDevice()
// Desc: Called during device initialization, this code checks the device
// for some minimum set of capabilities
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::ConfirmDevice( D3DCAPS9* pCaps, DWORD dwBehavior,
D3DFORMAT adapterFormat, D3DFORMAT backBufferFormat )
{
return S_OK;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -