|
@@ -1,14 +1,21 @@
|
|
|
-#include <iostream>
|
|
|
-
|
|
|
#include "rendering/Window.h"
|
|
|
|
|
|
+int Window::glfwCounter = 0;
|
|
|
+int Window::glewCounter = 0;
|
|
|
+
|
|
|
Window::Window(TextInput*& textInput, const WindowOptions& options)
|
|
|
- : textInput(textInput), window(nullptr) {
|
|
|
+ : textInput(textInput), window(nullptr), size(options.size) {
|
|
|
+ if(glfwCounter == 0 && !glfwInit()) {
|
|
|
+ error.message.clear().append("could not initialize GLFW");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ glfwCounter++;
|
|
|
+
|
|
|
glfwDefaultWindowHints();
|
|
|
- glfwWindowHint(GLFW_VISIBLE, 0);
|
|
|
- glfwWindowHint(GLFW_RESIZABLE, 1);
|
|
|
+ glfwWindowHint(GLFW_VISIBLE, false);
|
|
|
+ glfwWindowHint(GLFW_RESIZABLE, true);
|
|
|
glfwWindowHint(GLFW_DECORATED, !options.fullscreen);
|
|
|
- glfwWindowHint(GLFW_DOUBLEBUFFER, 1);
|
|
|
+ glfwWindowHint(GLFW_DOUBLEBUFFER, true);
|
|
|
|
|
|
if(options.es) {
|
|
|
glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API);
|
|
@@ -23,79 +30,78 @@ Window::Window(TextInput*& textInput, const WindowOptions& options)
|
|
|
window = glfwCreateWindow(options.size.width, options.size.height,
|
|
|
options.name, monitor, nullptr);
|
|
|
if(window == nullptr) {
|
|
|
- std::cout << "could not create window\n";
|
|
|
+ error.message.clear().append("could not create window");
|
|
|
return;
|
|
|
}
|
|
|
glfwSetWindowUserPointer(window, this);
|
|
|
- glfwSetKeyCallback(window, keyCallback);
|
|
|
- glfwSetCharCallback(window, charCallback);
|
|
|
+ glfwSetKeyCallback(window, onKey);
|
|
|
+ glfwSetCharCallback(window, onChar);
|
|
|
+ glfwSetFramebufferSizeCallback(window, onResize);
|
|
|
+ glfwSetMouseButtonCallback(window, onMouse);
|
|
|
+ glfwSetCursorPosCallback(window, onMouseMove);
|
|
|
|
|
|
glfwMakeContextCurrent(window);
|
|
|
glfwSwapInterval(options.vsync);
|
|
|
+
|
|
|
+ if(glewCounter == 0) {
|
|
|
+ GLenum err = glewInit();
|
|
|
+ if(err != GLEW_OK) {
|
|
|
+ error.message.clear()
|
|
|
+ .append("could not initialize GLEW: ")
|
|
|
+ .append(glewGetErrorString(err));
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ glewCounter++;
|
|
|
}
|
|
|
|
|
|
Window::~Window() {
|
|
|
if(window != nullptr) {
|
|
|
glfwDestroyWindow(window);
|
|
|
}
|
|
|
+ glfwCounter--;
|
|
|
+ if(glfwCounter <= 0) {
|
|
|
+ glfwTerminate();
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
-bool Window::hasError() const {
|
|
|
- return window == nullptr;
|
|
|
-}
|
|
|
-
|
|
|
-void Window::show() {
|
|
|
- glfwShowWindow(window);
|
|
|
-}
|
|
|
-
|
|
|
-bool Window::shouldClose() const {
|
|
|
- return glfwWindowShouldClose(window);
|
|
|
-}
|
|
|
-
|
|
|
-void Window::swapBuffers() {
|
|
|
- glfwSwapBuffers(window);
|
|
|
+const Error& Window::getError() const {
|
|
|
+ return error;
|
|
|
}
|
|
|
|
|
|
-void Window::trapCursor(bool trap) {
|
|
|
- glfwSetInputMode(window, GLFW_CURSOR,
|
|
|
- trap ? GLFW_CURSOR_DISABLED : GLFW_CURSOR_NORMAL);
|
|
|
+const Clock& Window::getFrameClock() const {
|
|
|
+ return fps;
|
|
|
}
|
|
|
|
|
|
-Size Window::getSize() const {
|
|
|
- Size size(0, 0);
|
|
|
- glfwGetWindowSize(window, &size.width, &size.height);
|
|
|
- return size;
|
|
|
+const Clock& Window::getTickClock() const {
|
|
|
+ return tps;
|
|
|
}
|
|
|
|
|
|
-Size Window::getFramebufferSize() const {
|
|
|
- Size size(0, 0);
|
|
|
- glfwGetFramebufferSize(window, &size.width, &size.height);
|
|
|
+const Size& Window::getSize() const {
|
|
|
return size;
|
|
|
}
|
|
|
|
|
|
-bool Window::isKeyDown(int key) const {
|
|
|
- return glfwGetKey(window, key) == GLFW_PRESS;
|
|
|
-}
|
|
|
-
|
|
|
-bool Window::isMouseDown(int mouse) const {
|
|
|
- return glfwGetMouseButton(window, mouse) == GLFW_PRESS;
|
|
|
+Buttons& Window::getButtons() {
|
|
|
+ return buttons;
|
|
|
}
|
|
|
|
|
|
-void Window::getMousePosition(double& x, double& y) const {
|
|
|
- glfwGetCursorPos(window, &x, &y);
|
|
|
+void Window::trapCursor(bool trap) {
|
|
|
+ glfwSetInputMode(window, GLFW_CURSOR,
|
|
|
+ trap ? GLFW_CURSOR_DISABLED : GLFW_CURSOR_NORMAL);
|
|
|
}
|
|
|
|
|
|
-void Window::keyCallback(GLFWwindow* w, int key, int scancode, int action,
|
|
|
- int mods) {
|
|
|
+void Window::onKey(GLFWwindow* w, int key, int scancode, int action, int mods) {
|
|
|
void* p = glfwGetWindowUserPointer(w);
|
|
|
if(p != nullptr) {
|
|
|
- TextInput* input = static_cast<Window*>(p)->textInput;
|
|
|
+ Window* rw = static_cast<Window*>(p);
|
|
|
+ TextInput* input = rw->textInput;
|
|
|
if(input != nullptr) {
|
|
|
input->onKeyEvent(key, scancode, action, mods);
|
|
|
}
|
|
|
+ rw->buttons.onKey(key, scancode, action, mods);
|
|
|
}
|
|
|
}
|
|
|
-void Window::charCallback(GLFWwindow* w, unsigned int codepoint) {
|
|
|
+void Window::onChar(GLFWwindow* w, unsigned int codepoint) {
|
|
|
void* p = glfwGetWindowUserPointer(w);
|
|
|
if(p != nullptr) {
|
|
|
TextInput* input = static_cast<Window*>(p)->textInput;
|
|
@@ -103,4 +109,27 @@ void Window::charCallback(GLFWwindow* w, unsigned int codepoint) {
|
|
|
input->onCharEvent(codepoint);
|
|
|
}
|
|
|
}
|
|
|
+}
|
|
|
+
|
|
|
+void Window::onResize(GLFWwindow* w, int width, int height) {
|
|
|
+ void* p = glfwGetWindowUserPointer(w);
|
|
|
+ if(p != nullptr) {
|
|
|
+ Window* rw = static_cast<Window*>(p);
|
|
|
+ rw->size.width = width;
|
|
|
+ rw->size.height = height;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+void Window::onMouse(GLFWwindow* w, int button, int action, int mods) {
|
|
|
+ void* p = glfwGetWindowUserPointer(w);
|
|
|
+ if(p != nullptr) {
|
|
|
+ static_cast<Window*>(p)->buttons.onMouse(button, action, mods);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+void Window::onMouseMove(GLFWwindow* w, double x, double y) {
|
|
|
+ void* p = glfwGetWindowUserPointer(w);
|
|
|
+ if(p != nullptr) {
|
|
|
+ static_cast<Window*>(p)->buttons.onMouseMove(x, y);
|
|
|
+ }
|
|
|
}
|