Direct3D Basecode Created By Ronny Andr
记住在LINK中加入 d3dx9.lib d3d9.lib 两个库
/*
* Direct3D Basecode Created By Ronny Andr?Reierstad
* Window Code Originaly Created By Jeff Molofee 2000
* A HUGE Thanks To Fredric Echols For Cleaning Up
* And Optimizing This Code, Making It More Flexible!
* If You Want More DirectX 3D Source Code
* Visit Aron Tutorials http://www.morrowland.com/apron
*/
#include // Header File For Windows
#include // Header File For DirectX 3D
#include // Header File For DirectX 3D
HDC hDC = NULL; // Private GDI Device Context
HWND hWnd = NULL; // Holds Our Window Handle
HINSTANCE hInstance; // Holds The Instance Of The Application
LPDIRECT3D9 pD3D = NULL; // DirectX 3D Version 9
LPDIRECT3DDEVICE9 pD3DDevice = NULL; // DirectX 3D Rendering Device
bool keys[256]; // Array Used For The Keyboard Routine
bool active=TRUE; // Window Active Flag Set To TRUE By Default
bool fullscreen=TRUE; // Fullscreen Flag Set To Fullscreen Mode By Default
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); // Declaration For WndProc
void ReSizeD3DScene(int width, int height) // Resize And Initialize The D3D Window
{
if (height==0) // Prevent A Divide By Zero By
{
height=1; // Making Height Equal One
}
D3DXMATRIXA16 matProjection; // Create A Projection Matrix
// Calculate The Aspect Ratio Of The Window
D3DXMatrixPerspectiveFovLH(&matProjection, 45.0f, (float)width/(float)height, 0.1f, 100.0f );
pD3DDevice->SetTransform( D3DTS_PROJECTION, &matProjection );
D3DXMatrixIdentity(&matProjection); // Reset The Projection Matrix
}
int InitD3D() // All Setup For D3D Goes Here
{
pD3DDevice->SetRenderState(D3DRS_ZENABLE, TRUE ); // Enable Z-Buffer (Depth Buffer)
pD3DDevice->SetRenderState(D3DRS_CULLMODE, FALSE); // Disable Backface Culling
pD3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE); // Disable Light
return TRUE; // Initialization Went OK
}
int DrawD3DScene() // Here's Where We Do All The Drawing
{
pD3DDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, // Clear Screen And Depth Buffer
D3DCOLOR_COLORVALUE(0.0f,0.0f,0.0f,0.0f), 1.0f, 0 );
pD3DDevice->BeginScene();
// Nothing To draw!!!
pD3DDevice->EndScene();
pD3DDevice->Present( NULL, NULL, NULL, NULL ); // Display Result
return TRUE; // Keep Going
}
void KillD3DWindow() // Properly Kill The Window
{
if (pD3DDevice != NULL) pD3DDevice->Release(); // Release D3D Device
if (pD3D != NULL) pD3D->Release(); // Release D3D Interface
if (fullscreen) // Are We In Fullscreen Mode?
{
ChangeDisplaySettings(NULL,0); // If So Switch Back To The Desktop
ShowCursor(TRUE); // Show Mouse Pointer
}
if (hDC && !ReleaseDC(hWnd,hDC)) // Are We Able To Release The DC
{
MessageBox(NULL,"Release Device Context Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
hDC=NULL; // Set DC To NULL
}
if (hWnd && !DestroyWindow(hWnd)) // Are We Able To Destroy The Window?
{
MessageBox(NULL,"Could Not Release hWnd.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
hWnd=NULL; // Set hWnd To NULL
}
if (!UnregisterClass("Direct3D",hInstance)) // Are We Able To Unregister Class
{
MessageBox(NULL,"Could Not Unregister Class.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
hInstance=NULL; // Set hInstance To NULL
}
}
/* This Code Creates Our DirectX 3D Window. Parameters Are: *
* title - Title To Appear At The Top Of The Window *
* width - Width Of The D3D Window Or Fullscreen Mode *
* height - Height Of The D3D Window Or Fullscreen Mode *
* fullscreenflag - Use Fullscreen Mode (TRUE) Or Windowed Mode (FALSE) */
BOOL CreateD3DWindow(char* title, int width, int height, bool fullscreenflag)
{
WNDCLASS wc;
DWORD dwExStyle; // Window Extended Style
DWORD dwStyle; // Window Style
RECT WindowRect; // Grabs Rectangle Upper Left / Lower Right Values
WindowRect.left=(long)0; // Set Left Value To 0
WindowRect.right=(long)width; // Set Right Value To Requested Width
WindowRect.top=(long)0; // Set Top Value To 0
WindowRect.bottom=(long)height; // Set Bottom Value To Requested Height
fullscreen=fullscreenflag; // Set The Global Fullscreen Flag
hInstance = GetModuleHandle(NULL); // Grab An Instance For Our Window
wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; // Redraw On Size, And Own DC For Window.
wc.lpfnWndProc = (WNDPROC) WndProc; // WndProc Handles Messages
wc.cbClsExtra = 0; // No Extra Window Data
wc.cbWndExtra = 0; // No Extra Window Data
wc.hInstance = hInstance; // Set The Instance
wc.hIcon = LoadIcon(NULL, IDI_WINLOGO); // Load The Default Icon
wc.hCursor = LoadCursor(NULL, IDC_ARROW); // Load The Arrow Pointe