aboutsummaryrefslogtreecommitdiffstats
path: root/yage/core/window.cpp
diff options
context:
space:
mode:
authorYann Herklotz <ymherklotz@gmail.com>2017-10-31 22:11:18 +0000
committerYann Herklotz <ymherklotz@gmail.com>2017-10-31 22:11:18 +0000
commitf776df6076725d14679b31168e3ede53c966182e (patch)
treeb843d7ef0d0722a1b3571ee09aa1345d19c60e7f /yage/core/window.cpp
parent1bb0ef8960c71ef505a351702bec54c01ba15e22 (diff)
downloadYAGE-f776df6076725d14679b31168e3ede53c966182e.tar.gz
YAGE-f776df6076725d14679b31168e3ede53c966182e.zip
renaming base folder
Diffstat (limited to 'yage/core/window.cpp')
-rw-r--r--yage/core/window.cpp103
1 files changed, 103 insertions, 0 deletions
diff --git a/yage/core/window.cpp b/yage/core/window.cpp
new file mode 100644
index 00000000..9774bf9e
--- /dev/null
+++ b/yage/core/window.cpp
@@ -0,0 +1,103 @@
+/* ----------------------------------------------------------------------------
+ * window.cpp
+ *
+ * Copyright (c) 2017 Yann Herklotz Grave <ymherklotz@gmail.com> -- MIT License
+ * See file LICENSE for more details
+ * ----------------------------------------------------------------------------
+ */
+
+#include "window.h"
+
+#include <stdexcept>
+
+
+
+namespace yage
+{
+
+void key_callback(GLFWwindow *window, int key, int scanCode, int action, int mods)
+{
+ if(key == GLFW_KEY_E && action == GLFW_PRESS) {
+ glClearColor(0.5f, 0.f, 0.f, 1.f);
+ } else {
+ glClearColor(0.f, 0.5f, 0.f, 1.f);
+ }
+}
+
+Window::Window() = default;
+
+Window::~Window()
+{
+ glfwDestroyWindow(window_);
+ glfwTerminate();
+}
+
+void Window::create(std::string window_name, int width, int height)
+{
+ if(glfwInit() == GLFW_FALSE) {
+ throw std::runtime_error("GLFW Initialisation failed");
+ }
+
+ glfwWindowHint(GLFW_VERSION_MAJOR, 4);
+ glfwWindowHint(GLFW_VERSION_MINOR, 5);
+
+ window_ =
+ glfwCreateWindow(width, height, window_name.c_str(), nullptr, nullptr);
+ if (window_ == nullptr) {
+ throw std::runtime_error("GLFW Window creation failed");
+ }
+
+ // initialize the gl context
+ glfwMakeContextCurrent(window_);
+
+ // initialize glad
+ gladLoadGLLoader((GLADloadproc) glfwGetProcAddress);
+
+ // set key callback
+ glfwSetKeyCallback(window_, key_callback);
+
+ // set vsync on
+ glfwSwapInterval(1);
+
+ // set the clear color to black
+ glClearColor(0.f, 0.5f, 0.f, 1.f);
+ // set alpha blending
+ glEnable(GL_BLEND);
+ glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+ // set the clear depth
+ glClearDepth(1.f);
+}
+
+void Window::swapBuffer()
+{
+ // swap the window buffer
+ glfwSwapBuffers(window_);
+}
+
+void Window::clearBuffer()
+{
+ // clears buffer with clear color
+ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+}
+
+void Window::hide()
+{
+ glfwHideWindow(window_);
+}
+
+void Window::show()
+{
+ glfwShowWindow(window_);
+}
+
+bool Window::shouldClose()
+{
+ return glfwWindowShouldClose(window_);
+}
+
+void Window::pollEvents() const
+{
+ glfwPollEvents();
+}
+
+} // namespace yage