| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | #include <util/glfwpp.hh> |
| 25 | #include <iostream> |
| 26 | |
| 27 | namespace glfwpp { |
| 28 | Instance::Instance() { |
| 29 | if (!glfwInit()) { |
| 30 | throw std::runtime_error("GLFWの初期化に失敗しました"); |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | Instance::~Instance() { |
| 35 | glfwTerminate(); |
| 36 | } |
| 37 | |
| 38 | static void glfw_error_callback(int error, cstr description) { |
| 39 | std::cerr << "GLFWエラー " << error << ":\n" << description << std::endl; |
| 40 | } |
| 41 | |
| 42 | GLFWerrorfun Instance::SetErrorCallback() { |
| 43 | glfwSetErrorCallback(glfw_error_callback); |
| 44 | return nullptr; |
| 45 | } |
| 46 | |
| 47 | void Instance::SetContextMajorVersion(int major) { |
| 48 | m_Major = major; |
| 49 | } |
| 50 | |
| 51 | void Instance::SetContextMinorVersion(int minor) { |
| 52 | m_Minor = minor; |
| 53 | } |
| 54 | |
| 55 | void Instance::SetOpenGLProfile(int profile) { |
| 56 | m_Profile = profile; |
| 57 | } |
| 58 | |
| 59 | void Instance::SetForwardCompatibility(bool fc) { |
| 60 | m_ForwardCompat = fc; |
| 61 | } |
| 62 | |
| 63 | void Instance::SetMaximized(bool fc) { |
| 64 | m_IsMaximized = fc; |
| 65 | } |
| 66 | |
| 67 | void Instance::CreateWindowHints() { |
| 68 | glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, m_Major); |
| 69 | glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, m_Minor); |
| 70 | glfwWindowHint(GLFW_OPENGL_PROFILE, m_Profile); |
| 71 | glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, m_ForwardCompat); |
| 72 | glfwWindowHint(GLFW_MAXIMIZED, m_IsMaximized); |
| 73 | } |
| 74 | |
| 75 | void Instance::PollEvents() { |
| 76 | glfwPollEvents(); |
| 77 | } |
| 78 | |
| 79 | void Instance::SwapInterval(int interval) { |
| 80 | glfwSwapInterval(interval); |
| 81 | } |
| 82 | |
| 83 | double Instance::GetTime() { |
| 84 | return glfwGetTime(); |
| 85 | } |
| 86 | |
| 87 | void Instance::SetTime(double time) { |
| 88 | return glfwSetTime(time); |
| 89 | } |
| 90 | |
| 91 | u64 Instance::GetTimerValue() { |
| 92 | return glfwGetTimerValue(); |
| 93 | } |
| 94 | |
| 95 | u64 Instance::GetTimerFrequency() { |
| 96 | return glfwGetTimerFrequency(); |
| 97 | } |
| 98 | |
| 99 | void Instance::InitHint(int hint, int value) { |
| 100 | return glfwInitHint(hint, value); |
| 101 | } |
| 102 | |
| 103 | void Instance::GetVersion(int *major, int *minor, int *rev) { |
| 104 | glfwGetVersion(major, minor, rev); |
| 105 | } |
| 106 | |
| 107 | cstr Instance::GetVersionString(void) { |
| 108 | return glfwGetVersionString(); |
| 109 | } |
| 110 | |
| 111 | int Instance::GetError(cstr *description) { |
| 112 | return glfwGetError(description); |
| 113 | } |
| 114 | |
| 115 | GLFWerrorfun Instance::SetErrorCallback(GLFWerrorfun cbfun) { |
| 116 | return glfwSetErrorCallback(cbfun); |
| 117 | } |
| 118 | |
| 119 | |
| 120 | |
| 121 | Window::Window(int width, int height, string title) { |
| 122 | m_Window = glfwCreateWindow(width, height, title.c_str(), nullptr, nullptr); |
| 123 | m_Width = width; |
| 124 | m_Height = height; |
| 125 | if (!m_Window) { |
| 126 | throw std::runtime_error("GLFWウィンドウの作成に失敗しました"); |
| 127 | } |
| 128 | |
| 129 | glfwMakeContextCurrent(m_Window); |
| 130 | } |
| 131 | |
| 132 | Window::~Window() { |
| 133 | glfwDestroyWindow(m_Window); |
| 134 | } |
| 135 | |
| 136 | void framebuffer_size_callback(GLFWwindow *window, int width, int height) { |
| 137 | (void)window; |
| 138 | glViewport(0, 0, width, height); |
| 139 | } |
| 140 | |
| 141 | GLFWframebuffersizefun Window::SetFramebufferSizeCallback() { |
| 142 | glfwSetFramebufferSizeCallback(m_Window, framebuffer_size_callback); |
| 143 | return nullptr; |
| 144 | } |
| 145 | |
| 146 | bool Window::ShouldClose() const { |
| 147 | return glfwWindowShouldClose(m_Window); |
| 148 | } |
| 149 | |
| 150 | void Window::SwapBuffers() { |
| 151 | glfwSwapBuffers(m_Window); |
| 152 | } |
| 153 | |
| 154 | void Window::GetSize(int *w, int *h) { |
| 155 | glfwGetWindowSize(m_Window, w, h); |
| 156 | } |
| 157 | |
| 158 | void Window::Maximize() { |
| 159 | glfwMaximizeWindow(m_Window); |
| 160 | } |
| 161 | |
| 162 | void Window::SetClipboardString(string str) { |
| 163 | glfwSetClipboardString(m_Window, str.c_str()); |
| 164 | } |
| 165 | |
| 166 | string Window::GetClipboardString() { |
| 167 | return string(glfwGetClipboardString(m_Window)); |
| 168 | } |
| 169 | |
| 170 | void Window::MakeContextCurrent() { |
| 171 | glfwMakeContextCurrent(m_Window); |
| 172 | } |
| 173 | |
| 174 | int Window::GetAttrib(int attrib) { |
| 175 | return glfwGetWindowAttrib(m_Window, attrib); |
| 176 | } |
| 177 | |
| 178 | int Window::GetInputMode(int mode) { |
| 179 | return glfwGetInputMode(m_Window, mode); |
| 180 | } |
| 181 | |
| 182 | void Window::SetInputMode(int mode, int value) { |
| 183 | glfwSetInputMode(m_Window, mode, value); |
| 184 | } |
| 185 | |
| 186 | int Window::RawMouseMotionSupported() { |
| 187 | return glfwRawMouseMotionSupported(); |
| 188 | } |
| 189 | |
| 190 | cstr Window::GetKeyName(int key, int scancode) { |
| 191 | return glfwGetKeyName(key, scancode); |
| 192 | } |
| 193 | |
| 194 | int Window::GetKeyScancode(int key) { |
| 195 | return glfwGetKeyScancode(key); |
| 196 | } |
| 197 | |
| 198 | int Window::GetKey(int key) { |
| 199 | return glfwGetKey(m_Window, key); |
| 200 | } |
| 201 | |
| 202 | int Window::GetMouseButton(int button) { |
| 203 | return glfwGetMouseButton(m_Window, button); |
| 204 | } |
| 205 | |
| 206 | void Window::GetCursorPos(double *xpos, double *ypos) { |
| 207 | return glfwGetCursorPos(m_Window, xpos, ypos); |
| 208 | } |
| 209 | |
| 210 | void Window::SetCursorPos(double xpos, double ypos) { |
| 211 | return glfwSetCursorPos(m_Window, xpos, ypos); |
| 212 | } |
| 213 | |
| 214 | GLFWwindow *Window::GetCurrentContext() { |
| 215 | return glfwGetCurrentContext(); |
| 216 | } |
| 217 | |
| 218 | bool Window::IsExtensionSupported(cstr ext) { |
| 219 | return glfwExtensionSupported(ext); |
| 220 | } |
| 221 | |
| 222 | GLFWglproc Window::GetProcAddress(cstr procname) { |
| 223 | return glfwGetProcAddress(procname); |
| 224 | } |
| 225 | |
| 226 | GLFWkeyfun Window::SetKeyCallback(GLFWkeyfun cbfun) { |
| 227 | return glfwSetKeyCallback(m_Window, cbfun); |
| 228 | } |
| 229 | |
| 230 | GLFWcharfun Window::SetCharCallback(GLFWcharfun cbfun) { |
| 231 | return glfwSetCharCallback(m_Window, cbfun); |
| 232 | } |
| 233 | |
| 234 | GLFWcharmodsfun Window::SetCharModsCallback(GLFWcharmodsfun cbfun) { |
| 235 | return glfwSetCharModsCallback(m_Window, cbfun); |
| 236 | } |
| 237 | |
| 238 | GLFWmousebuttonfun Window::SetMouseButtonCallback(GLFWmousebuttonfun cbfun) { |
| 239 | return glfwSetMouseButtonCallback(m_Window, cbfun); |
| 240 | } |
| 241 | |
| 242 | GLFWcursorposfun Window::SetCursorPosCallback(GLFWcursorposfun cbfun) { |
| 243 | return glfwSetCursorPosCallback(m_Window, cbfun); |
| 244 | } |
| 245 | |
| 246 | GLFWcursorenterfun Window::SetCursorEnterCallback(GLFWcursorenterfun cbfun) { |
| 247 | return glfwSetCursorEnterCallback(m_Window, cbfun); |
| 248 | } |
| 249 | |
| 250 | GLFWscrollfun Window::SetScrollCallback(GLFWscrollfun cbfun) { |
| 251 | return glfwSetScrollCallback(m_Window, cbfun); |
| 252 | } |
| 253 | |
| 254 | GLFWdropfun Window::SetDropCallback(GLFWdropfun cbfun) { |
| 255 | return glfwSetDropCallback(m_Window, cbfun); |
| 256 | } |
| 257 | |
| 258 | int Window::JoystickPresent(int jid) { |
| 259 | return glfwJoystickPresent(jid); |
| 260 | } |
| 261 | |
| 262 | const f32 *Window::GetJoystickAxes(int jid, int *count) { |
| 263 | return glfwGetJoystickAxes(jid, count); |
| 264 | } |
| 265 | |
| 266 | const unsigned char *Window::GetJoystickButtons(int jid, int *count) { |
| 267 | return glfwGetJoystickButtons(jid, count); |
| 268 | } |
| 269 | |
| 270 | const unsigned char *Window::GetJoystickHats(int jid, int *count) { |
| 271 | return glfwGetJoystickHats(jid, count); |
| 272 | } |
| 273 | |
| 274 | cstr Window::GetJoystickName(int jid) { |
| 275 | return glfwGetJoystickName(jid); |
| 276 | } |
| 277 | |
| 278 | cstr Window::GetJoystickGUID(int jid) { |
| 279 | return glfwGetJoystickGUID(jid); |
| 280 | } |
| 281 | |
| 282 | void Window::SetJoystickUserPointer(int jid, any ptr) { |
| 283 | return glfwSetJoystickUserPointer(jid, ptr); |
| 284 | } |
| 285 | |
| 286 | any Window::GetJoystickUserPointer(int jid) { |
| 287 | return glfwGetJoystickUserPointer(jid); |
| 288 | } |
| 289 | |
| 290 | GLFWjoystickfun Window::SetJoystickCallback(GLFWjoystickfun cbfun) { |
| 291 | return glfwSetJoystickCallback(cbfun); |
| 292 | } |
| 293 | |
| 294 | int Window::UpdateGamepadMappings(string str) { |
| 295 | return glfwUpdateGamepadMappings(str.c_str()); |
| 296 | } |
| 297 | |
| 298 | int Window::JoystickIsGamepad(int jid) { |
| 299 | return glfwJoystickIsGamepad(jid); |
| 300 | } |
| 301 | |
| 302 | cstr Window::GetGamepadName(int jid) { |
| 303 | return glfwGetGamepadName(jid); |
| 304 | } |
| 305 | |
| 306 | int Window::GetGamepadState(int jid, GLFWgamepadstate *state) { |
| 307 | return glfwGetGamepadState(jid, state); |
| 308 | } |
| 309 | |
| 310 | |
| 311 | |
| 312 | std::span<GLFWmonitor *> Monitor::GetMonitors(int *count) { |
| 313 | if (count) *count = 0; |
| 314 | GLFWmonitor **mon = glfwGetMonitors(count); |
| 315 | if (mon == nullptr || count == nullptr || *count <= 0) return {}; |
| 316 | return std::span<GLFWmonitor *>(mon, mon + *count); |
| 317 | } |
| 318 | |
| 319 | GLFWmonitor *Monitor::GetPrimary() { |
| 320 | return glfwGetPrimaryMonitor(); |
| 321 | } |
| 322 | |
| 323 | void Monitor::GetPos(int *xpos, int *ypos) { |
| 324 | glfwGetMonitorPos(m_Monitor, xpos, ypos); |
| 325 | } |
| 326 | |
| 327 | void Monitor::GetWorkarea(int *xpos, int *ypos, int *width, int *height) { |
| 328 | glfwGetMonitorWorkarea(m_Monitor, xpos, ypos, width, height); |
| 329 | } |
| 330 | |
| 331 | void Monitor::GetPhysicalSize(int *width, int *height) { |
| 332 | glfwGetMonitorPhysicalSize(m_Monitor, width, height); |
| 333 | } |
| 334 | |
| 335 | void Monitor::GetContentScale(float *xscale, float *yscale) { |
| 336 | glfwGetMonitorContentScale(m_Monitor, xscale, yscale); |
| 337 | } |
| 338 | |
| 339 | cstr Monitor::GetName() { |
| 340 | return glfwGetMonitorName(m_Monitor); |
| 341 | } |
| 342 | |
| 343 | void Monitor::SetUserPointer(any ptr) { |
| 344 | glfwSetMonitorUserPointer(m_Monitor, ptr); |
| 345 | } |
| 346 | |
| 347 | any Monitor::GetUserPointer() { |
| 348 | return glfwGetMonitorUserPointer(m_Monitor); |
| 349 | } |
| 350 | |
| 351 | GLFWmonitorfun Monitor::SetCallback(GLFWmonitorfun cbfun) { |
| 352 | return glfwSetMonitorCallback(cbfun); |
| 353 | } |
| 354 | |
| 355 | const GLFWvidmode *Monitor::GetVideoModes(int *count) { |
| 356 | return glfwGetVideoModes(m_Monitor, count); |
| 357 | } |
| 358 | |
| 359 | const GLFWvidmode *Monitor::GetVideoMode() { |
| 360 | return glfwGetVideoMode(m_Monitor); |
| 361 | } |
| 362 | |
| 363 | void Monitor::SetGamma(float gamma) { |
| 364 | return glfwSetGamma(m_Monitor, gamma); |
| 365 | } |
| 366 | |
| 367 | const GLFWgammaramp *Monitor::GetGammaRamp() { |
| 368 | return glfwGetGammaRamp(m_Monitor); |
| 369 | } |
| 370 | |
| 371 | void Monitor::SetGammaRamp(const GLFWgammaramp *ramp) { |
| 372 | return glfwSetGammaRamp(m_Monitor, ramp); |
| 373 | } |
| 374 | |
| 375 | |
| 376 | |
| 377 | Cursor::Cursor(GLFWimage *img, int xhot, int yhot) : m_Image(img) { |
| 378 | m_Cursor = glfwCreateCursor(m_Image, xhot, yhot); |
| 379 | } |
| 380 | |
| 381 | Cursor::~Cursor() { |
| 382 | glfwDestroyCursor(m_Cursor); |
| 383 | } |
| 384 | |
| 385 | GLFWcursor *Cursor::CreateStandardCursor(int shape) { |
| 386 | return glfwCreateStandardCursor(shape); |
| 387 | } |
| 388 | |
| 389 | void Cursor::Set(Window window) { |
| 390 | glfwSetCursor(window.Get(), m_Cursor); |
| 391 | } |
| 392 | } |