mirror of https://github.com/auygun/kaliber.git
Call ImGui::NewFrame() early in the main loop
This commit is contained in:
parent
75b5046684
commit
f144bfdb2d
|
@ -130,6 +130,8 @@ void Engine::Update(float delta_time) {
|
||||||
seconds_accumulated_ += delta_time;
|
seconds_accumulated_ += delta_time;
|
||||||
++tick_;
|
++tick_;
|
||||||
|
|
||||||
|
imgui_backend_.NewFrame(delta_time);
|
||||||
|
|
||||||
for (auto d : animators_)
|
for (auto d : animators_)
|
||||||
d->Update(delta_time);
|
d->Update(delta_time);
|
||||||
|
|
||||||
|
@ -140,6 +142,9 @@ void Engine::Update(float delta_time) {
|
||||||
fps_ = renderer_->GetAndResetFPS();
|
fps_ = renderer_->GetAndResetFPS();
|
||||||
fps_seconds_ = 0;
|
fps_seconds_ = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (stats_visible_)
|
||||||
|
ShowStats();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Engine::Draw(float frame_frac) {
|
void Engine::Draw(float frame_frac) {
|
||||||
|
@ -150,29 +155,12 @@ void Engine::Draw(float frame_frac) {
|
||||||
[](auto& a, auto& b) { return a->GetZOrder() < b->GetZOrder(); });
|
[](auto& a, auto& b) { return a->GetZOrder() < b->GetZOrder(); });
|
||||||
|
|
||||||
renderer_->PrepareForDrawing();
|
renderer_->PrepareForDrawing();
|
||||||
|
|
||||||
for (auto d : drawables_) {
|
for (auto d : drawables_) {
|
||||||
if (d->IsVisible())
|
if (d->IsVisible())
|
||||||
d->Draw(frame_frac);
|
d->Draw(frame_frac);
|
||||||
}
|
}
|
||||||
|
imgui_backend_.Draw();
|
||||||
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::Text("%s", renderer_->GetDebugName());
|
|
||||||
ImGui::Text("%d fps", fps_);
|
|
||||||
ImGui::End();
|
|
||||||
}
|
|
||||||
|
|
||||||
imgui_backend_.Render();
|
|
||||||
|
|
||||||
renderer_->Present();
|
renderer_->Present();
|
||||||
imgui_backend_.NewFrame();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Engine::AddDrawable(Drawable* drawable) {
|
void Engine::AddDrawable(Drawable* drawable) {
|
||||||
|
@ -696,4 +684,17 @@ void Engine::WaitForAsyncWork() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Engine::ShowStats() {
|
||||||
|
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::Text("%s", renderer_->GetDebugName());
|
||||||
|
ImGui::Text("%d fps", fps_);
|
||||||
|
ImGui::End();
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace eng
|
} // namespace eng
|
||||||
|
|
|
@ -242,6 +242,8 @@ class Engine : public PlatformObserver {
|
||||||
|
|
||||||
void WaitForAsyncWork();
|
void WaitForAsyncWork();
|
||||||
|
|
||||||
|
void ShowStats();
|
||||||
|
|
||||||
Engine(const Engine&) = delete;
|
Engine(const Engine&) = delete;
|
||||||
Engine& operator=(const Engine&) = delete;
|
Engine& operator=(const Engine&) = delete;
|
||||||
};
|
};
|
||||||
|
|
|
@ -52,7 +52,6 @@ void ImguiBackend::Initialize() {
|
||||||
Engine::Get().SetImageSource(
|
Engine::Get().SetImageSource(
|
||||||
"imgui_atlas",
|
"imgui_atlas",
|
||||||
[]() -> std::unique_ptr<Image> {
|
[]() -> std::unique_ptr<Image> {
|
||||||
// Build texture atlas
|
|
||||||
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);
|
||||||
|
@ -67,7 +66,7 @@ void ImguiBackend::Initialize() {
|
||||||
ImGui::GetIO().Fonts->SetTexID(
|
ImGui::GetIO().Fonts->SetTexID(
|
||||||
(ImTextureID)(intptr_t)Engine::Get().AcquireTexture("imgui_atlas"));
|
(ImTextureID)(intptr_t)Engine::Get().AcquireTexture("imgui_atlas"));
|
||||||
|
|
||||||
NewFrame();
|
NewFrame(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ImguiBackend::Shutdown() {
|
void ImguiBackend::Shutdown() {
|
||||||
|
@ -117,28 +116,31 @@ std::unique_ptr<InputEvent> ImguiBackend::OnInputEvent(
|
||||||
return event;
|
return event;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ImguiBackend::NewFrame() {
|
void ImguiBackend::NewFrame(float delta_time) {
|
||||||
|
if (is_new_frame_)
|
||||||
|
ImGui::EndFrame();
|
||||||
|
is_new_frame_ = true;
|
||||||
|
|
||||||
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 = delta_time;
|
||||||
ImGui::NewFrame();
|
ImGui::NewFrame();
|
||||||
}
|
}
|
||||||
|
|
||||||
void ImguiBackend::Render() {
|
void ImguiBackend::Draw() {
|
||||||
|
is_new_frame_ = false;
|
||||||
ImGui::Render();
|
ImGui::Render();
|
||||||
|
|
||||||
ImDrawData* draw_data = ImGui::GetDrawData();
|
ImDrawData* draw_data = ImGui::GetDrawData();
|
||||||
if (draw_data->CmdListsCount <= 0)
|
if (draw_data->CmdListsCount <= 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
float L = draw_data->DisplayPos.x;
|
|
||||||
float R = draw_data->DisplayPos.x + draw_data->DisplaySize.x;
|
|
||||||
float T = draw_data->DisplayPos.y;
|
|
||||||
float B = draw_data->DisplayPos.y + draw_data->DisplaySize.y;
|
|
||||||
base::Matrix4f ortho_projection;
|
base::Matrix4f ortho_projection;
|
||||||
ortho_projection.CreateOrthoProjection(L, R, B, T);
|
ortho_projection.CreateOrthoProjection(
|
||||||
|
draw_data->DisplayPos.x,
|
||||||
|
draw_data->DisplayPos.x + draw_data->DisplaySize.x,
|
||||||
|
draw_data->DisplayPos.y + draw_data->DisplaySize.y,
|
||||||
|
draw_data->DisplayPos.y);
|
||||||
shader_->Activate();
|
shader_->Activate();
|
||||||
shader_->SetUniform("projection", ortho_projection);
|
shader_->SetUniform("projection", ortho_projection);
|
||||||
shader_->UploadUniforms();
|
shader_->UploadUniforms();
|
||||||
|
|
|
@ -3,8 +3,6 @@
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
#include "base/timer.h"
|
|
||||||
|
|
||||||
namespace eng {
|
namespace eng {
|
||||||
|
|
||||||
class InputEvent;
|
class InputEvent;
|
||||||
|
@ -23,13 +21,13 @@ class ImguiBackend {
|
||||||
|
|
||||||
std::unique_ptr<InputEvent> OnInputEvent(std::unique_ptr<InputEvent> event);
|
std::unique_ptr<InputEvent> OnInputEvent(std::unique_ptr<InputEvent> event);
|
||||||
|
|
||||||
void NewFrame();
|
void NewFrame(float delta_time);
|
||||||
void Render();
|
void Draw();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::unique_ptr<Shader> shader_;
|
std::unique_ptr<Shader> shader_;
|
||||||
Renderer* renderer_ = nullptr;
|
Renderer* renderer_ = nullptr;
|
||||||
base::DeltaTimer timer_;
|
bool is_new_frame_ = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace eng
|
} // namespace eng
|
||||||
|
|
|
@ -0,0 +1,21 @@
|
||||||
|
The MIT License (MIT)
|
||||||
|
|
||||||
|
Copyright (c) 2014-2023 Omar Cornut
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
Loading…
Reference in New Issue