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
|
//Inclu l'API DirectX Graphics
#include <d3d9.h>
#include <d3dx9core.h>
//Variables globales
LPDIRECT3D9 g_pD3D = NULL; //Déclaration de l'objet D3D
LPDIRECT3DDEVICE9 g_pd3dDevice = NULL; //Déclaration de l'objet périphérique de rendu
// on déclare notre objet
LPD3DXSPRITE routineSprites=NULL;
// image
LPDIRECT3DTEXTURE9 image;
D3DXIMAGE_INFO imageInfo;
//Initialise Direct3D
bool InitD3D(HWND hWnd)
{
//Si la création de l'objet échoue, on retourne FALSE
if((g_pD3D = Direct3DCreate9(D3D_SDK_VERSION)) == NULL)
return FALSE;
//Cette partie initialise une structure qui permettra ensuite de créer l'objet périphérique de rendu
D3DPRESENT_PARAMETERS d3d_pp;
ZeroMemory(&d3d_pp, sizeof(d3d_pp));
d3d_pp.Windowed = TRUE;
d3d_pp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3d_pp.BackBufferFormat = D3DFMT_UNKNOWN;
//Créé l'objet périphérique de rendu, si il y a une erreur, on retourne FALSE
if(FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3d_pp,
&g_pd3dDevice)))
{
return FALSE;
}
// sprite
D3DXCreateSprite(g_pd3dDevice,&routineSprites);
// charger l'image et récupérer des infos dessus (notamment la hauteur et la largeur)
ZeroMemory(&imageInfo, sizeof(D3DXIMAGE_INFO));
if( FAILED(D3DXGetImageInfoFromFile( "back.bmp", &imageInfo ) ) )
MessageBox(hWnd,"marche pas","prob",0);
HRESULT hr = D3DXCreateTextureFromFileEx( g_pd3dDevice,
"back.bmp",
imageInfo.Width,
imageInfo.Height,
1,
0,
D3DFMT_UNKNOWN,
D3DPOOL_MANAGED,
D3DX_FILTER_NONE,
D3DX_DEFAULT,
0,
&imageInfo,
NULL,
&image
);
//OK
return TRUE;
}
//Quit() permet de quitter l'application correctement (en libérant la mémoire utilisée)
void Quit()
{
//Si l'objet de périphérique de rendu existe, on le supprime.
if(g_pd3dDevice)
g_pd3dDevice->Release();
//Si l'objet Direct3D existe...
if(g_pD3D)
g_pD3D->Release();
}
//Affiche le rendu
void Render()
{
//Si l'on a pas d'objet périphérique de rendu, on quitte
if(g_pd3dDevice == NULL)
return;
//On affiche un fond jaune à la place du rendu précédent
g_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(255, 255, 0), 1.0f, 0);
//On commence le rendu
if(SUCCEEDED(g_pd3dDevice->BeginScene()))
{
//C'est ici généralement que l'on insère les fonctions de dessin
routineSprites->Draw( image, NULL, NULL, NULL, 0,
NULL,D3DCOLOR_XRGB(255, 255,255) );
//Fin de la scène
g_pd3dDevice->EndScene();
}
//Affiche le dernier rendu
g_pd3dDevice->Present(NULL, NULL, NULL, NULL);
}
//Gère les messages windows
LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
switch(msg)
{
case WM_DESTROY:
{
Quit();
PostQuitMessage(0);
return 0;
break;
}
case WM_PAINT:
{
Render();
ValidateRect( hWnd, NULL );
return 0;
break;
}
default:
break;
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}
//Fonction principale
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
MSG msg;
//Initialise puis enregistre la classe de fenêtre
WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L,
GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
"DeusXL", NULL };
RegisterClassEx(&wc);
//Créer la fenêtre principale
HWND hWnd = CreateWindow("DeusXL", "DirectX 9.0",
WS_OVERLAPPEDWINDOW, 100, 100, 640, 480,
GetDesktopWindow(), NULL, wc.hInstance, NULL );
// Initialize Direct3D
if(InitD3D(hWnd))
{
//Affiche la fenêtre
ShowWindow( hWnd, SW_SHOWDEFAULT );
UpdateWindow( hWnd );
//Boucle de gestion des messages
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
//Supprime la classe de fenêtre "daron"
UnregisterClass("DeusXL", wc.hInstance);
//Fin du programme
return 0;
} |
Partager