Code:
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <d3d9.h>
#include <d3dx9.h>
#define VERTEXFVF (D3DFVF_XYZ | D3DFVF_TEX1)
#define SCREEN_WIDTH 400
#define SCREEN_HEIGHT 400
#define REFRESH_RATE 0
#define COLOR_FORMAT D3DFMT_R5G6B5
// MACROS //////////////////////////////////////////////////////////////////////
#define KEYDOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
#define KEYUP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)
// Window handles, class and caption text //////////////////////////////////////
HWND hWndApp;
HINSTANCE hInstApp;
const char szClassApp[] = "DirectX Demo";
const char szCaptionApp[] = "DX Texture Loading";
// Structures //////////////////////////////////////////////////////////////////
// 2-D vertex format and descriptor
typedef struct {
FLOAT x, y, z; // 2-D coordinates
FLOAT rhw; // rhw
FLOAT u, v; // texture coordinates
} sVertex;
// Globals /////////////////////////////////////////////////////////////////////
IDirect3D9 *g_pD3D = NULL; // Direct3D object
IDirect3DDevice9 *g_pD3DDevice = NULL; // Direct3D device
IDirect3DVertexBuffer9 *g_pVertexBuffer = NULL; // Vertex buffer
IDirect3DTexture9 *g_pTexture = NULL;
// Function prototypes /////////////////////////////////////////////////////////
LRESULT CALLBACK WindowProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
BOOL Initialize();
BOOL Shutdown();
BOOL DoFrame();
// WinMain and the Windows Procedure /////////////////////////////////////////
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev,
LPSTR szCmdLine, int nCmdShow) {
MSG Msg;
hInst = hInstApp;
WNDCLASSEX wcex = {
wcex.cbSize = sizeof(wcex),
wcex.style = CS_CLASSDC,
wcex.lpfnWndProc = WindowProc,
wcex.cbClsExtra = 0,
wcex.cbWndExtra = 0,
wcex.hInstance = hInst,
wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION),
wcex.hCursor = LoadCursor(NULL, IDC_ARROW),
wcex.hbrBackground = NULL,
wcex.lpszMenuName = NULL,
wcex.lpszClassName = szClassApp,
wcex.hIconSm = LoadIcon(NULL, IDI_APPLICATION)
};
if(!RegisterClassEx(&wcex) )
return FALSE;
if (!(hWndApp = CreateWindowEx(0, // extended style
szClassApp, // class
szCaptionApp, // title
WS_POPUP, // use WS_OVERLAPPEDWINDOW
// for a window app
// use WS_VISIBLE to show window
0,0, // initial x,y
SCREEN_WIDTH,
SCREEN_HEIGHT, // initial width, height
NULL, // handle to parent
NULL, // handle to menu
hInstApp, // instance of this application
NULL) ) ) // extra creation parms
return 0;
ShowWindow(hWndApp, SW_NORMAL);
UpdateWindow(hWndApp);
// Run init function and return on error
if(Initialize() == FALSE)
return FALSE;
// Start message pump, waiting for signal to quit
ZeroMemory(&Msg, sizeof(MSG));
while(Msg.message != WM_QUIT) {
if(PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE) ) {
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
if(DoFrame() == FALSE)
break;
}
// Run shutdown function
Shutdown();
UnregisterClass(szClassApp, hInst);
return Msg.wParam;
}
LRESULT CALLBACK WindowProc(HWND hWnd, UINT Msg,
WPARAM wParam, LPARAM lParam) {
switch(Msg) {
case WM_DESTROY: {
PostQuitMessage(0);
return 0;
} break;
}
return DefWindowProc(hWnd, Msg, wParam, lParam);
}
////////////////////////////////////////////////////////////////////////////////
// This initializes the program
BOOL Initialize() {
D3DPRESENT_PARAMETERS d3dPParam;
D3DDISPLAYMODE d3dDispMode;
BYTE *Ptr; // pointer to vertex buffer memory
sVertex Verts[4] = {
{ 50.0f, 50.0f, 1.0f, 1.0f, 0.0f, 0.0f },
{ 350.0f, 50.0f, 1.0f, 1.0f, 1.0f, 0.0f },
{ 50.0f, 350.0f, 1.0f, 1.0f, 0.0f, 1.0f },
{ 350.0f, 350.0f, 1.0f, 1.0f, 1.0f, 1.0f }
};
// Do a windowed mode initialization of Direct3D
// obtain the interface
if( (g_pD3D = Direct3DCreate9(D3D_SDK_VERSION) ) == NULL)
return FALSE;
// Set the Adapter Display Mode and Presentation Meathod
if(FAILED(g_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,&d3dDispMode) ) )
return FALSE;
ZeroMemory(&d3dPParam, sizeof(d3dPParam) );
d3dPParam.Windowed = true;
d3dPParam.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dPParam.BackBufferFormat = d3dDispMode.Format;
d3dPParam.EnableAutoDepthStencil = FALSE;
// Create the interface
if(FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWndApp,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dPParam, &g_pD3DDevice) ) )
return FALSE;
// Create the vertex buffer and set data
g_pD3DDevice->CreateVertexBuffer(sizeof(sVertex)*4, 0, VERTEXFVF,
D3DPOOL_DEFAULT, &g_pVertexBuffer, NULL);
g_pVertexBuffer->Lock(0, 0, (void**)&Ptr, 0);
memcpy(Ptr, Verts, sizeof(Verts) );
g_pVertexBuffer->Unlock();
// Load texture from disk and set stage parameters
if(FAILED(D3DXCreateTextureFromFile(g_pD3DDevice, "texture.bmp",
&g_pTexture) ) )
return FALSE;
g_pD3DDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
g_pD3DDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
g_pD3DDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
g_pD3DDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_DISABLE);
// Set Magnification and Minification filters for textures
if(FAILED(g_pD3DDevice->SetSamplerState(0, D3DSAMP_MAGFILTER,
D3DTEXF_POINT) ) )
return false;
if(FAILED(g_pD3DDevice->SetSamplerState(0, D3DSAMP_MINFILTER,
D3DTEXF_POINT) ) )
return false;
return TRUE;
}
////////////////////////////////////////////////////////////////////////////////
// This shuts down the program, releasing the system resources
BOOL Shutdown() {
// Release vertex buffer
if(g_pVertexBuffer != NULL)
g_pVertexBuffer->Release();
// Release texture
if(g_pTexture != NULL)
g_pTexture->Release();
// Release device and 3D objects
if(g_pD3DDevice != NULL)
g_pD3DDevice->Release();
if(g_pD3D != NULL)
g_pD3D->Release();
return TRUE;
}
////////////////////////////////////////////////////////////////////////////////
// Build the frame
BOOL DoFrame() {
if (KEYDOWN(VK_ESCAPE) ) {
SendMessage(hWndApp, WM_CLOSE, 0, 0);
}
// Clear device backbuffer and begin the scene
g_pD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET,
D3DCOLOR_RGBA(0, 0, 0, 255), 1.0f, 0);
if(SUCCEEDED(g_pD3DDevice->BeginScene() ) ) {
// Set the texture
g_pD3DDevice->SetTexture(0, g_pTexture);
// Set the stream source and the vertex shader
g_pD3DDevice->SetStreamSource(0, g_pVertexBuffer, 0,sizeof(sVertex));
g_pD3DDevice->SetFVF(VERTEXFVF);
// Release texture from memory
g_pD3DDevice->SetTexture(0, NULL);
// draw triangle list
g_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 2);
// End the scene
g_pD3DDevice->EndScene();
}
// Display the scene
g_pD3DDevice->Present(NULL, NULL, NULL, NULL);
return TRUE;
}