From 4629675329f872c9dd4672402b9b9e5e593fec19 Mon Sep 17 00:00:00 2001 From: quou Date: Sat, 6 May 2023 16:51:36 +1000 Subject: Building and running for emscripten. --- .gitignore | 1 + Makefile | 7 +++ config.h | 2 +- ems/post_run.js | 3 + ems/template.html | 48 +++++++++++++++ platform.c | 178 +++++++++++++++++++++++++++++++----------------------- 6 files changed, 162 insertions(+), 77 deletions(-) create mode 100644 ems/post_run.js create mode 100644 ems/template.html diff --git a/.gitignore b/.gitignore index 543c138..3db2128 100644 --- a/.gitignore +++ b/.gitignore @@ -30,3 +30,4 @@ tags /pack /packer /packer.exe +/ems/out diff --git a/Makefile b/Makefile index 25e0cec..b840482 100644 --- a/Makefile +++ b/Makefile @@ -132,6 +132,13 @@ pack_assets: $(compiler) -O3 -Wall pack.c -o packer $(pack_cmd) $(assets) +emscripten: $(target) + mkdir -p ems/out + emcc $(sources) -o ems/out/mallocbullet.html \ + --embed-file pack -Dplat_sdl2 -Dplat_emscripten -sUSE_SDL=2 \ + -sTOTAL_MEMORY=67108864 --shell-file ems/template.html \ + --post-js ems/post_run.js + -include $(objects:%.o=%.d) $(target): $(objects) assets pack_assets diff --git a/config.h b/config.h index ff95ba4..51a559a 100644 --- a/config.h +++ b/config.h @@ -8,7 +8,7 @@ #define renderer_w 320 #define renderer_h 240 -#define renderer_scale 4 +#define renderer_scale 2 #define no_sound 1 diff --git a/ems/post_run.js b/ems/post_run.js new file mode 100644 index 0000000..b52a931 --- /dev/null +++ b/ems/post_run.js @@ -0,0 +1,3 @@ +addOnPostRun(function() { + document.getElementById('canvas').focus(); +}); diff --git a/ems/template.html b/ems/template.html new file mode 100644 index 0000000..cc5985d --- /dev/null +++ b/ems/template.html @@ -0,0 +1,48 @@ + + + + + + malloc(bullet); + + + + + + + + + {{{ SCRIPT }}} + + diff --git a/platform.c b/platform.c index 3cb457a..940e57f 100644 --- a/platform.c +++ b/platform.c @@ -2,7 +2,11 @@ #include "render.h" #include "input.h" -#if defined(plat_posix) +#if defined(plat_emscripten) +#include +#endif + +#if defined(plat_posix) || defined(plat_emscripten) #define _POSIX_SOURCE #include @@ -167,66 +171,49 @@ void platform_quit() { app_running = 0; } -int main(int argc, char** argv) { - SDL_Window* window; - SDL_Renderer* renderer; - SDL_Texture* backbuffer; - void* bb_pixels; - SDL_Event event; - SDL_Rect src_rect, dst_rect; +/* lol, lmao + * haha */ +static SDL_Window* window; +static SDL_Renderer* renderer; +static SDL_Texture* backbuffer; +static void* bb_pixels; +static SDL_Event event; +static SDL_Rect src_rect, dst_rect; #if !no_sound - SDL_AudioSpec aud_spec; +static SDL_AudioSpec aud_spec; #endif - Button button; - int ww, wh, bb_pitch, ntw, nth; - int to_sleep, begin_time, end_time; - SDL_Joystick* joysticks[max_joysticks]; - - audio_pos = 0; - audio_len = 1024; - ticker = 0; - - SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK); - - SDL_CreateWindowAndRenderer( - default_window_w, - default_window_h, - SDL_WINDOW_RESIZABLE, - &window, - &renderer - ); - - SDL_SetWindowTitle(window, game_name); - -#if !no_sound - aud_spec.freq = 22050; - aud_spec.format = AUDIO_U16; - aud_spec.channels = 1; - aud_spec.samples = 1024; - aud_spec.callback = fill_audio; - aud_spec.userdata = 0; +static Button button; +static int ww, wh, bb_pitch, ntw, nth; +static int to_sleep, begin_time, end_time; +static SDL_Joystick* joysticks[max_joysticks]; + +static void fit_texture(int w, int h) { + ntw = renderer_w; + nth = renderer_h; + texture_w = ntw; + texture_h = nth; + + /* This is kinda shit lmao. */ + while (1) { + ntw *= 2; + nth *= 2; + + if ( + ntw > w || + nth > h + ) { + break; + } - if (SDL_OpenAudio(&aud_spec, 0) < 0) { - fprintf( - stderr, - "Sound error: %s.\n", - SDL_GetError() - ); + texture_w = ntw; + texture_h = nth; } - SDL_PauseAudio(0); -#endif - - on_init(argc, argv); - - backbuffer = SDL_CreateTexture( - renderer, - SDL_PIXELFORMAT_ABGR8888, - SDL_TEXTUREACCESS_STREAMING, - renderer_w, - renderer_h - ); +} +static void main_loop() { +#if !defined(plat_emscripten) while (app_running) { +#endif begin_time = SDL_GetTicks(); reset_input(); @@ -286,27 +273,7 @@ int main(int argc, char** argv) { case SDL_WINDOWEVENT: switch (event.window.event) { case SDL_WINDOWEVENT_RESIZED: - ntw = renderer_w; - nth = renderer_h; - texture_w = ntw; - texture_h = nth; - - /* This is kinda shit lmao. */ - while (1) { - ntw *= 2; - nth *= 2; - - if ( - ntw > event.window.data1 || - nth > event.window.data2 - ) { - break; - } - - texture_w = ntw; - texture_h = nth; - } - + fit_texture(event.window.data1, event.window.data2); break; } default: @@ -369,7 +336,66 @@ int main(int argc, char** argv) { if (to_sleep > 0) { SDL_Delay(to_sleep); } +#if !defined(plat_emscripten) } +#endif +} + +int main(int argc, char** argv) { + audio_pos = 0; + audio_len = 1024; + ticker = 0; + + SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK); + + SDL_CreateWindowAndRenderer( + default_window_w * renderer_scale, + default_window_h * renderer_scale, + SDL_WINDOW_RESIZABLE, + &window, + &renderer + ); + + SDL_SetWindowTitle(window, game_name); + +#if !no_sound + aud_spec.freq = 22050; + aud_spec.format = AUDIO_U16; + aud_spec.channels = 1; + aud_spec.samples = 1024; + aud_spec.callback = fill_audio; + aud_spec.userdata = 0; + + if (SDL_OpenAudio(&aud_spec, 0) < 0) { + fprintf( + stderr, + "Sound error: %s.\n", + SDL_GetError() + ); + } + SDL_PauseAudio(0); +#endif + + on_init(argc, argv); + + backbuffer = SDL_CreateTexture( + renderer, + SDL_PIXELFORMAT_ABGR8888, + SDL_TEXTUREACCESS_STREAMING, + renderer_w, + renderer_h + ); + + fit_texture( + renderer_w * renderer_scale, + renderer_h * renderer_scale + ); + +#if defined(plat_emscripten) + emscripten_set_main_loop(main_loop, 0, 1); +#else + main_loop(); +#endif on_deinit(); -- cgit v1.2.3-54-g00ecf