Skip to content
27 changes: 24 additions & 3 deletions include/Nebulite/Core/Renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,12 +264,19 @@ class Renderer final : public Interaction::Execution::Domain {
void moveCam(int dX, int dY) const;

/**
* @brief Scales a rectangle from logical size to window size based on the current window scale factor.
* @brief Scales a rectangle from logical size to window size.
* @param logicalRect The rect from the logical coordinate system to scale.
* @return The rect in the window coordinate system
*/
[[nodiscard]] SDL_FRect scaleRectFromLogicalSize(SDL_FRect const& logicalRect) const ;

/**
* @brief Scales a rect from logical size to the ImGui renderer window size.
* @param logicalRect The rect from the logical coordinate system to scale.
* @return The rect in the window coordinate system
*/
[[nodiscard]] SDL_FRect scaleRectFromLogicalSizeForImgui(SDL_FRect const& logicalRect) const ;

//------------------------------------------
// Getting

Expand Down Expand Up @@ -331,6 +338,16 @@ class Renderer final : public Interaction::Execution::Domain {
*/
[[nodiscard]] unsigned int getWindowScale() const noexcept { return windowScale; }

/**
* @brief Sets a debug multiplier for mouse coordinates forwarded to Dear ImGui.
*/
void setImguiMouseScale(float scale) noexcept { imguiMouseScale = scale; }

/**
* @brief Sets a debug multiplier for Dear ImGui's SDL render pass.
*/
void setImguiRenderScale(float scale) noexcept { imguiRenderScale = scale; }

//------------------------------------------
// Texture-Related

Expand Down Expand Up @@ -498,10 +515,14 @@ class Renderer final : public Interaction::Execution::Domain {
// Custom Subclasses
Environment env;

// Rendering
// Scaling
std::uint8_t windowScale = 1;
SDL_Window* window{};
float pixelScale = 1.0;
float imguiMouseScale = 1.0f;
float imguiRenderScale = 1.0f;

// SDL-Related
SDL_Window* window{};
SDL_Renderer* renderer{};

//------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions include/Nebulite/Data/Tiling.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ class Tile {
* @param capture The capture instance for logging errors during texture creation and rendering
* @param dispPosX The display position, horizontally
* @param dispPosY The display position, vertically
* @param windowScale The scaling factor of the window
* @param pixelScale The scaling factor of the tile texture
*/
void render(
Core::Renderer& nebuliteRenderer,
Expand All @@ -123,7 +123,7 @@ class Tile {
Utility::Io::Capture& capture,
int dispPosX,
int dispPosY,
int windowScale
float pixelScale
);
};

Expand Down
67 changes: 67 additions & 0 deletions include/Nebulite/Module/Domain/Renderer/Debug.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#ifndef NEBULITE_MODULE_DOMAIN_RENDERER_DEBUG_HPP
#define NEBULITE_MODULE_DOMAIN_RENDERER_DEBUG_HPP

//------------------------------------------
// Includes

// Nebulite
#include "Nebulite/Constants/Event.hpp"
#include "Nebulite/Module/Base/DomainModule.hpp"
#include "Nebulite/Utility/Args/SegmentedStringView.hpp"

//------------------------------------------
namespace Nebulite::Core {
class Renderer;
} // namespace Nebulite::Core

namespace Nebulite::Module::Domain::Renderer {
/**
* @class Nebulite::Module::Domain::Renderer::Debug
* @brief DomainModule for renderer debugging utilities.
*/
class Debug final : public Base::DomainModule<Core::Renderer> {
public:
[[nodiscard]] Constants::Event updateHook() override;
void reinit() override {}

//------------------------------------------
// Available Functions

[[nodiscard]] Constants::Event scale(Utility::Args::SegmentedStringView const& args) const;

static auto constexpr scaleName = "imguidb scale";
static auto constexpr scaleDesc = "Scales all Dear ImGui style sizes.\n"
"\n"
"Usage: imguidb scale <scale>\n";

[[nodiscard]] Constants::Event framebufferScale(Utility::Args::SegmentedStringView const& args) const;
static auto constexpr framebufferScaleName = "imguidb framebuffer-scale";
static auto constexpr framebufferScaleDesc = "Overrides Dear ImGui's framebuffer scale for the current frame.\n"
"\n"
"Usage: imguidb framebuffer-scale <scale>\n";

[[nodiscard]] Constants::Event mouseScale(Utility::Args::SegmentedStringView const& args) const;
static auto constexpr mouseScaleName = "imguidb mouse-scale";
static auto constexpr mouseScaleDesc = "Scales mouse coordinates forwarded to Dear ImGui.\n"
"\n"
"Usage: imguidb mouse-scale <scale>\n";

[[nodiscard]] Constants::Event renderScale(Utility::Args::SegmentedStringView const& args) const;
static auto constexpr renderScaleName = "imguidb render-scale";
static auto constexpr renderScaleDesc = "Scales Dear ImGui vertices during its SDL render pass.\n"
"\n"
"Usage: imguidb render-scale <scale>\n";

//------------------------------------------
// Categories

static auto constexpr imguiDebugName = "imguidb";
static auto constexpr imguiDebugDesc = "Dear ImGui debugging utilities.";

//------------------------------------------
// Setup

explicit Debug(ConstructorParams const& params);
};
} // namespace Nebulite::Module::Domain::Renderer
#endif // NEBULITE_MODULE_DOMAIN_RENDERER_DEBUG_HPP
60 changes: 49 additions & 11 deletions src/Nebulite/Core/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ void Renderer::setupDisplayValues() {
auto const resX = Global::settings().get<uint16_t>(Module::Domain::GlobalSpace::Settings::Key::resolutionX).value_or(1000);
auto const resY = Global::settings().get<uint16_t>(Module::Domain::GlobalSpace::Settings::Key::resolutionX).value_or(1000);
windowScale = Global::settings().get<uint8_t>(Module::Domain::GlobalSpace::Settings::Key::resolutionScaling).value_or(1);
pixelScale = static_cast<float>(windowScale);
fps.target = Global::settings().get<uint16_t>(Module::Domain::GlobalSpace::Settings::Key::targetFps).value_or(60);

// Set in workspace
Expand All @@ -133,7 +134,7 @@ void Renderer::initImgui() {
ImPlot::CreateContext();

// Pixel-friendly ImGui style for retro RPGs
ImGuiStyle &style = ImGui::GetStyle();
auto& style = ImGui::GetStyle();

// Rounding
style.WindowRounding = 0.0f;
Expand Down Expand Up @@ -161,7 +162,7 @@ void Renderer::initImgui() {
style.AntiAliasedFill = false;

// Scaling
float const fullScale = SDL_GetDisplayContentScale(SDL_GetPrimaryDisplay()) * static_cast<float>(windowScale) * 0.6f; // adjust to taste
float const fullScale = static_cast<float>(windowScale) * 0.6f; // adjust to taste
style.FontScaleDpi = fullScale;

// Color palette: dark backgrounds, warm accent for UI (tweak hex to taste)
Expand Down Expand Up @@ -268,6 +269,7 @@ void Renderer::initSdl() {
std::abort();
}
SDL_SetWindowPosition(window, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
pixelScale = static_cast<float>(windowScale) * SDL_GetWindowDisplayScale(window);

//------------------------------------------
// UI
Expand Down Expand Up @@ -471,7 +473,16 @@ void Renderer::pollEvents() {
events.push_back(event);

// Handle quit event
if (event.type == SDL_EVENT_QUIT) { status.quit = true; }
if (event.type == SDL_EVENT_QUIT) {
status.quit = true;
}

// Handle change in Scale
if (event.type == SDL_EVENT_WINDOW_DISPLAY_SCALE_CHANGED) {
auto const newScale = SDL_GetWindowDisplayScale(window);
imguiRenderScale = newScale;
pixelScale = newScale * static_cast<float>(windowScale);
}
}
}

Expand All @@ -482,7 +493,22 @@ void Renderer::render() {
// Event polling and processing
pollEvents();
for (auto const& event : events) {
ImGui_ImplSDL3_ProcessEvent(&event);
if (event.type == SDL_EVENT_MOUSE_MOTION) {
SDL_Event imguiEvent = event;
imguiEvent.motion.x *= imguiMouseScale;
imguiEvent.motion.y *= imguiMouseScale;
ImGui_ImplSDL3_ProcessEvent(&imguiEvent);
}
else if (event.type == SDL_EVENT_MOUSE_BUTTON_DOWN || event.type == SDL_EVENT_MOUSE_BUTTON_UP) {
SDL_Event imguiEvent = event;
imguiEvent.button.x *= imguiMouseScale;
imguiEvent.button.y *= imguiMouseScale;
ImGui_ImplSDL3_ProcessEvent(&imguiEvent);
}
else {
ImGui_ImplSDL3_ProcessEvent(&event);
}


// Skip tab key if Imgui wants text input. Otherwise, RmlUi tries to focus some element.
if (event.type == SDL_EVENT_KEY_DOWN && event.key.scancode == SDL_SCANCODE_TAB && ImGui::GetIO().WantTextInput) {
Expand Down Expand Up @@ -527,7 +553,9 @@ void Renderer::render() {
// Finalize render
if (status.showFps) renderFps();
ImGui::Render();
SDL_SetRenderScale(renderer, imguiRenderScale, imguiRenderScale);
ImGui_ImplSDLRenderer3_RenderDrawData(ImGui::GetDrawData(), renderer);
SDL_SetRenderScale(renderer, 1.0f, 1.0f);

// Present frame
SDL_RenderPresent(renderer);
Expand Down Expand Up @@ -683,8 +711,9 @@ void Renderer::changeWindowSize(int const w, int const h, std::uint8_t const sc
auto const currentCenterX = domainScope.get<int>(Constants::KeyNames::Renderer::positionX).value_or(0) + domainScope.get<int>(Constants::KeyNames::Renderer::dispResXLogical).value_or(0) / 2;
auto const currentCenterY = domainScope.get<int>(Constants::KeyNames::Renderer::positionY).value_or(0) + domainScope.get<int>(Constants::KeyNames::Renderer::dispResYLogical).value_or(0) / 2;

// Set new scalar
// Set new scalar, recalculate pixelScale
windowScale = scalar;
pixelScale = static_cast<float>(windowScale) * SDL_GetWindowDisplayScale(window);

// Set the new resolution in the workspace
domainScope.set<int>(Constants::KeyNames::Renderer::dispResXWindow, w * windowScale);
Expand Down Expand Up @@ -739,12 +768,21 @@ void Renderer::moveCam(int const dX, int const dY) const {
}

SDL_FRect Renderer::scaleRectFromLogicalSize(SDL_FRect const& logicalRect) const {
auto const scale = static_cast<float>(windowScale);
return SDL_FRect{
.x=logicalRect.x * scale,
.y=logicalRect.y * scale,
.w=logicalRect.w * scale,
.h=logicalRect.h * scale,
.x=logicalRect.x * pixelScale,
.y=logicalRect.y * pixelScale,
.w=logicalRect.w * pixelScale,
.h=logicalRect.h * pixelScale,
};
}

SDL_FRect Renderer::scaleRectFromLogicalSizeForImgui(SDL_FRect const& logicalRect) const {
auto const scalar = static_cast<float>(windowScale);
return SDL_FRect{
.x=logicalRect.x * scalar,
.y=logicalRect.y * scalar,
.w=logicalRect.w * scalar,
.h=logicalRect.h * scalar,
};
}

Expand Down Expand Up @@ -811,7 +849,7 @@ void Renderer::renderFrame() {
capture,
dispPosX,
dispPosY,
windowScale
pixelScale
);
});
}
Expand Down
14 changes: 7 additions & 7 deletions src/Nebulite/Data/Tiling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ void Tile::render(
Utility::Io::Capture& capture,
int const dispPosX,
int const dispPosY,
int const windowScale
float const pixelScale
){
auto* const renderer = nebuliteRenderer.getSdlRenderer();

Expand All @@ -128,8 +128,8 @@ void Tile::render(
renderer,
SDL_PIXELFORMAT_RGBA8888,
SDL_TEXTUREACCESS_TARGET,
2*windowScale*tilingInfo.w,
2*windowScale*tilingInfo.h
static_cast<int>(2.0f*pixelScale*static_cast<float>(tilingInfo.w)),
static_cast<int>(2.0f*pixelScale*static_cast<float>(tilingInfo.h))
);
if (!texture) {
capture.error.println("Failed to create render target texture.");
Expand All @@ -152,10 +152,10 @@ void Tile::render(
// Render tile to screen
SDL_SetRenderTarget(renderer, nullptr);
SDL_FRect const destRect{
.x = static_cast<float>(windowScale * (coordinate.x * tilingInfo.w - dispPosX)),
.y = static_cast<float>(windowScale * (coordinate.y * tilingInfo.h - dispPosY)),
.w = static_cast<float>(2 * windowScale * tilingInfo.w),
.h = static_cast<float>(2 * windowScale * tilingInfo.h),
.x = pixelScale * static_cast<float>(coordinate.x * tilingInfo.w - dispPosX),
.y = pixelScale * static_cast<float>(coordinate.y * tilingInfo.h - dispPosY),
.w = 2.0f * pixelScale * static_cast<float>(tilingInfo.w),
.h = 2.0f * pixelScale * static_cast<float>(tilingInfo.h),
};
if (!SDL_RenderTexture(renderer, texture, nullptr, &destRect)) {
capture.error.println("Failed to render background tile texture: ", SDL_GetError());
Expand Down
6 changes: 6 additions & 0 deletions src/Nebulite/Module/Domain/Initializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
// Renderer
#include "Nebulite/Module/Domain/Renderer/Audio.hpp"
#include "Nebulite/Module/Domain/Renderer/Console.hpp"
#include "Nebulite/Module/Domain/Renderer/Debug.hpp"
#include "Nebulite/Module/Domain/Renderer/General.hpp"
#include "Nebulite/Module/Domain/Renderer/Input.hpp"
#include "Nebulite/Module/Domain/Renderer/RenderObjectDraft.hpp"
Expand Down Expand Up @@ -245,6 +246,11 @@ void Initializer::initRenderer(Core::Renderer* target) {
Global::settings(),
*target
);
target->initModule<Core::Renderer, Debug>(
"Renderer Debug Functions",
Global::settings(),
*target
);
target->initModule<Core::Renderer, Input>(
"Renderer Input Functions",
Global::settings(),
Expand Down
Loading
Loading