summaryrefslogtreecommitdiff
path: root/src/window.c
blob: c7e83c628c27c5881aab3da57612407413ca2cd4 (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <string.h>

#include "voxel.h"
#include "window.h"

Window window;
double delta_time;

static void key_callback(GLFWwindow *, int key, int, int action, int)
{
	switch (action) {
	case GLFW_PRESS:
		window.key_down[key] = true;
		window.key_pressed[key] = true;
		break;
	case GLFW_RELEASE:
		window.key_down[key] = false;
		break;
	}
}

static void mouse_move_callback(GLFWwindow *, double xpos, double ypos)
{
	window.mouse_x_last = window.mouse_x;
	window.moues_y_last = window.mouse_y;
	window.mouse_x = xpos;
	window.mouse_y = ypos;
}

static void mouse_button_callback(GLFWwindow *, int button, int action, int)
{
	switch (action) {
	case GLFW_PRESS:
		window.btn_down[button] = true;
		window.btn_pressed[button] = true;
		break;
	case GLFW_RELEASE:
		window.btn_down[button] = false;
		break;
	}
}

static void framebuffer_callback(GLFWwindow *, int width, int height)
{
	window.width = width;
	window.height = height;
	glViewport(0, 0, width, height);
}

int window_init(void)
{
	if (glfwInit() == GLFW_FALSE) {
		ERROR("GLFW failed to initalize");
		return 1;
	}

	memset(&window, 0, sizeof(Window));
	delta_time = 0;

	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
	glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
	glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

	window.monitor = glfwGetPrimaryMonitor();
	window.window = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_TITLE,
					 window.monitor, NULL);
	if (window.window == NULL) {
		ERROR("failed to create window");
		return 1;
	}

	glfwMakeContextCurrent(window.window);
	glfwSwapInterval(0);

	glewExperimental = GL_TRUE;
	if (glewInit() != GLEW_OK) {
		ERROR("GLEW failed to initalize");
		return 1;
	}

	glfwSetKeyCallback(window.window, key_callback);
	glfwSetCursorPosCallback(window.window, mouse_move_callback);
	glfwSetMouseButtonCallback(window.window, mouse_button_callback);
	glfwSetFramebufferSizeCallback(window.window, framebuffer_callback);

	return 0;
}

bool window_closed(void)
{
	return glfwWindowShouldClose(window.window) == GLFW_TRUE;
}

void window_update(void)
{
	// reset "pressed"
	memset(&window.key_pressed, 0, sizeof(window.key_pressed));
	memset(&window.btn_pressed, 0, sizeof(window.btn_pressed));

	// update delta time
	double time = glfwGetTime();
	delta_time = time - window.last_time;
	window.last_time = time;

	// check for errors
	GLenum error;
	while ((error = glGetError()) != GL_NO_ERROR)
		ERROR("OpenGL error: %d", error);

	glfwPollEvents();
}

void window_swap(void)
{
	glfwSwapBuffers(window.window);
}

void window_close(void)
{
	glfwDestroyWindow(window.window);
	glfwTerminate();
}

int window_grab_cursor(void);
int window_release_cursor(void);

bool key_down(int key)
{
	return window.key_down[key];
}

bool key_pressed(int key)
{
	return window.key_pressed[key];
}

bool btn_down(int btn)
{
	return window.btn_down[btn];
}

bool btn_pressed(int btn)
{
	return window.btn_pressed[btn];
}