diff --git a/Makefile b/Makefile index 02e579d..4f2f2d6 100644 --- a/Makefile +++ b/Makefile @@ -14,7 +14,7 @@ OBJ = \ CFLAGS = \ -I./ext/cglm/include \ -I./ext/glfw/include \ - -Wall -Wno-unused-function -std=c99 \ + -Wall -Wno-unused-function -std=c99 -D_GNU_SOURCE \ WAYLAND-LIB = \ xdg-shell \ @@ -54,6 +54,11 @@ linux-x11: $(OBJ) $(CC) -o $(BIN) $(OBJ) -L. -lGLEW -lGL -lglfw -lm linux-wayland: $(OBJ) + for i in $(WAYLAND-LIB); \ + do \ + wayland-scanner client-header ext/glfw/deps/wayland/$$i.xml ext/glfw/deps/wayland/$$i-client-protocol.h; \ + wayland-scanner private-code ext/glfw/deps/wayland/$$i.xml ext/glfw/deps/wayland/$$i-client-protocol-code.h; \ + done $(MAKE) BKN=_GLFW_WAYLAND libglfw.so $(CC) -o $(BIN) $(OBJ) -L. -lGLEW -lGL -lglfw -lm @@ -66,11 +71,6 @@ cocoa: $(OBJ) $(CC) -framework OpenGL -o $(BIN) $(OBJ) -L. -lGLEW -lGL -lglfw libglfw.so: - for i in $(WAYLAND-LIB); \ - do \ - wayland-scanner client-header ext/glfw/deps/wayland/$$i.xml ext/glfw/deps/wayland/$$i-client-protocol.h; \ - wayland-scanner private-code ext/glfw/deps/wayland/$$i.xml ext/glfw/deps/wayland/$$i-client-protocol-code.h; \ - done $(CC) -fPIC -shared -D$(BKN) -D_GLFW_BUILD_DLL -Iext/glfw/deps/wayland ./ext/glfw/src/*.c -o $@ clean: diff --git a/compile.bat b/compile.bat new file mode 100644 index 0000000..de53c26 --- /dev/null +++ b/compile.bat @@ -0,0 +1,3 @@ +:: git submodule update --init --recursive +:: gcc -fPIC -shared -Iext/glfw/deps/MinGW -D_GLFW_WIN32 -D_GLFW_BUILD_DLL ./ext/glfw/src/*.c -o glfw.dll -lgdi32 +gcc -I ext/cglm/include -I ext/glfw/include src/*.c -o manigraph -L . -lglfw -lopengl32 -lglew32 \ No newline at end of file diff --git a/glfw.dll b/glfw.dll new file mode 100644 index 0000000..f4d02d2 Binary files /dev/null and b/glfw.dll differ diff --git a/manigraph.exe b/manigraph.exe new file mode 100644 index 0000000..0207951 Binary files /dev/null and b/manigraph.exe differ diff --git a/src/main.c b/src/main.c index b172d50..4fcadd4 100755 --- a/src/main.c +++ b/src/main.c @@ -4,6 +4,7 @@ #include #include +#include #define WIDTH 512 #define HEIGHT 512 @@ -51,72 +52,99 @@ float * fill_normal( float * d ) const char * wname = "manigraph: manifold grapher"; float * generate_surface(unsigned int, unsigned char *m); -int main( void ) + +void mlog(char * msg) +{ +#ifdef DEBUG + printf(msg); +#endif +} + +int main(void) { id_t shader, texture, shader_plain; mesh_t m_surface, m_axis; window_t window; - /* Setup window */ + mlog("[VENTANA] Inicializando...\n"); { if(!(window = init_window(WIDTH, HEIGHT, wname))) + { + mlog("[VENTANA] Error al inicializar...\n"); goto error_window; + } use_window(window); set_clean_color_context(0x2E, 0x07, 0x3F); } - /* Setup context */ + mlog("[CONTEXT] Inicializando...\n"); { if(!(init_context())) + { + mlog("[CONTEXT] Error al inicializar...\n"); goto error_context; + } } - /* Setup color texture */ + mlog("[TEXTURE] Inicializando...\n"); { texture=create_palette_texture(palette, 4); use_texture(texture); } - /* Setup shader */ + mlog("[SHADER] Inicializando...\n"); { if(!(shader = create_shader())) + { + mlog("[SHADER] Error al inicializar...\n"); goto error_shader; + } load_program_to_shader(shader, vs, VERTEX); load_program_to_shader(shader, fs, FRAGMENT); load_fix_matrix(shader, (float)WIDTH/HEIGHT); } - /* Setup shader w/out illumination */ + mlog("[SHADER] Inicializando...\n"); { if(!(shader_plain = create_shader())) + { + mlog("[SHADER] Error al inicializar...\n"); goto error_shader_plain; + } load_program_to_shader(shader_plain, vs, VERTEX); load_program_to_shader(shader_plain, fs_plain, FRAGMENT); load_fix_matrix(shader_plain, (float)WIDTH/HEIGHT); } - /* Fill mesh of surface */ + mlog("[MESH] Inicializando...\n"); { unsigned char m; float * n_surface, *d_surface; d_surface = generate_surface(16,&m); n_surface = fill_normal(d_surface); if( !(m_surface = create_mesh(d_surface, n_surface, coordanate))) + { + mlog("[MESH] Error al inicializar...\n"); goto error_mesh_surface; + } free(n_surface); free(d_surface); } - /* Fill mesh of axis */ + mlog("[MESH] Inicializando...\n"); { float * n_axis; n_axis = fill_normal(d_axis); if(!(m_axis = create_mesh(d_axis, n_axis, coordanate))) + { + mlog("[MESH] Error al inicializar...\n"); goto error_mesh_axis; + } free(n_axis); } + mlog("[MAIN LOOP] Inicializando...\n"); while(is_open_window(window)) { quat_t q; @@ -138,27 +166,41 @@ int main( void ) load_mdl_matrix(shader_plain, 0, 3); #endif draw_mesh(m_surface); - } + mlog("[MAIN LOOP] Terminando...\n"); + mlog("[MESH] Destruyendo...\n"); destroy_mesh(m_axis); + mlog("[MESH] Destruyendo...\n"); destroy_mesh(m_surface); + mlog("[SHADER] Destruyendo...\n"); destroy_shader(shader_plain); + mlog("[SHADER] Destruyendo...\n"); destroy_shader(shader); + mlog("[TEXTURE] Destruyendo...\n"); destroy_texture(texture); + mlog("[WINDOW] Destruyendo...\n"); close_window(window); return 0; + + mlog("[MESH] Destruyendo...\n"); + destroy_mesh(m_axis); error_mesh_axis: + mlog("[MESH] Destruyendo...\n"); destroy_mesh(m_surface); error_mesh_surface: + mlog("[SHADER] Destruyendo...\n"); destroy_shader(shader_plain); error_shader_plain: + mlog("[SHADER] Destruyendo...\n"); destroy_shader(shader); error_shader: - close_window(window); + mlog("[TEXTURE] Destruyendo...\n"); destroy_texture(texture); error_context: + mlog("[WINDOW] Destruyendo...\n"); + close_window(window); error_window: return 1; } diff --git a/src/window.c b/src/window.c index 392729a..6ad9a26 100644 --- a/src/window.c +++ b/src/window.c @@ -1,14 +1,48 @@ #include "main.h" #include +#include +#include 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(); }