From a21520508fe04919e7776ba2a243ac8a1f02f4dc Mon Sep 17 00:00:00 2001 From: Maya Woodward Date: Fri, 15 Sep 2023 00:07:58 -0400 Subject: [PATCH] Add compile_flags, merge engine code into working emscripten build --- Makefile | 29 +++++++++++++++++++++-------- README.md | 6 ++++++ compile_flags.txt | 6 ++++++ src/vestige.c | 37 +++++++++++++++++++++++++++++-------- 4 files changed, 62 insertions(+), 16 deletions(-) create mode 100644 compile_flags.txt diff --git a/Makefile b/Makefile index a6b2d96..24991da 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,4 @@ -TARGET=vestige - -CC=clang +TARGET?=native BUILD_DIR:=./build SRC_DIRS:=./src @@ -12,16 +10,31 @@ DEPS:=$(OBJS:.o=.d) INC_DIRS:=./include $(shell find $(SRC_DIRS) -type d) INC_FLAGS:=$(addprefix -I,$(INC_DIRS)) -CFLAGS:=$(INC_FLAGS) -std=c17 -Wall -Wpedantic -Werror -O2 -MMD -MP - LDFLAGS:=-lglfw -lGL +CFLAGS:=$(INC_FLAGS) -std=c17 -Wall -Wpedantic -Werror -O2 -MMD -MP -g + +ifeq ($(TARGET),web) + CC:=emcc + LDFLAGS:=$(LDFLAGS) -O2 --emrun -s USE_GLFW=3 -s FULL_ES3 + TARGET_EXEC=vestige.html +else + CC:=clang + TARGET_EXEC=vestige +endif .PHONY: clean -all: $(BUILD_DIR)/$(TARGET) +all: $(BUILD_DIR)/$(TARGET_EXEC) + +run: $(BUILD_DIR)/$(TARGET_EXEC) +ifeq ($(TARGET),web) + emrun $(BUILD_DIR)/$(TARGET_EXEC) +else + ./$(BUILD_DIR)/$(TARGET_EXEC) +endif -$(BUILD_DIR)/$(TARGET): $(OBJS) - $(CC) $(OBJS) -o $@ $(LDFLAGS) +$(BUILD_DIR)/$(TARGET_EXEC): $(OBJS) + $(CC) $(LDFLAGS) $(OBJS) -o $@ $(BUILD_DIR)/%.c.o: %.c mkdir -p $(dir $@) diff --git a/README.md b/README.md index 94aaace..16c66af 100644 --- a/README.md +++ b/README.md @@ -1 +1,7 @@ # Vestige Engine + +## Building + +### Dependencies + +* [Emscripten=3.1.45](https://emscripten.org/docs/getting_started/downloads.html#sdk-download-and-install) diff --git a/compile_flags.txt b/compile_flags.txt new file mode 100644 index 0000000..679ebcf --- /dev/null +++ b/compile_flags.txt @@ -0,0 +1,6 @@ +-std=c17 +-Wall +-Wpedantic +-Werror +-I./src +-I./include diff --git a/src/vestige.c b/src/vestige.c index 11ee9da..a63928f 100644 --- a/src/vestige.c +++ b/src/vestige.c @@ -1,27 +1,48 @@ +#include + +#ifdef __EMSCRIPTEN__ +#include +#endif #include + #include "window.h" #include "engine.h" -#include + +GLFWwindow* APP = NULL; + +static void main_loop(void) { + glClear(GL_COLOR_BUFFER_BIT); + glfwSwapBuffers(APP); + glfwPollEvents(); +} int main(int argc, char* argv[]) { engine_start(); + if (!glfwInit()) { + printf("Failed to initialize GLFW!\n"); return 1; } - GLFWwindow* app = glfwCreateWindow(640, 480, "Hello, Window!", NULL, NULL); - if (!app) { + APP = glfwCreateWindow(640, 480, "Hello, Window!", NULL, NULL); + if (!APP) { glfwTerminate(); return 1; } - glfwMakeContextCurrent(app); + glfwMakeContextCurrent(APP); + + glfwSwapInterval(1); + + glClearColor(255, 0, 0, 255); - while (!glfwWindowShouldClose(app)) { - glClear(GL_COLOR_BUFFER_BIT); - glfwSwapBuffers(app); - glfwPollEvents(); +#ifdef __EMSCRIPTEN__ + emscripten_set_main_loop(main_loop, 0, 1); +#else + while (!glfwWindowShouldClose(APP)) { + main_loop(); } +#endif glfwTerminate();