summaryrefslogtreecommitdiff
path: root/src/main.c
blob: 3000befb81354f9fac8bd72a81979cdc1d388ccb (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
113
#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(const Game *game);

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(game);
	pxFlip();
	if (TZR_DrawEnd())
		return 1;
	return 0;
}

//static void
//acid(const Game *game)
//{
//	PxCol cols[8] = { 0, 1, 2, 3, 4, 5, 6, 7 };
//	const int bg = (game->queue_restart_scene != 0);
//	for (int i = 0; i < 1024; i++) {
//		const int i = !bg + rand() % (7 + bg);
//		const int k = !bg + rand() % (7 + bg);
//		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]);
//}