diff options
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | Makefile | 7 | ||||
-rw-r--r-- | config.h | 2 | ||||
-rw-r--r-- | ems/post_run.js | 3 | ||||
-rw-r--r-- | ems/template.html | 48 | ||||
-rw-r--r-- | platform.c | 178 |
6 files changed, 162 insertions, 77 deletions
@@ -30,3 +30,4 @@ tags /pack /packer /packer.exe +/ems/out @@ -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 @@ -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 @@ +<!doctype html> +<html lang="en-us"> + <head> + <meta charset="utf-8"> + <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> + <title>malloc(bullet);</title> + + <style> + body { + padding: none; + margin-top: 0px; + margin-left: 0px; + background-color: #000000; + image-rendering: crisp-edges; + } + </style> + </head> + <body> + <canvas class="emscripten" id="canvas" oncontextmenu="event.preventDefault()" tabindex="0" onclick="focus_canvas()"></canvas> + + <script> + function focus_canvas() { + document.getElementById('canvas').focus(); + } + Module = { + print: + function(what) + { + console.log(what); + }, + printErr: + function(what) + { + console.error(what); + }, + canvas: + ( + function() + { + return document.getElementById('canvas'); + } + )() + }; + </script> + + {{{ SCRIPT }}} + </body> +</html> @@ -2,7 +2,11 @@ #include "render.h" #include "input.h" -#if defined(plat_posix) +#if defined(plat_emscripten) +#include <emscripten.h> +#endif + +#if defined(plat_posix) || defined(plat_emscripten) #define _POSIX_SOURCE #include <stdlib.h> @@ -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(); |