Skip to content

Commit

Permalink
Simplifies the Gui rendering setup.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kenix3 committed Aug 26, 2024
1 parent 92bce01 commit ab9ff11
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 73 deletions.
14 changes: 1 addition & 13 deletions src/graphic/Fast3D/gfx_pc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,6 @@ static int game_framebuffer_msaa_resolved;

uint32_t gfx_msaa_level = 1;

static bool has_drawn_imgui_menu;

static bool dropped_frame;

static const std::unordered_map<Mtx*, MtxF>* current_mtx_replacements;
Expand Down Expand Up @@ -3963,8 +3961,6 @@ void gfx_start_frame(void) {
gfx_wapi->handle_events();
gfx_wapi->get_dimensions(&gfx_current_window_dimensions.width, &gfx_current_window_dimensions.height,
&gfx_current_window_position_x, &gfx_current_window_position_y);
Ship::Context::GetInstance()->GetWindow()->GetGui()->DrawMenu();
has_drawn_imgui_menu = true;
if (gfx_current_dimensions.height == 0) {
// Avoid division by zero
gfx_current_dimensions.height = 1;
Expand Down Expand Up @@ -4030,18 +4026,11 @@ void gfx_run(Gfx* commands, const std::unordered_map<Mtx*, MtxF>& mtx_replacemen

if (!gfx_wapi->start_frame()) {
dropped_frame = true;
if (has_drawn_imgui_menu) {
Ship::Context::GetInstance()->GetWindow()->GetGui()->StartFrame();
Ship::Context::GetInstance()->GetWindow()->GetGui()->EndFrame();
has_drawn_imgui_menu = false;
}
return;
}
dropped_frame = false;

if (!has_drawn_imgui_menu) {
Ship::Context::GetInstance()->GetWindow()->GetGui()->DrawMenu();
}
Ship::Context::GetInstance()->GetWindow()->GetGui()->DrawMenu();

current_mtx_replacements = &mtx_replacements;

Expand Down Expand Up @@ -4096,7 +4085,6 @@ void gfx_run(Gfx* commands, const std::unordered_map<Mtx*, MtxF>& mtx_replacemen
Ship::Context::GetInstance()->GetWindow()->GetGui()->RenderViewports();
gfx_rapi->end_frame();
gfx_wapi->swap_buffers_begin();
has_drawn_imgui_menu = false;
}

void gfx_end_frame(void) {
Expand Down
121 changes: 61 additions & 60 deletions src/window/gui/Gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ void Gui::DrawMenu() {
ImGui::Begin("Main - Deck", nullptr, windowFlags);
ImGui::PopStyleVar(3);

ImVec2 topLeftPos = ImGui::GetWindowPos();
windowPosBeforeMenuBar = ImGui::GetWindowPos();

const ImGuiID dockId = ImGui::GetID("main_dock");

Expand Down Expand Up @@ -390,57 +390,6 @@ void Gui::DrawMenu() {
windowIter.second->Update();
windowIter.second->Draw();
}

ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0, 0));
ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.0f);
ImGui::PushStyleVar(ImGuiStyleVar_ChildBorderSize, 0.0f);
ImGui::PushStyleColor(ImGuiCol_ChildBg, ImVec4(0.0f, 0.0f, 0.0f, 0.0f));
ImGuiWindowFlags flags = ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoResize |
ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoBackground;
ImGui::Begin("Main Game", nullptr, flags);
ImGui::PopStyleVar(3);
ImGui::PopStyleColor();

ImVec2 mainPos = ImGui::GetWindowPos();
mainPos.x -= topLeftPos.x;
mainPos.y -= topLeftPos.y;
ImVec2 size = ImGui::GetContentRegionAvail();
gfx_current_dimensions.width = (uint32_t)(size.x * gfx_current_dimensions.internal_mul);
gfx_current_dimensions.height = (uint32_t)(size.y * gfx_current_dimensions.internal_mul);
gfx_current_game_window_viewport.x = (int16_t)mainPos.x;
gfx_current_game_window_viewport.y = (int16_t)mainPos.y;
gfx_current_game_window_viewport.width = (int16_t)size.x;
gfx_current_game_window_viewport.height = (int16_t)size.y;

if (CVarGetInteger(CVAR_PREFIX_ADVANCED_RESOLUTION ".Enabled", 0)) {
ApplyResolutionChanges();
}

switch (CVarGetInteger(CVAR_LOW_RES_MODE, 0)) {
case 1: { // N64 Mode
gfx_current_dimensions.width = 320;
gfx_current_dimensions.height = 240;
/*
const int sw = size.y * 320 / 240;
gfx_current_game_window_viewport.x += ((int)size.x - sw) / 2;
gfx_current_game_window_viewport.width = sw;*/
break;
}
case 2: { // 240p Widescreen
const int vertRes = 240;
gfx_current_dimensions.width = vertRes * size.x / size.y;
gfx_current_dimensions.height = vertRes;
break;
}
case 3: { // 480p Widescreen
const int vertRes = 480;
gfx_current_dimensions.width = vertRes * size.x / size.y;
gfx_current_dimensions.height = vertRes;
break;
}
}

GetGameOverlay()->Draw();
}

void Gui::ImGuiBackendNewFrame() {
Expand Down Expand Up @@ -588,8 +537,60 @@ int16_t Gui::GetIntegerScaleFactor() {
}

void Gui::StartFrame() {
const ImVec2 mainPos = ImGui::GetWindowPos();
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0, 0));
ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.0f);
ImGui::PushStyleVar(ImGuiStyleVar_ChildBorderSize, 0.0f);
ImGui::PushStyleColor(ImGuiCol_ChildBg, ImVec4(0.0f, 0.0f, 0.0f, 0.0f));
ImGuiWindowFlags flags = ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoResize |
ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoBackground;

