aboutsummaryrefslogtreecommitdiff
path: root/main.c
blob: dc2788defa7f6dba75d7e79575fabdbbfa41a412 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/* 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)
{
	(void)argc, (void)argv;

	/* Initialization. Arguments can be ommited. */
	if (TZR_Init(.width=256, .height=256, .pixel_perfect=false,
	             .light_system=true))
		return 1;

	/* Call TZR_Quit on exit. */
	if (atexit(TZR_Quit)) {
		perror("atexit(TZR_Quit)");
		return 1;
	}

	/* Load assets. */
	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). */
	/* static const char res_bmp[] =
	 *     #embed "res.bmp"
	 * ;
	 * const TZR_Uint id0 = TZR_LoadResourceFromMemory(TZR_RES_IMAGE,
	 *     res_bmp, sizeof(res_bmp)); */

	/* Print assets paths. */
	printf("%s %s\n", TZR_GetResourcePath(id0), TZR_GetResourcePath(id1));

	/* Main loop. */
	return TZR_MainLoop(main_loop, NULL);
}

static int
main_loop(void *udata)
{
	(void)udata;

	static int x = 10;
	static int y = 10;

	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;
}