cse 381 – advanced game programming user interface & game events management

19
CSE 381 – Advanced Game Programming User Interface & Game Events Management

Upload: john-norris

Post on 17-Jan-2016

223 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: CSE 381 – Advanced Game Programming User Interface & Game Events Management

CSE 381 – Advanced Game ProgrammingUser Interface &

Game Events Management

Page 2: CSE 381 – Advanced Game Programming User 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

Page 3: CSE 381 – Advanced Game Programming User Interface & Game Events Management

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)

Page 4: CSE 381 – Advanced Game Programming User Interface & Game Events Management

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

Page 5: CSE 381 – Advanced Game Programming User Interface & Game Events Management

Microsoft Spy++

Page 6: CSE 381 – Advanced Game Programming User Interface & Game Events Management

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”

Page 7: CSE 381 – Advanced Game Programming User Interface & Game Events Management

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

Page 8: CSE 381 – Advanced Game Programming User Interface & Game Events Management

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

Page 9: CSE 381 – Advanced Game Programming User Interface & Game Events Management

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() { };

};

Page 10: CSE 381 – Advanced Game Programming User Interface & Game Events Management

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.

Page 11: CSE 381 – Advanced Game Programming User Interface & Game Events Management

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

Page 12: CSE 381 – Advanced Game Programming User Interface & Game Events Management

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

Page 13: CSE 381 – Advanced Game Programming User Interface & Game Events Management

DXUT Controls

CDXUTButtonCDXUTStaticCDXUTCheckBoxCDXUTRadioButtonCDXUTComboBoxCDXUTSliderCDXUTEditBoxCDXUTIMEEditBoxCDXUTListBoxCDXUTScrollBar

Page 14: CSE 381 – Advanced Game Programming User Interface & Game Events Management

General Game Event System

• What’s the purpose?• minimize communications between subsystems• reduce subsystem interdependency

• Many-to-Many mappings present problems• refactoring alone

Page 15: CSE 381 – Advanced Game Programming User Interface & Game Events Management

Game Event System

• Has 3 basic parts:• Events & event data• Event handler delegates• Event manager

Page 16: CSE 381 – Advanced Game Programming User Interface & Game Events Management

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

Page 17: CSE 381 – Advanced Game Programming User Interface & Game Events Management

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• …

Page 18: CSE 381 – Advanced Game Programming User Interface & Game Events Management

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

Page 19: CSE 381 – Advanced Game Programming User Interface & Game Events Management

And finally, the Event Manager

• Matches events with listeners• IEventManager

• a global singleton• VAddListener• VRemoveListener• VTriggerEvent• VQueueEvent• VAbortEvent• VUpdate

• Implemented by EventManager