direct3d workshop november 17, 2005 workshop by geoff cagle presented by players 2 professionals

30
Direct3D Direct3D Workshop Workshop November 17, 2005 November 17, 2005 Workshop by Geoff Cagle Workshop by Geoff Cagle Presented by Players 2 Professionals Presented by Players 2 Professionals

Upload: russell-barker

Post on 13-Jan-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Direct3D Workshop November 17, 2005 Workshop by Geoff Cagle Presented by Players 2 Professionals

Direct3DDirect3DWorkshopWorkshop

November 17, 2005November 17, 2005

Workshop by Geoff CagleWorkshop by Geoff Cagle

Presented by Players 2 ProfessionalsPresented by Players 2 Professionals

Page 2: Direct3D Workshop November 17, 2005 Workshop by Geoff Cagle Presented by Players 2 Professionals

Workshop BreakdownWorkshop Breakdown

►Part 1: Graphics PipelinePart 1: Graphics Pipeline►Part 2: Projection SpacePart 2: Projection Space►Part 3: Windows ProgrammingPart 3: Windows Programming► IntermissionIntermission►Part 4: Setting Up Direct3DPart 4: Setting Up Direct3D►Part 5: Using Direct3DPart 5: Using Direct3D

Page 3: Direct3D Workshop November 17, 2005 Workshop by Geoff Cagle Presented by Players 2 Professionals

Notes and Code SamplesNotes and Code Samples

►You can download these notes and all You can download these notes and all the code samples from the code samples from http://p2pclub.eng.usf.edu/direct3d/http://p2pclub.eng.usf.edu/direct3d/

Page 4: Direct3D Workshop November 17, 2005 Workshop by Geoff Cagle Presented by Players 2 Professionals

Part 3Part 3Windows Windows

ProgrammingProgramming

Page 5: Direct3D Workshop November 17, 2005 Workshop by Geoff Cagle Presented by Players 2 Professionals

code1.cppcode1.cpp

►This part of the workshop is basically a This part of the workshop is basically a big explanation for code1.cpp which big explanation for code1.cpp which creates a window.creates a window.

►You may find it helpful to look at You may find it helpful to look at code1.cpp as you go through the code1.cpp as you go through the slides.slides.

Page 6: Direct3D Workshop November 17, 2005 Workshop by Geoff Cagle Presented by Players 2 Professionals

What is a “window?”What is a “window?”

►The most obvious windows are the The most obvious windows are the application windows on your desktop.application windows on your desktop.

►The term “window” also refers to any The term “window” also refers to any child window or control (such as a child window or control (such as a button) within a window.button) within a window.

►Windows are organized in a hierarchy.Windows are organized in a hierarchy.

Page 7: Direct3D Workshop November 17, 2005 Workshop by Geoff Cagle Presented by Players 2 Professionals

Example - CalculatorExample - Calculator

Page 8: Direct3D Workshop November 17, 2005 Workshop by Geoff Cagle Presented by Players 2 Professionals

Event Driven ProgrammingEvent Driven Programming

►Windows programming is Windows programming is event drivenevent driven..►An event driven program sits and waits An event driven program sits and waits

for an event to process.for an event to process.►Events include moving or sizing a Events include moving or sizing a

window, clicking the mouse button over window, clicking the mouse button over a window, or typing keys on the a window, or typing keys on the keyboard.keyboard.

►Windows programs are notified of Windows programs are notified of events by messages sent from the OS.events by messages sent from the OS.

Page 9: Direct3D Workshop November 17, 2005 Workshop by Geoff Cagle Presented by Players 2 Professionals

Win32 APIWin32 API

►We will use the Win32 Platform API to We will use the Win32 Platform API to write a windows program.write a windows program. Typically more tedious, but we only need Typically more tedious, but we only need

to create one window.to create one window. Faster and Smaller executable.Faster and Smaller executable. Written in C.Written in C.

►Other options include:Other options include: MFC, Qt, wxWindows, SDL, .NET FormsMFC, Qt, wxWindows, SDL, .NET Forms

Page 10: Direct3D Workshop November 17, 2005 Workshop by Geoff Cagle Presented by Players 2 Professionals

WinMainWinMain

► Instead of Instead of mainmain, windows programs use , windows programs use WinMainWinMain

► WinMainWinMain has different parameters: has different parameters: HINSTANCE hInstanceHINSTANCE hInstance – a handle to the program – a handle to the program HINSTANCE hPrevInstanceHINSTANCE hPrevInstance – no longer used. – no longer used. LPSTR lpCmdLineLPSTR lpCmdLine – – unparsedunparsed command line. command line. Doesn’tDoesn’t include executable’s filename. include executable’s filename.

int nCmdShowint nCmdShow – Specifies how the window should – Specifies how the window should be initially shown (ie. Minimized, Maximized, be initially shown (ie. Minimized, Maximized, Normal)Normal)

► WinMainWinMain must have a must have a WINAPIWINAPI modifier. modifier. int WINAPI WinMain (…)int WINAPI WinMain (…)

