diff --git a/src/platform/qt/DisplayGL.cpp b/src/platform/qt/DisplayGL.cpp index bf9f6a867..c90c19aac 100644 --- a/src/platform/qt/DisplayGL.cpp +++ b/src/platform/qt/DisplayGL.cpp @@ -60,6 +60,7 @@ typedef struct _XDisplay Display; #endif #include "OpenGLBug.h" +#include "utils.h" using namespace QGBA; @@ -1003,8 +1004,8 @@ VideoShader* PainterGL::shaders() { QSize PainterGL::contentSize() const { unsigned width, height; VideoBackendGetFrameSize(m_backend, &width, &height); - return {static_cast(width > static_cast(INT_MAX) ? INT_MAX : width), - static_cast(height > static_cast(INT_MAX) ? INT_MAX : height)}; + return {saturateCast(width), + saturateCast(height)}; } int PainterGL::glTex() { diff --git a/src/platform/qt/DisplayQt.cpp b/src/platform/qt/DisplayQt.cpp index 783f56efc..51874cb40 100644 --- a/src/platform/qt/DisplayQt.cpp +++ b/src/platform/qt/DisplayQt.cpp @@ -181,7 +181,7 @@ void DisplayQt::redoBounds() { QSize DisplayQt::contentSize() const { unsigned w, h; VideoBackendGetFrameSize(&m_backend, &w, &h); - return {w, h}; + return {saturateCast(w), saturateCast(h)}; } void DisplayQt::init(struct VideoBackend*, WHandle) { diff --git a/src/platform/qt/scripting/ScriptingTextBuffer.cpp b/src/platform/qt/scripting/ScriptingTextBuffer.cpp index 7a4785d01..fe85eda0e 100644 --- a/src/platform/qt/scripting/ScriptingTextBuffer.cpp +++ b/src/platform/qt/scripting/ScriptingTextBuffer.cpp @@ -6,6 +6,7 @@ #include "ScriptingTextBuffer.h" #include "GBAApp.h" +#include "utils.h" #include #include @@ -205,13 +206,8 @@ void ScriptingTextBuffer::setSize(struct mScriptTextBuffer* buffer, uint32_t col void ScriptingTextBuffer::moveCursor(struct mScriptTextBuffer* buffer, uint32_t x, uint32_t y) { ScriptingTextBuffer* self = static_cast(buffer)->p; - if (x > INT_MAX) { - x = INT_MAX; - } - if (y > INT_MAX) { - y = INT_MAX; - } - QMetaObject::invokeMethod(self, "moveCursor", Q_ARG(QPoint, QPoint(x, y))); + QPoint point(saturateCast(x), saturateCast(y)); + QMetaObject::invokeMethod(self, "moveCursor", Q_ARG(QPoint, point)); } void ScriptingTextBuffer::advance(struct mScriptTextBuffer* buffer, int32_t adv) { diff --git a/src/platform/qt/utils.h b/src/platform/qt/utils.h index 0ce974bc5..6e2f6ec96 100644 --- a/src/platform/qt/utils.h +++ b/src/platform/qt/utils.h @@ -72,6 +72,46 @@ constexpr const T& clamp(const T& v, const T& lo, const T& hi) { } #endif +template +constexpr T saturateCast(U value) { + if (std::numeric_limits::is_signed == std::numeric_limits::is_signed) { + if (value > std::numeric_limits::max()) { + return std::numeric_limits::max(); + } + if (value < std::numeric_limits::min()) { + return std::numeric_limits::min(); + } + } else if (std::numeric_limits::is_signed) { + if (value > static_cast(std::numeric_limits::max())) { + std::numeric_limits::max(); + } + } else { + if (value < 0) { + return 0; + } + if (static_cast(value) > std::numeric_limits::max()) { + std::numeric_limits::max(); + } + } + return static_cast(value); +} + +template<> +constexpr unsigned saturateCast(int value) { + if (value < 0) { + return 0; + } + return static_cast(value); +} + +template<> +constexpr int saturateCast(unsigned value) { + if (value > static_cast(std::numeric_limits::max())) { + return std::numeric_limits::max(); + } + return static_cast(value); +} + QString romFilters(bool includeMvl = false); bool extractMatchingFile(VDir* dir, std::function filter);