Visual C++ Programming
Download
Report
Transcript Visual C++ Programming
Chapter 14
Timers and Idle Processing
Department of Digital Contents
Sang Il Park
TIMER
When you need to set a repeating job
SetTimer function
void SetTimer (int id, int time, void * fp) ;
• Sending WM_TIMER message at specified interval
Ex) SetTimer(0, 100, NULL);
– id
• id of the timer(ex: 0, 1, 2, … )
• For indentifying multiple timers
– time:
• interval (=millisec)
• 1000 = 1 second
• Call this function after creating the window
– Usually set the timer in OnCreate function (the handler for
WM_CREATE message)
WM_TIMER’s handler
afx_msg void OnTimer (int nIDEvent)
• nIDEvent
– id of the timer which currently send WM_TIMER
• Ex)
void CChildView::OnTimer(int nIDEvent)
{
if(nIDEvent == 0)
{
// define any job here
}
}
Stopping a Timer
void KillTimer (int id) ;
• Stop the timer with the given id
Ex) KillTimer(0);
– id
• id of the timer(ex: 0, 1, 2, … )
Coding Practice
• Moving Rectangle Animation
1. Create a variable for the position
CPoint m_pt;
2. Draw a rect at the position
(OnPaint)
dc.Rectangle(m_pt.x, …);
3. Add WM_CREATE handler
(OnCreate)
4. Set Timer in OnCreate function
SetTimer(0,30,NULL);
5. Add WM_TIMER handler
(OnTimer)
if(nIDEvent == 0)
…..
An Example in TextBook
• Chapter 14 (The clock application)
Having more fun with the Timer
• Still Image
– One Image
Cézanne, Paul
Still Life
• Animation
– Lots of Images!
– http://www.youtube.com/watch?v=UocF4ycBnYE
Having more fun with the Timer
• Changing Image along with time= Dynamics
• values/properties of a polygon?
– Color
– Position
– Shape (triangle/rectangle/pentagon/…)
Dynamics
• Changing the position of a shape along with
time
• Ex)
Constant velocity(speed)
Not fun!
How can you make it more
complex?
Basic Physics for more fun
• Properties for describing a motion of an object
– position
– velocity
– acceleration
:p
p(t) : position at time t
: change of the position w.r.t. time (dp/dt)
v(t)= p(t+1) – p(t)
: change of the velocity w.r.t. time (dv/dt)
a(t) = v(t+1) – v(t)
If you know the positions at different times,
it is easy to compute velocities and accelerations.
How about the inverse?
Given Accelerations?
• Properties for describing a motion of an object
– acceleration
– velocity
– position
:a
a(t) : acceleration at time t
: After 1 sec., velocity will change by a
v(t+1) = v(t) + a(t)*1sec
: After 1 sec., position will change by v
p(t+1) = p(t) + v(t)*1sec
Equation of Motion
• Equation of Motion by Newton
–
f = ma
– If Force is given, Acceleration can be computed
– If Acceleration is given, Velocity can be computed
– If Velocity is given, Position can be computed
• Ex) Examples of forces
– Free fall
• force = gravity g ( =-9.8m/sec2 ) * mass
– Spring
• force = -displacement x k (k: spring coeff.)
Programming Eq. of Motion
1. Create variables for position/velocity/accel. (m_p,
m_v, m_a )
2. Repeat updating variables as follows:
1.
2.
3.
4.
5.
Compute Force
(ex) gravity or spring force)
Update acceleration
( a = f/m )
Update velocity
( v = v + a * dt )
Update position
( p = p + v * dt )
Draw an object at the position
Coding practice
• A falling ball
Code it as following order
1. Acceleration is just a gravity
a=g
2. By clicking a mouse, reset the position of the
ball
3. Creat a floor and make a ball bouncing
Change the properties as follow
p(t+dt) = at the position of the wall
v(t+dt) = - e * v (t)
(e: coeff. of restitution, usually 0.8)
Try to find out more examples
Idle Processing
• Idling?
• Idle: a state of nothing to do
– Ex.) No message to handle
OnIdle function
• CWinApp provides an OnIdle function to specify
a job when idling:
virtual BOOL CWinApp::OnIdle(LONG lCount);
• It will be called when there is no message to
handle.
• Override it for redefine your idling function
BOOL CMyApp::OnIdle (LONG lCount)
{
CWinApp::OnIdle (lCount);
DoIdleWork (); // Do custom idle processing.
return TRUE;
}
OnIdle function
virtual BOOL CWinApp::OnIdle(LONG lCount);
• lCount: number of times OnIdle has been called
– Starts from 0
– Increases by 1 until getting a new message
• Return value:
– determine whether OnIdle will be called again
– TRUE:
• OnIdle will be called again if the message que is still empty
– FALSE:
• Stop calling OnIdle and call OnIdle after reentering to a idle
state.