Update for ImguiBackend

- Use RobotoMono-Regular.ttf instead of the default font
- Use delta-time instead of elapsed-time
- Fix for scissor/clipping rectangle
This commit is contained in:
Attila Uygun 2023-10-25 19:33:48 +02:00
parent 8b47314e72
commit 669ed5e098
5 changed files with 38 additions and 16 deletions

View File

@ -45,7 +45,7 @@ Engine::Engine(Platform* platform)
platform_->SetObserver(this);
}
Engine::~Engine() {
Engine::~Engine() noexcept {
LOG(0) << "Shutting down engine.";
thread_pool_.CancelTasks();
@ -157,12 +157,13 @@ void Engine::Draw(float frame_frac) {
}
if (stats_visible_) {
ImVec2 center = ImGui::GetMainViewport()->GetCenter();
ImGui::SetNextWindowPos(center, ImGuiCond_Appearing, ImVec2(0.5f, 0.5f));
ImGuiWindowFlags window_flags =
ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoTitleBar |
ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoCollapse |
ImGuiWindowFlags_NoNav | ImGuiWindowFlags_AlwaysAutoResize;
ImGui::Begin("Stats", nullptr, window_flags);
ImGui::PushItemWidth(ImGui::GetFontSize() * -12);
ImGui::Text("%s", renderer_->GetDebugName());
ImGui::Text("%d fps", fps_);
ImGui::End();

View File

@ -6,6 +6,7 @@
#include "engine/asset/shader_source.h"
#include "engine/engine.h"
#include "engine/input_event.h"
#include "engine/platform/asset_file.h"
#include "engine/renderer/renderer.h"
#include "engine/renderer/shader.h"
#include "engine/renderer/texture.h"
@ -26,6 +27,22 @@ ImguiBackend::~ImguiBackend() = default;
void ImguiBackend::Initialize() {
IMGUI_CHECKVERSION();
ImGui::CreateContext();
size_t buffer_size = 0;
auto buffer = AssetFile::ReadWholeFile("engine/RobotoMono-Regular.ttf",
Engine::Get().GetRootPath().c_str(),
&buffer_size);
if (buffer) {
ImFontConfig font_cfg = ImFontConfig();
font_cfg.FontDataOwnedByAtlas = false;
float size_pixels = Engine::Get().IsMobile() ? 64 : 32;
ImGui::GetIO().Fonts->AddFontFromMemoryTTF(buffer.get(), (int)buffer_size,
size_pixels, &font_cfg);
ImGui::GetIO().Fonts->Build();
} else {
LOG(0) << "Failed to read font file.";
}
Engine::Get().SetImageSource(
"imgui_atlas",
[]() -> std::unique_ptr<Image> {
@ -33,7 +50,7 @@ void ImguiBackend::Initialize() {
unsigned char* pixels;
int width, height;
ImGui::GetIO().Fonts->GetTexDataAsRGBA32(&pixels, &width, &height);
LOG(0) << "imgui font width: " << width << " height: " << height;
LOG(0) << "Font atlas size: " << width << ", " << height;
auto image = std::make_unique<Image>();
image->Create(width, height);
memcpy(image->GetBuffer(), pixels, width * height * 4);
@ -98,7 +115,7 @@ void ImguiBackend::NewFrame() {
ImGuiIO& io = ImGui::GetIO();
io.DisplaySize = ImVec2((float)renderer_->GetScreenWidth(),
(float)renderer_->GetScreenHeight());
io.DeltaTime = timer_.Elapsed();
io.DeltaTime = timer_.Delta();
ImGui::NewFrame();
}
@ -131,14 +148,14 @@ void ImguiBackend::Render() {
for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++) {
const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i];
reinterpret_cast<Texture*>(pcmd->GetTexID())->Activate(0);
if (pcmd->ClipRect.z <= pcmd->ClipRect.x ||
pcmd->ClipRect.w <= pcmd->ClipRect.y)
continue;
if (pcmd->ClipRect.z > pcmd->ClipRect.x &&
pcmd->ClipRect.w > pcmd->ClipRect.y) {
reinterpret_cast<Texture*>(pcmd->GetTexID())->Activate(0);
renderer_->SetScissor(int(pcmd->ClipRect.x), int(pcmd->ClipRect.y),
int(pcmd->ClipRect.z - pcmd->ClipRect.x),
int(pcmd->ClipRect.w - pcmd->ClipRect.y));
}
renderer_->Draw(geometry_id, pcmd->ElemCount, pcmd->IdxOffset);
}
renderer_->DestroyGeometry(geometry_id);

View File

@ -29,7 +29,7 @@ class ImguiBackend {
private:
std::unique_ptr<Shader> shader_;
Renderer* renderer_ = nullptr;
base::ElapsedTimer timer_;
base::DeltaTimer timer_;
};
} // namespace eng

View File

@ -59,8 +59,6 @@ int RendererOpenGL::GetScreenHeight() const {
}
void RendererOpenGL::SetScissor(int x, int y, int width, int height) {
DCHECK(x >= 0 && y >= 0 && width >= 0 && height >= 0);
DCHECK(x + width <= GetScreenWidth() && y + height <= GetScreenHeight());
glScissor(x, screen_height_ - y - height, width, height);
glEnable(GL_SCISSOR_TEST);
}

View File

@ -391,8 +391,14 @@ int RendererVulkan::GetScreenHeight() const {
}
void RendererVulkan::SetScissor(int x, int y, int width, int height) {
DCHECK(x >= 0 && y >= 0 && width >= 0 && height >= 0);
DCHECK(x + width <= GetScreenWidth() && y + height <= GetScreenHeight());
if (x < 0)
x = 0;
if (y < 0)
y = 0;
if (x + width > GetScreenWidth())
width = GetScreenWidth() - x;
if (y + height > GetScreenHeight())
height = GetScreenHeight() - y;
VkRect2D scissor;
scissor.offset.x = x;