summaryrefslogtreecommitdiff
path: root/src/main.c
blob: 0cf5124641d0291283a8106547f953a1bb4baeb3 (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include "TZR.h"
#include "cfg.h"
#include "game.h"
#include "map.h"
#include "tileset.h"
#include <SDL2/SDL_video.h>
#include <stdio.h>
#include <stdlib.h>
#include <SDL2/SDL_main.h>
#include <time.h>

static Game *game = NULL;

static void deinit(void);
static int main_loop(void *udata);
static void acid(void);

int
main(int argc, char **argv)
{
	(void)argc, (void)argv;

	if (TZR_Init(.width=DWIDTH,
	             .height=DHEIGHT,
	             .scale=2,
	             .target_fps=TARGET_FPS,
	             .pixel_perfect=true,
	             .mixer=false,
	             .png_loading=false,
	             .light_system=false,
	             .title="goty of the day"))
		return 1;
	if (atexit(deinit)) {
		perror("main:atexit");
		deinit();
		return 1;
	}
	if (pxInit())
		return 1;

	srand(time(NULL));

	Mix_AllocateChannels(32);

	if ((game = malloc(sizeof(*game))) == NULL) {
		perror("main:malloc");
		return 1;
	}
	if (game_init(game))
		return 1;

	return TZR_MainLoop(main_loop, game);
}

static void
deinit(void)
{
	if (game != NULL)
		free(game);
	pxDeinit();
	TZR_Quit();
}

static int
main_loop(void *udata)
{
	Game *const game = udata;

	game_update(game);
	tileset_update();

	if (TZR_IsKeyPressed(SDL_SCANCODE_R)) {
		if (game_init(game))
			return 1;
	}
	if (TZR_IsKeyPressed(SDL_SCANCODE_F)) {
		static bool fullscreen = false;
		fullscreen = !fullscreen;
		SDL_SetWindowFullscreen(___tzr_window, fullscreen);
	}
	if (TZR_IsKeyPressed(SDL_SCANCODE_N))
		game->queue_next_scene = 1;

	if (TZR_DrawBegin())
		return 1;
	pxPalt();
	pxCls(0);
	pxPalt(0, true);
	game_draw(game);
	acid();
	pxFlip();
	if (TZR_DrawEnd())
		return 1;
	return 0;
}

static void
acid(void)
{
	PxCol cols[8] = { 0, 1, 2, 3, 4, 5, 6, 7 };
	for (int i = 0; i < 1024; i++) {
		const int i = 1 + rand() % 7;
		const int k = 1 + rand() % 7;
		if (i == k)
			continue;
		cols[i] ^= cols[k];
		cols[k] ^= cols[i];
		cols[i] ^= cols[k];
	}
	for (int i = 0; i < 8; i++)
		pxSpal(i, cols[i]);
}