cse 381 – advanced game programming user interface & game events management
TRANSCRIPT
CSE 381 – Advanced Game ProgrammingUser Interface &
Game Events Management
Reading Input Devices
• For the application layer
• Processed and handed to game view layer
• Game layer then generates response• Translates it into a command
• Who will deal with input hardware?• see GameCode4/interfaces.h
• IKeybaordHandler, IPointerHandler, IJoystickHandler, IGamepadHandler• subclasses (control classes) convert input to commands to
update the game state
Ex: Movement Controller
• Responds to mouse movement via player movement
Class MovementController: public IPointerHandler,
public IKeyboardHandler
{
Mat4x4 m_matFromWorld;
Mat4x4 m_matToWorld;
Mat4x4 m_matPosition;
Cpoint m_lastMousePos;
BYTE m_bKey[256];
float m_fTargetYaw;
…
bool VOnPointerMove(Cpoint &mousePos)
void OnUpdate(DWORD delta)
…
Again, a Command Based System
• We want a modular design
• AI bots generate commands
• Controllers generate commands
• Both affect control objects
• Control Objects don’t need to know who is sending them commands
• AI characters & Humans should be interchangeable
Microsoft Spy++
Mouse Dragging
• An important issue for us• Why?
• RTS Unit Selection
• For handling mouse dragging:1. Detect & initiate a drag event
• make sure it’s not on a double click• make sure it’s beyond a drag threshold
2. Handle the mouse movement & drag objects accordingly3. Detect the release and finalize the drag
• selection of units only if release point is legal• involves “picking”
Picking
• Translate mouse click to object selection
• Screen is in 2D space
• Objects are in 3D space
• We’ll learn how after covering 3D graphics, & collision math
Other Input Control Issues to Consider
• Automated Target Selection• Dead Zones• Directional Input Normalization• Dual Stick Input• Ramping• Character vs. Key Codes• Keyboard mapping & Key Combination Confusion• Polling vs. Message Pumps• Alt key issues
So how are UIs managed?class IGameView {
virtual HRESULT VOnRestore()=0;virtual void VOnRender(double fTime,
float fElapsedTime)=0;virtual HRESULT VOnLostDevice()=0;virtual GameViewType VGetType()=0;virtual GameViewId VGetId() const=0;virtual void VOnAttach(GameViewId vid,
ActorId aid)=0;virtual LRESULT CALLBACK VOnMsgProc( AppMsg msg )=0;virtual void VOnUpdate(unsigned long deltaMs)=0;virtual ~IGameView() { };
};
The Player’s Game Viewclass HumanView : public IGameView{
GameViewId m_ViewId;ActorId m_ActorId;ProcessManager* m_pProcessManager;DWORD m_currTick;DWORD m_lastDraw;bool m_runFullSpeed;BaseGameState m_BaseGameState;ScreenElementList m_ScreenElements;shared_ptr<ScreenElementScene> m_pScene;shared_ptr<CameraNode> m_pCamera;
bool LoadGame(TiXmlElement* pLevelData);// PLUS VOnRestore, VOnUpdate, VRenderText,// VOnLostDevice, VOnRender, VOnAttach, etc.
Basic UI elements
class BaseUI : public IScreenElement{
int m_PosX, m_PosY;int m_Width, m_Height;optional<int> m_Result;boolm_bIsVisible;
class StandardHUD : public BaseUI{
CDXUTDialog m_HUD;
class MessageBox : public BaseUI{
CDXUTDialog m_UI;int m_ButtonId;
• Note: In the textbook, StandardHUD is called CScreenBox
So how do UI controls get interactions?
• Forwarded to all game views from GameCodeApp::MsgProc
• To where?• VOnMsgProc
• And what does it do?• Forwards it to all screen elements
• Note forward messages to screen elements in reverse order from rendering
• What does DXUT do for us?• builds & renders our controls
DXUT Controls
CDXUTButtonCDXUTStaticCDXUTCheckBoxCDXUTRadioButtonCDXUTComboBoxCDXUTSliderCDXUTEditBoxCDXUTIMEEditBoxCDXUTListBoxCDXUTScrollBar
General Game Event System
• What’s the purpose?• minimize communications between subsystems• reduce subsystem interdependency
• Many-to-Many mappings present problems• refactoring alone
Game Event System
• Has 3 basic parts:• Events & event data• Event handler delegates• Event manager
Game Events
• When something important happens in your game:• fire an event• notify all appropriate subsystems of event• subsystems process event in their own way
• Generated by authoritative systems
• Specify a GUID for each event type• done in Visual Studio via Tools Create GUID• specifies unique number that survives recompilation
What type of events do we have?
• See EventManager/Events.h• EvtData_Destroy_Actor• EvtData_Environment_Loaded• EvtData_Move_Actor• EvtData_New_Actor• EvtData_Play_Sound• EvtData_Request_New_Actor• EvtData_Request_Start_Gamej• …
Event Listener Delegates
• What are delegates?• function pointer + object pointer
• used as callback• we can use fast delegates• http://www.codeproject.com/KB/cpp/FastDelegate.aspx
• All Event Listener delegates will conform to:
typedef shared_ptr<IEventData> IEventDataPtr;
void Delegate(IEventDataPtr pEventData);
• To make delegate & bind to object:• MakeDelegate from 3rdParty/FastDelegate
And finally, the Event Manager
• Matches events with listeners• IEventManager
• a global singleton• VAddListener• VRemoveListener• VTriggerEvent• VQueueEvent• VAbortEvent• VUpdate
• Implemented by EventManager