Skip to content

Commit

Permalink
Some tidying
Browse files Browse the repository at this point in the history
  • Loading branch information
pkdawson committed Aug 4, 2024
1 parent a461bf4 commit 4725ad0
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 48 deletions.
18 changes: 9 additions & 9 deletions gdext/src/Context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ void Init(const Ref<Resource>& cfg)
RenderingServer* RS = RenderingServer::get_singleton();
ProjectSettings* PS = ProjectSettings::get_singleton();

RendererType rendererType;
RendererType rendererType = RendererType::Dummy;

String rendererName = cfg->get("Renderer");
if (rendererName == "Dummy")
Expand All @@ -68,7 +68,7 @@ void Init(const Ref<Resource>& cfg)
rendererType = RendererType::Canvas;

// there's no way to get the actual current thread model, eg if --render-thread is used
int threadModel = PS->get_setting("rendering/driver/threads/thread_model");
const int threadModel = PS->get_setting("rendering/driver/threads/thread_model");

std::unique_ptr<Renderer> renderer;
switch (rendererType)
Expand Down Expand Up @@ -110,8 +110,8 @@ void Init(const Ref<Resource>& cfg)
{
Ref<Resource> fontres = fonts[i];
Ref<FontFile> fontData = fontres->get("FontData");
int fontSize = fontres->get("FontSize");
bool merge = fontres->get("Merge");
const int fontSize = fontres->get("FontSize");
const bool merge = fontres->get("Merge");
if (i == 0)
AddFont(fontData, fontSize);
else
Expand Down Expand Up @@ -178,8 +178,8 @@ void RebuildFontAtlas()
ERR_FAIL_COND(!ctx);
ERR_FAIL_COND(ctx->inProcessFrame);

bool scaleToDpi = ProjectSettings::get_singleton()->get_setting("display/window/dpi/allow_hidpi");
int dpiFactor = std::max(1, DisplayServer::get_singleton()->screen_get_dpi() / 96);
const bool scaleToDpi = ProjectSettings::get_singleton()->get_setting("display/window/dpi/allow_hidpi");
const int dpiFactor = std::max(1, DisplayServer::get_singleton()->screen_get_dpi() / 96);
ctx->fonts->RebuildFontAtlas(scaleToDpi ? dpiFactor * ctx->scale : ctx->scale);
}

Expand All @@ -200,9 +200,9 @@ void SetIniFilename(const String& fn)

