[nostalgia] Fix scaling of SceneEditorView not to be completely broken

This commit is contained in:
Gary Talent 2023-12-09 23:16:59 -06:00
parent 62c671e823
commit 371044d63d
6 changed files with 53 additions and 23 deletions

View File

@ -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;
}

View File

@ -6,5 +6,4 @@ target_sources(
target_link_libraries(
NostalgiaCore PUBLIC
GlUtils
lodepng
)

View File

@ -2,8 +2,6 @@
* Copyright 2016 - 2023 Gary Talent (gary@drinkingtea.net). All rights reserved.
*/
#include <lodepng.h>
#include <ox/std/array.hpp>
#include <ox/std/fmt.hpp>
#include <ox/std/vec.hpp>
@ -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));
}
}
}

View File

@ -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<int>(paneSize.x),
static_cast<int>(paneSize.y)};
m_view.draw(fbSize.width, fbSize.height);
auto const paneSize = ImGui::GetContentRegionAvail();
m_view.draw(ox::Size{static_cast<int>(paneSize.x), static_cast<int>(paneSize.y)});
auto &fb = m_view.framebuffer();
const uintptr_t buffId = fb.color.id;
auto const srcH = static_cast<float>(fb.height) / static_cast<float>(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<float>(fb.width) / static_cast<float>(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<void*>(buffId),
std::bit_cast<void*>(buffId),
paneSize,
ImVec2(0, 1),
ImVec2(1, 0));
ImVec2(0, yScale),
ImVec2(xScale, 0));
}
void SceneEditorImGui::onActivated() noexcept {

View File

@ -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 {

View File

@ -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;