Title: Chapter 4 Implementing a game timer
1Chapter 4Implementing a game timer
2New goodies
- perform full-screen animation using 3 frames of
animation - make the crow from Demo 1 look like its flying
- Use a timer to control rate at which frames are
displayed (a class/reuse)
3run demo 2
- crow flaps wings
- alttab iconifies game (inactive)
- reactivate surfaces restored reloaded
- works the same on all computers
- Esc key will exit the program
4How to compile
- add the following libraries
- ddraw.lib
- winmm.lib
- Need 3 images
- Middle.bmp
- Up.bmp
- Down.bmp
5The Timer
- class is CTimer, with constructor Ctimer()
- file is Timer.h
- int m_nStartTime records start time
- virtual void start() starts the timer
- virtual int time() returns time in ms
- BOOL elapsed(int start, int interval)
- returns TRUE if interval ms have elapsed since
start - returns FALSE otherwise
call by reference
6The Timer class
class CTimer //game timer class protected
int m_nStartTime //time that timer was started
public CTimer() //constructor virtual
void start() //start the timer virtual int
time() //return the time in ms //has
interval ms elapsed since start? BOOL
elapsed(int start,int interval)
7windows API function
include "timer.h" CTimerCTimer()
m_nStartTime(0) void CTimerstart()
m_nStartTimetimeGetTime() int
CTimertime() return timeGetTime()-m_nStartTime
int CTimerelapsed(int start, int
interval) //has interval elapsed from start?
int current_timetime() if(current_timegtstart
interval) startcurrent_time return TRUE
else return FALSE
elapsed_time ?
7
8timeGetTime
- returns number of ms since windows was rebooted
- accurate only to 5 ms good enuf!
- wraps around to 0 every 232 ms, about every 49.71
days - author cant keep windows from crashing every few
hours - he thinks theres no problem
9Main.cpp
CBmpFileReader frame0,frame1,frame2 int
current_frame0 //current frame of
animation CTimer Timer //game timer BOOL
LoadImages() //load graphics from files to
surfaces if(!frame0.load("Right.bmp"))return
FALSE frame0.draw(lpPrimary) //draw to
primary surface if(!frame0.setpalette(lpPrimaryP
alette))return FALSE if(!frame0.setpalette(lpSe
condaryPalette))return FALSE
if(!frame1.load("Middle.bmp"))return FALSE
if(!frame2.load("Left.bmp"))return FALSE
return TRUE
all 3 images use same palette
10demo1 vs demo2
while(TRUE) if(PeekMessage(msg,NULL,0,0,PM_N
OREMOVE)) if(!GetMessage(msg,NULL,0,0))ret
urn msg.wParam TranslateMessage(msg)
DispatchMessage(msg) else
if(!ActiveApp)WaitMessage()
while(TRUE) if(PeekMessage(msg,NULL,0,0,PM_N
OREMOVE)) if(!GetMessage(msg,NULL,0,0))ret
urn msg.wParam TranslateMessage(msg)
DispatchMessage(msg) else
if(ActiveApp)ProcessFrame() else
WaitMessage() //WinMain
10
11ProcessFrame()
BOOL ProcessFrame() //process a frame of
animation ComposeFrame() //compose a frame in
secondary surface return PageFlip() //flip
video memory surfaces //ProcessFrame
12BOOL ComposeFrame() static int
last_timeTimer.time() //paranoia
if(current_framelt0)current_frame0
if(current_framegt3)current_frame3 //show a
frame switch(current_frame) case 0
frame0.draw(lpSecondary) break case 1
frame1.draw(lpSecondary) break case 2
frame2.draw(lpSecondary) break case 3
frame1.draw(lpSecondary) break //go to
next frame if(Timer.elapsed(last_time,100))
if(current_framegt4)current_frame0 return
TRUE
Why 4?
not used outside this function. Why not make it
static?
12
13BOOL ComposeFrame() //compose a frame of
animation static int last_timeTimer.time()
static int current_frame 0 //show a frame
switch(current_frame) case 0
frame0.draw(lpSecondary) break case 1
frame1.draw(lpSecondary) break case 2
frame2.draw(lpSecondary) break case 3
frame1.draw(lpSecondary) break //go to
next frame if(Timer.elapsed(last_time,100))
if(current_framegt4)current_frame0 return
TRUE
Hard to do better!
13
14Oops
BOOL ProcessFrame() ComposeFrame() return
PageFlip() //flip video memory surfaces
switch(current_frame) case 0
frame0.draw(lpSecondary) break case 1
frame1.draw(lpSecondary) break case 2
frame2.draw(lpSecondary) break case 3
frame1.draw(lpSecondary) break
How does the page get to lpPrimay?