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 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
|
#define WIN32_LEAN_AND_MEAN
#include <d3dx9.h>
#include <windows.h>
#include <windowsx.h>
#include <mmsystem.h>
static char NomFen[] = "Ma classe";
static char CaptionFen[] = "Direct3D (DX9)";
LPDIRECT3D9 lpD3D; // Interface Direct3D
LPDIRECT3DDEVICE9 lpDevice; // Device Direct3D
D3DPRESENT_PARAMETERS d3dpp;// Structure contenant les paramètres du Device Direct3D
D3DCAPS9 d3dCaps; // Structure contenant les capacités de la carte GFX
D3DFORMAT zFormat;
D3DFORMAT Format;
int fpsCourant; // Variable du compteur FPS
int fpsAncien;
int fpsCpt;
LONGLONG cur_time; // Temps courant
LONGLONG last_time; // date du dernier cycle
float time_elapsed; // temps écoulé depuis le dernier cycle
float time_scale; // facteur d'accélération du temps
//////////////////////////////////////////////////////////////////////////
// TEST DEPTH //
//////////////////////////////////////////////////////////////////////////
// - Verifie si le format graphique demandé est supporté par le device //
//////////////////////////////////////////////////////////////////////////
bool TestDepth(D3DFORMAT xFormat)
{
if(lpD3D)
{
if(lpD3D->CheckDeviceFormat(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, Format, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, xFormat) != D3D_OK)
return false;
if(lpD3D->CheckDepthStencilMatch(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, Format, Format, xFormat) != D3D_OK)
return false;
}
return true;
}
//////////////////////////////////////////////////////////////////////////
// TEST FORMAT //
//////////////////////////////////////////////////////////////////////////
// - Verifie si le format graphique demandé est supporté par le device //
//////////////////////////////////////////////////////////////////////////
bool TestFormat(D3DFORMAT xFormat)
{
if(lpD3D)
{
if(lpD3D->CheckDeviceType(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, xFormat, xFormat, false) == D3D_OK)
return true;
}
return false;
}
void DrawText(char * Text, int x, int y)
{
LPD3DXFONT pD3DXFont;
D3DXFONT_DESC pD3DXDesc;
ZeroMemory (&pD3DXDesc, sizeof(D3DXFONT_DESC));
pD3DXDesc.Height = 20;
pD3DXDesc.Weight = 700;
if(FAILED(D3DXCreateFontIndirect(lpDevice, &pD3DXDesc, &pD3DXFont)))
return;
RECT rct;
rct.left = x;
rct.top = y;
rct.right = 640;
rct.bottom = 480;
// Trucs a tracer:
pD3DXFont->DrawText(NULL, Text, -1, &rct, DT_LEFT, D3DCOLOR_XRGB(255, 255, 255));
pD3DXFont->Release();
}
void CountFPS()
{
// On calcule le nombre d'images afficher par seconde :
if (GetTickCount() - fpsAncien >= 1000)
{
fpsCourant = fpsCpt;
fpsCpt = 0;
fpsAncien = GetTickCount();
}
fpsCpt = fpsCpt + 1;
// Affichage du texte
DrawText("FPS = ", 2, 1);
char * FPS = new char[10];
itoa(fpsCourant,FPS,10);
DrawText(FPS, 58, 1);
}
LRESULT CALLBACK WindowProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
switch(uMsg)
{
case WM_DESTROY:
//
lpDevice->Release();
lpD3D->Release();
PostQuitMessage(0);
break;
case WM_KEYDOWN:
switch(wParam)
{
case VK_ESCAPE:
lpDevice->Release();
lpD3D->Release();
PostQuitMessage(0);
break;
}
break;
case WM_CLOSE:
lpDevice->Release();
lpD3D->Release();
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, uMsg, wParam, lParam);
break;
}
return 0;
}
void Render()
{
lpDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0x00000000, 1.0f, 0);
lpDevice->BeginScene();
CountFPS();
lpDevice->EndScene();
lpDevice->Present( NULL, NULL, NULL, NULL );
}
bool InitHorloge()
{
// Determine quel timer sera utilisé
if(QueryPerformanceFrequency((LARGE_INTEGER *) &cur_time))
{
time_scale = 1.0f / cur_time;
QueryPerformanceCounter((LARGE_INTEGER *) &last_time);
return true;
}
else
{
time_scale = 0.001f;
last_time = timeGetTime();
return false;
}
}
//////////////////////////////////////////////////////////////////////////
// MAJ DE L'HORLOGE //
//////////////////////////////////////////////////////////////////////////
// - Calcule le temps écoulé entre 2 rendus (= entre 2 cycles) //
//////////////////////////////////////////////////////////////////////////
void MajHorloge(bool Flag)
{
// On récupère l'heure (2 méthodes =/=)
if(Flag)
QueryPerformanceCounter((LARGE_INTEGER *) &cur_time);
else
cur_time = timeGetTime();
// Calcul du temps écoulé depuis le dernier cycle
time_elapsed = (cur_time - last_time) * time_scale;
// Sauvegarde date du cycle courant
last_time = cur_time;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
// Initialisation des données membres
lpD3D = NULL;
lpDevice = NULL;
WNDCLASS wc;
// Classe window
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = (WNDPROC) WindowProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = sizeof(DWORD);
wc.hInstance = hInstance;
wc.hIcon = NULL;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = NomFen;
RegisterClass(&wc);
// Dimensions de l'écran
int ScreenWidth = GetSystemMetrics(SM_CXSCREEN);
int ScreenHeight = GetSystemMetrics(SM_CYSCREEN);
HWND hWnd = CreateWindow(NomFen, CaptionFen, WS_OVERLAPPEDWINDOW, 0, 0, ScreenWidth, ScreenHeight, NULL, NULL, hInstance, NULL);
if(!hWnd)
{
MessageBox(hWnd, "CreateWindow", "Erreur...", MB_OK);
return 0;
}
// On place la fenêtre et on l'affiche
SetWindowPos(hWnd, HWND_TOP, 0, 20, 1024, 768, SWP_SHOWWINDOW);
// Création de l'objet Direct3D
lpD3D = Direct3DCreate9( D3D_SDK_VERSION );
if(!lpD3D)
{
MessageBox(hWnd, "Direct3DCreate", "Erreur...", MB_OK);
return false;
}
// On prépare le format
memset(&d3dpp, 0, sizeof(d3dpp));
d3dpp.hDeviceWindow = hWnd;
// l'application est en mode fenêtré
d3dpp.Windowed = true;
d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;//D3DPRESENT_INTERVAL_DEFAULT;
D3DDISPLAYMODE d3ddm;
// On récupère les infos sur l'affichage courant
if(FAILED(lpD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm )))
{
MessageBox(hWnd, "GetAdapterDisplayMode", "Erreur...", MB_OK);
return false;
}
Format = d3dpp.BackBufferFormat = d3ddm.Format;
d3dpp.BackBufferWidth = 1024;
d3dpp.BackBufferHeight = 768;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
// Cherche un format de zBuffer compatible avec le mode choisi
zFormat = D3DFMT_UNKNOWN;
if(TestDepth(D3DFMT_D32))
zFormat = D3DFMT_D32;
else if(TestDepth(D3DFMT_D24S8))
zFormat = D3DFMT_D24S8;
else if(TestDepth(D3DFMT_D24X4S4))
zFormat = D3DFMT_D24X4S4;
else if(TestDepth(D3DFMT_D24X8))
zFormat = D3DFMT_D24X8;
else if(TestDepth(D3DFMT_D16))
zFormat = D3DFMT_D16;
else if(TestDepth(D3DFMT_D15S1))
zFormat = D3DFMT_D15S1;
else if(TestDepth(D3DFMT_D16_LOCKABLE))
zFormat = D3DFMT_D16_LOCKABLE;
if(zFormat != D3DFMT_UNKNOWN)
{
d3dpp.EnableAutoDepthStencil = true;
d3dpp.AutoDepthStencilFormat = zFormat;
}
d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE;
// Teste les capacités de la carte graphique
if(FAILED(lpD3D->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &d3dCaps)))
{
MessageBox(hWnd, "GetDeviceCaps", "Erreur...", MB_OK);
return false;
}
DWORD dwBehaviorFlags = 0;
if( d3dCaps.VertexProcessingCaps != 0 )
dwBehaviorFlags |= D3DCREATE_HARDWARE_VERTEXPROCESSING;
else
dwBehaviorFlags |= D3DCREATE_SOFTWARE_VERTEXPROCESSING;
// On créé le Device
if(FAILED(lpD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, dwBehaviorFlags, &d3dpp, &lpDevice)))
{
MessageBox(hWnd, "CreateDevice", "Erreur...", MB_OK);
return false;
}
D3DVIEWPORT9 d3dViewPort;
d3dViewPort.X = 0;
d3dViewPort.Y = 0;
d3dViewPort.Width = 1024;
d3dViewPort.Height = 768;
d3dViewPort.MinZ = 0.0f;
d3dViewPort.MaxZ = 1.0f;
if(FAILED(lpDevice->SetViewport(&d3dViewPort)))
{
MessageBox(hWnd, "SetViewport", "Erreur...", MB_OK);
return false;
}
bool flag_H = false; // Flag qui détermine quel timer est utilisé
// On initialise l'horloge
flag_H = InitHorloge();
// On reçoit et traite les messages Windows
BOOL ReceptionMsg;
MSG Msg;
PeekMessage(&Msg, NULL, 0, 0, PM_NOREMOVE);
while(Msg.message != WM_QUIT)
{
ReceptionMsg = PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE);
if(ReceptionMsg)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
else
{
// Mise à jour des vars de temps
MajHorloge(flag_H);
// Rendu
Render();
}
}
return 1;
} |
Partager