summaryrefslogtreecommitdiffhomepage
path: root/src/window.cpp
blob: c091b42cfe64ec7bad8f4b565fabc21bdff1d229 (plain)
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

// external libs
#include "glad/glad.h"
#include "GLFW/glfw3.h"

// project headers
#include "window.hpp"

// std libs
#include <iostream>

namespace Ogle {
	namespace Window {
		namespace { // private
			GLFWwindow* window;

			void framebuffer_size_callback(GLFWwindow* window, int width, int height)
			{
				glViewport(0, 0, width, height);
			}
		}

		GLFWwindow* get()
		{
			return window;
		}

		int init(
				unsigned int width,
				unsigned int height,
				const char* title
				)
		{
			glfwInit();
			glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // 4.6 is highest, but lets use 3.3 for compatability
			glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
			glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
			glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);

			glfwSwapInterval(1);

			window = glfwCreateWindow(width, height, title, NULL, NULL);
			if (window == NULL)
			{
				std::cout << "Failed to create GLFW window" << std::endl;
				glfwTerminate();
				return -1;
			}
			glfwMakeContextCurrent(window);

			if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
			{
				std::cout << "Failed to init GLAD" << std::endl;
				return -1;
			}

			glViewport(0, 0, width, height);

			glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);

			glEnable(GL_BLEND);
			glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

			return 0;
		}
	}
}