SDL2系列教程12-类封装SDL_Timer

本文介绍了如何使用C++封装SDL2的定时器功能,通过创建`timer`和`Time`类,实现对定时器的添加、删除、暂停、恢复和重置等操作,简化了SDL2定时器的使用。

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

为方便使用SDL的定时器,实现了类封装:

代码如下

time.h:

#pragma once

#include <map>
#include <string>
#include <SDL.h>

class timer {
    timer(Uint64 s, Uint64 p, bool perf);
    ~timer();

    Uint64 start;
    Uint64 pause;
    Uint64 lag;
    bool isPerfCounter;

    friend class Time;
};

class Time
{
public:
    Time();
    ~Time();

    bool init();
    bool kill();

    bool addTimer(const std::string& tID, bool startPaused = false);
    bool addPerfCounter(const std::string& tID, bool startPaused = false);

    bool remove(const std::string& tID);

    bool addCallback(Uint32(*callback)(Uint32, void*), Uint32 delay, void* param, bool save = false, const std::string& cID = "");
    bool removeCallback(const std::string& cID);

    bool pause(const std::string& tID);
    bool resume(const std::string& tID);
    bool reset(const std::string& tID);
    bool toggle(const std::string& tID);

    Uint64 get(const std::string& tID);

    Uint64 getPerfFreq();
    Uint64 ticksSinceStart();
    Uint64 perfSinceStart();

private:
    std::map<std::string, timer*> timers;
    std::map<std::string, Uint32> callbacks;
    bool good;
};

time.cpp:


#include "t

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Dwyane05

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值