This repository was archived by the owner on Feb 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwindow_glfw.cpp
More file actions
164 lines (129 loc) · 4.23 KB
/
Copy pathwindow_glfw.cpp
File metadata and controls
164 lines (129 loc) · 4.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/**
* Copyright (C) 2020 Tristan. All Rights Reserved.
* This file is licensed under the BSD 2-Clause license.
* See the COPYING file for licensing information.
*/
#include "window_glfw.hpp"
#include <iostream>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glx.h>
#include "logger.hpp"
#include "rendering/input.hpp"
namespace GLFWSuite {
[[nodiscard]] inline constexpr Rendering::MouseButton
ResolveMouseButton(int button) noexcept {
switch (button) {
case GLFW_MOUSE_BUTTON_LEFT:
return Rendering::MouseButton::LEFT;
case GLFW_MOUSE_BUTTON_MIDDLE:
return Rendering::MouseButton::MIDDLE;
case GLFW_MOUSE_BUTTON_RIGHT:
return Rendering::MouseButton::RIGHT;
default:
// Unknown mouse button that we don't care abt
return Rendering::MouseButton::ILLEGAL;
}
}
void
MouseButtonCallback(GLFWwindow *window, int button, int action, int mods) {
static_cast<void>(mods);
auto *renderer = reinterpret_cast<Rendering::WindowGLFW *>(glfwGetWindowUserPointer(window))->renderer;
const auto mouseButton = ResolveMouseButton(button);
if (mouseButton == Rendering::MouseButton::ILLEGAL) {
return;
}
double x, y;
glfwGetCursorPos(window, &x, &y);
switch (action) {
case GLFW_PRESS:
renderer->OnMouseDown(mouseButton, x, y);
break;
default:
break;
}
}
} // namespace GLFWSuite
namespace Rendering {
/* Since the window is the most important thing of the application, there
* isn't really need for graceful error handling (I think). Therefore I can
* call std::runtime_error() */
WindowGLFW::WindowGLFW() : WindowBase("GLFW"), internalWindow(nullptr) {
if (glfwInit() == GLFW_FALSE) {
throw std::runtime_error("GLFW initialization failed.");
}
}
WindowGLFW::~WindowGLFW() noexcept {
if (internalWindow != nullptr)
glfwDestroyWindow(internalWindow);
glfwTerminate();
}
std::vector<RendererType>
WindowGLFW::GetSupportedRenderers() const noexcept {
return {RendererType::OPENGL};
}
std::pair<bool, std::optional<void *>>
WindowGLFW::RegisterRenderer(Renderer &renderer) {
this->renderer = &renderer;
switch (renderer.type()) {
case RendererType::OPENGL:
return {InternalPrepareGL(), {}};
default:
Logger::Error("GLFW", "Renderer not recognised!");
return {false, {}};
}
}
void
GLFWErrorHandler(int error, const char *message) noexcept {
(void)error;
Logger::Error("GLFW", message);
}
bool
WindowGLFW::InternalPrepareGL() {
glfwSetErrorCallback(GLFWErrorHandler);
float sizeFactor = 0.8;
if (internalWindow != nullptr) {
Logger::Severe("GLFW", "InternalPrepareGL() called twice! Quitting...");
throw std::runtime_error("InternalPrepareGL called twice!");
}
GLFWmonitor *monitor = glfwGetPrimaryMonitor();
if (monitor == nullptr) {
Logger::Error("GLFW", "Failed to retrieve monitor information.");
return false;
}
const GLFWvidmode *videoMode = glfwGetVideoMode(monitor);
if (videoMode == nullptr) {
Logger::Error("GLFW", "Failed to retrieve monitor information.");
return false;
}
std::cout << "Width: " << videoMode->width << " Height: " << videoMode->height << std::endl;
width = static_cast<uint32_t>(static_cast<float>(videoMode->width) * sizeFactor);
height = static_cast<uint32_t>(static_cast<float>(videoMode->height) * sizeFactor);
std::cout << "Width: " << this->width << " Height: " << this->height << std::endl;
internalWindow = glfwCreateWindow(width, height, "WebEngine", nullptr, nullptr);
if (internalWindow == nullptr) {
Logger::Error("GLFW", "Failed to create window!");
return false;
}
/* Center the window */
glfwSetWindowPos(internalWindow, static_cast<uint32_t>((videoMode->width - this->width) / 2),
static_cast<uint32_t>((videoMode->height - this->height) / 2));
glfwSetWindowUserPointer(internalWindow, this);
glfwSetMouseButtonCallback(internalWindow, &GLFWSuite::MouseButtonCallback);
glfwMakeContextCurrent(internalWindow);
return true;
}
bool
WindowGLFW::PollClose() noexcept {
glfwPollEvents();
return glfwWindowShouldClose(internalWindow) == GLFW_TRUE;
}
void
WindowGLFW::SetTitle(Unicode::UString title) noexcept {
(void)title; // BUG
}
void
WindowGLFW::SwapBuffers() noexcept {
glfwSwapBuffers(internalWindow);
}
} // namespace Rendering