ScummVM API documentation
util.h
1/* ScummVM - Graphic Adventure Engine
2 *
3 * ScummVM is the legal property of its developers, whose names
4 * are too numerous to list here. Please refer to the COPYRIGHT
5 * file distributed with this source distribution.
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21
22#ifndef TWP_UTIL_H
23#define TWP_UTIL_H
24
25#include "common/formats/json.h"
26#include "common/hashmap.h"
27#include "common/rect.h"
28#include "math/vector2d.h"
29#include "math/matrix4.h"
30#include "twp/ids.h"
31#include "twp/objectanimation.h"
32
33namespace Twp {
34enum class Facing;
35}
36
37namespace Common {
38template<>
39struct Hash<Twp::Facing> : public UnaryFunction<Twp::Facing, uint> {
40 uint operator()(Twp::Facing val) const { return (uint)val; }
41};
42} // namespace Common
43
44namespace Twp {
45
46class Object;
47
48struct Vector2i {
49 int x = 0;
50 int y = 0;
51
52 Vector2i() {}
53 Vector2i(int x_, int y_) : x(x_), y(y_) {}
54 Vector2i(float x_, float y_) : x(round(x_)), y(round(y_)) {}
55 explicit Vector2i(const Math::Vector2d &p) : x(round(p.getX())), y(round(p.getY())) {}
56 explicit operator Math::Vector2d() const {
57 return Math::Vector2d(x, y);
58 }
59
60 Vector2i operator-(const Vector2i &v) const {
61 return Vector2i(x - v.x, y - v.y);
62 }
63
64 Vector2i operator+(const Vector2i &v) const {
65 return Vector2i(x + v.x, y + v.y);
66 }
67
68 Vector2i operator*(float f) const {
69 return Vector2i(x * f, y * f);
70 }
71
72 Vector2i operator/(float f) const {
73 return Vector2i(x / f, y / f);
74 }
75};
76
77// general util
78template<typename T, class DL = Common::DefaultDeleter<T> >
80
81// game util
82Facing getFacing(int dir, Facing facing);
83Facing flip(Facing facing);
84Facing getFacingToFaceTo(Common::SharedPtr<Object> actor, Common::SharedPtr<Object> obj);
85
86// parsing util
87bool toBool(const Common::JSONObject &jNode, const Common::String &key);
88Math::Vector2d parseVec2(const Common::String &s);
89Common::Rect parseRect(const Common::String &s);
90void parseObjectAnimations(const Common::JSONArray &jAnims, Common::Array<ObjectAnimation> &anims);
91
92// array util
93template<typename T>
94size_t find(const Common::Array<T> &array, const T &o) {
95 for (size_t i = 0; i < array.size(); i++) {
96 if (array[i] == o) {
97 return i;
98 }
99 }
100 return (size_t)-1;
101}
102
103template<typename T>
104size_t find(const Common::Array<Common::SharedPtr<T> > &array, const T *o) {
105 for (size_t i = 0; i < array.size(); i++) {
106 if (array[i].get() == o) {
107 return i;
108 }
109 }
110 return (size_t)-1;
111}
112
113template<typename T>
114size_t minIndex(const Common::Array<T> &values) {
115 if (values.empty())
116 return (size_t)-1;
117 T min = values[0];
118 size_t index = 0;
119 for (size_t i = 1; i < values.size(); i++) {
120 if (values[i] < min) {
121 index = i;
122 min = values[i];
123 }
124 }
125 return index;
126}
127
128template<typename T>
129Common::Array<T> reverse(const Common::Array<T> &arr) {
130 Common::Array<T> result(arr.size());
131 for (size_t i = 0; i < arr.size(); i++) {
132 result[arr.size() - 1 - i] = arr[i];
133 }
134 return result;
135}
136
137// string util
138Common::String join(const Common::Array<Common::String> &array, const Common::String &sep);
139Common::String remove(const Common::String &txt, char startC, char endC);
140Common::String replaceAll(const Common::String &s, const Common::String &what, const Common::String &by);
141
142// math util
143void scale(Math::Matrix4 &m, const Math::Vector2d &v);
144Math::Vector2d operator*(const Math::Vector2d &v, float f);
145float distance(const Math::Vector2d &p1, const Math::Vector2d &p2);
146float distanceSquared(const Math::Vector2d &p1, const Math::Vector2d &p2);
147float distanceToSegment(const Math::Vector2d &p, const Math::Vector2d &v, const Math::Vector2d &w);
148float dot(const Math::Vector2d &u, const Math::Vector2d &v);
149float length(const Math::Vector2d &v);
150bool lineSegmentsCross(const Math::Vector2d &a, const Math::Vector2d &b, const Math::Vector2d &c, const Math::Vector2d &d);
151
152} // namespace Twp
153
154#endif
Definition: array.h:52
size_type size() const
Definition: array.h:316
bool empty() const
Definition: array.h:352
Definition: hashmap.h:85
Definition: ptr.h:572
Definition: str.h:59
It remove(It first, It last, const T &val)
Definition: algorithm.h:466
In find(In first, In last, const T &v)
Definition: algorithm.h:225
Definition: algorithm.h:29
Definition: func.h:527
Definition: rect.h:536
Definition: func.h:43
Definition: util.h:48