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 412 413 414 415 416 417 418
|
#pragma comment(lib, "d3d10")
#pragma comment(lib, "d3dx10")
// Include the Windows header file, needed for all Windows applications
#include <windows.h>
#include <tchar.h>
#include <d3d10.h>
#include <d3dx10.h>
//Attention pour le linker il faut rajouter dans projet/propriétés/editeur de lien/entrée lib supplémentaire D3D10.lib
// Direct3D global vars
ID3D10Device* pD3DDevice = NULL;
IDXGISwapChain* pSwapChain = NULL;
ID3D10RenderTargetView* pRenderTargetView = NULL;
ID3DX10Sprite *spriteObject = NULL;
// Create a new sprite variable
D3DX10_SPRITE testSprite;
D3D10_VIEWPORT viewPort;
HINSTANCE hInst; // global handle to hold the application instance
HWND wndHandle; // global variable to hold the window handle
int width = 640;
int height = 480;
// forward declarations
bool InitWindow( HINSTANCE hInstance, int width, int height );
LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM );
/*******************************************************************
* InitDirect3D
* Initializes Direct3D
* Inputs - Parent window handle - HWND,
Window width - int
Window height - int
Updating the Code 31
* Outputs - true if successful, false if failed - bool
*******************************************************************/
bool InitDirect3D(HWND hWnd, int width, int height){
// Create the clear the DXGI_SWAP_CHAIN_DESC structure
DXGI_SWAP_CHAIN_DESC swapChainDesc;
ZeroMemory(&swapChainDesc, sizeof(swapChainDesc));
// Fill in the needed values
swapChainDesc.BufferCount = 1;
swapChainDesc.BufferDesc.Width = width;
swapChainDesc.BufferDesc.Height = height;
swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
swapChainDesc.BufferDesc.RefreshRate.Numerator = 60;
swapChainDesc.BufferDesc.RefreshRate.Denominator = 1;
swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
swapChainDesc.OutputWindow = hWnd;
swapChainDesc.SampleDesc.Count = 1;
swapChainDesc.SampleDesc.Quality = 0;
swapChainDesc.Windowed = TRUE;
// Create the D3D device and the swap chain
HRESULT hr = D3D10CreateDeviceAndSwapChain(NULL, D3D10_DRIVER_TYPE_REFERENCE, NULL, 0, D3D10_SDK_VERSION, &swapChainDesc, &pSwapChain, &pD3DDevice);
// Error checking. Make sure the device was created
if (hr != S_OK){
return false;
}
// Get the back buffer from the swapchain
ID3D10Texture2D *pBackBuffer;
hr = pSwapChain->GetBuffer(0, __uuidof(ID3D10Texture2D), (LPVOID*)&pBackBuffer);
if(hr != S_OK){
return false;
}
// create the render target view
hr = pD3DDevice->CreateRenderTargetView(pBackBuffer, NULL, &pRenderTargetView);
// release the back buffer
pBackBuffer->Release();
// Make sure the render target view was created successfully
if(hr != S_OK){
return false;
}
// set the render target
pD3DDevice->OMSetRenderTargets(1, &pRenderTargetView, NULL);
// create and set the viewport
viewPort.Width = width;
viewPort.Height = height;
viewPort.MinDepth = 0.0f;
viewPort.MaxDepth = 1.0f;
viewPort.TopLeftX = 0;
viewPort.TopLeftY = 0;
pD3DDevice->RSSetViewports(1, &viewPort);
return true;
}
/*******************************************************************
* Render
* All drawing happens in the Render function
* Inputs - void
* Outputs - void
*******************************************************************/
void Render(){
HRESULT hr;
if(pD3DDevice != NULL){
// clear the target buffer
pD3DDevice->ClearRenderTargetView(pRenderTargetView, D3DXCOLOR (0.0f, 0.0f, 0.0f, 0.0f));
// start drawing the sprites
hr = spriteObject->Begin(D3DX10_SPRITE_SORT_TEXTURE);
// Draw all the sprites
if((hr = spriteObject->DrawSpritesImmediate(&testSprite, 1, 0, 0)) !=S_OK){
int flag=0;
}
// Finish up and send the sprites to the hardware
hr = spriteObject->End();
// display the next item in the swap chain
hr = pSwapChain->Present(0, 0);
}
}
/*******************************************************************
* ShutdownDirect3D
* Closes down and releases the resources for Direct3D
* Inputs - void
* Outputs - void
*******************************************************************/
void ShutdownDirect3D()
{
// release the rendertarget
if (pRenderTargetView)
{
pRenderTargetView->Release();
}
// release the swapchain
if (pSwapChain)
{
pSwapChain->Release();
}
// release the D3D Device
if (pD3DDevice)
{
pD3DDevice->Release();
}
}
/*******************************************************************
* GetTexture2DFromFile
* Loads a texture from a file into a ID3D10Texture2D object
* Inputs - LPCSTR the path and filename of the texture
* Outputs - pointer to an ID3D10Texture2D object
*******************************************************************/
ID3D10Texture2D* GetTexture2DFromFile(LPCWSTR filename)
{
ID3D10Texture2D* texture2D = NULL;
ID3D10Resource* pD3D10Resource = NULL;
// Loads the texture into a temporary ID3D10Resource object
HRESULT hr = D3DX10CreateTextureFromFile(pD3DDevice,filename,NULL,NULL,&pD3D10Resource,NULL);
// Make sure the texture was loaded in successfully
if (FAILED(hr))
{
return NULL;
}
// Translates the ID3D10Resource object into a ID3D10Texture2D object
hr = pD3D10Resource ->QueryInterface(__uuidof( ID3D10Texture2D), (LPVOID*)&texture2D);
pD3D10Resource ->Release();
// returns the ID3D10Texture2D object
return texture2D;
}
// This is winmain, the main entry point for Windows applications
int APIENTRY _tWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,LPTSTR lpCmdLine, int nCmdShow )
{
// Initialize the window
if ( !InitWindow( hInstance, width, height ) )
{
return false;
}
// called after creating the window
if ( !InitDirect3D( wndHandle, width, height) )
{
return 0;
}
LPCWSTR filename=L"Textures/Alpha.png";
ID3D10Texture2D* texture=GetTexture2DFromFile(filename);
D3D10_SHADER_RESOURCE_VIEW_DESC SRVDesc;
if (texture != NULL)
{
// Get the texture details
D3D10_TEXTURE2D_DESC desc;
texture->GetDesc( &desc );
D3D10_RESOURCE_DIMENSION rr;
texture->GetType(&rr);
// Create a shader resource view of the texture
// Clear out the shader resource view description structure
ZeroMemory( &SRVDesc, sizeof(SRVDesc) );
// Set the texture format
SRVDesc.Format = desc.Format;
// Set the type of resource
SRVDesc.ViewDimension = D3D10_SRV_DIMENSION_TEXTURE2D;
SRVDesc.Texture2D.MipLevels = desc.MipLevels;
}
ID3D10ShaderResourceView *gSpriteTextureRV = NULL;
pD3DDevice->CreateShaderResourceView(texture, &SRVDesc, &gSpriteTextureRV);
// Set the sprites shader resource view
testSprite.pTexture = gSpriteTextureRV;
// top-left location in U,V coords
testSprite.TexCoord.x = 0;
testSprite.TexCoord.y = 0;
// Determine the texture size in U,V coords
testSprite.TexSize.x = 1.0f;
testSprite.TexSize.y = 1.0f;
// Set the texture index. Single textures will use 0
testSprite.TextureIndex = 0;
// The color to apply to this sprite, full color applies white.
testSprite.ColorModulate = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
// these variables describe the dimensions of the sprite
// and where it should be located.
float spritePosX = 300;
float spritePosY = 300;
float spriteWidth = 120;
float spriteHeight = 158;
// The translation matrix to be created
D3DXMATRIX matTranslation;
// Create the translation matrix
D3DXMatrixTranslation( &matTranslation,spritePosX,spritePosY,0.1f);
// Scale the sprite to its correct width and height
D3DXMATRIX matScaling;
D3DXMatrixScaling(&matScaling, spriteWidth, spriteHeight, 1.0f );
testSprite.matWorld = (matScaling * matTranslation);
// create the sprite object
HRESULT hr = D3DX10CreateSprite(pD3DDevice, 0, &spriteObject);
// Make sure the sprite creation was successful
if (hr != S_OK)
{
// Handle failure
}
// spriteObject contains a valid ID3DX10Sprite object
if (!spriteObject)
{
spriteObject->Release();
spriteObject = NULL;
}
//Projection
D3DXMATRIX matProjection;
// Create the projection matrix using the values in the viewport
D3DXMatrixOrthoOffCenterLH(&matProjection,
(float)viewPort.TopLeftX,
(float)viewPort.Width,
(float)viewPort.TopLeftY,
(float)viewPort.Height,
0.1f,
10);
// Set the projection matrix
hr = spriteObject->SetProjectionTransform(&matProjection);
//D3DXMATRIX matView;
//hr = spriteObject->SetViewTransform(&matView);
ID3D10BlendState* m_pFontBlendState10 = NULL;
D3D10_BLEND_DESC StateDesc;
ZeroMemory( &StateDesc, sizeof( D3D10_BLEND_DESC ) );
StateDesc.AlphaToCoverageEnable = FALSE;
StateDesc.BlendEnable[0] = TRUE;
StateDesc.SrcBlend = D3D10_BLEND_SRC_ALPHA;
StateDesc.DestBlend = D3D10_BLEND_INV_SRC_ALPHA;
StateDesc.BlendOp = D3D10_BLEND_OP_ADD;
StateDesc.SrcBlendAlpha = D3D10_BLEND_ZERO;
StateDesc.DestBlendAlpha = D3D10_BLEND_ZERO;
StateDesc.BlendOpAlpha = D3D10_BLEND_OP_ADD;
StateDesc.RenderTargetWriteMask[0] = 0xf;
hr = pD3DDevice->CreateBlendState( &StateDesc, &m_pFontBlendState10 );
FLOAT NewBlendFactor[4] = {0,0,0,0};
pD3DDevice->OMSetBlendState( m_pFontBlendState10, NewBlendFactor, 0xffffffff );
// main message loop:
MSG msg = {0};
while (WM_QUIT != msg.message)
{
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) == TRUE)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
// Additional game logic can be called from here
Render();
}
return (int) msg.wParam;
}
/*******************************************************************
* InitWindow
* Inits and creates and main app window
* Inputs - application instance - HINSTANCE
Window width - int
Window height - int
* Outputs - true if successful, false if failed - bool
*******************************************************************/
bool InitWindow(HINSTANCE hInstance, int width, int height)
{
WNDCLASSEX wcex;
// Fill in the WNDCLASSEX structure. This describes how the window
// will look to the system
wcex.cbSize = sizeof(WNDCLASSEX); // the size of the structure
wcex.style = CS_HREDRAW | CS_VREDRAW; // the class style
wcex.lpfnWndProc = (WNDPROC)WndProc; // the window procedure callback
wcex.cbClsExtra = 0; // extra bytes to allocate for this class
wcex.cbWndExtra = 0; // extra bytes to allocate for this instance
wcex.hInstance = hInstance; // handle to the application instance
wcex.hIcon = 0; // icon to associate with the application
wcex.hCursor = LoadCursor(NULL, IDC_ARROW); // the default cursor to use
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); // the background color
wcex.lpszMenuName = NULL; // the resource name for the menu
wcex.lpszClassName = TEXT("DirectXExample"); // the class name being created
wcex.hIconSm = 0; // the handle to the small icon
RegisterClassEx(&wcex);
// Resize the window
RECT rect = { 0, 0, width, height };
AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, FALSE);
// create the window from the class above
wndHandle = CreateWindow(TEXT("DirectXExample"),
TEXT("DirectXExample"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
rect.right - rect.left,
rect.bottom - rect.top,
NULL,
NULL,
hInstance,
NULL);
if (!wndHandle)
{
return false;
}
// Display the window on the screen
ShowWindow(wndHandle, SW_SHOW);
UpdateWindow(wndHandle);
return true;
}
/*******************************************************************
* WndProc
* The main window procedure for the application
* Inputs - application window handle - HWND
message sent to the window - UINT
wParam of the message being sent - WPARAM
lParam of the message being sent - LPARAM
* Outputs - LRESULT
*******************************************************************/
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
// Check any available messages from the queue
switch (message)
{
// Allow the user to press the Escape key to end the application
case WM_KEYDOWN:
switch(wParam)
{
// Check if the user hit the Escape key
case VK_ESCAPE:
PostQuitMessage(0);
break;
}
break;
// The user hit the close button, close the application
case WM_DESTROY:
PostQuitMessage(0);
break;
}
// Always return the message to the default window procedure for further
return DefWindowProc(hWnd, message, wParam, lParam);
} |
Partager