master
ブランチ master
cpputils / include / glfwpp / glfwpp.hh
9.3 KB、304行 生 Blame 履歴
1/*
2# 076 Free License
3
4Copyright (c) テクニカル諏訪子
5
6Permission is hereby granted to any person obtaining a copy of the software
7C++ Utils (the "Software") to use, modify, merge, copy, publish, distribute,
8sublicense, and/or sell copies of the Software, subject to the following conditions:
9
10 1. **Origin Attribution**:
11 - You must not misrepresent the origin of the Software; you must not claim
12 you created the original Software.
13 2. **Notice Preservation**:
14 - This license and the above copyright notice must remain intact in all copies
15 of the source code.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
18INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
19PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
21CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
22OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23*/
24#ifndef GLFWPP_HH
25#define GLFWPP_HH
26
27#if defined(USING_VULKAN)
28#define GLFW_INCLUDE_VULKAN
29#include <GLFW/glfw3.h>
30
31#include <vulkan/vulkan.hpp>
32
33#if defined(_WIN64)
34#define GLFW_EXPOSE_NATIVE_WIN32
35#elif defined(__APPLE__)
36#define GLFW_EXPOSE_NATIVE_COCOA
37#else
38#define GLFW_EXPOSE_NATIVE_X11
39#endif
40#include <GLFW/glfw3native.h>
41#else
42#include <glfw/glfw3.h>
43#endif
44#include <util/types.hh>
45#include <span>
46
47namespace glfwpp {
48 enum ErrorCodes {
49 NotInitialized = 0x00010001,
50 NoCurrentContext = 0x00010002,
51 InvalidEnum = 0x00010003,
52 InvalidValue = 0x00010004,
53 OutOfMemory = 0x00010005,
54 ApiUnavailable = 0x00010006,
55 VersionUnavailable = 0x00010007,
56 PlatformError = 0x00010008,
57 FormatUnavailable = 0x00010009,
58 NoWindowContent = 0x0001000A,
59 };
60
61 enum Attributes {
62 Focused = 0x00020001,
63 Iconified = 0x00020002,
64 Resizable = 0x00020003,
65 visible = 0x00020004,
66 Decorated = 0x00020005,
67 AutoNotify = 0x00020006,
68 Floating = 0x00020007,
69 Maximized = 0x00020008,
70
71 RedBits = 0x00021001,
72 GreenBits = 0x00021002,
73 BlueBits = 0x00021003,
74 AlphaBits = 0x00021004,
75 DepthBits = 0x00021005,
76 StencilBits = 0x00021006,
77 AccumRedBits = 0x00021007,
78 AccumGreenBits = 0x00021008,
79 AccumBlueBits = 0x00021009,
80 AccumAlphaBits = 0x0002100A,
81 AuxBuffers = 0x0002100B,
82 Stereo = 0x0002100C,
83 Samples = 0x0002100D,
84 SrgbCapable = 0x0002100E,
85 RefreshRate = 0x0002100F,
86 Doublebuffer = 0x00021010,
87
88 ClientApi = 0x00022001,
89 ContextVersionMajor = 0x00022002,
90 ContextVersionMinor = 0x00022003,
91 ContextRevision = 0x00022004,
92 ContextRobustness = 0x00022005,
93 OpenGLForwardCompatable = 0x00022006,
94 OpenGLDebugContext = 0x00022007,
95 OpenGLProfile = 0x00022008,
96 ContextReleaseBehavior = 0x00022009,
97 ContextNoError = 0x0002200A,
98
99 NoApi = 0,
100 OpenGLApi = 0x00030001,
101 OpenGLESApi = 0x00030002,
102
103 NoRobustness = 0,
104 NoResetNotification = 0x00031001,
105 LoseContextOnReset = 0x00031002,
106
107 AnyOpenGLProfile = 0,
108 OpenGLCore = 0x00032001,
109 OpenGLCompat = 0x00032002,
110
111 Cursor = 0x00033001,
112 StickyKeys = 0x00033002,
113 StickyMouseButtons = 0x00033003,
114
115 CursorNormal = 0x00034001,
116 CursorHidden = 0x00034002,
117 CursorDisabled = 0x00034003,
118
119 AnyReleaseBehavior = 0,
120 ReleaseBehaviorFlush = 0x00035001,
121 ReleaseBehaviorNone = 0x00035002,
122 };
123
124 enum CursorShapes {
125 ArrowCursor = 0x00036001,
126 IbeamCursor = 0x00036002,
127 CrosshairCursor = 0x00036003,
128 HandCursor = 0x00036004,
129 HResizeCursor = 0x00036005,
130 VResizeCursor = 0x00036006,
131 };
132
133 enum Connection {
134 Connected = 0x00040001,
135 Disconnected = 0x00040002,
136 DontCare = -1,
137 };
138
139 enum ButtonMode {
140 LockKeyMods = 0x00032004,
141 RawMouseMotion = 0x00032005,
142 };
143
144 enum ContextApi {
145 Native = 0x00032001,
146 EGL = 0x00032002,
147 OSMESA = 0x00032003,
148 };
149
150 class Instance {
151 public:
152 Instance();
153 ~Instance();
154
155 GLFWerrorfun SetErrorCallback();
156
157 void SetContextMajorVersion(int major);
158 void SetContextMinorVersion(int minor);
159 void SetOpenGLProfile(int profile);
160 void SetForwardCompatibility(bool fc);
161 void SetMaximized(bool fc);
162 void SetApi(int api);
163 void SetResizable(bool tf);
164
165 void CreateWindowHints();
166 void PollEvents();
167
168 void SwapInterval(int interval);
169 double GetTime();
170 void SetTime(double time);
171 u64 GetTimerValue();
172 u64 GetTimerFrequency();
173
174 void InitHint(int hint, int value);
175 void GetVersion(int *major, int *minor, int *rev);
176 cstr GetVersionString(void);
177#if defined(USING_VULKAN)
178 const char **GetRequiredInstanceExtensions(u32 *count);
179#endif
180
181 int GetError(cstr *description);
182 GLFWerrorfun SetErrorCallback(GLFWerrorfun cbfun);
183
184 private:
185 int m_Major = 0;
186 int m_Minor = 0;
187 int m_Profile = 0;
188 int m_Api = 0;
189 bool m_ForwardCompat = false;
190 bool m_IsMaximized = false;
191 bool m_IsResizable = false;
192 }; // class Instance
193
194 class Window {
195 public:
196 Window(int width, int height, string title);
197 ~Window();
198
199 GLFWwindow *Get() const { return m_Window; }
200#if defined(USING_GLAD)
201 GLFWframebuffersizefun SetFramebufferSizeCallback();
202#endif
203
204#if defined(USING_VULKAN)
205 vk::Result CreateWindowSurface(vk::Instance instance, vk::SurfaceKHR &surface);
206#endif
207
208 bool ShouldClose() const;
209 void SwapBuffers();
210
211 void SetWindowShouldClose(bool value);
212 void GetSize(int *w, int *h);
213 void Maximize();
214 void SetClipboardString(string str);
215 string GetClipboardString();
216 void MakeContextCurrent();
217 int GetAttrib(int attrib);
218 int GetInputMode(int mode);
219 void SetInputMode(int mode, int value);
220 int RawMouseMotionSupported();
221 cstr GetKeyName(int key, int scancode);
222 int GetKeyScancode(int key);
223 int GetKey(int key);
224 int GetMouseButton(int button);
225 void GetCursorPos(double *xpos, double *ypos);
226 void SetCursorPos(double xpos, double ypos);
227
228 GLFWwindow *GetCurrentContext();
229 bool IsExtensionSupported(cstr ext);
230 GLFWglproc GetProcAddress(cstr procname);
231
232 GLFWkeyfun SetKeyCallback(GLFWkeyfun cbfun);
233 GLFWcharfun SetCharCallback(GLFWcharfun cbfun);
234 GLFWcharmodsfun SetCharModsCallback(GLFWcharmodsfun cbfun);
235 GLFWmousebuttonfun SetMouseButtonCallback(GLFWmousebuttonfun cbfun);
236 GLFWcursorposfun SetCursorPosCallback(GLFWcursorposfun cbfun);
237 GLFWcursorenterfun SetCursorEnterCallback(GLFWcursorenterfun cbfun);
238 GLFWscrollfun SetScrollCallback(GLFWscrollfun cbfun);
239 GLFWdropfun SetDropCallback(GLFWdropfun cbfun);
240
241 int JoystickPresent(int jid);
242 const f32 *GetJoystickAxes(int jid, int *count);
243 const unsigned char *GetJoystickButtons(int jid, int *count);
244 const unsigned char *GetJoystickHats(int jid, int *count);
245 cstr GetJoystickName(int jid);
246 cstr GetJoystickGUID(int jid);
247 void SetJoystickUserPointer(int jid, any ptr);
248 any GetJoystickUserPointer(int jid);
249 GLFWjoystickfun SetJoystickCallback(GLFWjoystickfun cbfun);
250 int UpdateGamepadMappings(string str);
251 int JoystickIsGamepad(int jid);
252 cstr GetGamepadName(int jid);
253 int GetGamepadState(int jid, GLFWgamepadstate *state);
254
255 void GetFramebufferSize(int &width, int &height);
256
257 private:
258 GLFWwindow *m_Window = {};
259 int m_Width = 0;
260 int m_Height = 0;
261 }; // class Window
262
263 class Monitor {
264 public:
265 Monitor();
266 ~Monitor();
267
268 GLFWmonitor *Get() const { return m_Monitor; }
269 std::span<GLFWmonitor *> GetMonitors(int *count);
270 GLFWmonitor *GetPrimary();
271 void GetPos(int *xpos, int *ypos);
272 void GetWorkarea(int *xpos, int *ypos, int *width, int *height);
273 void GetPhysicalSize(int *width, int *height);
274 void GetContentScale(float *xscale, float *yscale);
275 cstr GetName();
276 void SetUserPointer(any ptr);
277 any GetUserPointer();
278 GLFWmonitorfun SetCallback(GLFWmonitorfun cbfun);
279 const GLFWvidmode *GetVideoModes(int *count);
280 const GLFWvidmode *GetVideoMode();
281 void SetGamma(float gamma);
282 const GLFWgammaramp *GetGammaRamp();
283 void SetGammaRamp(const GLFWgammaramp *ramp);
284
285 private:
286 GLFWmonitor *m_Monitor = {};
287 }; // class Monitor
288
289 class Cursor {
290 public:
291 Cursor(GLFWimage *img, int xhot, int yhot);
292 ~Cursor();
293
294 GLFWcursor *Get() const { return m_Cursor; }
295 GLFWcursor *CreateStandardCursor(int shape);
296 void Set(Window window);
297
298 private:
299 GLFWimage *m_Image;
300 GLFWcursor *m_Cursor;
301 }; // class Cursor
302} // namespace glfwpp
303
304#endif // GLFWPP_HH