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
| #include <d3d9.h>
#include <d3dx9.h>
#include <windows.h>
#include <windowsx.h>
#include <iostream>
#include <string>
using namespace std;
#pragma comment (lib, "d3d9.lib")
// FVF et CUSTOMVERTEX pour les buffers
#define CUSTOMFVF (D3DFVF_XYZRHW | D3DFVF_DIFFUSE)
struct CUSTOMVERTEX{
FLOAT x, y, z, rhw; // from the D3DFVF_XYZRHW flag
DWORD color; // from the D3DFVF_DIFFUSE flag
};
// Objet Window avec D3D inclus
class GWindow{
private:
HWND hWnd;
WNDCLASSEX wcex;
LRESULT CALLBACK WndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam );
public:
// Variables D3Deenes
LPDIRECT3D9 d3dO;
LPDIRECT3DDEVICE9 d3ddev;
D3DPRESENT_PARAMETERS d3dpp;
LPDIRECT3DVERTEXBUFFER9 v_buffer;
// Constructeur + Destructeur
GWindow(){
hWnd = NULL;
ZeroMemory(&wcex, sizeof(WNDCLASSEX));
ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dO = NULL;
d3ddev = NULL;
}
~GWindow(){
v_buffer->Release();
d3ddev->Release();
d3dO->Release();
}
// WndProc static pour accéder aux autres fonctions et avoir une initialisation correcte
// Dirige les messages vers le vrai WndProc du hWnd
static LRESULT CALLBACK StaticWndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam );
// Initialise la WNDCLASSEX et le hWnd
void initialization(char* title,int pos_x,int pos_y,int width,int height,HINSTANCE hInstance){
//WNDCLASSEX
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style= CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)GWindow::StaticWndProc;;
wcex.hInstance= hInstance;
wcex.hCursor= LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground= (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszClassName = title;
RegisterClassEx(&wcex);
hWnd = CreateWindow(title,
title,
WS_OVERLAPPEDWINDOW,
pos_x,
pos_y,
width,
height,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hWnd, SW_SHOW);
}
// Initialise le D3D et ses param
void D3DInit(){
// Parameters
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.hDeviceWindow = hWnd;
// Device
d3dO=Direct3DCreate9(D3D_SDK_VERSION);
d3dO->CreateDevice(D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
hWnd,
D3DCREATE_HARDWARE_VERTEXPROCESSING,
&d3dpp,
&d3ddev);
};
// Loop d'execution + draw line( Inutile a virer )
void Loop(){
MSG msg;
while( 1 ){
while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)){
TranslateMessage(&msg);
DispatchMessage(&msg);
}
if(msg.message == WM_QUIT){
break;
}
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, 0, sizeof(CUSTOMVERTEX));
// copy the vertex buffer to the back buffer
d3ddev->DrawPrimitive(D3DPT_LINELIST, 0, 1);
d3ddev->EndScene();
d3ddev->Present(NULL, NULL, NULL, NULL);
}
}
void drawLine(){
CUSTOMVERTEX vertices[] =
{
{220.0f, 50.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(0, 0, 255),},
{420.0f, 200.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(0, 255, 0),},
};
// create the vertex and store the pointer into v_buffer, which is created globally
d3ddev->CreateVertexBuffer(3*sizeof(CUSTOMVERTEX),
0,
CUSTOMFVF,
D3DPOOL_MANAGED,
&v_buffer,
NULL);
VOID* pVoid; // the void pointer
v_buffer->Lock(0, 0, (void**)&pVoid, 0); // lock the vertex buffer
memcpy(pVoid, vertices, sizeof(vertices)); // copy the vertices to the locked buffer
v_buffer->Unlock(); // unlock the vertex buffer
}
};
class GVertexBuffer2D{
LPDIRECT3DDEVICE9 d3ddev;
LPDIRECT3DVERTEXBUFFER9 v_buffer;
int nbVertex;
int nbPrimitive;
public:
// Constr
GVertexBuffer2D(){};
~GVertexBuffer2D(){}
void initialization(LPDIRECT3DDEVICE9 d3ddev_E,int nbVertex_E, CUSTOMVERTEX* vertices ){
d3ddev_E = d3ddev;
nbVertex = nbVertex_E;
d3ddev->CreateVertexBuffer(nbVertex*sizeof(CUSTOMVERTEX),
0,
CUSTOMFVF,
D3DPOOL_MANAGED,
&v_buffer,
NULL);
VOID* pData;
v_buffer->Lock(0, 0, (void**)&pData, 0); // lock the vertex buffer
memcpy(pData, vertices, sizeof(vertices)); // copy the vertices to the locked buffer
v_buffer->Unlock(); // unlock the vertex buffer
}
void render(D3DPRIMITIVETYPE forme){
d3ddev->SetFVF(CUSTOMFVF);
d3ddev->SetStreamSource(0, v_buffer, 0, sizeof(CUSTOMVERTEX));
d3ddev->DrawPrimitive(forme, 0, 1);
}
};
LRESULT CALLBACK GWindow::StaticWndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam ){
if ( msg == WM_CREATE ){
SetWindowLongPtr( hWnd, GWLP_USERDATA, (LONG)((CREATESTRUCT *)lParam)->lpCreateParams );
}
GWindow *targetApp = (GWindow*)GetWindowLongPtr( hWnd, GWLP_USERDATA );
if ( targetApp ){
return targetApp->WndProc( hWnd, msg, wParam, lParam );
}
return DefWindowProc( hWnd, msg, wParam, lParam );
}
LRESULT CALLBACK WndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam );
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow){
CUSTOMVERTEX vertices_2[] =
{
{120.0f, 50.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(255, 255, 255),},
{220.0f, 200.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(42, 255, 0),},
};
GWindow Fenetre;
Fenetre.initialization("Ma Fenêtre",500,500,420,420,hInstance);
Fenetre.D3DInit();
Fenetre.drawLine();
GVertexBuffer2D line;
line.initialization(Fenetre.d3ddev,2,vertices_2);
MSG msg;
while( 1 ){
while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)){
TranslateMessage(&msg);
DispatchMessage(&msg);
}
if(msg.message == WM_QUIT){
break;
}
Fenetre.d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
Fenetre.d3ddev->BeginScene();
line.render(D3DPT_LINELIST);
Fenetre.d3ddev->EndScene();
Fenetre.d3ddev->Present(NULL, NULL, NULL, NULL);
}
return 0;
}
LRESULT CALLBACK GWindow::WndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam ){
switch (msg){
case WM_DESTROY:
PostQuitMessage(0);
return 0;
default:
return DefWindowProc(hWnd, msg, wParam, lParam);
}
} |
Partager