summaryrefslogtreecommitdiff
path: root/src/main.cpp
blob: f5e7a19243914692fa1815e650a743a791ba7751 (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
#include "common.hpp"
#include <ctime>

bool debug_draw_cmodels = false;
bool debug_draw_paths = false;
bool debug_draw_tile_coords = false;
render::state_t *debug_render;

uint64_t nano_clock(void)
{
	struct timespec ts;

	clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
	return ts.tv_sec * 1000000000LLU + ts.tv_nsec;
}

void utf8_to_wchar(wchar_t *out, size_t size, std::string in)
{
	wchar_t *d = out, *end = out + size;

	for (size_t i = 0; i < in.size(); i++) {
		if (d + 1 >= end)
			break;

		if ((in[i] & 128) == 0)
			*(d++) = in[i];
		else {
			if (i + 1 >= in.size()) {
				printf("invalid UTF-8 sequence\n");
				abort();
			}

			*(d++) = ((in[i] & 31) << 6) | (in[i + 1] & 63);
			i++;
		}
	}

	*(d++) = 0;
}

int main()
{
	uint64_t t0 = nano_clock();
	sf::RenderWindow window(sf::VideoMode(800, 600), "Minitrem");
	render::state_t render(&window);
	game::state_t game;
	interface::state_t interface(&window, &game);
	size_t frame = 0;
	double before = NAN;

	game.interface = &interface;
	debug_render = &render;
	window.setVerticalSyncEnabled(true);

	game::load_assets();
	game.start();

	while (1) {
		double now = (nano_clock() - t0) * 1.0e-9, dt;

		if (frame)
			dt = now - before;
		else
			dt = 0.01;

		game.tick(now, dt);
		interface.tick(dt);
		if (!window.isOpen())
			break;

		window.clear();
		render.begin_frame(now, dt);
		render.render(&game);
		interface.render_to(&render);
		render.end_frame();

		before = now;
		frame++;
	}

	game.stop();
	return 0;
}