► WinMainWinMain still returns an integer like main. still returns an integer like main.

Page 11: Direct3D Workshop November 17, 2005 Workshop by Geoff Cagle Presented by Players 2 Professionals

HeadersHeaders

►To use the Win32 API we need to To use the Win32 API we need to #include windows.h#include windows.h

► If we #define If we #define WIN32_LEAN_AND_MEANWIN32_LEAN_AND_MEAN before #including windows.h, the before #including windows.h, the compiler will skip compiling the more compiler will skip compiling the more rarely used windows code.rarely used windows code.

Page 12: Direct3D Workshop November 17, 2005 Workshop by Geoff Cagle Presented by Players 2 Professionals

Windows Program OutlineWindows Program Outline

►Create Window ClassCreate Window Class►Create WindowCreate Window►Show the WindowShow the Window►Enter Message LoopEnter Message Loop

Page 13: Direct3D Workshop November 17, 2005 Workshop by Geoff Cagle Presented by Players 2 Professionals

Window ClassesWindow Classes

► A A window classwindow class acts as a template for window acts as a template for window creation.creation.

► Window classes are Window classes are notnot classes in the C++ classes in the C++ sense.sense.

► We can set such properties as:We can set such properties as: Name of the window class – Name of the window class – importantimportant for identifying for identifying

it later!it later! Window style – How it looks and its default behaviors.Window style – How it looks and its default behaviors. Window procedure – a pointer to function that Window procedure – a pointer to function that

handles messages for a window.handles messages for a window. Default cursor, icon, and menu.Default cursor, icon, and menu.

Page 14: Direct3D Workshop November 17, 2005 Workshop by Geoff Cagle Presented by Players 2 Professionals

Creating a Window ClassCreating a Window Class

►Fill out a Fill out a WNDCLASSWNDCLASS or or WNDCLASSEXWNDCLASSEX structure.structure.

►Use the structure to register the class Use the structure to register the class with a call to with a call to RegisterClassRegisterClass or or RegisterClassExRegisterClassEx, respectively., respectively.

Page 15: Direct3D Workshop November 17, 2005 Workshop by Geoff Cagle Presented by Players 2 Professionals

Create the WindowCreate the Window

► Create a Window with a call to Create a Window with a call to CreateWindowCreateWindow or or CreateWindowExCreateWindowEx..

► Some of the parameters you will need to Some of the parameters you will need to enter include:enter include: The name of a window class. The name of a window class. The name of the window.The name of the window. A handle to a parent window. (A handle to a parent window. (NULLNULL if no parent) if no parent) Initial size and position of the window.Initial size and position of the window.

► CreateWindowCreateWindow and and CreateWindowExCreateWindowEx will will return a return a window handlewindow handle ( (HWNDHWND) if successful ) if successful or or NULLNULL if failed. if failed.

Page 16: Direct3D Workshop November 17, 2005 Workshop by Geoff Cagle Presented by Players 2 Professionals

Showing the WindowShowing the Window

► When we create a window it starts off hidden.When we create a window it starts off hidden.► To show the window you must call To show the window you must call ShowWindow (hWnd, nCmdShow)ShowWindow (hWnd, nCmdShow) hWndhWnd – returned from – returned from CreateWindowCreateWindow//ExEx.. nCmdShownCmdShow – parameter from – parameter from WinMainWinMain.. This will send a message to the window telling it to This will send a message to the window telling it to

show.show.

► Call Call UpdateWindow (hWnd)UpdateWindow (hWnd) to force the to force the window to process the show message window to process the show message immediately. immediately.

Page 17: Direct3D Workshop November 17, 2005 Workshop by Geoff Cagle Presented by Players 2 Professionals

Message LoopsMessage Loops

► A A message loopmessage loop is needed to forward is needed to forward messages to their windows.messages to their windows.

► There are different ways to make a message There are different ways to make a message loop but here is the most common one:loop but here is the most common one:

MSG msg = {0};

// returns FALSE when message is WM_QUITwhile (GetMessage (&msg, NULL, 0, 0)){ TranslateMessage (&msg); // translate keyboard messages. DispatchMessage (&msg); // send message to its window.}

Page 18: Direct3D Workshop November 17, 2005 Workshop by Geoff Cagle Presented by Players 2 Professionals

Message Loop (Cont’d)Message Loop (Cont’d)

► The loop uses:The loop uses: A A MSGMSG structure – Holds message information. structure – Holds message information. GetMessageGetMessage – Retrieves the next message. Waits – Retrieves the next message. Waits

for a message if the message queue is empty.for a message if the message queue is empty. TranslateMessageTranslateMessage – Translate some keyboard – Translate some keyboard

messages.messages. DispatchMessageDispatchMessage – Dispatches the message to – Dispatches the message to

the appropriate windows procedure.the appropriate windows procedure.

Page 19: Direct3D Workshop November 17, 2005 Workshop by Geoff Cagle Presented by Players 2 Professionals

Windows ProcedureWindows Procedure

