diff options
author | Joursoir <chat@joursoir.net> | 2021-03-26 15:32:22 +0000 |
---|---|---|
committer | Joursoir <chat@joursoir.net> | 2021-03-26 15:32:22 +0000 |
commit | aee5bf7802f112111961faa824deb6f97ef80203 (patch) | |
tree | 97f264dff7e3f2fca508800ba0960523e84f8c7f /src/window | |
parent | 533abb2b643a0f8e1d41e0d570e08e3df5339f80 (diff) | |
download | space-simulator-aee5bf7802f112111961faa824deb6f97ef80203.tar.gz space-simulator-aee5bf7802f112111961faa824deb6f97ef80203.tar.bz2 space-simulator-aee5bf7802f112111961faa824deb6f97ef80203.zip |
add window subsystem
Diffstat (limited to 'src/window')
-rw-r--r-- | src/window/Window.cpp | 70 | ||||
-rw-r--r-- | src/window/Window.hpp | 26 |
2 files changed, 96 insertions, 0 deletions
diff --git a/src/window/Window.cpp b/src/window/Window.cpp new file mode 100644 index 0000000..7aef326 --- /dev/null +++ b/src/window/Window.cpp @@ -0,0 +1,70 @@ +#include <stdio.h> +#include <GL/glew.h> +#include <GLFW/glfw3.h> + +#include "Window.hpp" + +/* static method */ +Window *Window::Initialize(int width, int height, const char *title) +{ + GLFWwindow *win; + glfwDefaultWindowHints(); + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); + glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); + glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE); + /*glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GLFW_TRUE); + glfwSetErrorCallback(error_callback);*/ + + win = glfwCreateWindow(width, height, title, 0, 0); + if(!win) { + fprintf(stderr, "Failed to create GLFW window\n"); + return 0; + } + glfwMakeContextCurrent(win); + + glewExperimental = GL_TRUE; + int result = glewInit(); + if(result != GLEW_OK) { + fprintf(stderr, "Failed to init GLEW: %s\n", title); + return 0; + } + + Window *w = new Window(win, width, height); + w->MakeContextCurrent(); + return w; +} + +void Window::Resize(int w, int h) +{ + width = w; + height = h; + if(glfwGetCurrentContext() == win) + glViewport(0, 0, width, height); +} + +void Window::MakeContextCurrent() +{ + glfwMakeContextCurrent(win); + glViewport(0, 0, width, height); +} + +void Window::SetCursorMode(int mode) +{ + glfwSetInputMode(win, GLFW_CURSOR, mode); +} + +bool Window::IsShouldClose() +{ + return glfwWindowShouldClose(win); +} + +void Window::SetShouldClose(int flag) +{ + glfwSetWindowShouldClose(win, flag); +} + +void Window::SwapBuffers() +{ + glfwSwapBuffers(win); +} diff --git a/src/window/Window.hpp b/src/window/Window.hpp new file mode 100644 index 0000000..fe3b054 --- /dev/null +++ b/src/window/Window.hpp @@ -0,0 +1,26 @@ +#ifndef ENGINE_WINDOW_H +#define ENGINE_WINDOW_H + +class GLFWwindow; + +class Window { + GLFWwindow *win; + int width, height; + + Window(GLFWwindow *a_win, int a_w, int a_h) : win(a_win), + width(a_w), height(a_h) { } +public: + ~Window() { } + GLFWwindow *GetWin() { return win; } + + static Window *Initialize(int width, int height, const char *title); + + void Resize(int w, int h); + void MakeContextCurrent(); + void SetCursorMode(int mode); + bool IsShouldClose(); + void SetShouldClose(int flag); + void SwapBuffers(); +}; + +#endif /* ENGINE_WINDOW_H */ |