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 #include <mgba-util/socket.h>
10
11 #include <QHostAddress>
12 #include <QRect>
13 #include <QSize>
14 #include <QString>
15
16 #include <algorithm>
17
18 namespace QGBA {
19
20 enum class Endian {
21 NONE = 0b00,
22 BIG = 0b01,
23 LITTLE = 0b10,
24 UNKNOWN = 0b11
25 };
26
27 QString niceSizeFormat(size_t filesize);
28 QString nicePlatformFormat(mPlatform platform);
29
30 bool convertAddress(const QHostAddress* input, Address* output);
31
lockAspectRatio(const QSize & ref,QSize & size)32 inline void lockAspectRatio(const QSize& ref, QSize& size) {
33 if (size.width() * ref.height() > size.height() * ref.width()) {
34 size.setWidth(size.height() * ref.width() / ref.height());
35 } else if (size.width() * ref.height() < size.height() * ref.width()) {
36 size.setHeight(size.width() * ref.height() / ref.width());
37 }
38 }
39
lockIntegerScaling(const QSize & ref,QSize & size)40 inline void lockIntegerScaling(const QSize& ref, QSize& size) {
41 if (size.width() >= ref.width()) {
42 size.setWidth(size.width() - size.width() % ref.width());
43 }
44 if (size.height() >= ref.height()) {
45 size.setHeight(size.height() - size.height() % ref.height());
46 }
47 }
48
clampSize(const QSize & ref,const QSize & size,bool aspectRatio,bool integerScaling)49 inline QRect clampSize(const QSize& ref, const QSize& size, bool aspectRatio, bool integerScaling) {
50 QSize ds = size;
51 if (aspectRatio) {
52 lockAspectRatio(ref, ds);
53 }
54 if (integerScaling) {
55 QGBA::lockIntegerScaling(ref, ds);
56 }
57 QPoint origin = QPoint((size.width() - ds.width()) / 2, (size.height() - ds.height()) / 2);
58 return QRect(origin, ds);
59 }
60
61 #if __cplusplus >= 201703L
62 using std::clamp;
63 #else
64 template<class T>
clamp(const T & v,const T & lo,const T & hi)65 constexpr const T& clamp(const T& v, const T& lo, const T& hi) {
66 return std::max(lo, std::min(hi, v));
67 }
68 #endif
69
70 }
71