1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
|
#include <windows.h>
#include <windowsx.h>
#include <d3d9.h>
#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 600
LPDIRECT3D9 d3d;
LPDIRECT3DDEVICE9 d3ddev;
LPDIRECT3DVERTEXBUFFER9 v_buffer = NULL; // the pointer to the v buffer
void initD3D(HWND);
void render_frame(void);
void cleanD3D(void);
void init_graphics(void);
#define CUSTOMFVF (D3DFVF_XYZRHW | D3DFVF_DIFFUSE)
typedef struct CUSTOMVERTEXT{
FLOAT X,Y,Z,RHW;
DWORD COLOR;
}CUSTOMVERTEXT;
LRESULT CALLBACK winProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE pevInstance, LPSTR lpCmdLine,
int cmdShow)
{
HWND hwnd;
WNDCLASSEX wc;
ZeroMemory(&wc, sizeof(WNDCLASSEX));
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = winProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
//wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
wc.lpszClassName = L"WindowClass";
RegisterClassEx(&wc);
hwnd = CreateWindowEx(NULL,
L"WindowClass",
L"Our First D3D Program",
WS_POPUP|WS_VSCROLL|WS_HSCROLL,
0,0,
SCREEN_WIDTH,SCREEN_HEIGHT,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hwnd, cmdShow);
// init D3D
initD3D(hwnd);
// enter main loop
MSG msg;
while (TRUE)
{
while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
if(msg.message == WM_QUIT)
break;
render_frame();
}
// clean up DirectX and COM
cleanD3D();
return msg.wParam;
}
LRESULT CALLBACK winProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,message,wParam,lParam);
}
void initD3D(HWND hwnd)
{
d3d = Direct3DCreate9(D3D_SDK_VERSION); // create the d3d interface
D3DPRESENT_PARAMETERS d3dpp; // create a struct to old device info
ZeroMemory(&d3dpp, sizeof(d3dpp)); // clear out struct
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; // discard old frames
d3dpp.hDeviceWindow = hwnd; // set window to be used by d3d
d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
d3dpp.BackBufferWidth = SCREEN_WIDTH;
d3dpp.BackBufferHeight = SCREEN_HEIGHT;
// create the device
d3d->CreateDevice(D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
hwnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp,
&d3ddev);
init_graphics();
}
void render_frame()
{
// clear the window to a deep blue
d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 40, 100), 1.0f, 0);
d3ddev->BeginScene();
d3ddev->SetFVF(CUSTOMFVF);
d3ddev->SetStreamSource(0,v_buffer,0,sizeof(CUSTOMVERTEXT));
d3ddev->DrawPrimitive(D3DPT_TRIANGLELIST,0,1);
d3ddev->EndScene();
d3ddev->Present(NULL,NULL,NULL,NULL);
}
void cleanD3D(void)
{
v_buffer->Release();
d3ddev->Release();
d3d->Release();
}
void init_graphics(void) // check the comas if program doesnt run
{
//create the vertices using the custom vertext struct
static int i = 0;
CUSTOMVERTEXT vertices[] = {
{400.0f, 62.5f, 0.5f, 1.0f, D3DCOLOR_XRGB(0,0,255)},
{650.0f, 500.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(0,255,0)},
{150.0f, 500.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(255,0,0)}};
//create the vertext buffer
d3ddev->CreateVertexBuffer(3*sizeof(CUSTOMVERTEXT),
0,
CUSTOMFVF,
D3DPOOL_MANAGED,
&v_buffer,
NULL);
VOID* pVoid; // void pointer
// lock vert buffer and load verts into
v_buffer->Lock(0,0,(void**)&pVoid,0);
memcpy(pVoid,vertices,sizeof(vertices));
v_buffer->Unlock();
}
| |