#include #include #include #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]; }