CodeGuru
Earthweb Search
Forums Wireless Jars Gamelan Developer.com
CodeGuru Navigation
RSS Feeds

RSSAll

RSSVC++/C++

RSS.NET/C#

RSSVB

See more EarthWeb Network feeds

follow us on Twitter

Member Sign In
User ID:
Password:
Remember Me:
Forgot Password?
Not a member?
Click here for more information and to register.

Become a Marketplace Partner

jobs.internet.com

internet.commerce
Partners & Affiliates
















Home >> Visual C++ / C++ >> Graphics & Multimedia >> Multimedia >> Timers


A Class To Encapsulate MultiMedia Timers
Rating: none

Simon Wood (view profile)
May 17, 1999

This simple class encapsulates the multimedia timers. To use the class include mmTimers.cpp and mmTimers.h in your project. Link with winmm.lib. To use a timer derive a class from CMMTimers and override member timerProc. timerProc will be called when the timer goes off. Instantiate a variable of the new class. The parameter to the constructor is the timer resolution in ms. To start a timer call startTimer. The first parameter specifies the period of the timer in ms. The second parameter specifies whether the timer is a one shot or periodic timer. To stop a periodic timer call stopTimer.

This code was developed with Visual C++ 5.0 and has been tested on NT 4.0.
(continued)




The source follows:

The header file, mmTimers.h



#ifndef	___multimedia_timers___
#define	___multimedia_timers___


#include	



class CMMTimers
{
public:
 CMMTimers(UINT resolution);
 virtual ~CMMTimers();

 UINT	getTimerRes() { return timerRes; };

 bool	startTimer(UINT period,bool oneShot);
 bool	stopTimer();

 virtual void timerProc() {};

protected:
 UINT	timerRes;
 UINT	timerId;
};


#endif


The source file, mmTimers.cpp


#include	"StdAfx.h"
#include	"mmTimers.h"


CMMTimers::CMMTimers(UINT resolution) : timerRes(0), timerId(0)
{
 TIMECAPS	tc;

 if (TIMERR_NOERROR == timeGetDevCaps(&tc,sizeof(TIMECAPS)))
 {
  timerRes = min(max(tc.wPeriodMin,resolution),tc.wPeriodMax);
  timeBeginPeriod(timerRes);
 }
}


CMMTimers::~CMMTimers()
{
 stopTimer();
 if (0 != timerRes)
 {
  timeEndPeriod(timerRes);
  timerRes = 0;
 }
}


extern "C"
void CALLBACK internalTimerProc(UINT id, UINT msg,
DWORD dwUser, DWORD dw1, DWORD dw2)
{
 CMMTimers *	timer = (CMMTimers *)dwUser;

 timer->timerProc();
}


bool CMMTimers::startTimer(UINT period,bool oneShot)
{
 bool		res = false;
 MMRESULT	result;

 result = timeSetEvent(period, timerRes, internalTimerProc,
 (DWORD)this,oneShot ? TIME_ONESHOT : TIME_PERIODIC);
 if (NULL != result)
 {
  timerId = (UINT)result;
  res = true;
 }

 return res;
}


bool CMMTimers::stopTimer()
{
 MMRESULT	result;

 result = timeKillEvent(timerId);
 if (TIMERR_NOERROR == result)
  timerId = 0;

 return TIMERR_NOERROR == result;
}


Download source - 1 KB

Date Last Updated: May 17, 1999

Tools:
Add www.codeguru.com to your favorites
Add www.codeguru.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed







RATE THIS ARTICLE:   Excellent  Very Good  Average  Below Average  Poor  

(You must be signed in to rank an article. Not a member? Click here to register)

Latest Comments:
Question about callback - Legacy CodeGuru (11/24/2003)
a sample program for mmTimers - Legacy CodeGuru (03/21/2003)
Problem with huge interval - Legacy CodeGuru (02/11/2003)
PLEASE HELP ME! - Legacy CodeGuru (02/04/2003)
I understand but I can't use it - Legacy CodeGuru (07/31/2002)

View All Comments
Add a Comment:
Title:
Comment:
Pre-Formatted: Check this if you want the text to display with the formatting as typed (good for source code)



(You must be signed in to comment on an article. Not a member? Click here to register)