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