用C语言实现的定时器库---接口。

本文介绍了一款为游戏编程设计的简易定时器库。该库支持定时器的创建、判断是否超时、重置等功能,并提供了错误处理机制。通过这个库,开发者可以轻松管理游戏中的时间相关逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 在一些游戏编程中,经常需要查看某些时间点是否到达? 某些事件是否到了触发时间? 或者是设置一个定时器等等。为此,我设计了一个简单的定时器库,你可以在某时刻开启一个或多个定时器,然后在任何时候查看给定的定实器是否已经到时,或者重新设定定时器等等。在此基础上可以开发出更多高级复杂的操作,实现你所想要的功能。
以下的代码是定时器库的接口:timer.h
有关实现见另一篇文章。

#if !defined(TIMER_H)
#define TIMER_H

#include 
<time.h>

typedef struct mytimer mytimer;
typedef struct mytimer
* timer;
struct mytimer
{
    clock_t base_tm;
    
float     distm_second;
    
}
;

typedef 
int timerID;

typedef 
enum timer_errors timer_errors;
enum timer_errors
{
    timerOK,
    timerBadID,
    timerOutOfMem,
    timerAvoidDistm,
    timerNoSuchErrCode
}
;


/**************************** 
initlize a new timer.
    input: distm: the timer's limitation.
    output: timer's ID.
    used:
****************************
*/

timerID  initlize_timer(
float distm);

/**************************** 
judge the timer is over or not.
    input: id: the timer.
    output: 1 if the timer is overtimer otheiwise 0.
    used:
****************************
*/

int is_timer_over(timerID id);

/**************************** 
reset the timer , the timer begin to time again.
    input: id:the timer.
    output: nothing.
    used: ...
****************************
*/

void reset_timer(timerID id);

/**************************** 
get the timer's distm_second.
    input: id:the timer.
    output: the timer's ditm_second.
    used:...
****************************
*/

float get_timer_distm(timerID id);

/**************************** 
set the timer's distm_second.
    input: id:the timer.
    output: nothing.
    used:...
****************************
*/

void set_timer_distm(timerID id, float newdistm);

/**************************** 
ger the recent timer's result.
    input: nothing.
    output: timer_errors's code.
    used:...
****************************
*/

timer_errors timer_result(
void);

/**************************** 
get the recent timer's message .
    input: nothing
    output: a char poniter to a const string.
    used:....
****************************
*/

char * timer_msg(timer_errors err);

#endif
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值