Title: Windows and DirectX Programming
1Windows and DirectX Programming
- Shyh-Kang Jeng
- Department of Electrical Engineering/
- Graduate Institute of Communication Engineering
- National Taiwan University
2Reference
- C. Walnum, Teach Yourself Game Programming with
DirectX in 21 Days, Sams Publishing, 2003 - J. Adams ?, ????, ???????, 2D/3D RPG
????????????DirectX, ????, 2003
3Writing a Window Application Program
- File New Projects Win32 Application
- An empty project
- Edit the program
- Compile the program
- Build the program
- Execute the program
4A Complete WinMain Program (1/7)
5A Complete WinMain Program (2/7)
- // BasicWindowsApp\WinMain.cpp
- include ltwindows.hgt
- // Function prototypes
- LRESULT WINAPI WndProc( HWND hWnd, UINT msg,
- WPARAM wParam, LPARAM lParam )
- void RegisterWindowClass( HINSTANCE hInstance )
- void CreateAppWindow( HINSTANCE hInstance )
- WPARAM StartMessageLoop()
- // Global variables
- HWND g_hWnd
6A Complete WinMain Program (3/7)
- INT WINAPI WinMain( HINSTANCE hInstance,
HINSTANCE, LPSTR, INT ) - RegisterWindowClass( hInstance )
- CreateAppWindow( hInstance )
- ShowWindow( g_hWnd, SW_SHOWDEFAULT )
- UpdateWindow( g_hWnd )
- INT result StartMessageLoop()
- return result
- // WinMain
7A Complete WinMain Program (4/7)
- LRESULT WINAPI WndProc( HWND hWnd, UINT msg,
WPARAM wParam, LPARAM lParam ) - switch( msg )
- case WM_CREATE
- return 0
- case WM_DESTROY
- PostQuitMessage( 0 )
- return 0
- case WM_PAINT
- ValidateRect( g_hWnd, NULL )
- return 0
-
- return DefWindowProc( hWnd, msg, wParam, lParam
) - // WndProc
8A Complete WinMain Program (5/7)
- void RegisterWindowClass( HINSTANCE hInstance )
- WNDCLASSEX wc
- wc.cbSize sizeof( WNDCLASSEX )
- wc.style CS_HREDRAW CS_VREDRAW CS_OWNDC
- wc.lpfnWndProc WndProc
- wc.cbClsExtra 0
- wc.cbWndExtra 0
- wc.hInstance hInstance
- wc.hIcon LoadIcon( NULL, IDI_APPLICATION )
- wc.hCursor (HCURSOR) LoadCursor( NULL,
IDC_ARROW ) - wc.hbrBackground (HBRUSH) GetStockObject(
WHITE_BRUSH ) - wc.lpszMenuName NULL
- wc.lpszClassName "WinApp"
- wc.hIconSm NULL
- RegisterClassEx( wc )
- // RegisterWindowClass
9A Complete WinMain Program (6/7)
- void CreateAppWindow( HINSTANCE hInstance )
- g_hWnd CreateWindowEx(
- NULL,
- "WinApp",
- "Basic Window Application",
- WS_OVERLAPPEDWINDOW,
- 100,
- 100,
- 648,
- 514,
- GetDesktopWindow(),
- NULL,
- hInstance,
- NULL )
- // CreateAppWindow
10A Complete WinMain Program (7/7)
- WPARAM StartMessageLoop()
- MSG msg
- while( 1 )
- if( PeekMessage(msg, NULL, 0, 0,
- PM_REMOVE) )
- if( msg.message WM_QUIT ) break
- TranslateMessage( msg )
- DispatchMessage( msg )
- else
- // use idle time here
-
-
- return msg.wParam
- // StartMessageLoop
11Win32 SDK
- Windows 32-bit Software Development Kit
- Provides API (Application Program Interfaces)
functions for Windows programming
12Hungarian Notations
13Common Win32 Data Types (1/2)
14Common Win32 Data Types (2/2)
15WinMain Function
- int WINAPI WinMain(
- HINSTANCE hInstance,
- // Instance handle of application
- HINSTANCE hPrevInstance, // Unused
- LPSTR lpCmdLine,
- // Command line options (if any)
- int nCmdShow ) // Show window flag
16Structure WNDCLASSEX
- typedef struct _WNDCLASSEX
- UINT cbSize // Size of this structure
- UINT style // style of window
- WNDPROC lpfnWndProc// window message procedure
- int cbClsExtra // 0
- int cbWndExtra // 0
- HANDLE hInstance
- // Instance handle from WinMain
- HICON hIcon // Handle to application icon
- HCURSOR hCursor
- // Handle to application cursor
- HBRUSH hbrBackground
- // Handle to background brush
- LPCTSTR lpszMenuName
- // Handle to application menu
- LPCTSTR lpszClassName // Class name
- HICON hIconSm
- // Handle to small application icon
- WNDCLASSEX
17Style of Window
- CS_CLASSDC
- Share graphics resource with other windows of the
same class - CS_HREDRAW
- Redraw the window when the horizontal size change
- CS_VREDRAW
- Redraw the window when the vertical size change
18Registering and Unregistering a Window
- Register a window
- ATOM RegisterClassEx(
- const WNDCLASSEX lpwcx)
- Unregister a window
- BOOL UnregisterClass(
- LPCTSTR lpClassName,
- // Class name to unregister
- HINSTANCE hInstance )
- // Instance handle
19Function CreateWindow
- HWND CreateWindow(
- LPCTSTR lpClassName, // Class to use
- LPCTSTR lpWindowName, // Window name (caption)
- DWORD dwStyle, // Style of window
- int x, // x coordinate of window
- int y, // y coordinate of window
- int nWidth, // width of window
- int nHeight, // height of window
- HWND hWndParent, // NULL
- HMENU hMenu, // NULL (or menu handle)
- HANDLE hInstance,
- // instance handle from WinMain
- LPVOID lpParam) // NULL
20dwStyle Flag (1/2)
21dwStyle Flag (2/2)
22Events and Message
- Events
- Generated by users, other programs, or the
system - The system will pass message to the window
program when events occur
23Dealing with Message
Window Message Queue
Application Message Queue
PeekMessage(. . .) Or GetMessage(. . . )
WM_COMMAND WM_MOUSEMOVE WM_KEYDOWN
WindowProc(. . .)
24Window Procedure
- LRESULT CALLBACK WndProc(
- HWND hWnd, // window handle to which
- // the message belongs
- UINT uMsg, // message
- WPARAM wParam, // parameter related to
- // the message
- LPARAM lParam)// parameter related to
- // the message
25Pass the Message to the Default Message Handler
- LRESULT DefWindowProc(
- HWND hWnd,
- UINT uMsg,
- WPARAM wParam,
- LPARAM lParam )
26Standard Windows Message (1/2)
27Standard Windows Message (2/2)
28Message Box
- int MessageBox(
- HWND hWnd // Parent window or NULL for
- // none
- LPCTSTR lpText // Message to display
- // in box
- LPCTSTR lpCaption // Caption of window
- // to use
- UINT uType) // Buttons and icon
- // settings
29Message Box Demo (1/2)
30Message Box Demo (2/2)
- // MessageBoxDemo\WinMain.cpp
- // skj 10/25/2003
- include ltwindows.hgt
- INT WINAPI WinMain( HINSTANCE hInstance,
- HINSTANCE, LPSTR, int )
- MessageBox( NULL, "Test", "Message Box Demo",
- MB_OK )
- return 0
31DirectX SDK
- A set of low level APIs for game and multimedia
programs - Helps the programmers to communicate directly
with computer hardware
32Component Object Model
- COM
- Component Object Model
- DirectX is composed of COM objects
33Downloading DirectX
- ftp//ftp.ntu.edu.tw/Windows/MsDownload/
- http//msdn.microsoft.com/directx
- Or use search engines to find places to download
34Major Components (DirectX 8)
- DirectX Graphics (combines DirectDraw and
Direct3D) - DirectX Audio (combines DirectSound and
DirectMusic) - DirectPlay (Internet)
- DirectInput (Reads the keyboard, mouse, and
joystick)
35Setting DirectX Directories in Visual C
- Tools Options
- Directories
- Show directories for Include files
- New (button) \Include
- Show directories for Library files
- New (button) \lib
36Setting DirectX Directories
37Add Library Files
- Project Settings . . . Link
- Category General
- Object/Library modules
38Add Library Files
39DirectX Components, Include Files and Library
Files
40Old DirectDraw/Direct3D Architecture
41New DirectDraw/Direct3D Architecture
2D APP
3D APP
Direct3D
DirectDraw
Video Hardware
42First Direct3D Program (1/10)
- // BasicDirect3DApp\WinMain.cpp
- include ltwindows.hgt
- include ltd3d8.hgt
- // Function prototypes
- LRESULT WINAPI WndProc( HWND hWnd, UINT msg,
- WPARAM wParam, LPARAM lParam )
- void RegisterWindowClass( HINSTANCE hInstance )
- void CreateAppWindow( HINSTANCE hInstance )
- WPARAM StartMessageLoop()
- HRESULT InitFullScreenDirect3D()
- void Render()
- void CleanUpDirect3D()
- // Global variables
- HWND g_hWnd
- IDirect3D8 g_pDirect3D NULL
- IDirect3DDevice8 g_pDirect3DDevice NULL
43First Direct3D Program (2/10)
- INT WINAPI WinMain( HINSTANCE hInstance,
HINSTANCE, LPSTR, INT ) - RegisterWindowClass( hInstance )
- CreateAppWindow( hInstance )
- ShowWindow( g_hWnd, SW_SHOWDEFAULT )
- UpdateWindow( g_hWnd )
- HRESULT hResult InitFullScreenDirect3D()
- if( SUCCEEDED(hResult) )
- WPARAM result StartMessageLoop()
- CleanUpDirect3D()
- return 0
- // WinMain
44First Direct3D Program (3/10)
- LRESULT WINAPI WndProc( HWND hWnd, UINT msg,
WPARAM wParam, - LPARAM lParam )
- switch( msg )
- case WM_CREATE
- return 0
- case WM_DESTROY
- PostQuitMessage( 0 )
- return 0
- case WM_PAINT
- ValidateRect( g_hWnd, NULL )
- return 0
-
45First Direct3D Program (4/10)
- case WM_KEYDOWN
- switch(wParam)
- case VK_ESCAPE
- PostQuitMessage(WM_QUIT)
- break
-
-
- return DefWindowProc( hWnd, msg, wParam, lParam
) - // WndProc
46First Direct3D Program (5/10)
- void RegisterWindowClass( HINSTANCE hInstance )
- WNDCLASSEX wc
- wc.cbSize sizeof( WNDCLASSEX )
- wc.style CS_HREDRAW CS_VREDRAW CS_OWNDC
- wc.lpfnWndProc WndProc
- wc.cbClsExtra 0
- wc.cbWndExtra 0
- wc.hInstance hInstance
- wc.hIcon LoadIcon( NULL, IDI_APPLICATION )
- wc.hCursor (HCURSOR) LoadCursor( NULL,
IDC_ARROW ) - wc.hbrBackground (HBRUSH) GetStockObject(
WHITE_BRUSH ) - wc.lpszMenuName NULL
- wc.lpszClassName "Direct3DApp"
- wc.hIconSm NULL
- RegisterClassEx( wc )
- // RegisterWindowClass
47First Direct3D Program (6/10)
- void CreateAppWindow( HINSTANCE hInstance )
- g_hWnd CreateWindowEx(
- NULL,
- "Direct3DApp",
- "Basic Direct3D Application",
- WS_OVERLAPPEDWINDOW,
- 100,
- 100,
- 648,
- 514,
- GetDesktopWindow(),
- NULL,
- hInstance,
- NULL )
- // CreateAppWindow
48First Direct3D Program (7/10)
- WPARAM StartMessageLoop()
- MSG msg
- while( 1 )
- if( PeekMessage(msg, NULL, 0, 0, PM_REMOVE) )
- if( msg.message WM_QUIT ) break
- TranslateMessage( msg )
- DispatchMessage( msg )
- else
- // use idle time here
- Render()
-
-
- return msg.wParam
- // StartMessageLoop
49First Direct3D Program (8/10)
- HRESULT InitFullScreenDirect3D()
- g_pDirect3D Direct3DCreate8( D3D_SDK_VERSION
) - if( g_pDirect3D NULL ) return E_FAIL
- HRESULT hResult g_pDirect3D-gtCheckDeviceType
(D3DADAPTER_DEFAULT, - D3DDEVTYPE_REF, D3DFMT_X8R8G8B8,
D3DFMT_X8R8G8B8, FALSE) - if (hResult ! D3D_OK)
-
- MessageBox(g_hWnd,
- "Sorry. This program won't?\nrun on your
system.", - "DirectX Error", MB_OK)
- return E_FAIL
-
-
50First Direct3D Program (9/10)
- D3DPRESENT_PARAMETERS D3DPresentParams
- ZeroMemory( D3DPresentParams,
sizeof(D3DPRESENT_PARAMETERS) ) - D3DPresentParams.Windowed FALSE
- D3DPresentParams.BackBufferCount 1
- D3DPresentParams.BackBufferWidth 800
- D3DPresentParams.BackBufferHeight 600
- D3DPresentParams.BackBufferFormat
D3DFMT_X8R8G8B8 - D3DPresentParams.SwapEffect D3DSWAPEFFECT_DISCA
RD - D3DPresentParams.hDeviceWindow g_hWnd
- hResult g_pDirect3D-gt
- CreateDevice( D3DADAPTER_DEFAULT,
- D3DDEVTYPE_HAL, g_hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING, - D3DPresentParams, g_pDirect3DDevice )
51First Direct3D Program (10/10)
- if( FAILED(hResult) ) return E_FAIL
- return D3D_OK
- // InitFullScreenDirect3D
- void CleanUpDirect3D()
- if( g_pDirect3DDevice ) g_pDirect3DDevice-gtReleas
e() - if( g_pDirect3D ) g_pDirect3D-gtRelease()
- // CleanUpDirect3D
- void Render()
- g_pDirect3DDevice-gtClear(0, 0,
D3DCLEAR_TARGET, - D3DCOLOR_XRGB(0,0,255), 0, 0)
- g_pDirect3DDevice-gtPresent(NULL, NULL, NULL,
NULL) -
52Surfaces
- An area of memory in which you can store and
manipulate graphical information - The primary surface (front buffer)
- Back-buffer surfaces
- Image surfaces
- Texture surfaces
53Surfaces
Video Memory
Display
Primary Surface (Front Buffer)
Back Buffer
54Creating a Surface for Bitmap
- IDirect3DSurface8 g_pBitmapSurface NULL
- hResult g_pDirect3DDevice-gt
- CreateImageSurface( 640, 480, D3DFMT_X8R8G8B8,
g_pBitmapSurface ) - if( FAILED( hResult ) )
- // Handle error here
55Loading Bitmap into Surface
- HRESULT hResult D3DXLoadSurfaceFromFile(
g_pBitmapSurface, NULL, NULL, image.bmp, NULL,
D3DX_DEFAULT, 0, NULL ) - if( FAILED( hResult ) )
- DXTRACE_ERROR( Couldnt load bitmap., hResult
)
56Accessing Back Buffer
- IDirect3DSurface8 pBackBuffer NULL
- HRESULT hResult g_pDirect3DDevice-gt
- GetBackBuffer( 0, D3DBACKBUFFER_TYPE_MONO,
pBackBuffer ) - if( FAILED( hResult ) )
- // Handle error here
57Copying a Rectangle
- HRESULT hResult g_pDirect3DDevice-gt
- CopyRects( g_pBitmapSurface, NULL, 0,
pBackBuffer, NULL ) - if( FAILED( hResult ) )
- // Handle error here
58Presenting the BackBuffer
- g_pDirect3DDevice-gt
- Present(NULL, NULL, NULL, NULL)
59Demo Program SurfaceApp (1/16)
60Demo Program SurfaceApp (2/16)
- // SurfaceApp\WinMain.cpp
- include ltwindows.hgt
- include ltd3d8.hgt
- include ltd3dx8tex.hgt
- include ltdxerr8.hgt
- // Function prototypes.
- LRESULT WINAPI WndProc(HWND hWnd, UINT msg,
- WPARAM wParam, LPARAM lParam)
- void RegisterWindowClass(HINSTANCE hInstance)
- void CreateAppWindow(HINSTANCE hInstance)
- WPARAM StartMessageLoop()
- HRESULT InitFullScreenDirect3D()
- void Render()
- void CleanUpDirect3D()
61Demo Program SurfaceApp (3/16)
- // Global variables.
- HWND g_hWnd
- IDirect3D8 g_pDirect3D NULL
- IDirect3DDevice8 g_pDirect3DDevice NULL
- IDirect3DSurface8 g_pBitmapSurface NULL
- HRESULT g_hResult D3D_OK
- char g_szErrorMsg256
- INT WINAPI WinMain(HINSTANCE hInstance,
- HINSTANCE, LPSTR, INT)
- RegisterWindowClass(hInstance)
- CreateAppWindow(hInstance)
- ShowWindow(g_hWnd, SW_SHOWDEFAULT)
- UpdateWindow(g_hWnd)
62Demo Program SurfaceApp (4/16)
- HRESULT hResult InitFullScreenDirect3D()
- if (SUCCEEDED(hResult))
- WPARAM result StartMessageLoop()
- CleanUpDirect3D()
- CloseWindow(g_hWnd)
- if (g_hResult ! D3D_OK)
- DXTRACE_ERR(g_szErrorMsg, g_hResult)
- return 0
-
63Demo Program SurfaceApp (5/16)
- LRESULT WINAPI WndProc(HWND hWnd, UINT msg,
- WPARAM wParam, LPARAM lParam)
- switch(msg)
- case WM_CREATE
- return 0
- case WM_DESTROY
- PostQuitMessage( 0 )
- return 0
- case WM_PAINT
- ValidateRect(g_hWnd, NULL)
- return 0
64Demo Program SurfaceApp (6/16)
- case WM_KEYDOWN
- switch(wParam)
- case VK_ESCAPE
- PostQuitMessage(WM_QUIT)
- break
-
-
- return DefWindowProc(hWnd, msg, wParam,
- lParam)
-
65Demo Program SurfaceApp (7/16)
- void RegisterWindowClass(HINSTANCE hInstance)
- WNDCLASSEX wc
- wc.cbSize sizeof(WNDCLASSEX)
- wc.style CS_HREDRAW CS_VREDRAW
CS_OWNDC - wc.lpfnWndProc WndProc
- wc.cbClsExtra 0
- wc.cbWndExtra 0
- wc.hInstance hInstance
- wc.hIcon LoadIcon(NULL, IDI_APPLICATION)
- wc.hCursor (HCURSOR)LoadCursor(NULL,
- IDC_ARROW)
- wc.hbrBackground
- (HBRUSH)GetStockObject(WHITE_BRUSH)
- wc.lpszMenuName NULL
- wc.lpszClassName "SurfaceApp"
- wc.hIconSm NULL
- RegisterClassEx(wc)
66Demo Program SurfaceApp (8/16)
- void CreateAppWindow(HINSTANCE hInstance)
- g_hWnd CreateWindowEx(
- NULL,
- "SurfaceApp",
- "Direct3D Surface Application",
- WS_OVERLAPPEDWINDOW,
- 100,
- 100,
- 648,
- 514,
- GetDesktopWindow(),
- NULL,
- hInstance,
- NULL)
67Demo Program SurfaceApp (9/16)
- WPARAM StartMessageLoop()
- MSG msg
- while(1)
- if (PeekMessage(msg, NULL, 0, 0,
- PM_REMOVE))
- if (msg.message WM_QUIT) break
- TranslateMessage(msg)
- DispatchMessage(msg)
-
- else
- // Use idle time here.
- Render()
-
-
- return msg.wParam
68Demo Program SurfaceApp (10/16)
- HRESULT InitFullScreenDirect3D()
- g_pDirect3D
- Direct3DCreate8(D3D_SDK_VERSION)
- if (g_pDirect3D NULL)
- MessageBox(g_hWnd,
- "Couldn't create DirectX object.",
- "DirectX Error", MB_OK)
- return E_FAIL
-
- HRESULT hResult g_pDirect3D-gt
- CheckDeviceType(D3DADAPTER_DEFAULT,
- D3DDEVTYPE_HAL, D3DFMT_X8R8G8B8,
D3DFMT_X8R8G8B8, FALSE)
69Demo Program SurfaceApp (11/16)
- if (hResult ! D3D_OK)
- MessageBox(g_hWnd,
- "Sorry. This program won't \nrun on your
system.", - "DirectX Error", MB_OK)
- return E_FAIL
-
- D3DPRESENT_PARAMETERS D3DPresentParams
- ZeroMemory(D3DPresentParams,
- sizeof(D3DPRESENT_PARAMETERS))
- D3DPresentParams.Windowed FALSE
- D3DPresentParams.BackBufferCount 1
- D3DPresentParams.BackBufferWidth 640
- D3DPresentParams.BackBufferHeight 480
- D3DPresentParams.BackBufferFormat
D3DFMT_X8R8G8B8
70Demo Program SurfaceApp (12/16)
- D3DPresentParams.SwapEffect
D3DSWAPEFFECT_DISCARD - D3DPresentParams.hDeviceWindow g_hWnd
- hResult g_pDirect3D-gt
- CreateDevice(D3DADAPTER_DEFAULT,
- D3DDEVTYPE_HAL, g_hWnd, D3DCREATE_SOFTWARE_VE
RTEXPROCESSING, - D3DPresentParams, g_pDirect3DDevice)
- if (FAILED(hResult))
- MessageBox(g_hWnd,
- "Failed to create Direct3D device.",
- "DirectX Error", MB_OK)
- return E_FAIL
-
71Demo Program SurfaceApp (13/16)
- g_hResult g_pDirect3DDevice-gt
- CreateImageSurface(640, 480,
- D3DFMT_X8R8G8B8, g_pBitmapSurface)
- if (FAILED(g_hResult))
- strcpy(g_szErrorMsg,
- "Error creating bitmap surface.")
- PostQuitMessage(WM_QUIT)
-
- g_hResult D3DXLoadSurfaceFromFile(g_pBitmap
Surface, NULL, NULL, - "image.bmp", NULL, D3DX_DEFAULT, 0,
NULL) - if (FAILED(g_hResult))
- strcpy(g_szErrorMsg,
- "Couldn't load bitmap file.")
- PostQuitMessage(WM_QUIT)
-
- return D3D_OK
72Demo Program SurfaceApp (14/16)
- void CleanUpDirect3D()
- if (g_pBitmapSurface)
- g_pBitmapSurface-gtRelease()
- if (g_pDirect3DDevice)
- g_pDirect3DDevice-gtRelease()
- if (g_pDirect3D) g_pDirect3D-gtRelease()
-
73Demo Program SurfaceApp (15/16)
- void Render()
- IDirect3DSurface8 pBackBuffer NULL
- g_hResult g_pDirect3DDevice-gt
- GetBackBuffer(0,
- D3DBACKBUFFER_TYPE_MONO, pBackBuffer)
- if (FAILED(g_hResult))
- strcpy(g_szErrorMsg,
- "Error getting back buffer.")
- PostQuitMessage(WM_QUIT)
-
- g_hResult g_pDirect3DDevice-gt
- CopyRects(g_pBitmapSurface,
- NULL,0,pBackBuffer,NULL)
74Demo Program SurfaceApp (16/16)
- if (FAILED(g_hResult))
- strcpy(g_szErrorMsg,
- "Error copying image buffer.")
- PostQuitMessage(WM_QUIT)
-
- g_pDirect3DDevice-gt
- Present(NULL, NULL, NULL, NULL)
- if (pBackBuffer) pBackBuffer-gtRelease()