Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Microsoft says The power of multimedia in Windows, DirectX gives you the best possible experience with graphics, sound, music, and 3-D animation. API in MSDN:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_m/directx/directx9m.asp
DirectX Architecture
Windows Win32 Application
Direct3D
XAudio
Software emulation
DirectX Devices
Represent hardware
Different graphics cards, which means: different available screen resolutions different available color models
You want to reach a broad audience Whats the end result? a headache a lot of code devoted to dealing with these differences
Strategy for creating graphics devices Pick a common resolution, ex: 1024 X 768 Pick a common color model, ex: 32 XRGB When the application starts:
check to make sure the players graphics card has these capabilities What if the player doesnt have our desired format?
have backup choices tell the player to come back when they have a better computer
LPDIRECT3DDEVICE9 graphicsDevice; D3DPRESENT_PARAMETERS presentParameters; LPD3DXSPRITE spriteHandler; ZeroMemory(&presentParameters,sizeof(presentParameters)); presentParameters.Windowed = true; presentParameters.SwapEffect = D3DSWAPEFFECT_DISCARD; presentParameters.hDeviceWindow = hWnd; presentParameters.BackBufferFormat = formatToUse; presentParameters.BackBufferWidth = screenWidthToUse; presentParameters.BackBufferHeight = screenHeightToUse; HRESULT result = d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, presentParameters.hDeviceWindow, D3DCREATE_HARDWARE_VERTEXPROCESSING, &presentParameters, &graphicsDevice); result = D3DXCreateSprite(graphicsDevice, &spriteHandler);
So what?
LPD3DXSPRITE can draw 2D images
D3DXIMAGE_INFO info; HRESULT result = D3DXGetImageInfoFromFile(fileName, &info); if (result == S_OK) { D3DCOLOR colorKey = D3DCOLOR_XRGB(96, 128, 224); LPDIRECT3DTEXTURE9 textureToLoad; result = D3DXCreateTextureFromFileEx( graphicsDevice, fileName, info.Width, info.Height, 1, D3DPOOL_DEFAULT, D3DFMT_UNKNOWN, D3DPOOL_DEFAULT, texture, D3DX_DEFAULT, D3DX_DEFAULT, colorKey, &info, NULL, &textureToLoad );
Each Frame
1. 2. 3. 4. 5. 6. 7. 8. clear screen reserve the GPU for rendering start sprite batch render all textures render text end sprite batch unreserve the GPU force the GPU to the screen
Rendering a frame
graphicsDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(96, 96, 96), 1.0f, 0); graphicsDevice->BeginScene(); spriteHandler->Begin(D3DXSPRITE_ALPHABLEND); result = spriteHandler->Draw( texture, imageRect, NULL, &position, alphaColor); result = textFont->DrawText ( spriteHandler, L"Render this Text", -1, &textRect, DT_LEFT, D3DCOLOR_XRGB(96, 96, 96) ); spriteHandler->End(); graphicsDevice->EndScene(); graphicsDevice->Present(NULL, NULL, NULL, NULL);
Texture Management
Skilled game programmers recycle images
Why?
ints take up less memory than wstrings
Fullscreen
you get the resources how do we get out?
Game control (exit game for example) ALT-TAB
HRESULT result = graphicsDevice->TestCooperativeLevel(); if (SUCCEEDED(result)) { // RENDER THE GAME HERE } else if (result == D3DERR_DEVICELOST) { // SOME OTHER APPLICATION HAS THE GPU, MAYBE // AN ALT-TAB, WE JUST HAVE TO KEEP TRYING TO // GET IT BACK Sleep(100); } else if (result == D3DERR_DEVICENOTRESET) { // YAY! WE GOT THE GPU BACK, MAYBE SOMEONE ALT-TABBED BACK // RESET THE GRAPHICS CARD AND RELOAD ALL THE TEXTURES if (FAILED(graphicsDevice->Reset(&presentParameters))) // RELOAD ALL IMAGES }