ImGui::Begin("Main Game", nullptr, flags);
ImGui::PopStyleVar(3);
ImGui::PopStyleColor();

ImVec2 mainPos = ImGui::GetWindowPos();
mainPos.x -= windowPosBeforeMenuBar.x;
mainPos.y -= windowPosBeforeMenuBar.y;
ImVec2 size = ImGui::GetContentRegionAvail();
gfx_current_dimensions.width = (uint32_t)(size.x * gfx_current_dimensions.internal_mul);
gfx_current_dimensions.height = (uint32_t)(size.y * gfx_current_dimensions.internal_mul);
gfx_current_game_window_viewport.x = (int16_t)mainPos.x;
gfx_current_game_window_viewport.y = (int16_t)mainPos.y;
gfx_current_game_window_viewport.width = (int16_t)size.x;
gfx_current_game_window_viewport.height = (int16_t)size.y;

if (CVarGetInteger(CVAR_PREFIX_ADVANCED_RESOLUTION ".Enabled", 0)) {
ApplyResolutionChanges();
}

switch (CVarGetInteger(CVAR_LOW_RES_MODE, 0)) {
case 1: { // N64 Mode
gfx_current_dimensions.width = 320;
gfx_current_dimensions.height = 240;
/*
const int sw = size.y * 320 / 240;
gfx_current_game_window_viewport.x += ((int)size.x - sw) / 2;
gfx_current_game_window_viewport.width = sw;*/
break;
}
case 2: { // 240p Widescreen
const int vertRes = 240;
gfx_current_dimensions.width = vertRes * size.x / size.y;
gfx_current_dimensions.height = vertRes;
break;
}
case 3: { // 480p Widescreen
const int vertRes = 480;
gfx_current_dimensions.width = vertRes * size.x / size.y;
gfx_current_dimensions.height = vertRes;
break;
}
}

GetGameOverlay()->Draw();

mainPos = ImGui::GetWindowPos();
size = ImGui::GetContentRegionAvail();
ImVec2 pos = ImVec2(0, 0);
if (CVarGetInteger(CVAR_LOW_RES_MODE, 0) == 1) { // N64 Mode takes priority
const float sw = size.y * 320.0f / 240.0f;
Expand Down Expand Up @@ -650,6 +651,13 @@ void Gui::RenderViewports() {
}
}

void Gui::EndFrame() {
ImGui::EndFrame();
if (mImGuiIo->ConfigFlags & ImGuiConfigFlags_ViewportsEnable) {
ImGui::UpdatePlatformWindows();
}
}

ImTextureID Gui::GetTextureById(int32_t id) {
#ifdef ENABLE_DX11
if (Context::GetInstance()->GetWindow()->GetWindowBackend() == WindowBackend::FAST3D_DXGI_DX11) {
Expand Down Expand Up @@ -708,13 +716,6 @@ void Gui::ImGuiRenderDrawData(ImDrawData* data) {
}
}

void Gui::EndFrame() {
ImGui::EndFrame();
if (mImGuiIo->ConfigFlags & ImGuiConfigFlags_ViewportsEnable) {
ImGui::UpdatePlatformWindows();
}
}

void Gui::SaveConsoleVariablesOnNextTick() {
mNeedsConsoleVariableSave = true;
}
Expand Down
1 change: 1 addition & 0 deletions src/window/gui/Gui.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ class Gui {
std::unordered_map<std::string, GuiTextureMetadata> mGuiTextures;
std::map<std::string, std::shared_ptr<GuiWindow>> mGuiWindows;
bool mPadBtnTogglesMenu = false;
ImVec2 windowPosBeforeMenuBar;
};
} // namespace Ship

Expand Down

0 comments on commit ab9ff11

Please sign in to comment.