aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkdx <kikoodx@paranoici.org>2023-04-26 13:47:46 +0200
committerkdx <kikoodx@paranoici.org>2023-04-26 14:13:29 +0200
commit0ebbc7b57e9fa1df3115c7962e6501e2f187e935 (patch)
treea46a17aa5fe035b1207d4106730720f7fbcdf771
parentf15b689ebc0b351529428e2ba561220ee9d31487 (diff)
downloadtzr-0ebbc7b57e9fa1df3115c7962e6501e2f187e935.tar.gz
rework for web build
-rwxr-xr-xcreate_TZR.h.sh5
-rw-r--r--headers/TZR.h2
-rw-r--r--main.c86
-rw-r--r--res/clap.wav (renamed from clap.wav)bin38792 -> 38792 bytes
-rw-r--r--res/res.bmp (renamed from res.bmp)bin96138 -> 96138 bytes
-rw-r--r--res/smile.bmp (renamed from smile.bmp)bin12426 -> 12426 bytes
-rw-r--r--sources/TZR_DirectResourceLoad.c9
-rw-r--r--sources/TZR_DrawEnd.c2
-rw-r--r--sources/TZR_Init.c5
-rw-r--r--sources/TZR_MainLoop.c37
10 files changed, 107 insertions, 39 deletions
diff --git a/create_TZR.h.sh b/create_TZR.h.sh
index 505f093..393ccaa 100755
--- a/create_TZR.h.sh
+++ b/create_TZR.h.sh
@@ -22,6 +22,11 @@ HEADER=TZR ProcessHeader
printf '#pragma once\n' >>"$BUILDDIR/out"
sort -u "$BUILDDIR/includes" >>"$BUILDDIR/out"
+cat <<EOF >>"$BUILDDIR/out"
+#ifdef __EMSCRIPTEN__
+#include <emscripten.h>
+#endif
+EOF
printf '\n' >>"$BUILDDIR/out"
cat "$BUILDDIR/sources" >>"$BUILDDIR/out"
printf '\n' >>"$BUILDDIR/out"
diff --git a/headers/TZR.h b/headers/TZR.h
index 0c87f7a..688ba9f 100644
--- a/headers/TZR.h
+++ b/headers/TZR.h
@@ -36,4 +36,6 @@ void TZR_Quit(void);
/* Return non-zero value when a quit event has been received. */
int TZR_ShouldQuit(void);
+int TZR_MainLoop(int (*main_loop)(void *udata), void *udata);
+
unsigned long TZR_GetTick(void);
diff --git a/main.c b/main.c
index f358a11..dc2788d 100644
--- a/main.c
+++ b/main.c
@@ -1,7 +1,11 @@
/* gcc -std=c99 TZR.c main.c -lSDL2 -lSDL2_image -lSDL2_mixer */
+/* emcc -sUSE_SDL=2 -sUSE_SDL_IMAGE=2 -sUSE_SDL_MIXER=2 --preload-file res \
+ * -Wno-initializer-overrides -std=c99 -o index.html TZR.c main.c */
#include "TZR.h"
+static int main_loop(void *udata);
+
int
main(int argc, char **argv)
{
@@ -19,13 +23,11 @@ main(int argc, char **argv)
}
/* Load assets. */
- const TZR_Uint id0 = TZR_LoadResourceTyped(TZR_RES_IMAGE, "res.bmp");
- const TZR_Uint id1 = TZR_LoadResourceTyped(TZR_RES_RAW, "main.c");
- (void)TZR_LoadResourceTyped(TZR_RES_IMAGE, "res.bmp");
- (void)TZR_LoadResourceTyped(TZR_RES_RAW, "main.c");
- const TZR_Uint id2 = TZR_LoadResource("smile.bmp");
- const TZR_Uint id3 = TZR_LoadResourceTyped(TZR_RES_SOUND, "clap.wav");
- if (id0 != 1 || id1 != 2 || id2 != 3 || id3 != 4)
+ const TZR_Uint id0 = TZR_LoadResourceTyped(TZR_RES_IMAGE, "res/res.bmp");
+ (void)TZR_LoadResourceTyped(TZR_RES_IMAGE, "res/res.bmp");
+ const TZR_Uint id1 = TZR_LoadResource("res/smile.bmp");
+ const TZR_Uint id2 = TZR_LoadResourceTyped(TZR_RES_SOUND, "res/clap.wav");
+ if (id0 != 1 || id1 != 2 || id2 != 3)
return 1;
/* Asset loading w/ #embed (C23 proposal). */
@@ -39,37 +41,47 @@ main(int argc, char **argv)
printf("%s %s\n", TZR_GetResourcePath(id0), TZR_GetResourcePath(id1));
/* Main loop. */
- int x = 10;
- int y = 10;
- while (!TZR_ShouldQuit()) {
- TZR_CycleEvents();
- x -= TZR_IsKeyDown(SDL_SCANCODE_LEFT);
- x += TZR_IsKeyDown(SDL_SCANCODE_RIGHT);
- y -= TZR_IsKeyDown(SDL_SCANCODE_UP);
- y += TZR_IsKeyDown(SDL_SCANCODE_DOWN);
- if (TZR_IsKeyPressed(SDL_SCANCODE_SPACE))
- TZR_PlaySound(id3);
+ return TZR_MainLoop(main_loop, NULL);
+}
+
+static int
+main_loop(void *udata)
+{
+ (void)udata;
- if (TZR_LightBegin())
- return 1;
- TZR_DrawSetColor(0.1f, 0.1f, 0.1f, 1.0f);
- TZR_DrawClear();
- TZR_DrawSetColor(0.0f, 1.0f, 1.0f, 1.0f);
- TZR_DrawRectangle(10, 10, 128, 64, .fill=true);
- TZR_DrawSetColor(1.0f, 0.0f, 0.0f, 1.0f);
- TZR_DrawRectangle(64, 30, 128, 64, .fill=true);
- if (TZR_LightEnd())
- return 1;
+ static int x = 10;
+ static int y = 10;
- if (TZR_DrawBegin()
- || TZR_DrawSetColor(0.0f, 0.0f, 0.0f)
- || TZR_DrawClear()
- || TZR_DrawSetColor(1.0f, 1.0f, 1.0f, 0.2f)
- || TZR_DrawImage(id0, 128+x, 128+y, .sy=(float)x/10, .center=true)
- || TZR_DrawSetColor(.a=0.5f)
- || TZR_DrawImage(id2, y, x, .w=x*2)
- || TZR_DrawEnd())
- return 1;
- }
+ const TZR_Uint id0 = TZR_LoadResource("res/res.bmp");
+ const TZR_Uint id1 = TZR_LoadResource("res/smile.bmp");
+ const TZR_Uint id2 = TZR_LoadResource("res/clap.wav");
+
+ x -= TZR_IsKeyDown(SDL_SCANCODE_LEFT);
+ x += TZR_IsKeyDown(SDL_SCANCODE_RIGHT);
+ y -= TZR_IsKeyDown(SDL_SCANCODE_UP);
+ y += TZR_IsKeyDown(SDL_SCANCODE_DOWN);
+ if (TZR_IsKeyPressed(SDL_SCANCODE_SPACE))
+ TZR_PlaySound(id2);
+
+ if (TZR_LightBegin())
+ return 1;
+ TZR_DrawSetColor(0.1f, 0.1f, 0.1f, 1.0f);
+ TZR_DrawClear();
+ TZR_DrawSetColor(0.0f, 1.0f, 1.0f, 1.0f);
+ TZR_DrawRectangle(10, 10, 128, 64, .fill=true);
+ TZR_DrawSetColor(1.0f, 0.0f, 0.0f, 1.0f);
+ TZR_DrawRectangle(64, 30, 128, 64, .fill=true);
+ if (TZR_LightEnd())
+ return 1;
+
+ if (TZR_DrawBegin()
+ || TZR_DrawSetColor(0.0f, 0.0f, 0.0f)
+ || TZR_DrawClear()
+ || TZR_DrawSetColor(1.0f, 1.0f, 1.0f, 0.2f)
+ || TZR_DrawImage(id0, 128+x, 128+y, .sy=(float)x/10, .center=true)
+ || TZR_DrawSetColor(.a=0.5f)
+ || TZR_DrawImage(id1, y, x, .w=x*2)
+ || TZR_DrawEnd())
+ return 1;
return 0;
}
diff --git a/clap.wav b/res/clap.wav
index 4deb8d4..4deb8d4 100644
--- a/clap.wav
+++ b/res/clap.wav
Binary files differ
diff --git a/res.bmp b/res/res.bmp
index 5a15c55..5a15c55 100644
--- a/res.bmp
+++ b/res/res.bmp
Binary files differ
diff --git a/smile.bmp b/res/smile.bmp
index 733a0a3..733a0a3 100644
--- a/smile.bmp
+++ b/res/smile.bmp
Binary files differ
diff --git a/sources/TZR_DirectResourceLoad.c b/sources/TZR_DirectResourceLoad.c
index 3e0b155..d2ee637 100644
--- a/sources/TZR_DirectResourceLoad.c
+++ b/sources/TZR_DirectResourceLoad.c
@@ -24,8 +24,13 @@ TZR_DirectResourceLoad(TZR_Resource *res, const void *data, int size)
SDL_RWops *const rw = SDL_RWFromConstMem(data, size);
if (rw == NULL)
return sdl_error(-1);
- SDL_Surface *const surf = IMG_Load_RW(rw, 0);
- SDL_RWclose(rw);
+ SDL_Surface *surf = IMG_Load_RW(rw, 1);
+ if (surf == NULL) {
+ SDL_RWops *const rw = SDL_RWFromConstMem(data, size);
+ if (rw == NULL)
+ return sdl_error(-1);
+ surf = SDL_LoadBMP_RW(rw, 1);
+ }
if (surf == NULL)
return sdl_error(-1);
SDL_Texture *const tex =
diff --git a/sources/TZR_DrawEnd.c b/sources/TZR_DrawEnd.c
index d79d4f2..bc3ce95 100644
--- a/sources/TZR_DrawEnd.c
+++ b/sources/TZR_DrawEnd.c
@@ -44,6 +44,7 @@ TZR_DrawEnd(void)
&dest) < 0)
return sdl_error(-1);
}
+
if (___tzr_config.target_fps > 0) {
___tzr_next_time += ___tzr_min_dt;
const unsigned long cur_time = SDL_GetTicks64();
@@ -53,6 +54,7 @@ TZR_DrawEnd(void)
SDL_Delay(___tzr_next_time - cur_time);
}
SDL_RenderPresent(___tzr_renderer);
+
___tzr_tick += 1;
return 0;
}
diff --git a/sources/TZR_Init.c b/sources/TZR_Init.c
index c2cfea7..ea505b7 100644
--- a/sources/TZR_Init.c
+++ b/sources/TZR_Init.c
@@ -47,8 +47,13 @@ _TZR_Init(const TZR_Config *config)
if (___tzr_window == NULL)
return _sdl_error();
+#ifdef __EMSCRIPTEN__
+ ___tzr_renderer = SDL_CreateRenderer(___tzr_window, -1,
+ SDL_RENDERER_SOFTWARE);
+#else
___tzr_renderer = SDL_CreateRenderer(___tzr_window, -1,
SDL_RENDERER_ACCELERATED);
+#endif
if (___tzr_renderer == NULL)
return _sdl_error();
diff --git a/sources/TZR_MainLoop.c b/sources/TZR_MainLoop.c
new file mode 100644
index 0000000..325a0ce
--- /dev/null
+++ b/sources/TZR_MainLoop.c
@@ -0,0 +1,37 @@
+#include "TZR.h"
+#include "TZR_globals.h"
+
+#ifdef __EMSCRIPTEN__
+
+static int (*_main_loop)(void *udata);
+static void *_udata;
+static void
+_em_loop(void)
+{
+ TZR_CycleEvents();
+ _main_loop(_udata);
+}
+
+int
+TZR_MainLoop(int (*main_loop)(void *udata), void *udata)
+{
+ _main_loop = main_loop;
+ _udata = udata;
+ emscripten_set_main_loop(_em_loop, 0, 1);
+ return 0;
+}
+
+#else
+
+int
+TZR_MainLoop(int (*main_loop)(void *udata), void *udata)
+{
+ while (!TZR_ShouldQuit()) {
+ TZR_CycleEvents();
+ if (main_loop(udata))
+ return 1;
+ }
+ return 0;
+}
+
+#endif