From 2c4b835fe1c6ad10ff16aa96140ff17a69282a43 Mon Sep 17 00:00:00 2001 From: zlago Date: Wed, 9 Oct 2024 20:21:58 +0200 Subject: emscripten support --- GNUmakefile | 11 ++++++----- build.sh | 9 ++++++--- src/incbin.S | 12 ++++++++++++ src/incbin.s | 7 ------- src/main.c | 44 +++++++++++++++++++++++++++++++++++++++++--- src/zip.c | 2 +- 6 files changed, 66 insertions(+), 19 deletions(-) create mode 100644 src/incbin.S delete mode 100644 src/incbin.s diff --git a/GNUmakefile b/GNUmakefile index 8b08ac0..f1720b7 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -1,9 +1,10 @@ #!/usr/bin/env -S gmake -f -export MKDIR ?= mkdir -p +MKDIR ?= mkdir -p libs := SDL2 m z +EXTENSION ?= out CFLAGS ?= -Wall -Wpedantic -g -Og cflags := ${CFLAGS} ldflags := $(addprefix -l,${libs}) ${LDFLAGS} @@ -14,9 +15,9 @@ deps := $(addprefix out/${NS}/,$(notdir $(patsubst %.c,%.d,$(wildcard src/*.c))) .PHONY: all run clean -all: out/${NS}/a.out out/assets.res +all: out/${NS}/a.${EXTENSION} out/assets.res -run: out/${NS}/a.out out/assets.res +run: out/${NS}/a.${EXTENSION} out/assets.res ./$^ clean: @@ -33,13 +34,13 @@ out/${NS}/libplum.o: cflags += -w out/${NS}/%.o: src/%.c out/${NS}/%.d | out/${NS}/ ${CC} -c -o $@ $< ${cflags} -out/${NS}/incbin.o: src/incbin.s | out/${NS}/ +out/${NS}/incbin.o: src/incbin.S | out/${NS}/ ${CC} -c -o $@ $< -Wa,-I,src/ ${cflags} out/${NS}/%.d: src/%.c | out/${NS}/ ${CC} ${cflags} ${CPPFLAGS} -MM -MG -MF $@ -MT "${@:.d=.o} $@" $< -out/${NS}/a.out: ${objs} out/${NS}/incbin.o | out/${NS}/ +out/${NS}/a.${EXTENSION}: ${objs} out/${NS}/incbin.o | out/${NS}/ ${CC} -o $@ $^ ${cflags} ${ldflags} include assets.mk diff --git a/build.sh b/build.sh index 7f72b4d..aeb3c3d 100755 --- a/build.sh +++ b/build.sh @@ -2,16 +2,19 @@ case $1 in gnu-linux) - NS=gl CFLAGS='-g1 -Os' make + NS=gl EXTENSION=x86_64 CFLAGS='-g1 -Os' make ;; ms-windows) - NS=w CFLAGS='-g1 -Os -mwindows' CC='x86_64-w64-mingw32-cc' make + NS=w EXTENSION=exe CFLAGS='-g1 -Os -mwindows' CC='x86_64-w64-mingw32-cc' make ;; leak) NS=leak CC='cc -fsanitize=leak' CFLAGS='-g -O0' make ;; ub) - NS=leak CC='cc -fsanitize=undefined' CFLAGS='-g -O0' make + NS=ub CC='cc -fsanitize=undefined' CFLAGS='-g -O0' make + ;; + emscripten) + NS=em EXTENSION=html CC='/usr/lib/emscripten/emcc' CFLAGS='-g2 -O1 -sUSE_SDL=2 -sUSE_ZLIB' LDFLAGS='-sASYNCIFY --embed-file out/assets.res@./assets.res' make ;; *) echo >&2 "dont know how to build '$1'" diff --git a/src/incbin.S b/src/incbin.S new file mode 100644 index 0000000..c9d6fd4 --- /dev/null +++ b/src/incbin.S @@ -0,0 +1,12 @@ +// binary includes + +.data + +_game_icon: .incbin "res/icon.png" +_game_icon_end: +.global _game_icon, _game_icon_end + +#if defined(__EMSCRIPTEN__) +.size _game_icon, 1 +.size _game_icon_end, 1 +#endif diff --git a/src/incbin.s b/src/incbin.s deleted file mode 100644 index 9d6420b..0000000 --- a/src/incbin.s +++ /dev/null @@ -1,7 +0,0 @@ -// binary includes - -.data - -_game_icon: .incbin "res/icon.png" -_game_icon_end: -.global _game_icon, _game_icon_end diff --git a/src/main.c b/src/main.c index fbbb371..5cb3960 100644 --- a/src/main.c +++ b/src/main.c @@ -25,6 +25,13 @@ char *game_next_level; static void *particle_tex = NULL; +static SDL_Texture *framebuffer; + +#if defined(__EMSCRIPTEN__) +#include +void main_loop(void); +#endif + #define WINDOW_WIDTH 160 #define WINDOW_HEIGHT 90 #define INIT_SUBSYSTEMS SDL_INIT_VIDEO @@ -282,6 +289,9 @@ int main(int const argc, char *const *const argv) { return EXIT_FAILURE; } SDL_StopTextInput(); + #if defined(__EMSCRIPTEN__) + scale = 1; + #else if (scale <= 0) { // this looks very wrong SDL_DisplayMode dm; if (SDL_GetDesktopDisplayMode(0, &dm) != 0) { @@ -304,6 +314,7 @@ int main(int const argc, char *const *const argv) { } } } + #endif window = SDL_CreateWindow(":3", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WINDOW_WIDTH * scale, WINDOW_HEIGHT * scale, flags | SDL_WINDOW_HIDDEN); if (window == NULL) { @@ -318,7 +329,7 @@ int main(int const argc, char *const *const argv) { SDL_RenderSetLogicalSize(renderer, WINDOW_WIDTH, WINDOW_HEIGHT); SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND); - SDL_Texture *framebuffer = SDL_CreateTexture(renderer, SDL_PIXELTYPE_UNKNOWN, SDL_TEXTUREACCESS_TARGET, WINDOW_WIDTH, WINDOW_HEIGHT); + framebuffer = SDL_CreateTexture(renderer, SDL_PIXELTYPE_UNKNOWN, SDL_TEXTUREACCESS_TARGET, WINDOW_WIDTH, WINDOW_HEIGHT); { res_init(); @@ -376,12 +387,29 @@ int main(int const argc, char *const *const argv) { SDL_ShowWindow(window); - int x = 0, y = 0, fade = 0; player_new(&next_entities); player_property(next_entities.player, "x", "40"); player_property(next_entities.player, "y", "64"); memcpy(&entities, &next_entities, sizeof (entities)); +#if defined(__EMSCRIPTEN__) + emscripten_set_main_loop(main_loop, 60, 0); // TODO: how do i query the framerate if i set it to 0? + return 0; + + end: + tilemap_free(tilemap), tilemap = NULL; + SDL_DestroyRenderer(renderer); + SDL_DestroyWindow(window); + SDL_QuitSubSystem(INIT_SUBSYSTEMS); + return 0; +} + +int x = 0, y = 0, fade = 0; + +void main_loop(void) { +#else + int x = 0, y = 0, fade = 0; while (1) { +#endif input_pressed = input_held; SDL_Event evt; while (SDL_PollEvent(&evt)) { @@ -427,6 +455,10 @@ int main(int const argc, char *const *const argv) { break; case SDL_CLIPBOARDUPDATE: // annoying break; + case SDL_FINGERDOWN: + case SDL_FINGERUP: + case SDL_FINGERMOTION: + break; case SDL_WINDOWEVENT: // window related events switch (evt.window.event) { case SDL_WINDOWEVENT_SHOWN: @@ -547,12 +579,18 @@ int main(int const argc, char *const *const argv) { } break; } +#if defined(__EMSCRIPTEN__) + return; +#else } +#endif end: tilemap_free(tilemap), tilemap = NULL; SDL_DestroyRenderer(renderer); SDL_DestroyWindow(window); - //SDL_QuitSubSystem(INIT_SUBSYSTEMS); + SDL_QuitSubSystem(INIT_SUBSYSTEMS); +#if !defined(__EMSCRIPTEN__) return 0; +#endif } diff --git a/src/zip.c b/src/zip.c index 76820b5..97ef028 100644 --- a/src/zip.c +++ b/src/zip.c @@ -61,7 +61,7 @@ struct zip_file *zip_index(char *const restrict file, size_t const size, int *co static struct zip_footer *zip_find_footer(char *const file, size_t const size, int *const error) { // check if zip file is too big, and if size_t can store values bigger than a max* size zip file - if ((size_t) 1 << 32 && size >= (size_t) 1 << 32) { + if ((size_t) 1 << 31 << 1 && size >= (size_t) 1 << 31 << 1) { *error = ZIP_BIG; return NULL; } -- cgit 1.4.1-2-gfad0