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
|
#include <windows.h>
#include <windowsx.h>
#include <d3d9.h>
#include <d3dx9.h>
#pragma comment (lib, "d3d9.lib")
#pragma comment (lib, "d3dx9.lib")
#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 600
LPDIRECT3D9 d3d;
LPDIRECT3DDEVICE9 d3ddev;
LPDIRECT3DVERTEXBUFFER9 v_buffer = NULL;
void initD3D(HWND hWnd);
void render_frame(void);
void cleanD3D(void);
void init_graphics(void);
struct CUSTOMVERTEX {FLOAT x, y, Z; DWORD colour;};
#define CUSTOMFVF (D3DFVF_XYZ | D3DFVF_DIFFUSE)
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
HWND hWnd;
WNDCLASSEX wc;
ZeroMemory(&wc, sizeof(WNDCLASSEX));
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.lpszClassName = "WindowClass"
//wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
RegisterClassEx(&wc);
hWnd = CreateWindowEx(NULL,
"WindowClass",
"TemplateD3DWindow", WS_OVERLAPPEDWINDOW, 100, 100, SCREEN_WIDTH, SCREEN_HEIGHT, NULL,NULL,hInstance,NULL);
ShowWindow(hWnd, nCmdShow);
initD3D(hWnd);
MSG msg;
while(TRUE)
{
while(PeekMessage(&msg, NULL, 0, 0,PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
if(msg.message == WM_QUIT)
{
break;
}
render_frame();
}
cleanD3D();
return msg.wParam;
}
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
} break;
}
return DefWindowProc (hWnd, message, lParam, wParam);
}
void initD3D(HWND hWnd)
{
d3d=Direct3DCreate9(D3D_SDK_VERSION);
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.Windowed = TRUE; d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.hDeviceWindow = hWnd;
d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
d3dpp.BackBufferWidth = SCREEN_WIDTH;
d3dpp.BackBufferHeight = SCREEN_HEIGHT;
d3d->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL, hWnd,D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &d3ddev);
init_graphics();
d3ddev->SetRenderState(D3DRS_LIGHTING, FALSE); }
void render_frame(void)
{
d3ddev->Clear(0, NULL,D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
d3ddev->BeginScene();
d3ddev->SetFVF(CUSTOMFVF);
D3DXMATRIX matRotateY;
static float index = 0.0f; index += 0.05f;
D3DXMatrixRotationY(&matRotateY, index);
d3ddev->SetTransform(D3DTS_WORLD, &matRotateY); D3DXMATRIX matView;
D3DXMatrixLookAtLH(&matView, &D3DXVECTOR3(0.0f, 0.0f, 10.0f), &D3DXVECTOR3(0.0f, 0.0f, 0.0f), &D3DXVECTOR3(0.0f, 1.0f, 0.0f));
d3ddev->SetTransform(D3DTS_VIEW, &matView);
D3DXMATRIX matProjection;
D3DXMatrixPerspectiveFovLH(&matProjection,
D3DXToRadian(45), (FLOAT)SCREEN_WIDTH/(FLOAT)SCREEN_HEIGHT, 1.0f, 100.0f);
d3ddev->SetTransform(D3DTS_PROJECTION, &matProjection);
d3ddev->SetStreamSource(0, v_buffer, 0,sizeof(CUSTOMVERTEX)); // needs to know how large EACH vertex is
d3ddev->DrawPrimitive(D3DPT_TRIANGLELIST, // what type of vertices are we drawing
0, // which vertex we want to start at
1); // how mny primitives are being drawn in total - 1 for 1 triangle, or 3 for 3 point
d3ddev->EndScene();
d3ddev->Present(NULL, NULL, NULL, NULL); // not sure what they're used for...
}
void cleanD3D(void)
{
v_buffer->Release(); // close and release the vertex buffer
d3ddev->Release(); // close and release the D3D device
d3d->Release(); // close and release the D3D interface
}
void init_graphics(void)
{
// here we fill our vertex struct with the information for each individual vertices
CUSTOMVERTEX OurVertices[]= // leave brackets blank so we can add/edit at later date - leaving them blank helps
{
{3.0f, -3.0f, 0.0f, D3DCOLOR_XRGB(0, 0, 255),},
{0.0f, 3.0f, 0.0f, D3DCOLOR_XRGB(0, 255, 0),},
{-3.0f, -3.0f, 0.0f, D3DCOLOR_XRGB(255, 0 ,0),},
};
// we need to create the buffer in the video RAM before we can fill it
d3ddev->CreateVertexBuffer(3*sizeof(CUSTOMVERTEX), // CUSTOMVERTEX is a single struct, so we need to give
// it the size of three of them, as we have 3 in our
// array
0, // we aren't using the vertices in a special way yet
CUSTOMFVF, // we need to tell it what FVF options we are using
D3DPOOL_MANAGED, // we want the vertex buffer to be stored in Video RAM
&v_buffer, // need to fill our point with the correct adress of where
// it's being created
NULL); // this paramter is 'reserved' by microsoft for later date
VOID *pVoid; // create a non-type pointer so we can point to a start point in memory
// lock the buffer so that we have exclusive access to it
v_buffer->Lock(0, // we want the entire buffer (as it is the exact size of our array) so start from the
// very beginning. Use 0 as we are starting from the very beginning of the buffer
0, // as we're gonna want the entire buffer leave this as zero (means all)
(void**)&pVoid, // fill pVoid with the address of the locked buffer THAT WE CAN RIGHT TO
// v_buffer refers to the buffer interface whereas pVoid refers to the
// buffer memory that we can use
0); // this is an advanced flag - leave to 0 for now
// copy to the address from pVoid. We want to copy from OurVertices, and we want to copy the equivelant
// of the entirety of OurVertices
memcpy(pVoid, OurVertices, sizeof(OurVertices));
v_buffer->Unlock(); // now simply unlock the buffer now that we're done with it.
}
|