summaryrefslogtreecommitdiff
path: root/src/window.c
diff options
context:
space:
mode:
authorFreya Murphy <freya@freyacat.org>2025-12-04 13:16:21 -0500
committerFreya Murphy <freya@freyacat.org>2025-12-04 13:18:33 -0500
commitba2f810f0752bdecf8253b34bd245c7939f23534 (patch)
tree23a5fa60afb7f9f5d0ff1b51d11bb8dd24f46e04 /src/window.c
downloadvoxel-ba2f810f0752bdecf8253b34bd245c7939f23534.tar.gz
voxel-ba2f810f0752bdecf8253b34bd245c7939f23534.tar.bz2
voxel-ba2f810f0752bdecf8253b34bd245c7939f23534.zip
initial chunk rendering
Diffstat (limited to 'src/window.c')
-rw-r--r--src/window.c147
1 files changed, 147 insertions, 0 deletions
diff --git a/src/window.c b/src/window.c
new file mode 100644
index 0000000..c7e83c6
--- /dev/null
+++ b/src/window.c
@@ -0,0 +1,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];
+}