all repos — mgba @ 1089285b45c1213fff87e0518fc45a327b8ede8b

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
18QString niceSizeFormat(size_t filesize);
19QString nicePlatformFormat(mPlatform platform);
20
21inline void lockAspectRatio(const QSize& ref, QSize& size) {
22	if (size.width() * ref.height() > size.height() * ref.width()) {
23		size.setWidth(size.height() * ref.width() / ref.height());
24	} else if (size.width() * ref.height() < size.height() * ref.width()) {
25		size.setHeight(size.width() * ref.height() / ref.width());
26	}
27}
28
29inline void lockIntegerScaling(const QSize& ref, QSize& size) {
30	if (size.width() >= ref.width()) {
31		size.setWidth(size.width() - size.width() % ref.width());
32	}
33	if (size.height() >= ref.height()) {
34		size.setHeight(size.height() - size.height() % ref.height());
35	}
36}
37
38inline QRect clampSize(const QSize& ref, const QSize& size, bool aspectRatio, bool integerScaling) {
39	QSize ds = size;
40	if (aspectRatio) {
41		lockAspectRatio(ref, ds);
42	}
43	if (integerScaling) {
44		QGBA::lockIntegerScaling(ref, ds);
45	}
46	QPoint origin = QPoint((size.width() - ds.width()) / 2, (size.height() - ds.height()) / 2);
47	return QRect(origin, ds);
48}
49
50#if __cplusplus >= 201703L
51using std::clamp;
52#else
53template<class T>
54constexpr const T& clamp(const T& v, const T& lo, const T& hi) {
55	return std::max(lo, std::min(hi, v));
56}
57#endif
58
59}