230 likes | 373 Views
CSE3AGT. Paul Taylor 2010. Stupid Conventions!. l = Long p = Pointer h = handle g = global wnd = Windows WM = Windows Message d3d = Direct3D hr = HRESULT (windows error message value). Some Short Examples. ID3D10Texture2D* pBackBuffer ; HWND g_hWnd = NULL; HRESULT hr;
E N D
CSE3AGT Paul Taylor 2010
Stupid Conventions! l = Long p = Pointer h = handle g = global wnd = Windows WM = Windows Message d3d = Direct3D hr = HRESULT (windows error message value)
Some Short Examples ID3D10Texture2D* pBackBuffer; HWND g_hWnd= NULL; HRESULT hr; hr = g_pSwapChain->GetBuffer(...) if( FAILED( hr) )
The Windows Message Pump • What do messages do? • Handles Window Messages (Maximise, Minimise, etc) • Handles basic Keyboard and Mouse commands • How do you send a message? • SendMessage() or • PostMessage()
What does a message look like? typedefstruct { HWND hwnd; // Handle to the procedure that receives the message. UINT message; // Specifies the message identifier. WPARAM wParam; // Specifies additional information about the message. LPARAM lParam; // Specifies additional information about the message. DWORD time; // time at which the message was posted. POINT pt; // Specifies the cursor position when the message was posted. } MSG, *PMSG; Declaration: MSG myMsg; MSDN: http://msdn.microsoft.com/en-us/library/ms644958%28VS.85%29.aspx
The WM_MOUSEMOVE Message UINT Message = WM_MOUSEMOVE wParam: MK_CONTROL The CTRL key is down. MK_LBUTTON The left mouse button is down. MK_MBUTTON The middle mouse button is down. MK_RBUTTON The right mouse button is down. MK_SHIFT The SHIFT key is down. lParam: The low-order word specifies the x-coordinate of the cursor. The coordinate is relative to the upper-left corner of the client area. The high-order word specifies the y-coordinate of the cursor. The coordinate is relative to the upper-left corner of the client area. xPos = GET_X_LPARAM(lParam); yPos = GET_Y_LPARAM(lParam);
The Basic Windows Message Loop // Program Init while(GetMessage(&Msg, NULL, 0, 0) > 0) { TranslateMessage(&Msg); DispatchMessage(&Msg); Game Code! } // Program Shutdown
Issues for Games • GetMessage() will wait until it receives a message to return http://msdn.microsoft.com/en-us/library/ms644936%28VS.85%29.aspx • Messages happen regularly, but not regularly enough for us • The solution is PeekMessage() http://msdn.microsoft.com/en-us/library/ms644943%28VS.85%29.aspx
A Better Message Loop PeekMessage( &mssg, NULL, 0, 0, PM_NOREMOVE); while (mssg.message!=WM_QUIT) { if (PeekMessage( &mssg, NULL, 0, 0, PM_REMOVE)) { TranslateMessage(&mssg); DispatchMessage(&mssg); } else { // Game Code! } }
The Best Message Loop PeekMessage( &mssg, NULL, 0, 0, PM_NOREMOVE); while (mssg.message!=WM_QUIT) { if (PeekMessage( &mssg, NULL, 0, 0, PM_REMOVE)) { TranslateMessage(&mssg); DispatchMessage(&mssg); } // Game Code! }
>>A Nice Program Flow<< http://www.directxtutorial.com/tutorial9/A-Win32/dx9A4.aspx
XInput = Simple! • Comprises only two extra sources • XInput.h • Xinput.lib • Main Functions: • XInputGetState • XInputSetState • XInputGetDSoundAudioDeviceGuids
All we can get is the current state • How do we detect button presses? • Avoiding Multiple Trigger situations?
We store the previous state typedefstruct _XINPUT_GAMEPAD { WORD wButtons; BYTE bLeftTrigger; BYTE bRightTrigger; SHORT sThumbLX; SHORT sThumbLY; SHORT sThumbRX; SHORT sThumbRY; } XINPUT_GAMEPAD, *PXINPUT_GAMEPAD;
Direct Input • Uses The Common Object Model (COM) • Input Devices • Keyboard, Mouse, Joystick, Force-Feedback • Consists of two types of objects: • IDirectInput8 Interface • DirectInputDevice (one for each device) LPDIRECTINPUTDEVICE8 lpdiSomeDevice;
DirectInput Process Overview • Create the DirectInput Object • Enumerate DirectInput Devices • Create Device Object(s) • Set up the Device(s) • Acquire the Device (Get access to it) • Retrieve Data (using access) • Use the Data! • Release Devices (Un-Acquire then Release) • Release DirectInput
Action Mapping • Instead of using the specific axis or buttons • Abstract the input to a set of ‘Actions’ #define INPUT_LEFTRIGHT_AXIS 1L DIINPUT structures: // Genre Specific Bindings {INPUT_LEFTRIGHT_AXIS, DIAXIS_SPACESIM_LATERAL, 0, "Turn",}, // Programmer Created Mapping {INPUT_TURNLEFT, DIKEYBOARD_LEFT, 0, "Turn left", }, {INPUT_LEFTRIGHT_AXIS, DIMOUSE_XAXIS, 0, "Turn", }, SpaceSim Genre: http://msdn.microsoft.com/en-us/library/ee418804%28VS.85%29.aspx
Is Action Mapping useful? • Pros • Makes multi-device programming more manageable • Cons • Takes Effort to set up • Has many Predefined Mappings you may not like • Best used only on multi-device games • Otherwise complexity > reward
Descriptors • Structures used by API calls for initialisation • You’ve seen a few already. • Why are they used? • Is there any way to make them easier / quicker to fill out?
MSG example: • typedefstruct • { HWND hwnd; • UINT message; • WPARAMwParam; • LPARAMlParam; • DWORD time; • POINT pt; • } MSG, *PMSG; BOOL PeekMessage( LPMSGlpMsg, HWND hWnd, UINT wMsgFilterMin, UINTwMsgFilterMax, UINT wRemoveMsg); But we still have to fill out all those values
Cheating with DESCRIPTORS • Most Descriptors require a LOT of NULL values • This takes ages to fill in by hand. • It’s standard to use a block format to set the whole descriptor to null before . Declare the structure: DXGI_SWAP_CHAIN_DESC swapDesc; Wipe it!: ZeroMemory( & swapDesc, sizeof(swapDesc));
News Time! • OpenGL 4.0 Spec Released http://www.opengl.org/news/permalink/khronos-unleashes-cutting-edge-cross-platform-graphics-acceleration-with-op/ Wii Death http://www.telegraph.co.uk/news/worldnews/northamerica/usa/7407373/Girl-3-thought-loaded-gun-was-Wii-controller-in-fatal-accident.html
Resources http://www.winprog.org/tutorial/message_loop.html http://www.mvps.org/directx/articles/writing_the_game_loop.htm