From 2c4b835fe1c6ad10ff16aa96140ff17a69282a43 Mon Sep 17 00:00:00 2001 From: zlago Date: Wed, 9 Oct 2024 20:21:58 +0200 Subject: emscripten support --- src/incbin.S | 12 ++++++++++++ src/incbin.s | 7 ------- src/main.c | 44 +++++++++++++++++++++++++++++++++++++++++--- src/zip.c | 2 +- 4 files changed, 54 insertions(+), 11 deletions(-) create mode 100644 src/incbin.S delete mode 100644 src/incbin.s (limited to 'src') 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