Bring in the gnu omni font for text, and rewrite my stats code to use c++ and then use that to calc FPS stats for an FPS display on the left. Debug build gets about 48, release gets about 500fps. Amit's code will probably do even better.

This commit is contained in:
Zed A. Shaw 2025-01-18 02:42:41 -05:00
parent e3e0f0a322
commit cf9682ed70
8 changed files with 85 additions and 95 deletions

BIN
assets/text.otf Normal file

Binary file not shown.

View file

@ -4,6 +4,7 @@
#include <numeric>
#include <functional>
#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<double>(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);
}

View file

@ -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)

View file

@ -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<RGBA[]>($width * $height);
$textures.load_textures();

65
stats.c
View file

@ -1,65 +0,0 @@
#include <math.h>
#include <lcthw/stats.h>
#include <stdlib.h>
#include <lcthw/dbg.h>
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));
}

10
stats.cpp Normal file
View file

@ -0,0 +1,10 @@
#include "stats.hpp"
#include <fmt/core.h>
void Stats::dump()
{
fmt::println("sum: {}, sumsq: {}, n: {}, "
"min: {}, max: {}, mean: {}, stddev: {}",
sum, sumsq, n, min, max, mean(),
stddev());
}

25
stats.h
View file

@ -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

45
stats.hpp Normal file
View file

@ -0,0 +1,45 @@
#pragma once
#include <cmath>
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();
};