Browse Source

window and options

Kajetan Johannes Hammerle 3 years ago
parent
commit
e3afd01bbb
7 changed files with 141 additions and 4 deletions
  1. 3 1
      meson.build
  2. 9 2
      wrapper/GL.cpp
  3. 3 1
      wrapper/GL.h
  4. 70 0
      wrapper/Window.cpp
  5. 32 0
      wrapper/Window.h
  6. 6 0
      wrapper/WindowOptions.cpp
  7. 18 0
      wrapper/WindowOptions.h

+ 3 - 1
meson.build

@@ -37,7 +37,9 @@ sources = ['Main.cpp',
     'wrapper/GLEW.cpp', 
     'wrapper/Shader.cpp', 
     'wrapper/VertexBuffer.cpp',
-    'wrapper/Attributes.cpp']
+    'wrapper/Attributes.cpp',
+    'wrapper/Window.cpp',
+    'wrapper/WindowOptions.cpp']
 
 glewDep = dependency('glew')
 glfwDep = dependency('glfw3')

+ 9 - 2
wrapper/GL.cpp

@@ -43,9 +43,12 @@ void GL::disableDepthTesting() {
     glDisable(GL_DEPTH_TEST);
 }
 
-void GL::prepareMainFramebuffer() {
+void GL::bindMainFramebuffer() {
     glBindFramebuffer(GL_FRAMEBUFFER, 0);
-    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
+}
+
+void GL::clearFramebuffer() {
+    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 }
 
 void GL::enableBlending() {
@@ -56,4 +59,8 @@ void GL::enableBlending() {
 
 void GL::disableBlending() {
     glDisable(GL_BLEND);
+}
+
+void GL::setViewport(int width, int height) {
+    glViewport(0, 0, width, height);
 }

+ 3 - 1
wrapper/GL.h

@@ -5,9 +5,11 @@ namespace GL {
     bool checkAndPrintError(const char* message);
     void enableDepthTesting();
     void disableDepthTesting();
-    void prepareMainFramebuffer();
+    void bindMainFramebuffer();
+    void clearFramebuffer();
     void enableBlending();
     void disableBlending();
+    void setViewport(int width, int height);
 }
 
 

+ 70 - 0
wrapper/Window.cpp

@@ -0,0 +1,70 @@
+#include <iostream>
+
+#include "wrapper/Window.h"
+
+Window::Window(const WindowOptions& options) : window(nullptr) {
+    glfwDefaultWindowHints();
+    glfwWindowHint(GLFW_VISIBLE, 0);
+    glfwWindowHint(GLFW_RESIZABLE, 1);
+    glfwWindowHint(GLFW_DECORATED, !options.fullscreen);
+    glfwWindowHint(GLFW_DOUBLEBUFFER, 1);
+
+    if(options.es) {
+        glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API);
+    } else {
+        glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
+    }
+    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, options.majorVersion);
+    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, options.minorVersion);
+
+    GLFWmonitor* monitor = options.fullscreen ? glfwGetPrimaryMonitor() : nullptr;
+    window = glfwCreateWindow(options.size.width, options.size.height, options.name, monitor, nullptr);
+    if(window == nullptr) {
+        std::cout << "could not create window\n";
+        return;
+    }
+    glfwMakeContextCurrent(window);
+    glfwSwapInterval(options.vsync);
+}
+
+Window::~Window() {
+    if(window != nullptr) {
+        glfwDestroyWindow(window);
+    }
+}
+
+bool Window::hasError() const {
+    return window == nullptr;
+}
+
+void Window::show() {
+    glfwShowWindow(window);
+}
+
+bool Window::shouldClose() const {
+    return glfwWindowShouldClose(window);
+}
+
+void Window::swapBuffers() {
+    glfwSwapBuffers(window);
+}
+
+void Window::trapCursor(bool trap) {
+    glfwSetInputMode(window, GLFW_CURSOR, trap ? GLFW_CURSOR_DISABLED : GLFW_CURSOR_NORMAL);
+}
+
+Size Window::getSize() const {
+    Size size(0, 0);
+    glfwGetWindowSize(window, &size.width, &size.height);
+    return size;
+}
+
+Size Window::getFramebufferSize() const {
+    Size size(0, 0);
+    glfwGetFramebufferSize(window, &size.width, &size.height);
+    return size;
+}
+
+bool Window::isKeyDown(int key) const {
+    return glfwGetKey(window, key) == GLFW_PRESS;
+}

+ 32 - 0
wrapper/Window.h

@@ -0,0 +1,32 @@
+#ifndef WINDOW_H
+#define WINDOW_H
+
+#include <GL/glew.h>
+#include <GLFW/glfw3.h>
+
+#include "wrapper/WindowOptions.h"
+
+class Window final {
+    GLFWwindow* window;
+
+public:
+    Window(const WindowOptions& options);
+    ~Window();
+    
+    Window(const Window&) = delete;
+    Window& operator=(const Window&) = delete;
+    Window(Window&&) = delete;
+    Window& operator=(Window&&) = delete;
+
+    bool hasError() const;
+    void show();
+    bool shouldClose() const;
+    void swapBuffers();
+    
+    void trapCursor(bool trap);
+    Size getSize() const;
+    Size getFramebufferSize() const;
+    bool isKeyDown(int key) const;
+};
+
+#endif

+ 6 - 0
wrapper/WindowOptions.cpp

@@ -0,0 +1,6 @@
+#include "WindowOptions.h"
+
+WindowOptions::WindowOptions(int majorVersion, int minorVersion, const Size& size, bool es, const char* name) :
+majorVersion(majorVersion), minorVersion(minorVersion), size(size), fullscreen(false), es(es), vsync(true),
+name(name) {
+}

+ 18 - 0
wrapper/WindowOptions.h

@@ -0,0 +1,18 @@
+#ifndef WINDOWOPTIONS_H
+#define WINDOWOPTIONS_H
+
+#include "utils/Size.h"
+
+struct WindowOptions final {
+    int majorVersion;
+    int minorVersion;
+    const Size& size;
+    bool fullscreen;
+    bool es;
+    bool vsync;
+    const char* name;
+
+    WindowOptions(int majorVersion, int minorVersion, const Size& size, bool es, const char* name);
+};
+
+#endif