aboutsummaryrefslogtreecommitdiff
path: root/platform.c
diff options
context:
space:
mode:
authorquou <quou@disroot.org>2023-05-06 16:51:36 +1000
committerquou <quou@disroot.org>2023-05-06 16:51:36 +1000
commit4629675329f872c9dd4672402b9b9e5e593fec19 (patch)
treec2e42b6cd93d4b96ebea23177b2dfee3eeaed9ec /platform.c
parentec51d32ea0a30c138e690f637f38d83c086e02b9 (diff)
Building and running for emscripten.
Diffstat (limited to 'platform.c')
-rw-r--r--platform.c178
1 files changed, 102 insertions, 76 deletions
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 <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();