Compare commits

..

No commits in common. "75b504668443e1aae5d2aac2d89f77b09415fc9d" and "8b47314e72366542b6298de271f084a3b7337d68" have entirely different histories.

5 changed files with 16 additions and 44 deletions

View File

@ -45,7 +45,7 @@ Engine::Engine(Platform* platform)
platform_->SetObserver(this); platform_->SetObserver(this);
} }
Engine::~Engine() noexcept { Engine::~Engine() {
LOG(0) << "Shutting down engine."; LOG(0) << "Shutting down engine.";
thread_pool_.CancelTasks(); thread_pool_.CancelTasks();
@ -157,13 +157,12 @@ 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,7 +6,6 @@
#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"
@ -27,28 +26,6 @@ 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> {
@ -56,7 +33,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) << "Font atlas size: " << width << ", " << height; LOG(0) << "imgui font width: " << width << " height: " << 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);
@ -121,7 +98,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_.Delta(); io.DeltaTime = timer_.Elapsed();
ImGui::NewFrame(); ImGui::NewFrame();
} }
@ -154,14 +131,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];
if (pcmd->ClipRect.z <= pcmd->ClipRect.x ||
pcmd->ClipRect.w <= pcmd->ClipRect.y)
continue;
reinterpret_cast<Texture*>(pcmd->GetTexID())->Activate(0); reinterpret_cast<Texture*>(pcmd->GetTexID())->Activate(0);
if (pcmd->ClipRect.z > pcmd->ClipRect.x &&
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::DeltaTimer timer_; base::ElapsedTimer timer_;
}; };
} // namespace eng } // namespace eng

View File

@ -59,6 +59,8 @@ 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,14 +391,8 @@ 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) {
if (x < 0) DCHECK(x >= 0 && y >= 0 && width >= 0 && height >= 0);
x = 0; DCHECK(x + width <= GetScreenWidth() && y + height <= GetScreenHeight());
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;