bool SubViewportWidget(SubViewport* svp)
{
ImVec2 vpSize = svp->get_size();
ImVec2 pos = ImGui::GetCursorScreenPos();
ImVec2 pos_max = {pos.x + vpSize.x, pos.y + vpSize.y};
const ImVec2 vpSize = svp->get_size();
const ImVec2 pos = ImGui::GetCursorScreenPos();
const ImVec2 pos_max = {pos.x + vpSize.x, pos.y + vpSize.y};
ImGui::GetWindowDrawList()->AddImage((ImTextureID)svp->get_texture()->get_rid().get_id(), pos, pos_max);

ImGui::PushID(svp->get_instance_id());
Expand Down
6 changes: 3 additions & 3 deletions gdext/src/Fonts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ struct Fonts::Impl

static void ResetStyle()
{
ImGuiStyle defaultStyle;
const ImGuiStyle defaultStyle;
ImGuiStyle& style = ImGui::GetStyle();

style.WindowPadding = defaultStyle.WindowPadding;
Expand Down Expand Up @@ -79,7 +79,7 @@ struct Fonts::Impl
void Fonts::Impl::AddFontToAtlas(const FontParams& fp, float scale)
{
auto& io = ImGui::GetIO();
int fontSize = fp.fontSize * scale;
const int fontSize = fp.fontSize * scale;
ImFontConfig fc;

if (fp.merge)
Expand All @@ -106,7 +106,7 @@ void Fonts::Impl::AddFontToAtlas(const FontParams& fp, float scale)
std::copy(fontdesc.begin(), fontdesc.end(), fc.Name);
fc.Name[fontdesc.length()] = '\0';

int64_t len = fp.font->get_data().size();
const int64_t len = fp.font->get_data().size();
// let ImGui manage this memory
void* p = ImGui::MemAlloc(len);
memcpy(p, fp.font->get_data().ptr(), len);
Expand Down
8 changes: 4 additions & 4 deletions gdext/src/GdsCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ struct GdsCache::Impl
static void CopyInput(std::vector<char>& buf, const Array& a)
{
CharString cs = String(a[0]).utf8();
std::string_view sv = cs.get_data();
const std::string_view sv = cs.get_data();
if (sv.size() >= buf.size())
return;
std::copy(sv.begin(), sv.end(), buf.begin());
Expand Down Expand Up @@ -54,7 +54,7 @@ void GdsCache::OnNewFrame()

std::vector<char>& GdsCache::GetTextBuf(const StringName& label, size_t size, const Array& a)
{
int64_t hash = ImGui::GetID((void*)label.hash());
const int64_t hash = ImGui::GetID((void*)label.hash());
impl->used[hash] = true;
auto it = impl->bufs.find(hash);
if (it == impl->bufs.end())
Expand All @@ -75,7 +75,7 @@ std::vector<char>& GdsCache::GetTextBuf(const StringName& label, size_t size, co

const std::vector<char>& GdsCache::GetZeroArray(const Array& a)
{
int64_t hash = a.hash();
const int64_t hash = a.hash();
impl->used[hash] = true;
if (auto it = impl->bufs.find(hash); it != impl->bufs.end())
{
Expand All @@ -87,7 +87,7 @@ const std::vector<char>& GdsCache::GetZeroArray(const Array& a)
for (int i = 0; i < a.size(); ++i)
{
CharString cs = String(a[i]).utf8();
std::string_view sv = cs.get_data();
const std::string_view sv = cs.get_data();
std::copy(sv.begin(), sv.end(), std::back_inserter(buf));
buf.push_back('\0');
}
Expand Down
5 changes: 3 additions & 2 deletions gdext/src/ImGuiController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ ImGuiController* instance = nullptr;

struct ImGuiController::Impl
{
Window* window;
Window* window = nullptr;
ImGuiControllerHelper* helper = nullptr;

void CheckContentScale() const;
Expand Down Expand Up @@ -168,7 +168,8 @@ void ImGuiController::SetMainViewport(Viewport* vp)
add_child(newLayer);
else
vp->add_child(newLayer);
ImGui::GetIO().BackendFlags |= ImGuiBackendFlags_PlatformHasViewports | ImGuiBackendFlags_HasMouseHoveredViewport;
ImGui::GetIO().BackendFlags |=
ImGuiBackendFlags_PlatformHasViewports | ImGuiBackendFlags_HasMouseHoveredViewport;
}
else
{
Expand Down
4 changes: 2 additions & 2 deletions gdext/src/ImGuiLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ void ImGuiLayer::UpdateViewport()
vpSize = Object::cast_to<SubViewport>(impl->parentViewport)->get_size();
GetContext()->viewportSize = vpSize;

Transform2D ft = impl->parentViewport->get_final_transform();
const Transform2D ft = impl->parentViewport->get_final_transform();

if (impl->subViewportSize != vpSize ||
impl->finalTransform != ft
Expand All @@ -133,7 +133,7 @@ void ImGuiLayer::UpdateViewport()

RenderingServer* RS = RenderingServer::get_singleton();
RS->viewport_set_size(impl->subViewportRid, impl->subViewportSize.x, impl->subViewportSize.y);
RID vptex = RS->viewport_get_texture(impl->subViewportRid);
const RID vptex = RS->viewport_get_texture(impl->subViewportRid);
RS->canvas_item_clear(impl->canvasItem);
RS->canvas_item_set_transform(impl->canvasItem, ft.affine_inverse());
RS->canvas_item_add_texture_rect(impl->canvasItem,
Expand Down
2 changes: 1 addition & 1 deletion gdext/src/Input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ struct Input::Impl
Vector2 currentSubViewportPos;
Vector2 mouseWheel;
ImGuiMouseCursor currentCursor = ImGuiMouseCursor_None;
bool hasMouse;
bool hasMouse = false;
};

namespace {
Expand Down
38 changes: 19 additions & 19 deletions gdext/src/RdRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ RID RdRenderer::GetFramebuffer(RID vprid)
if (!vprid.is_valid())
return RID();

RenderingServer* RS = RenderingServer::get_singleton();
const RenderingServer* RS = RenderingServer::get_singleton();
RenderingDevice* RD = RS->get_rendering_device();
auto it = impl->framebuffers.find(vprid);
if (it != impl->framebuffers.end())
Expand All @@ -71,7 +71,7 @@ RID RdRenderer::GetFramebuffer(RID vprid)
return fb;
}

RID vptex = RS->texture_get_rd_texture(RS->viewport_get_texture(vprid));
const RID vptex = RS->texture_get_rd_texture(RS->viewport_get_texture(vprid));
godot::TypedArray<godot::RID> arr;
arr.push_back(vptex);
RID fb = RD->framebuffer_create(arr);
Expand All @@ -86,11 +86,11 @@ void RdRenderer::Impl::SetupBuffers(ImDrawData* drawData)
int globalIdxOffset = 0;
int globalVtxOffset = 0;

int idxBufSize = drawData->TotalIdxCount * sizeof(ImDrawIdx);
const int idxBufSize = drawData->TotalIdxCount * sizeof(ImDrawIdx);
godot::PackedByteArray idxBuf;
idxBuf.resize(idxBufSize);

int vertBufSize = drawData->TotalVtxCount * sizeof(ImDrawVert);
const int vertBufSize = drawData->TotalVtxCount * sizeof(ImDrawVert);
godot::PackedByteArray vertBuf;
vertBuf.resize(vertBufSize);

Expand All @@ -112,10 +112,10 @@ void RdRenderer::Impl::SetupBuffers(ImDrawData* drawData)
for (int cmdi = 0; cmdi < cmdList->CmdBuffer.Size; ++cmdi)
{
const ImDrawCmd& drawCmd = cmdList->CmdBuffer[cmdi];
ImTextureID texid = drawCmd.GetTexID();
const ImTextureID texid = drawCmd.GetTexID();
if (!texid)
continue;
RID texrid = make_rid(texid);
const RID texrid = make_rid(texid);
if (!RD->texture_is_valid(texrid))
continue;

Expand Down Expand Up @@ -212,7 +212,7 @@ bool RdRenderer::Init()
TypedArray<RDAttachmentFormat> afs;
afs.push_back(af);

int64_t fb_format = RD->framebuffer_format_create(afs);
const int64_t fb_format = RD->framebuffer_format_create(afs);

impl->pipeline = RD->render_pipeline_create(impl->shader,
fb_format,
Expand Down Expand Up @@ -280,12 +280,12 @@ void RdRenderer::Render(RID fb, ImDrawData* drawData)
impl->SetupBuffers(drawData);

// draw
int64_t dl = RD->draw_list_begin(fb,
RenderingDevice::INITIAL_ACTION_CLEAR,
RenderingDevice::FINAL_ACTION_READ,
RenderingDevice::INITIAL_ACTION_CLEAR,
RenderingDevice::FINAL_ACTION_READ,
impl->clearColors);
const int64_t dl = RD->draw_list_begin(fb,
RenderingDevice::INITIAL_ACTION_CLEAR,
RenderingDevice::FINAL_ACTION_READ,
RenderingDevice::INITIAL_ACTION_CLEAR,
RenderingDevice::FINAL_ACTION_READ,
impl->clearColors);

RD->draw_list_bind_render_pipeline(dl, impl->pipeline);
RD->draw_list_set_push_constant(dl, pcbuf, pcbuf.size());
Expand All @@ -298,19 +298,19 @@ void RdRenderer::Render(RID fb, ImDrawData* drawData)

for (int cmdi = 0; cmdi < cmdList->CmdBuffer.Size; ++cmdi)
{
ImDrawCmd& drawCmd = cmdList->CmdBuffer[cmdi];
const ImDrawCmd& drawCmd = cmdList->CmdBuffer[cmdi];
if (drawCmd.ElemCount == 0)
continue;
if (!impl->uniformSets.contains(drawCmd.GetTexID()))
continue;

RID idxArray =
const RID idxArray =
RD->index_array_create(impl->idxBuffer, drawCmd.IdxOffset + globalIdxOffset, drawCmd.ElemCount);

int64_t voff = (drawCmd.VtxOffset + globalVtxOffset) * sizeof(ImDrawVert);
const int64_t voff = (drawCmd.VtxOffset + globalVtxOffset) * sizeof(ImDrawVert);
impl->srcBuffers[0] = impl->srcBuffers[1] = impl->srcBuffers[2] = impl->vtxBuffer;
impl->vtxOffsets[0] = impl->vtxOffsets[1] = impl->vtxOffsets[2] = voff;
RID vtxArray =
const RID vtxArray =
RD->vertex_array_create(cmdList->VtxBuffer.Size, impl->vtxFormat, impl->srcBuffers, impl->vtxOffsets);

RD->draw_list_bind_uniform_set(dl, impl->uniformSets[drawCmd.GetTexID()], 0);
Expand Down Expand Up @@ -380,7 +380,7 @@ void RdRenderer::Render()
if (!(vp->Flags & ImGuiViewportFlags_IsMinimized))
{
ReplaceTextureRIDs(vp->DrawData);
RID vprid = make_rid(vp->RendererUserData);
const RID vprid = make_rid(vp->RendererUserData);
Render(GetFramebuffer(vprid), vp->DrawData);
}
}
Expand All @@ -389,7 +389,7 @@ void RdRenderer::Render()

void RdRenderer::ReplaceTextureRIDs(ImDrawData* drawData)
{
RenderingServer* RS = RenderingServer::get_singleton();
const RenderingServer* RS = RenderingServer::get_singleton();
for (int i = 0; i < drawData->CmdListsCount; ++i)
{
ImDrawList* cmdList = drawData->CmdLists[i];
Expand Down
3 changes: 2 additions & 1 deletion gdext/src/RdRendererThreadSafe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ struct RdRendererThreadSafe::Impl
RdRendererThreadSafe::RdRendererThreadSafe() : impl(std::make_unique<Impl>())
{
RenderingServer* RS = RenderingServer::get_singleton();
RS->connect("frame_pre_draw", Callable(Engine::get_singleton()->get_singleton("ImGuiController"), "on_frame_pre_draw"));
RS->connect("frame_pre_draw",
Callable(Engine::get_singleton()->get_singleton("ImGuiController"), "on_frame_pre_draw"));
}

RdRendererThreadSafe::~RdRendererThreadSafe()
Expand Down
11 changes: 5 additions & 6 deletions gdext/src/Viewports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ static void Godot_CreateWindow(ImGuiViewport* vp)
}
}

Rect2i winRect = Rect2i(vp->Pos, vp->Size);
const Rect2i winRect = Rect2i(vp->Pos, vp->Size);

ImGuiWindow* igwin = memnew(ImGuiWindow);
igwin->init(vp);
Expand All @@ -60,7 +60,7 @@ static void Godot_CreateWindow(ImGuiViewport* vp)
vd->window->set_flag(Window::FLAG_TRANSPARENT, true);

// it's our window, so just draw directly to the root viewport
RID vprid = vd->window->get_viewport_rid();
const RID vprid = vd->window->get_viewport_rid();
vp->RendererUserData = (void*)vprid.get_id();

int32_t windowID = vd->window->get_window_id();
Expand Down Expand Up @@ -163,8 +163,8 @@ void Viewports::Impl::InitPlatformInterface()
void Viewports::Impl::UpdateMonitors()
{
auto& pio = ImGui::GetPlatformIO();
DisplayServer* DS = DisplayServer::get_singleton();
int screenCount = DS->get_screen_count();
const DisplayServer* DS = DisplayServer::get_singleton();
const int screenCount = DS->get_screen_count();

pio.Monitors.resize(0);
for (int i = 0; i < screenCount; ++i)
Expand All @@ -174,7 +174,7 @@ void Viewports::Impl::UpdateMonitors()
monitor.MainSize = DS->screen_get_size(i);
monitor.DpiScale = DS->screen_get_scale(i);

Rect2i rect = DS->screen_get_usable_rect(i);
const Rect2i rect = DS->screen_get_usable_rect(i);
monitor.WorkPos = rect.position;
monitor.WorkSize = rect.size;

Expand All @@ -198,7 +198,6 @@ void Viewports::Impl::UpdateMonitors()

Viewports::Viewports() : impl(std::make_unique<Impl>())
{
auto& io = ImGui::GetIO();
impl->InitPlatformInterface();
impl->UpdateMonitors();
}
Expand Down
3 changes: 2 additions & 1 deletion gdext/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ void initialize_ign_module(ModuleInitializationLevel p_level)

if (!ProjectSettings::get_singleton()->has_setting("autoload/ImGuiRoot"))
{
UtilityFunctions::push_warning("[imgui-godot] Plugin is not enabled. If you call ImGui methods, your project will crash!");
UtilityFunctions::push_warning(
"[imgui-godot] Plugin is not enabled. If you call ImGui methods, your project will crash!");
}
}

Expand Down

0 comments on commit 4725ad0

Please sign in to comment.