Qt: Add a saturateCast template

This commit is contained in:
Vicki Pfau 2023-05-01 22:22:51 -07:00
parent 201f0df4c2
commit ac9ffdd765
4 changed files with 47 additions and 10 deletions

View File

@ -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<int>(width > static_cast<unsigned>(INT_MAX) ? INT_MAX : width),
static_cast<int>(height > static_cast<unsigned>(INT_MAX) ? INT_MAX : height)};
return {saturateCast<int>(width),
saturateCast<int>(height)};
}
int PainterGL::glTex() {

View File

@ -181,7 +181,7 @@ void DisplayQt::redoBounds() {
QSize DisplayQt::contentSize() const {
unsigned w, h;
VideoBackendGetFrameSize(&m_backend, &w, &h);
return {w, h};
return {saturateCast<int>(w), saturateCast<int>(h)};
}
void DisplayQt::init(struct VideoBackend*, WHandle) {

View File

@ -6,6 +6,7 @@
#include "ScriptingTextBuffer.h"
#include "GBAApp.h"
#include "utils.h"
#include <QMutexLocker>
#include <QPlainTextDocumentLayout>
@ -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<ScriptingTextBuffer::ScriptingBufferShim*>(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<int>(x), saturateCast<int>(y));
QMetaObject::invokeMethod(self, "moveCursor", Q_ARG(QPoint, point));
}
void ScriptingTextBuffer::advance(struct mScriptTextBuffer* buffer, int32_t adv) {

View File

@ -72,6 +72,46 @@ constexpr const T& clamp(const T& v, const T& lo, const T& hi) {
}
#endif
template<typename T, typename U>
constexpr T saturateCast(U value) {
if (std::numeric_limits<T>::is_signed == std::numeric_limits<T>::is_signed) {
if (value > std::numeric_limits<T>::max()) {
return std::numeric_limits<T>::max();
}
if (value < std::numeric_limits<T>::min()) {
return std::numeric_limits<T>::min();
}
} else if (std::numeric_limits<T>::is_signed) {
if (value > static_cast<uintmax_t>(std::numeric_limits<T>::max())) {
std::numeric_limits<T>::max();
}
} else {
if (value < 0) {
return 0;
}
if (static_cast<uintmax_t>(value) > std::numeric_limits<T>::max()) {
std::numeric_limits<T>::max();
}
}
return static_cast<T>(value);
}
template<>
constexpr unsigned saturateCast<unsigned, int>(int value) {
if (value < 0) {
return 0;
}
return static_cast<unsigned>(value);
}
template<>
constexpr int saturateCast<int, unsigned>(unsigned value) {
if (value > static_cast<unsigned>(std::numeric_limits<int>::max())) {
return std::numeric_limits<int>::max();
}
return static_cast<int>(value);
}
QString romFilters(bool includeMvl = false);
bool extractMatchingFile(VDir* dir, std::function<QString (VDirEntry*)> filter);