diff --git a/src/kiwano/2d/DebugActor.cpp b/src/kiwano/2d/DebugActor.cpp index 9e39f268..9a8a34c3 100644 --- a/src/kiwano/2d/DebugActor.cpp +++ b/src/kiwano/2d/DebugActor.cpp @@ -45,6 +45,7 @@ private: } // namespace DebugActor::DebugActor() + : frame_buffer_(70 /* pre-alloc for 70 frames */) { SetName("kiwano-debug-actor"); SetPosition(Point{ 10, 10 }); @@ -77,10 +78,10 @@ void DebugActor::OnRender(RenderContext& ctx) ctx.FillRoundedRectangle(GetBounds(), Vec2{ 5.f, 5.f }); ctx.DrawTextLayout(debug_text_, Point(10, 10)); - frame_time_.push_back(Time::Now()); - while (frame_time_.back() - frame_time_.front() >= Duration::Second) + frame_buffer_.PushBack(Time::Now()); + while (frame_buffer_.Back() - frame_buffer_.Front() >= Duration::Second) { - frame_time_.erase(frame_time_.begin()); + frame_buffer_.PopFront(); } } @@ -93,7 +94,7 @@ void DebugActor::OnUpdate(Duration dt) // For formatting integers with commas (void)ss.imbue(comma_locale_); - ss << "Fps: " << frame_time_.size() << std::endl; + ss << "Fps: " << frame_buffer_.Size() << std::endl; #if defined(KGE_DEBUG) if (ObjectBase::IsTracingLeaks()) @@ -106,7 +107,7 @@ void DebugActor::OnUpdate(Duration dt) ss << "Render: " << status.duration.Milliseconds() << "ms" << std::endl; - ss << "Primitives / sec: " << std::fixed << status.primitives * frame_time_.size() << std::endl; + ss << "Primitives / sec: " << std::fixed << status.primitives * frame_buffer_.Size() << std::endl; ss << "Memory: "; { diff --git a/src/kiwano/2d/DebugActor.h b/src/kiwano/2d/DebugActor.h index 27a7a8ed..35247486 100644 --- a/src/kiwano/2d/DebugActor.h +++ b/src/kiwano/2d/DebugActor.h @@ -24,6 +24,99 @@ namespace kiwano { + +template +class SimpleRingBuffer +{ +public: + SimpleRingBuffer(size_t capcity) + { + Reserve(capcity); + } + + void PushBack(const T& val) + { + if (IsFull()) + Reserve(Capacity() * 2); + buffer_[rear_] = val; + IncreaseRear(); + } + + void PopFront() + { + IncreaseFront(); + } + + const T& Front() const + { + return buffer_[front_]; + } + + T& Front() + { + return buffer_[front_]; + } + + const T& Back() const + { + return buffer_[ClampCursor(rear_, 1)]; + } + + T& Back() + { + return buffer_[ClampCursor(rear_, 1)]; + } + + bool IsEmpty() const noexcept + { + return front_ = rear_; + } + + bool IsFull() const noexcept + { + return front_ == (rear_ + 1) % Capacity(); + } + + size_t Size() const + { + return ClampCursor(rear_, front_); + } + + size_t Capacity() const + { + return buffer_.size(); + } + + void Reserve(size_t capacity) + { + buffer_.resize(capacity); + } + +private: + void IncreaseFront() + { + if (buffer_.empty()) + return; + front_ = (front_ + 1) % Capacity(); + } + + void IncreaseRear() + { + rear_ = (rear_ + 1) % Capacity(); + } + + size_t ClampCursor(size_t cursor, size_t off) const + { + return (cursor + Capacity() - off) % Capacity(); + } + +private: + Vector buffer_; + size_t front_ = 0; + size_t rear_ = 0; +}; + + /** * \addtogroup Actors * @{ @@ -52,7 +145,8 @@ private: BrushPtr background_brush_; TextStyle debug_text_style_; TextLayout debug_text_; - List