aboutsummaryrefslogtreecommitdiffstats
path: root/src/window/Window.cpp
blob: 7aef326ef4c0aa8e0b4a7fcbf4461ca33a73a519 (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
68
69
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);
}