beginning direct3d gameprogramming01_20161102_jintaeks

Post on 13-Feb-2017

50 Views

Category:

Engineering

3 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Beginning Direct3D Game Programming:Window Game Program-

ming Foundationjintaeks@gmail.com

Division of Digital Contents, DongSeo University.November 2016

Contents at a GlanceChapter 1 The History of Direct3D/DirectX GraphicsChapter 2 Overview of HAL and COMChapter 3 Programming ConventionsChapter 4 3D Fundamentals, Gouraud Shading,and Texture-Mapping BasicsChapter 5 The Basics - Setup Environment, DirectX BasicsChapter 6 First Steps to AnimationChapter 7 Texture-Mapping FundamentalsChapter 8 Using Multiple TexturesChapter 9 Shader Programming with the High-Level Shader LanguageChapter 10 More Advanced Shader EffectsChapter 11 Working with FilesChapter 12 Using Mesh Files2

Window Game Programming Foundation

A game programmer mainly needs to know three topics for Windows programming:

• How to create a window• How to use the window message procedure• How to use resources (icons, menus, shortcuts, and so on) with the help of Visual C/C++

3

How Windows Interacts with Your Game A Windows-based game has to wait until it is sent a

message by Windows. This message is passed to your program through a spe-

cial function called by Windows. Once a message is received, your program is expected

to take an appropriate action. Messages arrive randomly, so every Windows game has

to check for new messages constantly.

4

The Components of a Window

5

A Window Skeleton Develop a Windows application that provides the neces-

sary features common to all Windows applications. This Windows program will contain two functions:• WinMain()• A window procedure such as WndProc()

6

WinMain() function 1. Define a window class. 2. Register that class with Windows. 3. Create a window of that class. 4. Display the window. 5. Begin running the message loop.

7

Demo: Create a Skeleton Windows Application

8

Step 5: Create the Message LoopBOOL bGotMsg;MSG msg;While(WM_QUIT != msg.message){ if (m_bActive) bGotMsg = PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE); else bGotMsg = GetMessage(&msg, NULL, 0U, 0U); ...

9

Install DirectX Sdk June 2008 Extract ‘DxSdk_Jun2008.rar’ and install DirectX9 Sdk. Set destination folder as ‘C:/DxSdk9_200806/’

10

Additional include directories:

11

Additional library directories:

12

DxSdk: Tutorials/Tut01_CreateDevice#include <d3d9.h>#pragma warning( disable : 4996 ) // disable deprecated warning #include <strsafe.h>#pragma warning( default : 4996 )

//-----------------------------------------------------------------------------// Global variables//-----------------------------------------------------------------------------LPDIRECT3D9 g_pD3D = NULL; // Used to create the D3DDeviceLPDIRECT3DDEVICE9 g_pd3dDevice = NULL; // Our rendering device

13

HRESULT InitD3D( HWND hWnd ){ // Create the D3D object, which is needed to create the D3DDevice. if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) ) return E_FAIL;

D3DPRESENT_PARAMETERS d3dpp; ZeroMemory( &d3dpp, sizeof( d3dpp ) ); d3dpp.Windowed = TRUE; d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;

if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &g_pd3dDevice ) ) ) { return E_FAIL; }

// Device state would normally be set here

return S_OK;}

14

VOID Cleanup(){ if( g_pd3dDevice != NULL ) g_pd3dDevice->Release();

if( g_pD3D != NULL ) g_pD3D->Release();}

15

VOID Render(){ if( NULL == g_pd3dDevice ) return;

// Clear the backbuffer to a blue color g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB( 0, 0, 255 ), 1.0f, 0 );

// Begin the scene if( SUCCEEDED( g_pd3dDevice->BeginScene() ) ) { // Rendering of scene objects can happen here

// End the scene g_pd3dDevice->EndScene(); }

// Present the backbuffer contents to the display g_pd3dDevice->Present( NULL, NULL, NULL, NULL );}

16

LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam ){ switch( msg ) { case WM_DESTROY: Cleanup(); PostQuitMessage( 0 ); return 0;

case WM_PAINT: Render(); ValidateRect( hWnd, NULL ); return 0; }

return DefWindowProc( hWnd, msg, wParam, lParam );}

17

INT WINAPI wWinMain( HINSTANCE hInst, HINSTANCE, LPWSTR, INT ){ UNREFERENCED_PARAMETER( hInst );

// Register the window class WNDCLASSEX wc = { sizeof( WNDCLASSEX ), CS_CLASSDC, MsgProc, 0L, 0L, GetModuleHandle( NULL ), NULL, NULL, NULL, NULL, L"D3D Tutorial", NULL }; RegisterClassEx( &wc );

// Create the application's window HWND hWnd = CreateWindow( L"D3D Tutorial", L"D3D Tutorial 01: CreateDevice", WS_OVERLAPPEDWINDOW, 100, 100, 300, 300, NULL, NULL, wc.hInstance, NULL );

18

INT WINAPI wWinMain( HINSTANCE hInst, HINSTANCE, LPWSTR, INT ){… // Initialize Direct3D if( SUCCEEDED( InitD3D( hWnd ) ) ) { // Show the window ShowWindow( hWnd, SW_SHOWDEFAULT ); UpdateWindow( hWnd );

// Enter the message loop MSG msg; while( GetMessage( &msg, NULL, 0, 0 ) ) { TranslateMessage( &msg ); DispatchMessage( &msg ); } }

UnregisterClass( L"D3D Tutorial", wc.hInstance ); return 0;}19

Window Resources Resource Files *.rc(resource script) file

20

Resource View

21

Practice: Change clear color Add a feature:

– Terminate the program when 'Esc' is pressed.– Modify the message loop with PeekMessage() and call Render()

in message loop.– Modify the Render() function that the Clear color changes from

(0,0,0) to (0,0,255) exactly in 1 second.

22

Practice: Draw a linestruct D3DTLVERTEX{ float x, y, z, rhw; DWORD color;};

void DrawLine(IDirect3DDevice9* m_pD3Ddev, float X, float Y, float X2, float Y2, float Width, D3DCOLOR Color){ D3DTLVERTEX qV[2] = { { (float)X, (float)Y, 0.0f, 1.0f, Color }, { (float)X2, (float)Y2, 0.0f, 1.0f, Color }, }; const DWORD D3DFVF_TL = D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1; m_pD3Ddev->SetFVF(D3DFVF_TL); m_pD3Ddev->SetTexture(0, NULL); m_pD3Ddev->DrawPrimitiveUP(D3DPT_LINELIST, 1, qV, sizeof(D3DTLVERTEX));}

23

References

24

top related