Port to web for free

This commit is contained in:
PedroEdiaz
2024-11-19 21:54:22 -06:00
parent c4016d239a
commit 61b014a803
4 changed files with 71 additions and 27 deletions

View File

@@ -34,6 +34,7 @@ help:
@echo " $(MAKE) linux-x11" @echo " $(MAKE) linux-x11"
@echo " $(MAKE) linux-wayland" @echo " $(MAKE) linux-wayland"
@echo " $(MAKE) cocoa" @echo " $(MAKE) cocoa"
@echo " $(MAKE) CC=emcc wasm"
@echo "Para limpiar los archivos compilados se puede usar" @echo "Para limpiar los archivos compilados se puede usar"
@echo " $(MAKE) clean" @echo " $(MAKE) clean"
@echo "Para ejecturar el programa sin instalarlos se puede usar:" @echo "Para ejecturar el programa sin instalarlos se puede usar:"
@@ -72,6 +73,9 @@ cocoa: $(OBJ)
$(MAKE) DFLAGS="-D_GLFW_COCOA" libglfw.so $(MAKE) DFLAGS="-D_GLFW_COCOA" libglfw.so
$(CC) -framework OpenGL -o $(BIN) $(OBJ) -L. -lGLEW -lGL -lglfw $(CC) -framework OpenGL -o $(BIN) $(OBJ) -L. -lGLEW -lGL -lglfw
wasm: $(OBJ)
$(CC) -sUSE_WEBGL2=1 -sUSE_GLFW=3 -o mani.html $(OBJ)
libglfw.so: libglfw.so:
$(CC) -fPIC -shared $(DFLAGS) -D_GLFW_BUILD_DLL -Iext/glfw/deps/wayland ./ext/glfw/src/*.c -o $@ $(CC) -fPIC -shared $(DFLAGS) -D_GLFW_BUILD_DLL -Iext/glfw/deps/wayland ./ext/glfw/src/*.c -o $@
@@ -81,5 +85,4 @@ clean:
.SUFFIXES: .c .o .SUFFIXES: .c .o
.c.o: src/main.h .c.o: src/main.h
clang-format -i $<
$(CC) $(CFLAGS) -c -o $@ $< $(CC) $(CFLAGS) -c -o $@ $<

View File

@@ -1,10 +1,15 @@
#include "main.h" #include "main.h"
#ifdef EMSCRIPTEN
#include <GL/gl.h>
#else
#ifdef GLAD #ifdef GLAD
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include <glad.h> #include <glad.h>
#else #else
#include <GL/glew.h> #include <GL/glew.h>
#endif #endif
#endif
void set_clean_color_context(unsigned char r, unsigned char g, unsigned char b) void set_clean_color_context(unsigned char r, unsigned char g, unsigned char b)
{ {
@@ -15,12 +20,16 @@ void set_clean_color_context(unsigned char r, unsigned char g, unsigned char b)
int init_context(void) int init_context(void)
{ {
#ifdef EMSCRIPTEN
return 1;
#else
#ifdef GLAD #ifdef GLAD
return gladLoadGLLoader((GLADloadproc)glfwGetProcAddress); return gladLoadGLLoader((GLADloadproc)glfwGetProcAddress);
#else #else
return glewInit(); return glewInit();
#endif #endif
#endif
} }
void clean_context(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); } void clean_context(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); }

View File

@@ -1,6 +1,10 @@
const char * vs = const char * vs =
#ifdef EMSCRIPTEN
"#version 300 es\n"
"precision highp float;"
#else
"#version 330 core\n" "#version 330 core\n"
#endif
"layout (location = 0) in float aPos_x;" "layout (location = 0) in float aPos_x;"
"layout (location = 1) in float aPos_y;" "layout (location = 1) in float aPos_y;"
"layout (location = 2) in float aPos_z;" "layout (location = 2) in float aPos_z;"
@@ -32,8 +36,14 @@ const char * vs =
const char * fs_plain = const char * fs_plain =
"#version 330 core\n" #ifdef EMSCRIPTEN
"#version 300 es\n"
"precision highp float;"
"precision highp sampler2DArray;"
#else
"#version 330 core\n"
#endif
"uniform sampler2DArray palette;" "uniform sampler2DArray palette;"
"in float index;" "in float index;"
@@ -47,7 +57,13 @@ const char * fs_plain =
"}"; "}";
const char * fs = const char * fs =
#ifdef EMSCRIPTEN
"#version 300 es\n"
"precision highp float;"
"precision highp sampler2DArray;"
#else
"#version 330 core\n" "#version 330 core\n"
#endif
"uniform sampler2DArray palette;" "uniform sampler2DArray palette;"

64
src/main.c Executable file → Normal file
View File

@@ -8,6 +8,10 @@
#define WIDTH 512 #define WIDTH 512
#define HEIGHT 512 #define HEIGHT 512
#ifdef EMSCRIPTEN
#include <emscripten.h>
#endif
float *generate_data_surface(unsigned int, unsigned char *); float *generate_data_surface(unsigned int, unsigned char *);
float *generate_normals_surface(float *, unsigned char); float *generate_normals_surface(float *, unsigned char);
@@ -29,11 +33,40 @@ void mlog(char *msg)
#endif #endif
} }
window_t window;
mesh_t m_surface, m_axis;
id_t shader, shader_plain;
#ifndef EMSCRIPTEN
static inline
#endif
void main_loop(void)
{
quat_t q;
q = poll_input(window);
load_rot_matrix(shader, q);
load_rot_matrix(shader_plain, q);
clean_context();
#ifndef DEBUG
load_mdl_matrix(shader_plain, 0, 0);
draw_mesh(m_axis);
load_mdl_matrix(shader_plain, 1, 1);
draw_mesh(m_axis);
load_mdl_matrix(shader_plain, 2, 2);
draw_mesh(m_axis);
load_mdl_matrix(shader, 0, 3);
#else
load_mdl_matrix(shader_plain, 0, 3);
#endif
draw_mesh(m_surface);
}
int main(void) int main(void)
{ {
id_t shader, texture, shader_plain; id_t texture;
mesh_t m_surface, m_axis;
window_t window;
mlog("[VENTANA] Inicializando...\n"); mlog("[VENTANA] Inicializando...\n");
{ {
@@ -117,29 +150,12 @@ int main(void)
} }
mlog("[MAIN LOOP] Inicializando...\n"); mlog("[MAIN LOOP] Inicializando...\n");
while (is_open_window(window)) #ifdef EMSCRIPTEN
{ emscripten_set_main_loop(&main_loop, 0, 1);
quat_t q;
q = poll_input(window);
load_rot_matrix(shader, q);
load_rot_matrix(shader_plain, q);
clean_context();
#ifndef DEBUG
load_mdl_matrix(shader_plain, 0, 0);
draw_mesh(m_axis);
load_mdl_matrix(shader_plain, 1, 1);
draw_mesh(m_axis);
load_mdl_matrix(shader_plain, 2, 2);
draw_mesh(m_axis);
load_mdl_matrix(shader, 0, 3);
#else #else
load_mdl_matrix(shader_plain, 0, 3); while (is_open_window(window))
main_loop();
#endif #endif
draw_mesh(m_surface);
}
mlog("[MAIN LOOP] Terminando...\n"); mlog("[MAIN LOOP] Terminando...\n");
mlog("[MESH] Destruyendo...\n"); mlog("[MESH] Destruyendo...\n");