summaryrefslogtreecommitdiff
path: root/main.c
blob: 7c573a8c10b8fe14a22d0a983c5aa6477ecff954 (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
#include <SDL.h>

#include "world.h"
#include "random.h"
#include "save.h"

static SDL_Window *win;
static SDL_Renderer *rend;

static world game = {0};

static int run() {
	SDL_Event e;
	while (SDL_PollEvent(&e)) {
		seed_rand(e.common.timestamp);

		switch (e.type) {
		case SDL_QUIT:
			save_world(&game);
			return 0;
		case SDL_KEYDOWN:
			break;
		case SDL_KEYUP:
			break;
		default:
			break;
		}
	}

	int nkeys;
	const Uint8 *k = SDL_GetKeyboardState(&nkeys);

	if (k[SDL_SCANCODE_W])
		player_walk(&game, 0, -1);
	if (k[SDL_SCANCODE_S])
		player_walk(&game, 0, 1);
	if (k[SDL_SCANCODE_A])
		player_walk(&game, -1, 0);
	if (k[SDL_SCANCODE_D])
		player_walk(&game, 1, 0);

	if (!k[SDL_SCANCODE_LSHIFT]) {
		if (k[SDL_SCANCODE_UP])
			player_place(&game, 0, -1);
		if (k[SDL_SCANCODE_DOWN])
			player_place(&game, 0, 1);
		if (k[SDL_SCANCODE_LEFT])
			player_place(&game, -1, 0);
		if (k[SDL_SCANCODE_RIGHT])
			player_place(&game, 1, 0);
	} else {
		if (k[SDL_SCANCODE_UP])
			player_destroy(&game, 0, -1);
		if (k[SDL_SCANCODE_DOWN])
			player_destroy(&game, 0, 1);
		if (k[SDL_SCANCODE_LEFT])
			player_destroy(&game, -1, 0);
		if (k[SDL_SCANCODE_RIGHT])
			player_destroy(&game, 1, 0);
	}

	tick_world(&game);
	draw_world(&game, win, rend);
	SDL_RenderPresent(rend);
	SDL_Delay(1);
	return 1;
}

int main(int argc, char *argv[]) {
	if (SDL_Init(SDL_INIT_VIDEO) < 0) {
		SDL_LogCritical(SDL_LOG_CATEGORY_ERROR,
			"error initializing SDL: %s", SDL_GetError());
		return 1;
	}
	win = SDL_CreateWindow("find",
		SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 960, 720,
		SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);
	if (!win) {
		SDL_LogCritical(SDL_LOG_CATEGORY_ERROR,
			"error creating window: %s", SDL_GetError());
		return 1;
	}
	rend = SDL_CreateRenderer(win, -1, SDL_RENDERER_PRESENTVSYNC);
	if (!rend) {
		SDL_LogCritical(SDL_LOG_CATEGORY_ERROR,
			"error creating renderer: %s", SDL_GetError());
		return 1;
	}
	SDL_SetRenderDrawBlendMode(rend, SDL_BLENDMODE_BLEND);

	init_world(&game);
	while (run());

	SDL_DestroyRenderer(rend);
	SDL_DestroyWindow(win);
	return 0;
}