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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
| #include<d3d9.h>
#pragma comment(lib, "d3d9.lib")
#define WINDOW_CLASS L"UGPDX" #define WINDOW_NAME L"Drawing Points"
BOOL InitializeD3D(HWND hWnd, BOOL fullscreen); BOOL InitializeObjects(); void RenderScene(); void Shutdown();
LPDIRECT3D9 g_D3D = NULL; LPDIRECT3DDEVICE9 g_D3DDevice = NULL;
LPDIRECT3DVERTEXBUFFER9 g_VertexBuffer = NULL;
typedef struct { float x, y, z, rhw; unsigned long color; } stD3DVertex;
#define D3DFVF_VERTEX (D3DFVF_XYZRHW | D3DFVF_DIFFUSE)
LRESULT WINAPI MsgProc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp) { switch (msg) { case WM_DESTROY: PostQuitMessage(0); return 0; break; case WM_PAINT: { PAINTSTRUCT ps; HDC hdc = BeginPaint(hWnd, &ps); FillRect(hdc, &ps.rcPaint, (HBRUSH)(COLOR_WINDOW + 1)); EndPaint(hWnd, &ps); } break; case WM_KEYUP: if (wp == VK_ESCAPE) PostQuitMessage(0); break; }
return DefWindowProc(hWnd, msg, wp, lp); }
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE prevhInst, LPSTR cmdLine, int show) { WNDCLASSEXW wc = { sizeof(WNDCLASSEXW), CS_CLASSDC, MsgProc, 0, 0, GetModuleHandle(NULL), NULL, LoadCursor(NULL,IDC_ARROW), NULL, NULL, WINDOW_CLASS, NULL }; RegisterClassEx(&wc);
HWND hWnd = CreateWindowW(WINDOW_CLASS, WINDOW_NAME, WS_OVERLAPPEDWINDOW, 100, 100, 640, 480, GetDesktopWindow(), NULL, wc.hInstance, NULL);
if (InitializeD3D(hWnd, FALSE)) { ShowWindow(hWnd, SW_SHOWDEFAULT); UpdateWindow(hWnd);
MSG msg; ZeroMemory(&msg, sizeof(msg));
while (msg.message != WM_QUIT) { if (PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE)) { TranslateMessage(&msg); DispatchMessage(&msg); } else RenderScene(); } }
Shutdown();
UnregisterClass(WINDOW_CLASS, wc.hInstance); return 0; }
BOOL InitializeD3D(HWND hWnd, BOOL fullscreen) { D3DDISPLAYMODE displayMode;
g_D3D = Direct3DCreate9(D3D_SDK_VERSION); if (g_D3D == NULL) return FALSE;
if (FAILED(IDirect3D9_GetAdapterDisplayMode(g_D3D, D3DADAPTER_DEFAULT, &displayMode))) return FALSE;
D3DPRESENT_PARAMETERS d3dpp; ZeroMemory(&d3dpp, sizeof(d3dpp));
if (fullscreen) { d3dpp.Windowed = FALSE; d3dpp.BackBufferWidth = 640; d3dpp.BackBufferHeight = 480; } else d3dpp.Windowed = TRUE; d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.BackBufferFormat = displayMode.Format;
if (FAILED(IDirect3D9_CreateDevice(g_D3D, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dpp, &g_D3DDevice))) return FALSE;
if (!InitializeObjects()) return FALSE;
return TRUE; }
BOOL InitializeObjects() { unsigned long col = D3DCOLOR_XRGB(255, 0, 255);
stD3DVertex objData[] = { { 100.0f, 100.0f, 0.5f, 1.0f, col, }, { 200.0f, 100.0f, 0.5f, 1.0f, col, }, { 300.0f, 100.0f, 0.5f, 1.0f, col, }, { 400.0f, 100.0f, 0.5f, 1.0f, col, }, { 500.0f, 100.0f, 0.5f, 1.0f, col, }, };
if (FAILED(IDirect3DDevice9_CreateVertexBuffer(g_D3DDevice, sizeof(objData), 0, D3DFVF_VERTEX, D3DPOOL_DEFAULT, &g_VertexBuffer, NULL))) return FALSE;
void* ptr;
if (FAILED(IDirect3DVertexBuffer9_Lock(g_VertexBuffer, 0, sizeof(objData), (void**)&ptr, 0))) return FALSE;
memcpy(ptr, objData, sizeof(objData));
IDirect3DVertexBuffer9_Unlock(g_VertexBuffer);
return TRUE; }
void RenderScene() { IDirect3DDevice9_Clear(g_D3DDevice, 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(255, 255, 255), 1.0f, 0);
IDirect3DDevice9_BeginScene(g_D3DDevice);
IDirect3DDevice9_SetStreamSource(g_D3DDevice, 0, g_VertexBuffer, 0, sizeof(stD3DVertex)); IDirect3DDevice9_SetFVF(g_D3DDevice, D3DFVF_VERTEX); IDirect3DDevice9_DrawPrimitive(g_D3DDevice, D3DPT_POINTLIST, 0, 5);
IDirect3DDevice9_EndScene(g_D3DDevice);
IDirect3DDevice9_Present(g_D3DDevice, NULL, NULL, NULL, NULL); }
void Shutdown() { if (g_D3DDevice != NULL) IDirect3DDevice9_Release(g_D3DDevice); if (g_D3D != NULL) IDirect3D9_Release(g_D3D); if (g_VertexBuffer != NULL) IDirect3DVertexBuffer9_Release(g_VertexBuffer);
g_D3DDevice = NULL; g_D3D = NULL; g_VertexBuffer = NULL; }
|