From 371044d63d941a054ea228fff134abc1b7d0fae5 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Sat, 9 Dec 2023 23:16:59 -0600 Subject: [PATCH] [nostalgia] Fix scaling of SceneEditorView not to be completely broken --- .../core/include/nostalgia/core/gfx.hpp | 15 ++++++++-- .../modules/core/src/opengl/CMakeLists.txt | 1 - src/nostalgia/modules/core/src/opengl/gfx.cpp | 13 ++++++-- .../scene/src/studio/sceneeditor-imgui.cpp | 30 ++++++++++++------- .../scene/src/studio/sceneeditorview.cpp | 14 +++++---- .../scene/src/studio/sceneeditorview.hpp | 3 +- 6 files changed, 53 insertions(+), 23 deletions(-) diff --git a/src/nostalgia/modules/core/include/nostalgia/core/gfx.hpp b/src/nostalgia/modules/core/include/nostalgia/core/gfx.hpp index 7c59dae2..4c6de8f5 100644 --- a/src/nostalgia/modules/core/include/nostalgia/core/gfx.hpp +++ b/src/nostalgia/modules/core/include/nostalgia/core/gfx.hpp @@ -78,7 +78,16 @@ void setSprite(Context &ctx, Sprite const&s) noexcept; } namespace nostalgia::core::gl { -constexpr ox::CStringView GlslVersion = "#version 330"; -void draw(core::Context&, ox::Size const&) noexcept; -} +constexpr ox::CStringView GlslVersion = "#version 330"; + +constexpr auto DefaultScale = 5; + +[[nodiscard]] +ox::Size drawSize(int scale = DefaultScale) noexcept; + +void draw(core::Context &ctx, ox::Size const&renderSz) noexcept; + +void draw(core::Context&, int scale = 5) noexcept; + +} diff --git a/src/nostalgia/modules/core/src/opengl/CMakeLists.txt b/src/nostalgia/modules/core/src/opengl/CMakeLists.txt index 060dbb35..48a0e7fb 100644 --- a/src/nostalgia/modules/core/src/opengl/CMakeLists.txt +++ b/src/nostalgia/modules/core/src/opengl/CMakeLists.txt @@ -6,5 +6,4 @@ target_sources( target_link_libraries( NostalgiaCore PUBLIC GlUtils - lodepng ) diff --git a/src/nostalgia/modules/core/src/opengl/gfx.cpp b/src/nostalgia/modules/core/src/opengl/gfx.cpp index f1492133..fc6f9d47 100644 --- a/src/nostalgia/modules/core/src/opengl/gfx.cpp +++ b/src/nostalgia/modules/core/src/opengl/gfx.cpp @@ -2,8 +2,6 @@ * Copyright 2016 - 2023 Gary Talent (gary@drinkingtea.net). All rights reserved. */ -#include - #include #include #include @@ -21,7 +19,7 @@ namespace nostalgia::core { -constexpr auto Scale = 10; +constexpr auto Scale = 1; namespace renderer { @@ -234,6 +232,7 @@ static glutils::GLTexture createTexture( GLsizei w, GLsizei h, const void *pixels) noexcept { + oxDebugf("createTexture(w: {}, h: {}, pixels: ...)", w, h); GLuint texId = 0; glGenTextures(1, &texId); glutils::GLTexture tex(texId); @@ -607,6 +606,10 @@ void setTile( namespace gl { +ox::Size drawSize(int scale) noexcept { + return {240 * scale, 160 * scale}; +} + void draw(core::Context &ctx, ox::Size const&renderSz) noexcept { glViewport(0, 0, renderSz.width, renderSz.height); glutils::clearScreen(); @@ -616,6 +619,10 @@ void draw(core::Context &ctx, ox::Size const&renderSz) noexcept { } } +void draw(core::Context &ctx, int scale) noexcept { + draw(ctx, drawSize(scale)); +} + } } diff --git a/src/nostalgia/modules/scene/src/studio/sceneeditor-imgui.cpp b/src/nostalgia/modules/scene/src/studio/sceneeditor-imgui.cpp index 282cad1a..c16a4f75 100644 --- a/src/nostalgia/modules/scene/src/studio/sceneeditor-imgui.cpp +++ b/src/nostalgia/modules/scene/src/studio/sceneeditor-imgui.cpp @@ -12,7 +12,7 @@ namespace nostalgia::scene { SceneEditorImGui::SceneEditorImGui(turbine::Context &ctx, ox::StringView path): m_ctx(ctx), - m_itemPath(std::move(path)), + m_itemPath(path), m_editor(m_ctx, m_itemPath), m_view(m_ctx, m_editor.scene()) { const auto lastSlash = std::find(m_itemPath.rbegin(), m_itemPath.rend(), '/').offset(); @@ -29,18 +29,28 @@ ox::CRString SceneEditorImGui::itemDisplayName() const noexcept { } void SceneEditorImGui::draw(turbine::Context*) noexcept { - const auto paneSize = ImGui::GetContentRegionAvail(); - const ox::Size fbSize{ - static_cast(paneSize.x), - static_cast(paneSize.y)}; - m_view.draw(fbSize.width, fbSize.height); + auto const paneSize = ImGui::GetContentRegionAvail(); + m_view.draw(ox::Size{static_cast(paneSize.x), static_cast(paneSize.y)}); auto &fb = m_view.framebuffer(); - const uintptr_t buffId = fb.color.id; + auto const srcH = static_cast(fb.height) / static_cast(fb.width); + auto const dstH = paneSize.y / paneSize.x; + float xScale{}, yScale{}; + if (dstH > srcH) { + // crop off width + xScale = srcH / dstH; + yScale = 1; + } else { + auto const srcW = static_cast(fb.width) / static_cast(fb.height); + auto const dstW = (paneSize.x / paneSize.y); + xScale = 1; + yScale = srcW / dstW; + } + uintptr_t const buffId = fb.color.id; ImGui::Image( - reinterpret_cast(buffId), + std::bit_cast(buffId), paneSize, - ImVec2(0, 1), - ImVec2(1, 0)); + ImVec2(0, yScale), + ImVec2(xScale, 0)); } void SceneEditorImGui::onActivated() noexcept { diff --git a/src/nostalgia/modules/scene/src/studio/sceneeditorview.cpp b/src/nostalgia/modules/scene/src/studio/sceneeditorview.cpp index e215678f..7344b935 100644 --- a/src/nostalgia/modules/scene/src/studio/sceneeditorview.cpp +++ b/src/nostalgia/modules/scene/src/studio/sceneeditorview.cpp @@ -9,21 +9,25 @@ namespace nostalgia::scene { SceneEditorView::SceneEditorView(turbine::Context &tctx, SceneStatic const&sceneStatic): + m_cctx(core::init(tctx, {.glInstallDrawer = false}).unwrapThrow()), m_sceneStatic(sceneStatic), m_scene(m_sceneStatic) { - oxThrowError(core::init(tctx, {.glInstallDrawer = false}).moveTo(&m_cctx)); } ox::Error SceneEditorView::setupScene() noexcept { + glutils::resizeInitFrameBuffer(m_frameBuffer, core::gl::drawSize(1)); return m_scene.setupDisplay(*m_cctx); } -void SceneEditorView::draw(int width, int height) noexcept { - if (width != m_frameBuffer.width || height != m_frameBuffer.height) { - glutils::resizeInitFrameBuffer(m_frameBuffer, width, height); +void SceneEditorView::draw(ox::Size const&targetSz) noexcept { + auto const scaleSz = targetSz / core::gl::drawSize(1); + auto const scale = ox::max(1, ox::max(scaleSz.width, scaleSz.height)); + if (m_scale != scale) { + m_scale = scale; + glutils::resizeInitFrameBuffer(m_frameBuffer, core::gl::drawSize(m_scale)); } glutils::FrameBufferBind const frameBufferBind(m_frameBuffer); - core::gl::draw(*m_cctx, {width, height}); + core::gl::draw(*m_cctx, m_scale); } glutils::FrameBuffer const&SceneEditorView::framebuffer() const noexcept { diff --git a/src/nostalgia/modules/scene/src/studio/sceneeditorview.hpp b/src/nostalgia/modules/scene/src/studio/sceneeditorview.hpp index 46b64a86..d5a524e8 100644 --- a/src/nostalgia/modules/scene/src/studio/sceneeditorview.hpp +++ b/src/nostalgia/modules/scene/src/studio/sceneeditorview.hpp @@ -18,13 +18,14 @@ class SceneEditorView { SceneStatic const&m_sceneStatic; Scene m_scene; glutils::FrameBuffer m_frameBuffer; + int m_scale = 1; public: SceneEditorView(turbine::Context &ctx, SceneStatic const&sceneStatic); ox::Error setupScene() noexcept; - void draw(int width, int height) noexcept; + void draw(ox::Size const&targetSz) noexcept; [[nodiscard]] glutils::FrameBuffer const&framebuffer() const noexcept;