Merge: crisel -> main
This commit is contained in:
52
src/window.c
52
src/window.c
@@ -1,14 +1,48 @@
|
||||
#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)
|
||||
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 deciados.
|
||||
*/
|
||||
|
||||
static
|
||||
void __limit_fps_window(int max_fps)
|
||||
{
|
||||
void * window;
|
||||
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;
|
||||
@@ -17,20 +51,21 @@ window_t init_window(unsigned int w, unsigned int h, const char * name)
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||
|
||||
window = glfwCreateWindow(w, h, name, NULL, NULL);
|
||||
|
||||
if( !window )
|
||||
window = (window_t)glfwCreateWindow(width, height, title, NULL, NULL);
|
||||
if (!(window))
|
||||
{
|
||||
glfwTerminate();
|
||||
return NULL;
|
||||
}
|
||||
|
||||
glfwSetWindowSizeCallback(window, __window_callback_input);
|
||||
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( window, w, h );
|
||||
__window_callback_input((GLFWwindow*)window, width, height );
|
||||
|
||||
return window;
|
||||
}
|
||||
@@ -45,11 +80,12 @@ 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((void*)window);
|
||||
glfwDestroyWindow((GLFWwindow *)window);
|
||||
glfwTerminate();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user