diff --git a/assets/text.otf b/assets/text.otf new file mode 100644 index 0000000..3094772 Binary files /dev/null and b/assets/text.otf differ diff --git a/main.cpp b/main.cpp index 55fa202..7e622f6 100644 --- a/main.cpp +++ b/main.cpp @@ -4,6 +4,7 @@ #include #include #include "constants.hpp" +#include "stats.hpp" Matrix MAP{ {1,1,1,1,1,1,1,1,1}, @@ -17,16 +18,27 @@ Matrix MAP{ {1,1,1,1,1,1,1,1,1} }; -void draw_gui(sf::RenderWindow &window) { +void draw_gui(sf::RenderWindow &window, sf::Text &text, Stats &stats) { sf::RectangleShape rect({SCREEN_WIDTH - RAY_VIEW_WIDTH, SCREEN_HEIGHT}); + rect.setPosition({0,0}); - rect.setFillColor({100, 100, 100}); + rect.setFillColor({50, 50, 50}); window.draw(rect); + + text.setString( + fmt::format("FPS\nmean:{:>8.5}\nsdev: {:>8.5}\nmin: {:>8.5}\nmax: {:>8.5}\ncount:{:<10}\n\nHit R to reset.", + stats.mean(), stats.stddev(), stats.min, stats.max, stats.n)); + window.draw(text); } int main() { sf::RenderWindow window(sf::VideoMode({SCREEN_WIDTH, SCREEN_HEIGHT}), "Zed's Ray Caster Game Thing"); + sf::Font font{"./assets/text.otf"}; + sf::Text text{font}; + text.setFillColor({255,255,255}); + text.setPosition({10,10}); + //ZED this should set with a function float player_x = matrix::width(MAP) / 2; float player_y = matrix::height(MAP) / 2; @@ -43,9 +55,16 @@ int main() { window.close(); }; + Stats stats; + while(window.isOpen()) { + auto start = std::chrono::high_resolution_clock::now(); rayview.render(); - draw_gui(window); + auto end = std::chrono::high_resolution_clock::now(); + auto elapsed = std::chrono::duration(end - start); + stats.sample(1/elapsed.count()); + + draw_gui(window, text, stats); window.display(); rayview.rotate(rotSpeed, -1); @@ -61,6 +80,10 @@ int main() { rayview.rotate(rotSpeed, 1); } + if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::R)) { + stats.reset(); + } + window.handleEvents(onClose); } diff --git a/meson.build b/meson.build index 5044227..bdd6a14 100644 --- a/meson.build +++ b/meson.build @@ -5,6 +5,7 @@ cc = meson.get_compiler('cpp') catch2 = dependency('catch2-with-main') fmt = dependency('fmt') +freetype = dependency('freetype2') json = dependency('nlohmann_json') opengl32 = cc.find_library('opengl32', required: true) winmm = cc.find_library('winmm', required: true) @@ -17,7 +18,7 @@ sfml_system = dependency('sfml_system') sfml_window = dependency('sfml_window') dependencies = [ - fmt, json, opengl32, + fmt, json, opengl32, freetype, winmm, gdi32, sfml_audio, sfml_graphics, sfml_main, sfml_network, sfml_system, sfml_window @@ -36,6 +37,7 @@ executable('zedcaster', [ 'config.cpp', 'texture.cpp', 'raycaster.cpp', + 'stats.cpp', 'main.cpp' ], dependencies: dependencies) diff --git a/raycaster.cpp b/raycaster.cpp index ea97d40..f888a75 100644 --- a/raycaster.cpp +++ b/raycaster.cpp @@ -37,7 +37,7 @@ Raycaster::Raycaster(sf::RenderWindow& window, Matrix &map, int width, int heigh spriteDistance($textures.NUM_SPRITES), ZBuffer(width) { - $window.setVerticalSyncEnabled(true); + // $window.setVerticalSyncEnabled(true); $view_sprite.setPosition({0, 0}); $pixels = make_unique($width * $height); $textures.load_textures(); diff --git a/stats.c b/stats.c deleted file mode 100644 index 4209365..0000000 --- a/stats.c +++ /dev/null @@ -1,65 +0,0 @@ -#include -#include -#include -#include - -Stats *Stats_recreate(double sum, double sumsq, unsigned long n, - double min, double max) -{ - Stats *st = malloc(sizeof(Stats)); - check_mem(st); - - st->sum = sum; - st->sumsq = sumsq; - st->n = n; - st->min = min; - st->max = max; - - return st; - -error: - return NULL; -} - -Stats *Stats_create() -{ - return Stats_recreate(0.0, 0.0, 0L, 0.0, 0.0); -} - -double Stats_mean(Stats * st) -{ - return st->sum / st->n; -} - -double Stats_stddev(Stats * st) -{ - return sqrt((st->sumsq - (st->sum * st->sum / st->n)) / - (st->n - 1)); -} - -void Stats_sample(Stats * st, double s) -{ - st->sum += s; - st->sumsq += s * s; - - if (st->n == 0) { - st->min = s; - st->max = s; - } else { - if (st->min > s) - st->min = s; - if (st->max < s) - st->max = s; - } - - st->n += 1; -} - -void Stats_dump(Stats * st) -{ - fprintf(stderr, - "sum: %f, sumsq: %f, n: %ld, " - "min: %f, max: %f, mean: %f, stddev: %f", - st->sum, st->sumsq, st->n, st->min, st->max, Stats_mean(st), - Stats_stddev(st)); -} diff --git a/stats.cpp b/stats.cpp new file mode 100644 index 0000000..0508b85 --- /dev/null +++ b/stats.cpp @@ -0,0 +1,10 @@ +#include "stats.hpp" +#include + +void Stats::dump() +{ + fmt::println("sum: {}, sumsq: {}, n: {}, " + "min: {}, max: {}, mean: {}, stddev: {}", + sum, sumsq, n, min, max, mean(), + stddev()); +} diff --git a/stats.h b/stats.h deleted file mode 100644 index 95d3609..0000000 --- a/stats.h +++ /dev/null @@ -1,25 +0,0 @@ -#ifndef lcthw_stats_h -#define lcthw_stats_h - -typedef struct Stats { - double sum; - double sumsq; - unsigned long n; - double min; - double max; -} Stats; - -Stats *Stats_recreate(double sum, double sumsq, unsigned long n, - double min, double max); - -Stats *Stats_create(); - -double Stats_mean(Stats * st); - -double Stats_stddev(Stats * st); - -void Stats_sample(Stats * st, double s); - -void Stats_dump(Stats * st); - -#endif diff --git a/stats.hpp b/stats.hpp new file mode 100644 index 0000000..3be96d2 --- /dev/null +++ b/stats.hpp @@ -0,0 +1,45 @@ +#pragma once +#include + +struct Stats { + double sum = 0.0; + double sumsq = 0.0; + unsigned long n = 0L; + double min = 0.0; + double max = 0.0; + + + inline void reset() { + sum = 0; + sumsq = 0; + n = 0L; + min = 0; + max = 0; + } + + inline double mean() { + return sum / n; + } + + inline double stddev() { + return std::sqrt((sumsq - (sum * sum / n)) / + (n - 1)); + } + + inline void sample(double s) { + sum += s; + sumsq += s * s; + + if (n == 0) { + min = s; + max = s; + } else { + if (min > s) min = s; + if (max < s) max = s; + } + + n += 1; + } + + void dump(); +};