/*
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 .
*/
#include "common.hpp"
#include
#ifdef CONFIG_SHOW_RSS
#include
#include
#include
#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;
}
freq_counter_t::freq_counter_t(size_t num_samples_)
{
num_samples = num_samples_;
samples = (ntime_t*)malloc(sizeof(ntime_t) * num_samples);
}
freq_counter_t::~freq_counter_t(void)
{
free(samples);
}
void freq_counter_t::push(ntime_t sample)
{
collected++;
samples[collected % num_samples] = sample;
}
void freq_counter_t::tick(void)
{
ntime_t now = nclock();
if (last_tick)
push(now - last_tick);
last_tick = now;
}
float freq_counter_t::freq_ma(void)
{
ntime_t now;
float res = 0.0f;
size_t to_count;
now = nclock();
if (last_ma_time && now - last_ma_time < 500000000)
return last_ma;
to_count = std::min(collected, num_samples);
for (size_t i = 0; i < to_count; i++)
res += samples[i] * 1.0e-9f;
res = 1.0f / (res / to_count);
last_ma_time = now;
last_ma = res;
return res;
}
freq_counter_t fc_render = freq_counter_t(20);
freq_counter_t fc_game = freq_counter_t(10);
#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;
}