Compare commits

...

2 Commits

Author SHA1 Message Date
Attila Uygun 75b5046684 Arbitrary scale-up 2023-10-25 23:40:07 +02:00
Attila Uygun 669ed5e098 Update for ImguiBackend
- Use RobotoMono-Regular.ttf instead of the default font
- Use delta-time instead of elapsed-time
- Fix for scissor/clipping rectangle
2023-10-25 20:03:26 +02:00
5 changed files with 44 additions and 16 deletions

View File

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

View File

@ -6,6 +6,7 @@
#include "engine/asset/shader_source.h" #include "engine/asset/shader_source.h"
#include "engine/engine.h" #include "engine/engine.h"
#include "engine/input_event.h" #include "engine/input_event.h"
#include "engine/platform/asset_file.h"
#include "engine/renderer/renderer.h" #include "engine/renderer/renderer.h"
#include "engine/renderer/shader.h" #include "engine/renderer/shader.h"
#include "engine/renderer/texture.h" #include "engine/renderer/texture.h"
@ -26,6 +27,28 @@ ImguiBackend::~ImguiBackend() = default;
void ImguiBackend::Initialize() { void ImguiBackend::Initialize() {
IMGUI_CHECKVERSION(); IMGUI_CHECKVERSION();
ImGui::CreateContext(); ImGui::CreateContext();
ImGui::GetIO().IniFilename = nullptr;
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.";
}
// Arbitrary scale-up for mobile devices.
// TODO: Put some effort into DPI awareness.
if (Engine::Get().IsMobile())
ImGui::GetStyle().ScaleAllSizes(2.0f);
Engine::Get().SetImageSource( Engine::Get().SetImageSource(
"imgui_atlas", "imgui_atlas",
[]() -> std::unique_ptr<Image> { []() -> std::unique_ptr<Image> {
@ -33,7 +56,7 @@ void ImguiBackend::Initialize() {
unsigned char* pixels; unsigned char* pixels;
int width, height; int width, height;
ImGui::GetIO().Fonts->GetTexDataAsRGBA32(&pixels, &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>(); auto image = std::make_unique<Image>();
image->Create(width, height); image->Create(width, height);
memcpy(image->GetBuffer(), pixels, width * height * 4); memcpy(image->GetBuffer(), pixels, width * height * 4);
@ -98,7 +121,7 @@ void ImguiBackend::NewFrame() {
ImGuiIO& io = ImGui::GetIO(); ImGuiIO& io = ImGui::GetIO();
io.DisplaySize = ImVec2((float)renderer_->GetScreenWidth(), io.DisplaySize = ImVec2((float)renderer_->GetScreenWidth(),
(float)renderer_->GetScreenHeight()); (float)renderer_->GetScreenHeight());
io.DeltaTime = timer_.Elapsed(); io.DeltaTime = timer_.Delta();
ImGui::NewFrame(); ImGui::NewFrame();
} }
@ -131,14 +154,14 @@ void ImguiBackend::Render() {
for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++) { for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++) {
const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[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 && reinterpret_cast<Texture*>(pcmd->GetTexID())->Activate(0);
pcmd->ClipRect.w > pcmd->ClipRect.y) { renderer_->SetScissor(int(pcmd->ClipRect.x), int(pcmd->ClipRect.y),
renderer_->SetScissor(int(pcmd->ClipRect.x), int(pcmd->ClipRect.y), int(pcmd->ClipRect.z - pcmd->ClipRect.x),
int(pcmd->ClipRect.z - pcmd->ClipRect.x), int(pcmd->ClipRect.w - pcmd->ClipRect.y));
int(pcmd->ClipRect.w - pcmd->ClipRect.y));
}
renderer_->Draw(geometry_id, pcmd->ElemCount, pcmd->IdxOffset); renderer_->Draw(geometry_id, pcmd->ElemCount, pcmd->IdxOffset);
} }
renderer_->DestroyGeometry(geometry_id); renderer_->DestroyGeometry(geometry_id);

View File

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

View File

@ -59,8 +59,6 @@ int RendererOpenGL::GetScreenHeight() const {
} }
void RendererOpenGL::SetScissor(int x, int y, int width, int height) { 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); glScissor(x, screen_height_ - y - height, width, height);
glEnable(GL_SCISSOR_TEST); 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) { void RendererVulkan::SetScissor(int x, int y, int width, int height) {
DCHECK(x >= 0 && y >= 0 && width >= 0 && height >= 0); if (x < 0)
DCHECK(x + width <= GetScreenWidth() && y + height <= GetScreenHeight()); x = 0;
if (y < 0)
y = 0;
if (x + width > GetScreenWidth())
width = GetScreenWidth() - x;
if (y + height > GetScreenHeight())
height = GetScreenHeight() - y;
VkRect2D scissor; VkRect2D scissor;
scissor.offset.x = x; scissor.offset.x = x;