cpp-sdl2
C++ header-only SDL2 wrapper
utils.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <SDL.h>
4 #include <string>
5 
6 #include "window.hpp"
7 
8 namespace sdl
9 {
11 inline std::string version()
12 {
13  // Need to use SDL_GetVersion, not the SDL_VERSION macro.
14  // SDL_VERSION give you the version of SDL used to build the program
15  // SDL_GetVersion get the version number from the dynamically linked library
16  SDL_version v;
17  SDL_GetVersion(&v);
18  return std::to_string(int(v.major)) + '.' + std::to_string(int(v.minor)) + '.'
19  + std::to_string(int(v.patch));
20 }
21 
23 inline void show_message_box(uint32_t flags, std::string const& title, std::string const& message)
24 {
25  SDL_ShowSimpleMessageBox(flags, title.c_str(), message.c_str(), nullptr);
26 }
27 
29 inline void show_message_box(
30  uint32_t flags, std::string const& title, std::string const& message, sdl::Window const& parent)
31 {
32  SDL_ShowSimpleMessageBox(flags, title.c_str(), message.c_str(), parent.ptr());
33 }
34 
36 namespace system
37 {
38 // new in 2.0.9
39 #if SDL_VERSION_ATLEAST(2, 0, 9)
40 inline bool is_tablet()
41 {
42  return SDL_IsTablet();
43 }
44 
45 inline bool has_AVX512F()
46 {
47  return SDL_HasAVX512F();
48 }
49 #endif
50 
52 inline std::string platform()
53 {
54  return SDL_GetPlatform();
55 }
56 
59 inline int cpu_cacheline_size()
60 {
61  return SDL_GetCPUCacheLineSize();
62 }
63 
65 inline int cpu_count()
66 {
67  return SDL_GetCPUCount();
68 }
69 
72 inline int system_ram()
73 {
74  return SDL_GetSystemRAM();
75 }
76 
78 inline bool has_3DNow()
79 {
80  return SDL_Has3DNow();
81 }
82 
84 inline bool has_AVX()
85 {
86  return SDL_HasAVX();
87 }
88 
90 inline bool has_AVX2()
91 {
92  return SDL_HasAVX2();
93 }
94 
96 inline bool has_AltiVec()
97 {
98  return SDL_HasAltiVec();
99 }
100 
102 inline bool has_MMX()
103 {
104  return SDL_HasMMX();
105 }
106 
108 inline bool has_RDTSC()
109 {
110  return SDL_HasRDTSC();
111 }
112 
114 inline bool has_SSE()
115 {
116  return SDL_HasSSE();
117 }
118 
120 inline bool has_SSE2()
121 {
122  return has_SSE2();
123 }
124 
126 inline bool has_SSE3()
127 {
128  return has_SSE3();
129 }
130 
132 inline bool has_SSE41()
133 {
134  return has_SSE41();
135 }
136 
138 inline bool has_SSE42()
139 {
140  return has_SSE42();
141 }
142 } // namespace system
143 
145 namespace power
146 {
148 enum class state
149 {
150  unknown = SDL_POWERSTATE_UNKNOWN,
151  on_battery = SDL_POWERSTATE_ON_BATTERY,
152  no_battery = SDL_POWERSTATE_NO_BATTERY,
153  charging = SDL_POWERSTATE_CHARGING,
154  charged = SDL_POWERSTATE_CHARGED
155 };
156 
158 inline state get_state()
159 {
160  return state(SDL_GetPowerInfo(nullptr, nullptr));
161 }
162 
166 {
167  int time = 0;
168  SDL_GetPowerInfo(&time, nullptr);
169  return time;
170 }
171 
175 {
176  int charge = 0;
177  SDL_GetPowerInfo(nullptr, &charge);
178  return charge;
179 }
180 } // namespace power
181 
183 namespace clipboard
184 {
186 inline bool has_text()
187 {
188  return SDL_HasClipboardText();
189 }
190 
192 inline std::string text()
193 {
194  return SDL_GetClipboardText();
195 }
196 
198 inline int get_text(std::string const& text)
199 {
200  return SDL_SetClipboardText(text.c_str());
201 }
202 } // namespace clipboard
203 } // namespace sdl
int system_ram()
Get the amount of ram in the system `.
Definition: utils.hpp:72
int get_battery_remaining_charge()
Get the current battery charge.
Definition: utils.hpp:174
std::string text()
Returns the content of the clipboard.
Definition: utils.hpp:192
bool is_tablet()
Definition: utils.hpp:40
bool has_SSE3()
Return true if cpu supports SSE3.
Definition: utils.hpp:126
bool has_AVX512F()
Definition: utils.hpp:45
bool has_SSE42()
Return true if cpu supports SSE42.
Definition: utils.hpp:138
bool has_AVX()
Return true if CPU has AVX instruction set support.
Definition: utils.hpp:84
Represent an SDL window.
Definition: window.hpp:19
bool has_SSE41()
Return true if cpu supports SSE41.
Definition: utils.hpp:132
bool has_AVX2()
Return true if CPU has AVX2 instruction set support.
Definition: utils.hpp:90
bool has_text()
Returns true if clipboard has text.
Definition: utils.hpp:186
std::string platform()
Get used platform as a string.
Definition: utils.hpp:52
state get_state()
Get current powerstate. See sdl::power::state enumeration for values.
Definition: utils.hpp:158
int cpu_count()
Get the numbers of CPU in the system.
Definition: utils.hpp:65
Define to deactivate exception support.
Definition: color.hpp:7
int cpu_cacheline_size()
Get the size of a cacheline.
Definition: utils.hpp:59
bool has_SSE2()
Return true if cpu supports SSE2.
Definition: utils.hpp:120
int get_text(std::string const &text)
Set the clipboard to a specific value.
Definition: utils.hpp:198
SDL_Window * ptr() const
Getter for the raw SDL2 window pointer.
Definition: window.hpp:61
int get_battery_remaining_time()
Get battery remaining time.
Definition: utils.hpp:165
bool has_SSE()
Return true if cpu supports 1st gen SSE.
Definition: utils.hpp:114
bool has_3DNow()
Returns true if system has AMD 3DNow! support.
Definition: utils.hpp:78
bool has_AltiVec()
Return true if cpu has Apple/IBM/Motorola AltiVec SIMD support.
Definition: utils.hpp:96
bool has_MMX()
Return true if cpu has Intel MMX support.
Definition: utils.hpp:102
bool has_RDTSC()
Return true if current cpu has a TSC register.
Definition: utils.hpp:108
state
Power states.
Definition: utils.hpp:148
std::string version()
Get version as string.
Definition: utils.hpp:11
void show_message_box(uint32_t flags, std::string const &title, std::string const &message)
Show a message box, usefull to display error messages.
Definition: utils.hpp:23