Title: General Game
1General Game Programming Approach
1
2Event-driven Programming (1/3)
- The program is event-driven
- Messages events
- So as all windows system (for example X window)
- We need a loop to check all incoming events
- The Loop
- Check all incoming events (messages)
- Handle the events
- Check timing and do something in regular
- Incoming Events
- Interrupts
- System requests
3Event-driven Programming (2/3)
- Timers (do something in regular timing)
- The sub-system to handle timing
- Must be precise to at least 1 ms or less
- 30fps 1/30 second 33.333 ms
- On win32 platform, we can use performance
counter instead of the win32s WM_TIMER
message - For windows9x, WM_TIMER 18.2 fps (maximum)
- Events
- Input devices
- Mouse
- Keyboard
- Something coming from network
- System requests
- Re-draw
- Losing/getting the input focus
4Event-driven Programming (3/3)
- Therefore, we have two types of jobs
- In regular
- Timers callbacks
- By requests
- Input device callbacks
- Same as a game main program
- A game is an interactive application
- A game is time-bound
- Rendering in 30fps or 60fps
- Motion data in 30fps
- Game running in 30fps
5Game Loop (1/2)
6Game Loop (2/2)
Loop
y
Check game over
Exit
n
Peek user input
Receive messages
From network
Timer callbacks
To network
Send messages
Rendering
7Jobs in Regular (Typically)
- Check Win/Loose
- Check Quit
- Objects Moving
- Play Characters Motion to Next Frame
- Play Animation to Next Frame
- Models
- Textures
-
- Perform Some Game Calculation
- Perform Geometry Calculation
- LOD
- Perform AI Thinking
- Perform Collision Detection
- Perform the 3D Rendering
8Jobs By Request (Typically)
- Mouse Input
- Press/release the mouse button
- Drag
- Double-click
- Move
- Keyboard Input
- Hotkey
- Typing
- Gamepad
- Same as the hotkey
- Network
- System
9Introduction to OGRE3D Programming Main Loop
9
9
10Major part of call graph
GameApplicationgo()
GameApplicationsetup()
RootstartRendering()
RootrenderOneFrame()
Root_fireFrameStarted()
Root_fireFrameEnded()
Root_updateAllRenderTargets()
10
11Main program
int main(int argc, char argv) GameApplicatio
n gameApp new GameApplication try
gameApp-gtgo() catch( Exception e )
return 1
12The implementation of go()
virtual void GameApplicationgo(void) if
(!setup()) return mRoot-gtstartRendering()
// clean up destroyScene()
The function setup() instantiates mRoot, setups
resources, loads plugins, creates scene and
creates frame listener.
13Major functions (1/3)
14Major functions (2/3)
15Major functions (3/3)
bool myFrameListenerframeStarted(const
FrameEvent evt) if(mWindow-gtisClosed())
return false ... ... //Need to
capture/update each device mKeyboard-gtcapture()
mMouse-gtcapture() if( mJoy )
mJoy-gtcapture() ... ... handleKeyEvent(evt) h
andleMouseEvent(evt) ... ... ...
... ... ... return true
16Major part of call graph
GameApplicationgo()
GameApplicationsetup()
RootstartRendering()
RootrenderOneFrame()
Root_fireFrameStarted()
Root_fireFrameEnded()
Root_updateAllRenderTargets()
16
17Frame listener
Frame listeners are the only way we can invoke
your own code during the Ogre render loop when
using the startRendering() method. A frame
listener is simply a class that implements the
FrameListener interface, and is just a callback
that allows OGRE to invoke our code at the
beginning and/or end of each.
18Demo main_loop
Use ExampleApplication and ExampleFrameListener
In the main .cpp file, we have class
GameApplication public ExampleApplication publi
c GameApplication() void createScene()
GameApplication app new
GameApplication int main(int argc, char
argv) try app-gtgo() catch(
Exception e ) .
18
19Object-Oriented Graphics Rendering Engine ( OGRE
3D) - The Main Program
20FrameStarted
bool myFrameListenerframeStarted(const
FrameEvent evt) if(mWindow-gtisClosed())
return false . . . . . . //Need to
capture/update each device mKeyboard-gtcapture()
mMouse-gtcapture() if( mJoy )
mJoy-gtcapture() . . . . . . handleKeyEvent(evt)
handleMouseEvent(evt) . . . . .
. moveMainChar(evt) updateCreaturesAction(evt)
. . . . . . . . . . . . . . . . . . return
true
21moveMainChar
void moveMainChar(const FrameEvent
evt) mCameraNode-gtyaw(mRotX) // move
the head node along the camera viewing
direction const Quaternion q
mCameraNode-gtgetOrientation() mCameraNode-gtsetO
rientation(q) SceneNode node
mHeadNode-gtgetSceneNode() node-gtsetOrientation(
q) node-gttranslate( q(-mTranslateVector)
) // maintain the distance between the camera
and mHeadNode const Vector3 v3
node-gtgetPosition() Vector3 cv v3 Vector3
hv Vector3(0.0, 0.0, -1.0) hv
qhv float d0 -mCamerViewDistanceFromMainChar
float d1 30 mCameraNode-gtsetPosition(cv-
hvd0Vector3(0.0, d1, 0.0))
22updateCreatureActions
void updateCreaturesAction(const FrameEvent
evt) if (mRobot) mRobot-gtcheckAlive(evt)
if (mRobot mHeadNode) mRobot-gtUpdateActi
on(evt, mHeadNode)
23FrameEvent
namespace Ogre struct FrameEvent
/ Elapsed time in seconds since the last
event. This gives you time between
frame start frame end, and between
frame end and next frame start.
_at_remarks This may not be the
elapsed time but the average
elapsed time between recently fired events.
/ Real timeSinceLastEvent /
Elapsed time in seconds since the last event of
the same type, i.e. time for a
complete frame. _at_remarks
This may not be the elapsed time but the
average elapsed time between
recently fired events of the same type.
/ Real timeSinceLastFrame
24Question from last class Do we have to
implement functions for classes derived from
MovableObject ?
class _OgreExport MovableObject public
ShadowCaster, public AnimableObject virtual
const String getMovableType(void) const
0 class _OgreExport Entity public
MovableObject, public ResourceListener
virtual const String getMovableType(void)
const
25Useful website for introduction to Orgre and Ogre
programming. http//www.ogre3d.org/wiki/index.php
/Main_Page Ogre Tutorials http//www.ogre3d.org/w
iki/index.php/Ogre_Tutorials