#include <d3d9.h>
#include "d3d9tex.h"
LPDIRECT3D9 g_pD3D = NULL;
LPDIRECT3D9DEVICE g_pd3dDevice = NULL;
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
{
// Register the window class.
WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L,
GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
"Direct3D Tutorial", NULL };
RegisterClassEx( &wc );
// Create the application's window.
HWND hWnd = CreateWindow( "Direct3D Tutorial", "Direct3D Tutorial 01: CreateDevice",
WS_OVERLAPPEDWINDOW, 100, 100, 300, 300,
GetDesktopWindow(), NULL, wc.hInstance, NULL );
// The message loop.
MSG msg;
while( GetMessage( &msg, NULL, 0, 0 ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
}
class CMyD3DApp : public CD3DApp
{
public:
CMyD3DApp();
private:
HRESULT ConfirmDevice( D3DCAPS9*, DWORD, D3DFORMAT );
HRESULT OneTimeInit();
HRESULT RestoreDeviceObjects();
HRESULT FrameMove();
HRESULT Render();
LPDIRECT3DVERTEXBUFFER9 m_pVB; // VertexBuffer for rendering objects
}
HRESULT CMyD3DApp::OneTimeInit();
{
//create your objects here
return S_OK;
}
CMyD3DApp::CMyD3DApp()
{
//already set by another method but you usually would use this
// m_strWindowTitle = _T("Direct3D Example"); // title bar
m_bUseDepthBuffer = TRUE; // maintains basic zbuffer
m_pVB = NULL; // clear buffer
// d3d pointer fill
if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
return E_FAIL;
/* the parameters are the global setup
if you change these then make sure that you also recreate the device
else they'll do nothing */
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory( &d3dpp, sizeof(d3dpp) );
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
// create device
if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp, &g_pd3dDevice ) ) )
return E_FAIL;
D3DBACKBUFFER_TYPE m_d3dBackBuffer;
ZeroMemory( &m_d3dBackBuffer, sizeof(m_d3dBackBuffer) );
g_pd3dDevice->GetBackBuffer( NULL, NULL, D3DBACKBUFFER_TYPE_MONO, &m_d3dBackBuffer ); //declare backbuffer
}
HRESULT ConfirmDevice(D3DCAPS9 *pCaps, DWORD dwBehavior,
D3DFORMAT fmtBackBuffer)
{
// put in here any special device needs, suchas shader capability check
}
HRESULT CMyD3DApplication::RestoreDeviceObjects()
{
// Create the vertex buffer... creates buffer size from default pool
if( FAILED( g_pd3dDevice->CreateVertexBuffer(
NUM_VERTS*sizeof(CUSTOMVERTEX),
0,
D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &m_pVB ) ) )
{
return E_FAIL;
}
// fills the buffer with render data
VOID* pVertices;
if( FAILED( m_pVB->Lock( 0, sizeof(g_Vertices),
(BYTE**)&pVertices, 0 ) ) )
return E_FAIL;
memcpy( pVertices, g_Vertices, sizeof(g_Vertices) );
m_pVB->Unlock();
// veiw projection
D3DXMATRIX matProj;
FLOAT fAspect = m_d3dBackBuffer.Width / (FLOAT)m_d3dBackBuffer.Height;
D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, fAspect, 1.0f, 100.0f );
g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
// view matrix
D3DXMATRIX matView;
D3DXMatrixLookAtLH( &matView, &D3DXVECTOR3( 0.0f, 1.0f,-4.0f ),
&D3DXVECTOR3( 0.0f, 0.0f, 0.0f ),
&D3DXVECTOR3( 0.0f, 1.0f, 0.0f ) );
g_pd3dDevice->SetTransform( D3DTS_VIEW, &matView );
// render defualts
g_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );
return S_OK;
}
HRESULT CMyD3DApplication::FrameMove()
{
// edit this matrix to alter the current object
D3DXMATRIX matWorld;
g_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
return S_OK;
}
HRESULT CMyD3DApplication::Render()
{
// clear & set background to black
g_pd3dDevice->Clear( 0L, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,
D3DCOLOR_XRGB(0,0,0), 1.0f, 0L );
// start rendering
if( SUCCEEDED( g_pd3dDevice->BeginScene() ) )
{
g_pd3dDevice->SetStreamSource( 0, m_pVB, sizeof(CUSTOMVERTEX) );
g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLEFAN, 0, NUM_TRIS );
g_pd3dDevice->EndScene();
g_pd3dDevice->Present( NULL, NULL, NULL, NULL ); // sync
}
return S_OK;
}
LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
switch( msg )
{
case WM_DESTROY:
PostQuitMessage( 0 );
return 0;
case WM_PAINT:
Render();
ValidateRect( hWnd, NULL );
return 0;
}
return DefWindowProc( hWnd, msg, wParam, lParam );
}
VOID Cleanup()
{
if( g_pd3dDevice != NULL)
g_pd3dDevice->Release();
if( g_pD3D != NULL)
g_pD3D->Release();
}
personally i thought that was more what he wanted ^_^ but hey... as i said it's lengthy
To Survive You Must Evolve... This Time Van Will Not Escape His Fate!
900mhz | 256mb | FX5200Ti 52.16 | Dx9 | WXP-Pro