aboutsummaryrefslogtreecommitdiff
path: root/main.c
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 /main.c
parentf15b689ebc0b351529428e2ba561220ee9d31487 (diff)
downloadtzr-0ebbc7b57e9fa1df3115c7962e6501e2f187e935.tar.gz
rework for web build
Diffstat (limited to 'main.c')
-rw-r--r--main.c86
1 files changed, 49 insertions, 37 deletions
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;
}