all repos — mgba @ b6dbbb14abc159b9378353df2d978527f3414389

mGBA Game Boy Advance Emulator

Qt: Add clamp function, using stl if available
Vicki Pfau vi@endrift.com
Sat, 16 Jan 2021 00:47:41 -0800
commit

b6dbbb14abc159b9378353df2d978527f3414389

parent

8947b80a1cefc78458f447b6583c17d084998f8c

M src/platform/qt/InputController.cppsrc/platform/qt/InputController.cpp

@@ -10,6 +10,7 @@ #include "GamepadAxisEvent.h"

#include "GamepadButtonEvent.h" #include "InputProfile.h" #include "LogController.h" +#include "utils.h" #include <QApplication> #include <QTimer>

@@ -733,7 +734,7 @@ }

void InputController::setLuminanceLevel(int level) { int value = 0x16; - level = std::max(0, std::min(10, level)); + level = clamp(level, 0, 10); if (level > 0) { value += GBA_LUX_LEVELS[level - 1]; }
M src/platform/qt/MemoryModel.cppsrc/platform/qt/MemoryModel.cpp

@@ -9,6 +9,7 @@ #include "GBAApp.h"

#include "CoreController.h" #include "LogController.h" #include "VFileDevice.h" +#include "utils.h" #include <QAction> #include <QApplication>

@@ -628,11 +629,7 @@ viewport()->update();

} void MemoryModel::boundsCheck() { - if (m_top < 0) { - m_top = 0; - } else if (m_top > (m_size >> 4) + 1 - viewport()->size().height() / m_cellHeight) { - m_top = (m_size >> 4) + 1 - viewport()->size().height() / m_cellHeight; - } + m_top = clamp(m_top, 0, static_cast<int32_t>(m_size >> 4) + 1 - viewport()->size().height() / m_cellHeight); } bool MemoryModel::isInSelection(uint32_t address) {
M src/platform/qt/SensorView.cppsrc/platform/qt/SensorView.cpp

@@ -8,6 +8,7 @@

#include "CoreController.h" #include "GamepadAxisEvent.h" #include "InputController.h" +#include "utils.h" #include <mgba/core/core.h> #include <mgba/internal/gba/gba.h>

@@ -129,7 +130,7 @@ }

} void SensorView::setLuminanceValue(int value) { - value = std::max(0, std::min(value, 255)); + value = clamp(value, 0, 255); if (m_input) { m_input->setLuminanceValue(value); }
M src/platform/qt/utils.hsrc/platform/qt/utils.h

@@ -11,6 +11,8 @@ #include <QRect>

#include <QSize> #include <QString> +#include <algorithm> + namespace QGBA { QString niceSizeFormat(size_t filesize);

@@ -44,5 +46,14 @@ }

QPoint origin = QPoint((size.width() - ds.width()) / 2, (size.height() - ds.height()) / 2); return QRect(origin, ds); } + +#if __cplusplus >= 201703L +using std::clamp; +#else +template<class T> +constexpr const T& clamp(const T& v, const T& lo, const T& hi) { + return std::max(lo, std::min(hi, v)); +} +#endif }