ebmDevMag.com home
ebm Developer's Magazine
links

developer's mag
main page

article
part 1
part 2
part 3
part 4
part 5
part 6
part 7
part 8
part 9


8 - Timing is Everything

Now that we have our line, how do we animate it? Welcome to the world of timers. Our animation consists of drawing a line, then returning to the operating system. Left at that, we'd wait forever unless we use a timer. Our timing begins with the on/off toggle button, which sends a MSG_BUTTON_SELECT message to our MsgHandler() function:
  case MSG_BUTTON_SELECT: // on/off button - toggle timer
    if (m_timerOn)
      m_timerOn=false;
    else // start timing - use small interval so as to display quickly
    {
      m_timerOn=true;
      SetTimer(1,false,0); // non-repeating timer (one-shot)
    }
    return 1;
Our timer flag variable (m_timerOn) lets us check if we are timing or not; if not, we start it up with the call SetTimer(1,false,0), which creates a one-shot rather than repeating timer (as determined by the 'false' parameter). The last entry is the data to pass; since we don't need it, we set it to zero. The first parameter is the delay time. A value of zero is invalid, and is used to stop a timer, so one millisecond is the minimum wait we can allow - this means we process the timer message almost immediately, and therefore appear to respond right away to the button press.

Once started, we will eventually receive a single MSG_TIMER message:
  case MSG_TIMER:
    if ( 0==data ) // our original message?
    {
      // retransmit it with different data value
      GUI_EventMessage(MSG_TIMER,this,1);
    }
    else // our retransmitted message - now process it
    {
      if ( m_timerOn ) // timer enabled?
      {
        DrawAnimation(++m_position); // do our work
        // start up timer with latest scrollbar value
        int msec=ScrollBar1->GetPosition()+m_scrollBarBase;
        SetTimer(msec,false,0); // and start up timer again
      }
    }
return 1;
We check and retransmit the message if the data value is zero, after changing the data to one (so we don't enter into an infinite loop). The reason is that the timer is a hardware event arriving via another execution thread, and so can interrupt whatever our program is currently doing (including the GUI, which can make for a messy display). By resending the message, we have it processed by the program thread, and no conflicts occur.

At this point, we now get into drawing. The m_timerOn flag is checked, since we may have turned off drawing before the MSG_TIMER message was received. If on, we draw the rotating line, and start up the timer once again, using the current position of the scroll bar as the timer interval. In this way, we program a variable speed line rotating on screen.

The SDK documentation goes into more detail about the timer, but this code example should be enough to give you a taste of how you can work it into your programming.

Previous Section
Next Section

Copyright © 2001-2006 ebmDevMag.com - Legal Notice