89 lines
2.2 KiB
C
89 lines
2.2 KiB
C
#include "main.h"
|
|
#include <GLFW/glfw3.h>
|
|
#include <stdio.h>
|
|
#include <time.h>
|
|
|
|
void __window_callback_input(GLFWwindow *, int, int);
|
|
void __mouse_callback_input(GLFWwindow *, int, int, int);
|
|
void __scroll_callback_input(GLFWwindow *, double, double);
|
|
void __key_callback_input(GLFWwindow *, int, int, int, int);
|
|
|
|
window_t init_window(unsigned int w, unsigned int h, const char *name);
|
|
|
|
/*
|
|
Limitamos los FPS, contando el tiempo en el que
|
|
se ejecuta el main loop, y esperando el tiempo restante
|
|
para lograr los fps deseados.
|
|
*/
|
|
|
|
static void __limit_fps_window(int max_fps)
|
|
{
|
|
static double previous_time = 0.0;
|
|
double current_time;
|
|
double frame_time;
|
|
double elapsed_time;
|
|
|
|
current_time = glfwGetTime();
|
|
frame_time = 1.0 / max_fps;
|
|
elapsed_time = current_time - previous_time;
|
|
|
|
if (elapsed_time < frame_time)
|
|
{
|
|
struct timespec sleep_time;
|
|
sleep_time.tv_sec = 0;
|
|
sleep_time.tv_nsec = (long)((frame_time - elapsed_time) * 1e9);
|
|
nanosleep(&sleep_time, NULL);
|
|
|
|
current_time = glfwGetTime();
|
|
}
|
|
|
|
previous_time = current_time;
|
|
}
|
|
|
|
window_t init_window(unsigned int width, unsigned int height, const char *title)
|
|
{
|
|
window_t window;
|
|
|
|
if (!glfwInit())
|
|
return NULL;
|
|
|
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
|
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
|
|
|
window = (window_t)glfwCreateWindow(width, height, title, NULL, NULL);
|
|
if (!(window))
|
|
{
|
|
glfwTerminate();
|
|
return NULL;
|
|
}
|
|
|
|
glfwMakeContextCurrent((GLFWwindow *)(window));
|
|
|
|
glfwSetWindowSizeCallback((GLFWwindow *)window, __window_callback_input);
|
|
glfwSetMouseButtonCallback((GLFWwindow *)window, __mouse_callback_input);
|
|
glfwSetScrollCallback((GLFWwindow *)window, __scroll_callback_input);
|
|
glfwSetKeyCallback((GLFWwindow *)window, __key_callback_input);
|
|
|
|
__window_callback_input((GLFWwindow *)window, width, height);
|
|
|
|
return window;
|
|
}
|
|
|
|
void use_window(window_t window) { glfwMakeContextCurrent((void *)window); }
|
|
|
|
int is_open_window(window_t window)
|
|
{
|
|
glfwSwapBuffers((void *)window);
|
|
glfwPollEvents();
|
|
|
|
__limit_fps_window(60);
|
|
return !glfwWindowShouldClose((void *)window);
|
|
}
|
|
|
|
void close_window(window_t window)
|
|
{
|
|
glfwDestroyWindow((GLFWwindow *)window);
|
|
glfwTerminate();
|
|
}
|