►Every window has an associate Every window has an associate procedure for handling messages.procedure for handling messages.

►Windows procedures have the Windows procedures have the following format:following format:

LRESULT CALLBACK ProcName (HWND LRESULT CALLBACK ProcName (HWND hwnd, UINT msg, WPARAM wp, LPARAM hwnd, UINT msg, WPARAM wp, LPARAM lp);lp);

Page 20: Direct3D Workshop November 17, 2005 Workshop by Geoff Cagle Presented by Players 2 Professionals

Window Procedure ExampleWindow Procedure Example

►Here is an example procedure:Here is an example procedure:LRESULT CALLBACK WndProc (HWND hwnd, UINT msg, WPARAM wp, LPARAM lp){

switch (msg){case WM_DESTROY:

PostQuitMessage (0);return 0;

}

return DefWindowProc (hwnd, msg, wp, lp);}

Page 21: Direct3D Workshop November 17, 2005 Workshop by Geoff Cagle Presented by Players 2 Professionals

Window Procedure Window Procedure ParametersParameters

►The procedure has 4 parameters:The procedure has 4 parameters: HWND hwndHWND hwnd – handle to the window – handle to the window

receiving the message. This is useful when receiving the message. This is useful when multiple windows share the same multiple windows share the same procedure. You can think of it sort of like a procedure. You can think of it sort of like a thisthis pointer. pointer.

UINT msgUINT msg – The message. – The message. WPARAM wp WPARAM wp andand LPARAM lp LPARAM lp – These are – These are

parameters to the message and may parameters to the message and may represent different things.represent different things.

Page 22: Direct3D Workshop November 17, 2005 Workshop by Geoff Cagle Presented by Players 2 Professionals

Window Procedure ReturnWindow Procedure Return

►When the procedure completely When the procedure completely handles a message, it should return handles a message, it should return 00..

►The procedure should let the OS The procedure should let the OS handle all other messages by calling handle all other messages by calling DefWindowProcDefWindowProc and then return its and then return its result.result.

Page 23: Direct3D Workshop November 17, 2005 Workshop by Geoff Cagle Presented by Players 2 Professionals

WM_DESTROY and WM_QUITWM_DESTROY and WM_QUIT

►For this workshop we only want to For this workshop we only want to handle one message: handle one message: WM_DESTROYWM_DESTROY.. This message is called when a window is This message is called when a window is

closed.closed. Note that just because the window is Note that just because the window is

destroyed, the program will still run.destroyed, the program will still run. To end the program we send To end the program we send WM_QUITWM_QUIT by by

calling calling PostQuitMessagePostQuitMessage..

Page 24: Direct3D Workshop November 17, 2005 Workshop by Geoff Cagle Presented by Players 2 Professionals

WM_*WM_*

► Other messages you may want to handle Other messages you may want to handle include:include: WM_CREATEWM_CREATE WM_SIZEWM_SIZE WM_LBUTTONDOWNWM_LBUTTONDOWN WM_LBUTTONUPWM_LBUTTONUP WM_MOUSEMOVEWM_MOUSEMOVE WM_KEYDOWNWM_KEYDOWN WM_KEYUPWM_KEYUP

► Check MSDN literature for information about Check MSDN literature for information about these messages: these messages: http://http://msdn.microsoft.commsdn.microsoft.com//

Page 25: Direct3D Workshop November 17, 2005 Workshop by Geoff Cagle Presented by Players 2 Professionals

Code1.cppCode1.cpp

►This completes Part 3 of the workshop.This completes Part 3 of the workshop.►To see how this all works check out To see how this all works check out

code1.cpp, included with this code1.cpp, included with this workshop.workshop.

Page 26: Direct3D Workshop November 17, 2005 Workshop by Geoff Cagle Presented by Players 2 Professionals

Make sure you select Win32 Make sure you select Win32 Project.Project.

Page 27: Direct3D Workshop November 17, 2005 Workshop by Geoff Cagle Presented by Players 2 Professionals

Make sure you create an empty Make sure you create an empty project.project.

Page 28: Direct3D Workshop November 17, 2005 Workshop by Geoff Cagle Presented by Players 2 Professionals

Code1.exe in ActionCode1.exe in Action

Page 29: Direct3D Workshop November 17, 2005 Workshop by Geoff Cagle Presented by Players 2 Professionals

ReferencesReferences

1.1. Programming Windows 5Programming Windows 5thth Edition Edition by by Charles Petzold. The definitive guide Charles Petzold. The definitive guide to Win32 API Programming.to Win32 API Programming.

2.2. MSDN’s Win32 API Reference: MSDN’s Win32 API Reference: http://msdn.microsoft.com/library/defhttp://msdn.microsoft.com/library/default.asp?url=/library/en-us/ault.asp?url=/library/en-us/winprog/winprog/windows_api_start_pwinprog/winprog/windows_api_start_page.aspage.asp

Page 30: Direct3D Workshop November 17, 2005 Workshop by Geoff Cagle Presented by Players 2 Professionals