Merge: crisel -> main
This commit is contained in:
12
Makefile
12
Makefile
@@ -14,7 +14,7 @@ OBJ = \
|
|||||||
CFLAGS = \
|
CFLAGS = \
|
||||||
-I./ext/cglm/include \
|
-I./ext/cglm/include \
|
||||||
-I./ext/glfw/include \
|
-I./ext/glfw/include \
|
||||||
-Wall -Wno-unused-function -std=c99 \
|
-Wall -Wno-unused-function -std=c99 -D_GNU_SOURCE \
|
||||||
|
|
||||||
WAYLAND-LIB = \
|
WAYLAND-LIB = \
|
||||||
xdg-shell \
|
xdg-shell \
|
||||||
@@ -54,6 +54,11 @@ linux-x11: $(OBJ)
|
|||||||
$(CC) -o $(BIN) $(OBJ) -L. -lGLEW -lGL -lglfw -lm
|
$(CC) -o $(BIN) $(OBJ) -L. -lGLEW -lGL -lglfw -lm
|
||||||
|
|
||||||
linux-wayland: $(OBJ)
|
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
|
$(MAKE) BKN=_GLFW_WAYLAND libglfw.so
|
||||||
$(CC) -o $(BIN) $(OBJ) -L. -lGLEW -lGL -lglfw -lm
|
$(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
|
$(CC) -framework OpenGL -o $(BIN) $(OBJ) -L. -lGLEW -lGL -lglfw
|
||||||
|
|
||||||
libglfw.so:
|
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 $@
|
$(CC) -fPIC -shared -D$(BKN) -D_GLFW_BUILD_DLL -Iext/glfw/deps/wayland ./ext/glfw/src/*.c -o $@
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
|
|||||||
3
compile.bat
Normal file
3
compile.bat
Normal file
@@ -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
|
||||||
BIN
manigraph.exe
Normal file
BIN
manigraph.exe
Normal file
Binary file not shown.
62
src/main.c
62
src/main.c
@@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <cglm/vec3.h>
|
#include <cglm/vec3.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
#define WIDTH 512
|
#define WIDTH 512
|
||||||
#define HEIGHT 512
|
#define HEIGHT 512
|
||||||
@@ -51,72 +52,99 @@ float * fill_normal( float * d )
|
|||||||
const char * wname = "manigraph: manifold grapher";
|
const char * wname = "manigraph: manifold grapher";
|
||||||
float * generate_surface(unsigned int, unsigned char *m);
|
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;
|
id_t shader, texture, shader_plain;
|
||||||
mesh_t m_surface, m_axis;
|
mesh_t m_surface, m_axis;
|
||||||
window_t window;
|
window_t window;
|
||||||
|
|
||||||
/* Setup window */
|
mlog("[VENTANA] Inicializando...\n");
|
||||||
{
|
{
|
||||||
if(!(window = init_window(WIDTH, HEIGHT, wname)))
|
if(!(window = init_window(WIDTH, HEIGHT, wname)))
|
||||||
|
{
|
||||||
|
mlog("[VENTANA] Error al inicializar...\n");
|
||||||
goto error_window;
|
goto error_window;
|
||||||
|
}
|
||||||
|
|
||||||
use_window(window);
|
use_window(window);
|
||||||
set_clean_color_context(0x2E, 0x07, 0x3F);
|
set_clean_color_context(0x2E, 0x07, 0x3F);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Setup context */
|
mlog("[CONTEXT] Inicializando...\n");
|
||||||
{
|
{
|
||||||
if(!(init_context()))
|
if(!(init_context()))
|
||||||
|
{
|
||||||
|
mlog("[CONTEXT] Error al inicializar...\n");
|
||||||
goto error_context;
|
goto error_context;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Setup color texture */
|
mlog("[TEXTURE] Inicializando...\n");
|
||||||
{
|
{
|
||||||
texture=create_palette_texture(palette, 4);
|
texture=create_palette_texture(palette, 4);
|
||||||
use_texture(texture);
|
use_texture(texture);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Setup shader */
|
mlog("[SHADER] Inicializando...\n");
|
||||||
{
|
{
|
||||||
if(!(shader = create_shader()))
|
if(!(shader = create_shader()))
|
||||||
|
{
|
||||||
|
mlog("[SHADER] Error al inicializar...\n");
|
||||||
goto error_shader;
|
goto error_shader;
|
||||||
|
}
|
||||||
load_program_to_shader(shader, vs, VERTEX);
|
load_program_to_shader(shader, vs, VERTEX);
|
||||||
load_program_to_shader(shader, fs, FRAGMENT);
|
load_program_to_shader(shader, fs, FRAGMENT);
|
||||||
load_fix_matrix(shader, (float)WIDTH/HEIGHT);
|
load_fix_matrix(shader, (float)WIDTH/HEIGHT);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Setup shader w/out illumination */
|
mlog("[SHADER] Inicializando...\n");
|
||||||
{
|
{
|
||||||
if(!(shader_plain = create_shader()))
|
if(!(shader_plain = create_shader()))
|
||||||
|
{
|
||||||
|
mlog("[SHADER] Error al inicializar...\n");
|
||||||
goto error_shader_plain;
|
goto error_shader_plain;
|
||||||
|
}
|
||||||
load_program_to_shader(shader_plain, vs, VERTEX);
|
load_program_to_shader(shader_plain, vs, VERTEX);
|
||||||
load_program_to_shader(shader_plain, fs_plain, FRAGMENT);
|
load_program_to_shader(shader_plain, fs_plain, FRAGMENT);
|
||||||
load_fix_matrix(shader_plain, (float)WIDTH/HEIGHT);
|
load_fix_matrix(shader_plain, (float)WIDTH/HEIGHT);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Fill mesh of surface */
|
mlog("[MESH] Inicializando...\n");
|
||||||
{
|
{
|
||||||
unsigned char m;
|
unsigned char m;
|
||||||
float * n_surface, *d_surface;
|
float * n_surface, *d_surface;
|
||||||
d_surface = generate_surface(16,&m);
|
d_surface = generate_surface(16,&m);
|
||||||
n_surface = fill_normal(d_surface);
|
n_surface = fill_normal(d_surface);
|
||||||
if( !(m_surface = create_mesh(d_surface, n_surface, coordanate)))
|
if( !(m_surface = create_mesh(d_surface, n_surface, coordanate)))
|
||||||
|
{
|
||||||
|
mlog("[MESH] Error al inicializar...\n");
|
||||||
goto error_mesh_surface;
|
goto error_mesh_surface;
|
||||||
|
}
|
||||||
free(n_surface);
|
free(n_surface);
|
||||||
free(d_surface);
|
free(d_surface);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Fill mesh of axis */
|
mlog("[MESH] Inicializando...\n");
|
||||||
{
|
{
|
||||||
float * n_axis;
|
float * n_axis;
|
||||||
n_axis = fill_normal(d_axis);
|
n_axis = fill_normal(d_axis);
|
||||||
if(!(m_axis = create_mesh(d_axis, n_axis, coordanate)))
|
if(!(m_axis = create_mesh(d_axis, n_axis, coordanate)))
|
||||||
|
{
|
||||||
|
mlog("[MESH] Error al inicializar...\n");
|
||||||
goto error_mesh_axis;
|
goto error_mesh_axis;
|
||||||
|
}
|
||||||
free(n_axis);
|
free(n_axis);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mlog("[MAIN LOOP] Inicializando...\n");
|
||||||
while(is_open_window(window))
|
while(is_open_window(window))
|
||||||
{
|
{
|
||||||
quat_t q;
|
quat_t q;
|
||||||
@@ -138,27 +166,41 @@ int main( void )
|
|||||||
load_mdl_matrix(shader_plain, 0, 3);
|
load_mdl_matrix(shader_plain, 0, 3);
|
||||||
#endif
|
#endif
|
||||||
draw_mesh(m_surface);
|
draw_mesh(m_surface);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
mlog("[MAIN LOOP] Terminando...\n");
|
||||||
|
|
||||||
|
mlog("[MESH] Destruyendo...\n");
|
||||||
destroy_mesh(m_axis);
|
destroy_mesh(m_axis);
|
||||||
|
mlog("[MESH] Destruyendo...\n");
|
||||||
destroy_mesh(m_surface);
|
destroy_mesh(m_surface);
|
||||||
|
mlog("[SHADER] Destruyendo...\n");
|
||||||
destroy_shader(shader_plain);
|
destroy_shader(shader_plain);
|
||||||
|
mlog("[SHADER] Destruyendo...\n");
|
||||||
destroy_shader(shader);
|
destroy_shader(shader);
|
||||||
|
mlog("[TEXTURE] Destruyendo...\n");
|
||||||
destroy_texture(texture);
|
destroy_texture(texture);
|
||||||
|
mlog("[WINDOW] Destruyendo...\n");
|
||||||
close_window(window);
|
close_window(window);
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
|
|
||||||
|
mlog("[MESH] Destruyendo...\n");
|
||||||
|
destroy_mesh(m_axis);
|
||||||
error_mesh_axis:
|
error_mesh_axis:
|
||||||
|
mlog("[MESH] Destruyendo...\n");
|
||||||
destroy_mesh(m_surface);
|
destroy_mesh(m_surface);
|
||||||
error_mesh_surface:
|
error_mesh_surface:
|
||||||
|
mlog("[SHADER] Destruyendo...\n");
|
||||||
destroy_shader(shader_plain);
|
destroy_shader(shader_plain);
|
||||||
error_shader_plain:
|
error_shader_plain:
|
||||||
|
mlog("[SHADER] Destruyendo...\n");
|
||||||
destroy_shader(shader);
|
destroy_shader(shader);
|
||||||
error_shader:
|
error_shader:
|
||||||
close_window(window);
|
mlog("[TEXTURE] Destruyendo...\n");
|
||||||
destroy_texture(texture);
|
destroy_texture(texture);
|
||||||
error_context:
|
error_context:
|
||||||
|
mlog("[WINDOW] Destruyendo...\n");
|
||||||
|
close_window(window);
|
||||||
error_window:
|
error_window:
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|||||||
52
src/window.c
52
src/window.c
@@ -1,14 +1,48 @@
|
|||||||
#include "main.h"
|
#include "main.h"
|
||||||
#include <GLFW/glfw3.h>
|
#include <GLFW/glfw3.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
void __window_callback_input(GLFWwindow *, int, int);
|
void __window_callback_input(GLFWwindow *, int, int);
|
||||||
void __mouse_callback_input(GLFWwindow *, int, int, int);
|
void __mouse_callback_input(GLFWwindow *, int, int, int);
|
||||||
void __scroll_callback_input(GLFWwindow *, double, double);
|
void __scroll_callback_input(GLFWwindow *, double, double);
|
||||||
void __key_callback_input(GLFWwindow *, int, int, int, int);
|
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() )
|
if( !glfwInit() )
|
||||||
return NULL;
|
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_CONTEXT_VERSION_MINOR, 3);
|
||||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||||
|
|
||||||
window = glfwCreateWindow(w, h, name, NULL, NULL);
|
window = (window_t)glfwCreateWindow(width, height, title, NULL, NULL);
|
||||||
|
if (!(window))
|
||||||
if( !window )
|
|
||||||
{
|
{
|
||||||
glfwTerminate();
|
glfwTerminate();
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
glfwSetWindowSizeCallback(window, __window_callback_input);
|
glfwMakeContextCurrent((GLFWwindow*)(window));
|
||||||
|
|
||||||
|
glfwSetWindowSizeCallback((GLFWwindow*)window, __window_callback_input);
|
||||||
glfwSetMouseButtonCallback((GLFWwindow*)window, __mouse_callback_input);
|
glfwSetMouseButtonCallback((GLFWwindow*)window, __mouse_callback_input);
|
||||||
glfwSetScrollCallback((GLFWwindow*)window, __scroll_callback_input);
|
glfwSetScrollCallback((GLFWwindow*)window, __scroll_callback_input);
|
||||||
glfwSetKeyCallback((GLFWwindow*)window, __key_callback_input);
|
glfwSetKeyCallback((GLFWwindow*)window, __key_callback_input);
|
||||||
|
|
||||||
__window_callback_input( window, w, h );
|
__window_callback_input((GLFWwindow*)window, width, height );
|
||||||
|
|
||||||
return window;
|
return window;
|
||||||
}
|
}
|
||||||
@@ -45,11 +80,12 @@ int is_open_window(window_t window)
|
|||||||
glfwSwapBuffers((void*)window);
|
glfwSwapBuffers((void*)window);
|
||||||
glfwPollEvents();
|
glfwPollEvents();
|
||||||
|
|
||||||
|
__limit_fps_window(60);
|
||||||
return !glfwWindowShouldClose((void*)window);
|
return !glfwWindowShouldClose((void*)window);
|
||||||
}
|
}
|
||||||
|
|
||||||
void close_window(window_t window)
|
void close_window(window_t window)
|
||||||
{
|
{
|
||||||
glfwDestroyWindow((void*)window);
|
glfwDestroyWindow((GLFWwindow *)window);
|
||||||
glfwTerminate();
|
glfwTerminate();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user