diff --git a/include/GL_Testing.h b/include/GL_Testing.h index a4c3520..5aeb8aa 100644 --- a/include/GL_Testing.h +++ b/include/GL_Testing.h @@ -3,7 +3,7 @@ #include #include -#ifdef defined _WIN32 +#ifdef _WIN32 #define GLEW_STATIC #include diff --git a/include/util.hpp b/include/util.hpp index 61596d5..fc35ae8 100644 --- a/include/util.hpp +++ b/include/util.hpp @@ -14,24 +14,31 @@ const double PI = 3.141592653589793238463; const float PI_F = 3.14159265358979f; - struct fps_counter { public: - fps_counter(){ - if(!f.loadFromFile("../assets/fonts/Arial.ttf")){ - std::cout << "couldn't find the fall back Arial font in ../assets/fonts/" << std::endl; - } else { - t.setFont(f); + fps_counter() : + backdrop(sf::Vector2f(200, 100)), vertex_array(sf::LinesStrip) { + + backdrop.setFillColor(sf::Color(0x0000003F)); + + if (!f.loadFromFile("../assets/fonts/Arial.ttf")) { + std::cout << "couldn't find the fall back Arial font in ../assets/fonts/" << std::endl; + } + else { + t.setFont(f); + t.setCharacterSize(18); + t.setColor(sf::Color::White); } } - void frame(double delta_time){ - if (frame_count == 100){ + void frame(double delta_time) { + // Apply 100 units of smoothing + if (frame_count == 100) { frame_count = 0; fps_average = 0; } - frame_count++; - fps_average += (delta_time - fps_average) / frame_count; + frame_count++; + fps_average += (delta_time - fps_average) / frame_count; } void flip_units() { @@ -40,9 +47,36 @@ public: else milliseconds = true; } - - void draw(sf::RenderWindow *r){ - + + void set_position(sf::Vector2f position) { + backdrop.setPosition(position); + } + + sf::Vector2f get_position() { + return backdrop.getPosition(); + } + + void draw(sf::RenderWindow *r) { + + // FPS Line graph + r->draw(backdrop); + + if (vertex_position == 200) + vertex_position = 0; + + sf::Vector2f origin = backdrop.getPosition(); + sf::Vector2f point = origin + sf::Vector2f(vertex_position, backdrop.getSize().y - (1.0 / fps_average)); + + if (vertex_array.getVertexCount() < 200) + vertex_array.append(sf::Vertex(point, sf::Color::Red)); + else + vertex_array[vertex_position] = sf::Vertex(point, sf::Color::Red); + + r->draw(vertex_array); + + vertex_position++; + + // FPS Text std::string out; if (milliseconds) @@ -50,16 +84,24 @@ public: else out = std::to_string(floor(1 / fps_average)); + t.setPosition(origin); t.setString(out); - r->draw(t); + r->draw(t); } private: - bool milliseconds = false; + + sf::RectangleShape backdrop; + sf::VertexArray vertex_array; + sf::Font f; sf::Text t; + int frame_count = 0; double fps_average = 0; + bool milliseconds = false; + + int vertex_position = 0; }; struct debug_text { diff --git a/src/main.cpp b/src/main.cpp index f12b3c9..3342b15 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -166,6 +166,7 @@ int main() { // ========== DEBUG ========== fps_counter fps; + fps.set_position(sf::Vector2f(WINDOW_X - 200, WINDOW_Y - 100)); sf::Vector2f *dp = camera->get_direction_pointer(); debug_text cam_text_x(1, 30, &dp->x, "incli: ");