一个时钟类的设计

       游戏中时间,事件的管理是必不可少的,今天我也走到了这一步,那就是我的2D游戏小引擎的图象,声音的处理已经略有皮毛,考虑到2D目前已经有点日薄西山,所以小弟我也不再想在这方面深入学习了.所以呢,我开始着手学习时间,事件管理机制的处理.

      SCLOCK类的介绍:

      游戏中常常需要有自己的时间机制,像CS,每局时间,雷炸时间.魔兽中的时间转换,还有MMRPG中的时间等等,他们都有一些基本的功能,那就是:用一个自定义的时间来模拟,取代系统时间,能够暂停,等.

      SCLOCK的时间机制是这样的,在SCLOCK类初始化的时候,设置一个模拟系统时间,也就是游戏中所用到的系统时间,比如说游戏名字叫大唐双龙传,那就可以是公元900年左右.(英雄人物还要成长一下嘛.)这里我将它设为0.

每次暂停后,系统时间亦会停止累加,在重新开始后,新的系统时间可以通过当前真实时间,减去暂停所花的时间来获得.

以下源代码:

/*************************************************************************
 *
 * Copyright (c) 2005 Azure,All rights received.
 *
 * You can use,copy,and change it at will.But you can not use or
 * distribute it for any commercial purpose without permission from
 * Azure.And if you want to reship it,please let me know.
 *
 * Author:Azure
 * Email:bluejugar@sohu.com
 *
 * Created Date: Apr.,29th,2005.
 * 
 * Revision: 1.0.1
 **************************************************************************/

#ifndef _CLOCK_H_
#define _CLOCK_H_

#include <windows.h>
#include <mmsystem.h>
//For three functions:timeGetTime(),timeBeginPeriod(...),timeEndPeriod(...).

typedef size_t time32;
typedef size_t size_type;

template<typename Time = time32,size_type nPrecision = 1>
class SClock
{
public:
 explicit SClock(void);
 virtual ~SClock(void);
 

 //Ispectors:
 bool IsRunning(void) const;
 Time GetSystemTime(void) const;

 //Facilitators:
 void ResetClock(void);
 void Pause(void);
 void CancelPause(void);
 void Update(void);
 void Destroy(void);

protected:
 Time m_tSystemTime_;  //The simulate system time.
     //Also the system's actual time.
 Time m_tPauseTime_;  //The time when paused.
 Time m_tOffsetTime_;  
 bool m_bRunning_; 
    
};

template<typename Time,size_t nPrecision>
SClock<Time,nPrecision>::SClock(void)
{
 timeBeginPeriod(nPrecision);
 ResetClock();
}

template<typename Time,size_t nPrecision>
SClock<Time,nPrecision>::~SClock(void)
{
 Destroy();
}

template<typename Time,size_t nPrecision>
bool SClock<Time,nPrecision>::IsRunning(void) const
{
 return (m_bRunning_);
}

template<typename Time,size_t nPrecision>
Time SClock<Time,nPrecision>::GetSystemTime(void) const
{
 return (m_tSystemTime_);
}

template<typename Time,size_t nPrecision>
void SClock<Time,nPrecision>::ResetClock(void)
{
 m_tSystemTime_ = 0;
 m_tPauseTime_ = 0;
 m_tOffsetTime_ = timeGetTime();
 m_bRunning_ = true;
}

template<typename Time,size_t nPrecision>
void SClock<Time,nPrecision>::Pause(void)
{
 //Pause the clock.
 //When the clock is paused,the clock still works,and the wasted time
 //will be recorded.
 m_bRunning_ = false;
 m_tPauseTime_ = timeGetTime();
}

template<typename Time,size_t nPrecision>
void SClock<Time,nPrecision>::CancelPause(void)
{
 //Cancel the pause to the clock.
 m_tOffsetTime_ += timeGetTime() - m_tPauseTime_;
 m_tSystemTime_ = timeGetTime() - m_tOffsetTime_;
 m_bRunning_ = true;
}

template<typename Time,size_t nPrecision>
void SClock<Time,nPrecision>::Update(void)
{
 //Cancel the pause to the clock.
 //When the pause of the clock is canceled,the
 m_tOffsetTime_ += timeGetTime() - m_tPauseTime_;
 m_tSystemTime_ = timeGetTime() - m_tOffsetTime_;
}

template<typename Time,size_t nPrecision>
void SClock<Time,nPrecision>::Destroy(void)
{
 timeEndPeriod(nPrecision);
}

#endif

作为一个游戏来说,一般有一个时钟就足够了,明天我会把它改成singleton模式,

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值