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 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
| #include "StdAfx.h"
#include <d3d9.h>
#include <d3dx9.h>
#include <windows.h>
#include <windowsx.h>
#include <iostream>
#pragma comment (lib, "d3d9.lib")
// define the screen resolution
#define SCREEN_WIDTH 1000
#define SCREEN_HEIGHT 600
// global declarations
LPDIRECT3D9 d3d; // the pointer to our Direct3D interface
LPDIRECT3DDEVICE9 d3ddev; // the pointer to the device class
LPDIRECT3DVERTEXBUFFER9 v_buffer_bas = NULL; // pour stocker le coté gauche
LPDIRECT3DVERTEXBUFFER9 v_buffer_droit = NULL; // pour stocker le coté droit
LPDIRECT3DVERTEXBUFFER9 v_buffer_gauche = NULL;
LPDIRECT3DVERTEXBUFFER9 v_buffer_carre = NULL;
bool afficheDroit = false;
bool afficheGauche = false;
bool afficheBas = false;
// function prototypes
void initD3D(HWND hWnd); // sets up and initializes Direct3D
void render_line(void);
void render_carre(void);
void cleanD3D(void); // closes Direct3D and releases memory
void init_graphicsB(void); // 3D declarations
void init_graphicsD(void);
void init_graphicsG(void);
void init_graphicsCarre(void);
struct CUSTOMVERTEX {FLOAT X, Y, Z, RHW; DWORD COLOR;};
#define CUSTOMFVF (D3DFVF_XYZRHW | D3DFVF_DIFFUSE)
// the WindowProc function prototype
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
// the entry point for any Windows program
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 = L"WindowClass";
RegisterClassEx(&wc);
hWnd = CreateWindowEx(NULL,
L"WindowClass",
L"Our Direct3D Program",
WS_OVERLAPPEDWINDOW,
0, 0,
SCREEN_WIDTH, SCREEN_HEIGHT,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hWnd, nCmdShow);
// set up and initialize Direct3D
initD3D(hWnd);
// initialise les vb
init_graphicsB();
init_graphicsD();
init_graphicsG();
init_graphicsCarre();
// enter the main loop:
MSG msg;
while(TRUE)
{
d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)){
TranslateMessage(&msg);
DispatchMessage(&msg);
}
if(msg.message == WM_QUIT){
break;
}
render_line();
render_carre();
}
// clean up DirectX and COM
cleanD3D();
return msg.wParam;
}
// this is the main message handler for the program
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message){
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
} break;
case WM_KEYDOWN:
if (wParam == VK_RIGHT){
afficheDroit = true;
}
if (wParam == VK_LEFT){
afficheGauche = true;
}
if (wParam == VK_DOWN){
afficheBas = true;
}
break;
case WM_KEYUP:
if(wParam == VK_RIGHT){
afficheDroit = false;
}
if (wParam == VK_LEFT){
afficheGauche = false;
}
if (wParam == VK_DOWN){
afficheBas = false;
}
break;
}
return DefWindowProc (hWnd, message, wParam, lParam);
}
// this function initializes and prepares Direct3D for use
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;
// create a device class using this information and the info from the d3dpp stuct
d3d->CreateDevice(D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp,
&d3ddev);
}
// this is the function used to render a single frame
void render_line(void){
d3ddev->BeginScene();
// select which vertex format we are using
d3ddev->SetFVF(CUSTOMFVF);
// select the vertex buffer to display
if(afficheDroit == true){
d3ddev->SetStreamSource(0, v_buffer_droit, 0, sizeof(CUSTOMVERTEX));
d3ddev->DrawPrimitive(D3DPT_LINELIST, 0, 1);
}
if(afficheGauche == true){
d3ddev->SetStreamSource(0, v_buffer_gauche, 0, sizeof(CUSTOMVERTEX));
d3ddev->DrawPrimitive(D3DPT_LINELIST, 0, 1);
}
if(afficheBas == true){
d3ddev->SetStreamSource(0, v_buffer_bas, 0, sizeof(CUSTOMVERTEX));
d3ddev->DrawPrimitive(D3DPT_LINELIST, 0, 1);
}
d3ddev->EndScene();
d3ddev->Present(NULL, NULL, NULL, NULL);
}
void render_carre(void){
d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
d3ddev->BeginScene();
// select which vertex format we are using
d3ddev->SetFVF(CUSTOMFVF);
// select the vertex buffer to display
d3ddev->SetStreamSource(0, v_buffer_carre, 0, sizeof(CUSTOMVERTEX));
d3ddev->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 2);
d3ddev->EndScene();
d3ddev->Present(NULL, NULL, NULL, NULL);
}
// this is the function that cleans up Direct3D and COM
void cleanD3D(void){
v_buffer_bas->Release(); // close and release the vertex buffer
v_buffer_droit->Release();
v_buffer_gauche->Release();
v_buffer_carre->Release();
d3ddev->Release(); // close and release the 3D device
d3d->Release(); // close and release Direct3D
}
// this is the function that puts the 3D models into video RAM
void init_graphicsB(void)
{
// create the vertices using the CUSTOMVERTEX struct
CUSTOMVERTEX vertices[] =
{
{ 650.0f, 500.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(255, 0, 0), },
{ 350.0f, 500.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(250, 0, 0), },
};
// create a vertex buffer interface called v_buffer
d3ddev->CreateVertexBuffer(2*sizeof(CUSTOMVERTEX),
0,
CUSTOMFVF,
D3DPOOL_MANAGED,
&v_buffer_bas,
NULL);
VOID* pVoid; // a void pointer
// lock v_buffer and load the vertices into it
v_buffer_bas->Lock(0, 0, (void**)&pVoid, 0);
memcpy(pVoid, vertices, sizeof(vertices));
v_buffer_bas->Unlock();
}
void init_graphicsD(void)
{
// create the vertices using the CUSTOMVERTEX struct
CUSTOMVERTEX vertices[] =
{
{ 500.0f, 200.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(0, 255, 0), },
{ 650.0f, 500.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(0, 255, 0), },
};
// create a vertex buffer interface called v_buffer
d3ddev->CreateVertexBuffer(2*sizeof(CUSTOMVERTEX),
0,
CUSTOMFVF,
D3DPOOL_MANAGED,
&v_buffer_droit,
NULL);
VOID* pVoid; // a void pointer
// lock v_buffer and load the vertices into it
v_buffer_droit->Lock(0, 0, (void**)&pVoid, 0);
memcpy(pVoid, vertices, sizeof(vertices));
v_buffer_droit->Unlock();
}
void init_graphicsG(void){
CUSTOMVERTEX vertices[] =
{
{ 500.0f, 200.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(0, 0, 255), },
{ 350.0f, 500.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(0, 0, 255), },
};
d3ddev->CreateVertexBuffer(2*sizeof(CUSTOMVERTEX),
0,
CUSTOMFVF,
D3DPOOL_MANAGED,
&v_buffer_gauche,
NULL);
VOID* pVoid;
v_buffer_gauche->Lock(0,0,(void**)&pVoid, 0);
memcpy(pVoid, vertices, sizeof(vertices));
v_buffer_droit->Unlock();
}
void init_graphicsCarre(void){
CUSTOMVERTEX vertices[] =
{
{ 10.0f, 100.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(0, 255, 255), },
{ 10.0f, 10.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(0, 0, 255), },
{ 100.0f, 100.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(255, 0, 255), },
{ 100.0f, 100.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(255, 0, 255), },
{ 10.0f, 10.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(0, 0, 255), },
{ 100.0f, 10.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(0, 0, 255), },
};
d3ddev->CreateVertexBuffer(6*sizeof(CUSTOMVERTEX),
0,
CUSTOMFVF,
D3DPOOL_MANAGED,
&v_buffer_carre,
NULL);
VOID* pVoid;
v_buffer_carre->Lock(0,0,(void**)&pVoid, 0);
memcpy(pVoid, vertices, sizeof(vertices));
v_buffer_carre->Unlock();
} |
Partager