aboutsummaryrefslogtreecommitdiffstats
path: root/src/window/Window.cpp
blob: bedeaf99128c4c8c0a5dac700df3e807d75021c9 (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
71
72
73
74
75
76
77
78
#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();
	w->ToggleCursorMode();
	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);
}

int Window::GetCursorMode()
{
	return glfwGetInputMode(win, GLFW_CURSOR);
}

void Window::ToggleCursorMode()
{
	int mode = GetCursorMode() == GLFW_CURSOR_NORMAL ?
		GLFW_CURSOR_DISABLED : GLFW_CURSOR_NORMAL;
	glfwSetInputMode(win, GLFW_CURSOR, mode);
}

bool Window::IsShouldClose()
{
	return glfwWindowShouldClose(win);
}

void Window::SetShouldClose(int flag)
{
	glfwSetWindowShouldClose(win, flag);
}

void Window::SwapBuffers()
{
	glfwSwapBuffers(win);
}