summaryrefslogtreecommitdiff
path: root/src/main.c
blob: 3d6fb77fb2ed8111ae7407a4fcbc9cfb148c1be9 (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
#include "lzy.h"
#include "game.h"
#include "player.h"
#include "background.h"
#include "cfg.h"
#include "input.h"
#include <stdio.h>
#include <stdlib.h>

static Game *game = NULL;

static void deinit(void);

int main(void)
{
	if (LZY_Init("hyperultra!", 30, "res/tset.png", "res/font.png")) {
		LZY_Log("LZY_Init failed: %s", LZY_GetError());
		LZY_Quit();
		return 1;
	}

	if (atexit(deinit)) {
		perror("atexit(deinit)");
		deinit();
		return 1;
	}

	game = malloc(sizeof(Game));
	if (game == NULL) {
		LZY_Log("malloc failed");
		return 1;
	}
	game_init(game);

	double hold = 0;
	int stage = 0;
	while (!LZY_ShouldQuit()) {
		extern double tick;
		LZY_CycleEvents();
		input_update();
		if (input_down(K_O)) {
			hold += 1;
			tick += 1;
		} else {
			hold *= 0.9;
			tick *= 0.9;
		}
		if (hold < 1.0 / 128) {
			hold = 0;
			tick = 0;
		}
		if (hold > 50) {
			if (stage == 1)
				break;
			stage += 1;
			hold = 0;
		}

		if (LZY_DrawBegin()) return LZY_LogError(1);
		LZY_DrawSetColor(WHITE);
		(void)LZY_DrawClear();
		LZY_DrawSetColor(BLACK);
		int y = -16;
		if (stage == 1) {
			(void)LZY_DrawText(48, y += 32, "WALKING is AUTOMATIC");
			(void)LZY_DrawText(48, y += 32, "press SHIFT to jump");
			(void)LZY_DrawText(48, y += 32, "hold UP to jump HIGHER");
			(void)LZY_DrawText(48, y += 32, "hold DOWN to jump LOWER");
		} else {
			(void)LZY_DrawText(48, y += 32, "HYPERULTRA");
			(void)LZY_DrawText(48, y += 32, "a game made by KDX.re");
			(void)LZY_DrawText(48, y += 32, "powered by GINT and SDL");
		}
		(void)LZY_DrawText(48, y += 48, "hold SHIFT to continue");
		if (hold)
			(void)LZY_FillRect(0, DISPLAY_HEIGHT - 24, hold * 8, 24);
		background_draw();
		if (LZY_DrawEnd()) return LZY_LogError(1);
	}

	while (!LZY_ShouldQuit()) {
		LZY_CycleEvents();
		input_update();
		game_update(game);
		background_update(game);

		if (LZY_DrawBegin()) return LZY_LogError(1);
		LZY_DrawSetColor(WHITE);
		if (LZY_DrawClear()) return LZY_LogError(1);
		game_draw(game);
		background_draw();
		if (LZY_DrawEnd()) return LZY_LogError(1);
	}

	return 0;
}

static void
deinit(void)
{
	if (game != NULL) {
		game_deinit(game);
		free(game);
	}
	LZY_Quit();
}