cpp-sdl2
C++ header-only SDL2 wrapper
timer.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include "exception.hpp"
4 #include <SDL_timer.h>
5 #include <cassert>
6 #include <chrono>
7 #include <cstdint>
8 
9 namespace sdl
10 {
12 class Timer
13 {
14  SDL_TimerID timer_ = 0;
15 
17  Timer(SDL_TimerID timer) : timer_{timer} {}
18 
19 public:
21  Timer() = delete;
22 
24  using Callback = SDL_TimerCallback;
25 
27  bool remove()
28  {
29  if (timer_ > 0)
30  return SDL_RemoveTimer(timer_);
31  else
32  return false;
33  }
34 
36  static Timer create(uint32_t interval, Callback function, void* user_context)
37  {
38  const auto id = SDL_AddTimer(interval, function, user_context);
39 
40  if (!id)
41  {
42  throw Exception("SDL_AddTimer");
43  }
44 
45  return {id};
46  }
47 
49  static Timer create(std::chrono::milliseconds interval, Callback function, void* user_context)
50  {
51  return create(static_cast<uint32_t>(interval.count()), function, user_context);
52  }
53 
55  static void delay(std::chrono::milliseconds millisec)
56  {
57  assert(millisec.count() >= 0);
58  delay(static_cast<uint32_t>(millisec.count()));
59  }
60 
62  static void delay(uint32_t millisec) { SDL_Delay(millisec); }
63 
65  static uint32_t ticks_u32() { return static_cast<uint32_t>(ticks().count()); }
66 
68  static std::chrono::milliseconds ticks() { return std::chrono::milliseconds(SDL_GetTicks()); }
69 
71  static uint64_t perf_counter() { return SDL_GetPerformanceCounter(); }
72 
74  static uint64_t perf_frequency() { return SDL_GetPerformanceFrequency(); }
75 
77  SDL_TimerID timer_id() const { return timer_; }
78 
79  operator SDL_TimerID() const { return timer_id(); }
80 };
81 } // namespace sdl
Represent an SDL timer.
Definition: timer.hpp:12
static std::chrono::milliseconds ticks()
Retruns the number of milliseconds.
Definition: timer.hpp:68
static Timer create(std::chrono::milliseconds interval, Callback function, void *user_context)
Factory function using std::chrono.
Definition: timer.hpp:49
SDL_TimerID timer_
Definition: timer.hpp:14
Timer(SDL_TimerID timer)
construct
Definition: timer.hpp:17
Define to deactivate exception support.
Definition: color.hpp:7
static uint64_t perf_counter()
Return the performance counter value.
Definition: timer.hpp:71
SDL_TimerID timer_id() const
Get the id of this timer.
Definition: timer.hpp:77
SDL_TimerCallback Callback
Just so that the type of a callback function can be.
Definition: timer.hpp:24
static uint64_t perf_frequency()
Return the performace frequency value.
Definition: timer.hpp:74
static void delay(std::chrono::milliseconds millisec)
Wait for millisec milliseconds.
Definition: timer.hpp:55
static void delay(uint32_t millisec)
Wait for millisec milliseconds.
Definition: timer.hpp:62
static uint32_t ticks_u32()
Returns the number of milliseconds elapsed as a unint32_t (standard SDL API)
Definition: timer.hpp:65
static Timer create(uint32_t interval, Callback function, void *user_context)
Factory function.
Definition: timer.hpp:36
Define to deactivate exception support.
Definition: exception.hpp:21
Timer()=delete
A timer object that is not tied to a timer id in SDL doesn&#39;t make senes.