Files

55 lines
1.8 KiB
C++

#include "core/rendering/sdl3gpu/viewport.hpp"
#include <algorithm>
#include <cmath>
namespace Rendering {
auto computeViewport(Defaults::ScalingMode mode,
std::uint32_t swapchain_w,
std::uint32_t swapchain_h,
int logical_w,
int logical_h) -> SDL_GPUViewport {
const auto SW = static_cast<float>(swapchain_w);
const auto SH = static_cast<float>(swapchain_h);
const auto LW = static_cast<float>(logical_w);
const auto LH = static_cast<float>(logical_h);
float vw = 0.0F;
float vh = 0.0F;
switch (mode) {
case Defaults::ScalingMode::DISABLED:
vw = LW;
vh = LH;
break;
case Defaults::ScalingMode::STRETCH:
vw = SW;
vh = SH;
break;
case Defaults::ScalingMode::LETTERBOX: {
const float SCALE = std::min(SW / LW, SH / LH);
vw = LW * SCALE;
vh = LH * SCALE;
break;
}
case Defaults::ScalingMode::OVERSCAN: {
const float SCALE = std::max(SW / LW, SH / LH);
vw = LW * SCALE;
vh = LH * SCALE;
break;
}
case Defaults::ScalingMode::INTEGER: {
const int SCALE = std::max(1,
std::min(static_cast<int>(SW) / logical_w, static_cast<int>(SH) / logical_h));
vw = LW * static_cast<float>(SCALE);
vh = LH * static_cast<float>(SCALE);
break;
}
}
const float VX = std::floor((SW - vw) * 0.5F);
const float VY = std::floor((SH - vh) * 0.5F);
return {.x = VX, .y = VY, .w = vw, .h = vh, .min_depth = 0.0F, .max_depth = 1.0F};
}
} // namespace Rendering