Implemented a quick and dirty FPS graph, other small refactorings.

This commit is contained in:
2025-06-04 04:52:19 -05:00
parent 7ac0436a1f
commit 48b56213d8
9 changed files with 150 additions and 29 deletions

View File

@@ -14,6 +14,7 @@
#include <Color4.hpp>
#include <J3ML/LinearAlgebra/Vector2.hpp>
#include <JGL/types/Texture.h>
#include <JGL/types/RenderTarget.h>
using J3ML::LinearAlgebra::Vector2;
@@ -27,11 +28,15 @@ namespace JUI {
public:
/// The default constructor initializes this ImageBase with a null pointer texture.
ImageBase();
ImageBase(JGL::RenderTarget *content);
/// This constructor initializes this ImageBase with a JGL::Texture pointer.
explicit ImageBase(JGL::Texture* texture);
public:
[[nodiscard]] JGL::RenderTarget* RenderTarget() const { return texture;}
/// Returns the texture pointer held by this object.
[[nodiscard]] JGL::Texture* Content() const;
[[nodiscard]] const JGL::Texture* Content() const;
/// Returns the color of this object. The texture is rendered with this color.
[[nodiscard]] Color4 Color() const;
/// Returns the scale factor of this object. The texture is scaled with this value.
@@ -40,7 +45,8 @@ namespace JUI {
[[nodiscard]] Vector2 Origin() const;
/// Sets the texture of this object.
void Content(JGL::Texture* content);
void Content(const JGL::Texture* content);
void Content(JGL::RenderTarget* content);
/// Sets the color of this object.
void Color(const Color4& newColor);
/// Sets the scale factor of this object.
@@ -52,7 +58,7 @@ namespace JUI {
/// Draws the image at the given pos, manually scaled to fit the given size.
void Draw(const Vector2& pos, const Vector2& size);
protected:
JGL::Texture* texture;
JGL::RenderTarget* texture;
Color4 image_color = Color4(255,255,255);
Vector2 scale = Vector2(1,1);
Vector2 origin = Vector2(0,0);

View File

@@ -0,0 +1,93 @@
#pragma once
#include "Rect.hpp"
#include "ImageRect.hpp"
namespace JUI {
struct DataPoint {
Vector2 pos;
Color4 color;
};
class Graph : public JUI::ImageRect {
public:
protected:
private:
};
class FpsGraph : public JUI::ImageRect
{
public:
void RenderDataPoints() {
J2D::Begin(canvas, true);
DataPoint prev = data[0];
for (auto& data_pt : data) {
//J2D::DrawGradientLine(prev.color, data_pt.color, prev.pos, data_pt.pos, 1);
J2D::DrawLine(data_pt.color, data_pt.pos, {data_pt.pos.x, GetAbsoluteSize().y }, 1);
prev = data_pt;
}
float target_line_height = (1.f / 60.f) * 1000;
J2D::DrawString(Colors::Black, "60 FPS / 16.7ms (Target)", 0, 100 - target_line_height, 1.f, 10);
J2D::DrawLine(Colors::White, {0, 100 - target_line_height}, {GetAbsoluteSize().x, 100 - target_line_height}, 1);
J2D::End();
}
FpsGraph() : JUI::ImageRect() {
for (int i = 0; i < 500; i++) {
Plot({i / 1.f, 0.f}, Colors::Black);
}
canvas = new JGL::RenderTarget({500, 100});
RenderDataPoints();
Content(canvas);
};
explicit FpsGraph(Widget* parent) : FpsGraph() {
Parent(parent);
}
void Plot(const Vector2& pt, const Color4& col) {
data.push_back(DataPoint{ pt, col });
}
void Update(float delta) override {
ImageRect::Update(delta);
data.erase(data.begin());
for (auto& data_pt : data) {
data_pt.pos.x -= 1;
}
Color4 color = Colors::Green;
if (delta > 1e-3f)
color = Colors::Blue;
if (delta > 1e-2f)
color = Colors::Yellow;
if (delta > 1e-1f)
color = Colors::Red;
if (delta > 1e+1f)
color = Colors::Purples::Magenta;
if (delta > 1e+2f)
color = Colors::Black;
Plot({(data.size()-1.f), 100 - (delta*1000)}, color);
RenderDataPoints();
Content(canvas);
}
std::vector<DataPoint> data;
JGL::RenderTarget* canvas;
protected:
private:
};
}

View File

@@ -72,7 +72,7 @@ namespace JUI {
/// Returns the text displayed as the Window's title.
[[nodiscard]] std::string Title() const;
void SetTitle(const std::string& title);
void Title(const std::string& title);
/// Returns whether dragging the window about with the mouse is enabled.
/// @see class Draggable.
bool IsDraggable() const;