summaryrefslogtreecommitdiff
path: root/src/main.cpp
blob: 0a5977f0696d65ff100213fb462cb1a8b018c350 (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
This file is part of Minitrem.

Minitrem is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.

Minitrem is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Minitrem.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "common.hpp"
#include <chrono>
#ifdef CONFIG_SHOW_RSS
#include <sys/types.h>
#include <unistd.h>
#include <fstream>
#endif

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

static ntime_t nclock_ref;

// always > 0
ntime_t nclock(void)
{
	return std::chrono::high_resolution_clock::now().time_since_epoch() /
	       std::chrono::nanoseconds(1) - nclock_ref;
}

void freq_counter_t::tick(void)
{
	if (!t0)
		t0 = nclock();

	ticks++;
}

float freq_counter_t::read(ntime_t ival)
{
	ntime_t now;

	now = nclock();

	if (!t0)
		return 0.0f;

	if (now - t0 < ival)
		return last_reading;

	last_reading = ticks / (float)(now - t0) * 1.0e+9f;
	t0 = now;
	ticks = 0;

	return last_reading;
}

freq_counter_t fc_render;
freq_counter_t fc_game;
freq_counter_t fc_traces, fc_trace_tiles, fc_trace_ents;
freq_counter_t fc_get_entities, fc_get_entities_ents;

#ifdef CONFIG_SHOW_RSS
size_t sys_get_rss(void)
{
	std::ifstream file;
	pid_t pid;
	std::string line;
	size_t res = 0;

	pid = getpid();

	file.open("/proc/" + std::to_string(pid) + "/status");
	while (file >> line) {
		if (line == "VmRSS:") {
			file >> res;
			break;
		}
	}

	return res * 1024;
}
#endif

int main()
{
	nclock_ref = nclock() - 1;

	sf::RenderWindow window(sf::VideoMode(800, 600), "Minitrem");
	render::state_t render(&window);
	game::state_t game;
	interface::state_t interface(&window, &game);

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

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

	while (1) {
		static ntime_t before = 0;
		ntime_t now = nclock();
		double dt;

		if (before)
			dt = (now - before) * 1.0e-9;
		else
			dt = 0.1;
		before = now;

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

		sys_get_rss();

		window.clear();
		render.begin_frame(now, dt);
		render.render(&game);
		game.compute_ambience(&render);
		interface.render_to(&render);
		audio::update(interface.camera_3d, game.paused);
		render.end_frame();
	}

	game.stop();
	return 0;
}