all repos — mgba @ 217d1b238b5d7c194a20d75cf3e48bc98d1411dd

mGBA Game Boy Advance Emulator

src/platform/qt/utils.h (view raw)

 1/* Copyright (c) 2013-2017 Jeffrey Pfau
 2 *
 3 * This Source Code Form is subject to the terms of the Mozilla Public
 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 6#pragma once
 7
 8#include <mgba/core/core.h>
 9
10#include <QRect>
11#include <QSize>
12#include <QString>
13
14#include <algorithm>
15
16namespace QGBA {
17
18enum class Endian {
19	NONE    = 0b00,
20	BIG     = 0b01,
21	LITTLE  = 0b10,
22	UNKNOWN = 0b11
23};
24
25QString niceSizeFormat(size_t filesize);
26QString nicePlatformFormat(mPlatform platform);
27
28inline void lockAspectRatio(const QSize& ref, QSize& size) {
29	if (size.width() * ref.height() > size.height() * ref.width()) {
30		size.setWidth(size.height() * ref.width() / ref.height());
31	} else if (size.width() * ref.height() < size.height() * ref.width()) {
32		size.setHeight(size.width() * ref.height() / ref.width());
33	}
34}
35
36inline void lockIntegerScaling(const QSize& ref, QSize& size) {
37	if (size.width() >= ref.width()) {
38		size.setWidth(size.width() - size.width() % ref.width());
39	}
40	if (size.height() >= ref.height()) {
41		size.setHeight(size.height() - size.height() % ref.height());
42	}
43}
44
45inline QRect clampSize(const QSize& ref, const QSize& size, bool aspectRatio, bool integerScaling) {
46	QSize ds = size;
47	if (aspectRatio) {
48		lockAspectRatio(ref, ds);
49	}
50	if (integerScaling) {
51		QGBA::lockIntegerScaling(ref, ds);
52	}
53	QPoint origin = QPoint((size.width() - ds.width()) / 2, (size.height() - ds.height()) / 2);
54	return QRect(origin, ds);
55}
56
57#if __cplusplus >= 201703L
58using std::clamp;
59#else
60template<class T>
61constexpr const T& clamp(const T& v, const T& lo, const T& hi) {
62	return std::max(lo, std::min(hi, v));
63}
64#endif
65
66}