Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 14 Timers and Idle Processing Department of Digital Contents Sang Il Park.

Similar presentations


Presentation on theme: "Chapter 14 Timers and Idle Processing Department of Digital Contents Sang Il Park."— Presentation transcript:

1 Chapter 14 Timers and Idle Processing Department of Digital Contents Sang Il Park

2 TIMER When you need to set a repeating job

3 SetTimer function Sending WM_TIMER message at specified interval –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) void SetTimer (int id, int time, void * fp) ; Ex) SetTimer(0, 100, NULL);

4 WM_TIMER’s handler nIDEvent –id of the timer which currently send WM_TIMER Ex) afx_msg void OnTimer (int nIDEvent ) void CChildView::OnTimer(int nIDEvent) { if(nIDEvent == 0) { // define any job here } void CChildView::OnTimer(int nIDEvent) { if(nIDEvent == 0) { // define any job here }

5 Stopping a Timer Stop the timer with the given id –id id of the timer(ex: 0, 1, 2, … ) void KillTimer (int id) ; Ex) KillTimer(0);

6 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) ….. 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) …..

7 An Example in TextBook Chapter 14 (The clock application)

8 Having more fun with the Timer Still Image –One Image Animation –Lots of Images! –http://www.youtube.com/watch?v=UocF4ycBnYEhttp://www.youtube.com/watch?v=UocF4ycBnYE Cézanne, Paul Still Life

9 Having more fun with the Timer Changing Image along with time= Dynamics values/properties of a polygon? –Color –Position –Shape (triangle/rectangle/pentagon/…) Value(number)

10 Dynamics Changing the position of a shape along with time Ex) Constant velocity(speed)  Not fun! How can you make it more complex?

11 Basic Physics for more fun Properties for describing a motion of an object –position: p p(t) : position at time t –velocity: change of the position w.r.t. time (dp/dt) v(t)= p(t+1) – p(t) –acceleration: 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? If you know the positions at different times, it is easy to compute velocities and accelerations. How about the inverse?

12 Given Accelerations? Properties for describing a motion of an object –acceleration: a a(t) : acceleration at time t –velocity: After 1 sec., velocity will change by a v(t+1) = v(t) + a(t)*1sec –position: After 1 sec., position will change by v p(t+1) = p(t) + v(t)*1sec

13 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/sec 2 ) * mass –Spring force = -displacement x k (k: spring coeff.)

14 Programming Eq. of Motion 1.Create variables for position/velocity/accel. (m_p, m_v, m_a ) 2.Repeat updating variables as follows: 1.Compute Force (ex ) gravity or spring force) 2.Update acceleration ( a = f/m ) 3.Update velocity ( v = v + a * dt ) 4.Update position ( p = p + v * dt ) 5.Draw an object at the position

15 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) 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)

16 Try to find out more examples

17 Idle Processing Idling? Idle: a state of nothing to do –Ex.) No message to handle

18 OnIdle function CWinApp provides an OnIdle function to specify a job when idling: It will be called when there is no message to handle. Override it for redefine your idling function virtual BOOL CWinApp::OnIdle(LONG lCount); BOOL CMyApp::OnIdle (LONG lCount) { CWinApp::OnIdle (lCount); DoIdleWork (); // Do custom idle processing. return TRUE; } BOOL CMyApp::OnIdle (LONG lCount) { CWinApp::OnIdle (lCount); DoIdleWork (); // Do custom idle processing. return TRUE; }

19 OnIdle function 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. virtual BOOL CWinApp::OnIdle(LONG lCount);


Download ppt "Chapter 14 Timers and Idle Processing Department of Digital Contents Sang Il Park."

Similar presentations


Ads by Google