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
|
/*-------------------------------------------------
|
| CMain.cpp
|
| Point d'entree du programme
|
\-------------------------------------------------*/
#include <d3d9.h>
#include <d3dx9.h>
#include "CVertex.h"
#include "CWindow.h"
#include "CWndproc.h"
/* ----- WinMain : Point d'entree Windows ----- */
int WINAPI WinMain(HINSTANCE hinstance, /* Handle d'instance de l'applciation */
HINSTANCE hPrevInstance,/* NULL sous Win32 */
LPSTR lpCmdLine,/*Pointeur vers la ligne de commande */
int nCmdShow) /*Indique comment Window affiche la fenetre*/
{
CWindow Main_Window;
/* Creation de la fenetre */
Main_Window.Create(640,480,hinstance,WinProc);
/* Creation de l'objet Direct3D */
LPDIRECT3D9 pD3D = NULL;
pD3D = Direct3DCreate9(D3D_SDK_VERSION);
/* Mode d'affichage */
D3DDISPLAYMODE d3ddm;
pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,&d3ddm);
/* */
D3DCAPS9 d3dCaps;
pD3D->GetDeviceCaps(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,&d3dCaps);
/* Les flags */
DWORD dwBehaviorFlags = 0;
dwBehaviorFlags |= D3DCREATE_HARDWARE_VERTEXPROCESSING;
/* Parametre de creation du Device */
D3DPRESENT_PARAMETERS d3dpp;
memset(&d3dpp, 0, sizeof(d3dpp));
d3dpp.BackBufferFormat = d3ddm.Format;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.Windowed = TRUE;
d3dpp.EnableAutoDepthStencil = TRUE;
d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
/* Creation du peripherique 3D */
LPDIRECT3DDEVICE9 pD3Device = NULL;
int error = pD3D->CreateDevice(D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
Main_Window.GetHandle(),
dwBehaviorFlags,
&d3dpp,
&pD3Device);
/* Erreur lors de la creation du device ? */
switch(error)
{
case D3DERR_NOTAVAILABLE:
MessageBox(Main_Window.GetHandle(),
"Technique demandee non disponible",
"Erreur: D3DERR_NOTAVAILABLE", 0);
exit(1);
break;
case D3DERR_OUTOFVIDEOMEMORY:
MessageBox(Main_Window.GetHandle(),
"Pas assez de memoire pour l'operation demandee.",
"Erreur: D3DERR_OUTOFVIDEOMEMORY", 0);
exit(1);
break;
case D3DERR_INVALIDCALL:
MessageBox(Main_Window.GetHandle(),
"Methode appellee invalide",
"Erreur: D3DERR_INVALIDCALL", 0);
exit(1);
break;
}
/* Creation du vertex buffer */
LPDIRECT3DVERTEXBUFFER9 vertexBuffer = NULL;
pD3Device->CreateVertexBuffer(sizeof(struct VERTEX_SIMPLE)*3,
D3DUSAGE_WRITEONLY,
D3DFVF_VERTEXSIMPLE,
D3DPOOL_DEFAULT,
&vertexBuffer,
NULL);
/* Initialisation du vertex buffer */
struct VERTEX_SIMPLE* buffer;
vertexBuffer->Lock(0,sizeof(struct VERTEX_SIMPLE)*3,(void**)&buffer,D3DLOCK_DISCARD);
/* Vertex a gauche */
buffer[0].x = -1.0f;
buffer[0].y = 0.0f;
buffer[0].z = 0.0f;
buffer[0].color = D3DCOLOR_COLORVALUE( 1.0, 0.0, 0.0, 1.0 ); /* Rouge */
/* Vertex haut */
buffer[1].x = 0.0f;
buffer[1].y = 1.0f;
buffer[1].z = 0.0f;
buffer[1].color = D3DCOLOR_COLORVALUE( 0.0, 1.0, 0.0, 1.0 ); /* Vert */
/* Vertex a droite */
buffer[2].x = 1.0f;
buffer[2].y = 0.0f;
buffer[2].z = 0.0f;
buffer[2].color = D3DCOLOR_COLORVALUE( 0.0, 0.0, 1.0, 1.0 ); /* Bleu */
vertexBuffer->Unlock();
/* On definit la source pour l'affichage */
pD3Device->SetStreamSource(0,vertexBuffer,0,sizeof(struct VERTEX_SIMPLE));
pD3Device->SetFVF(D3DFVF_VERTEXSIMPLE);
/* Affichage de la fenetre */
Main_Window.Show();
/* Variable pour stocker les messages a traiter */
MSG msg;
/* Boucle principale */
while(1)
{
/* Est ce qu'il y a un message ? */
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
/* Message pour quitter l'application ? */
if(msg.message == WM_QUIT)
break;
/* On passe la main pour le traitement du message */
TranslateMessage(&msg);
DispatchMessage(&msg);
}
/* Aucun message on continu l'affichage */
else
{
/* Fonction principale pour le 'rendering' */
pD3Device->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
D3DCOLOR_COLORVALUE(0.0f,1.0f,0.0f,1.0f),
1.0f,
0);
pD3Device->BeginScene();
/* Dessin du triangle ! */
pD3Device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1 );
pD3Device->EndScene();
pD3Device->Present( NULL, NULL, NULL, NULL );
}
}
/* Liberation de la memoire */
vertexBuffer->Release();
pD3Device->Release();
pD3D->Release();
return 0;
} |
Partager