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 
33 namespace Twp {
34 enum class Facing;
35 }
36 
37 namespace Common {
38 template<>
39 struct Hash<Twp::Facing> : public UnaryFunction<Twp::Facing, uint> {
40  uint operator()(Twp::Facing val) const { return (uint)val; }
41 };
42 } // namespace Common
43 
44 namespace Twp {
45 
46 class Object;
47 
48 struct 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
78 template<typename T, class DL = Common::DefaultDeleter<T> >
80 
81 // game util
82 Facing getFacing(int dir, Facing facing);
83 Facing flip(Facing facing);
84 Facing getFacingToFaceTo(Common::SharedPtr<Object> actor, Common::SharedPtr<Object> obj);
85 
86 // parsing util
87 bool toBool(const Common::JSONObject &jNode, const Common::String &key);
88 Math::Vector2d parseVec2(const Common::String &s);
89 Common::Rect parseRect(const Common::String &s);
90 void parseObjectAnimations(const Common::JSONArray &jAnims, Common::Array<ObjectAnimation> &anims);
91 
92 // array util
93 template<typename T>
94 size_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 
103 template<typename T>
104 size_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 
113 template<typename T>
114 size_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 
128 template<typename T>
129 Common::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
138 Common::String join(const Common::Array<Common::String> &array, const Common::String &sep);
139 Common::String remove(const Common::String &txt, char startC, char endC);
140 Common::String replaceAll(const Common::String &s, const Common::String &what, const Common::String &by);
141 
142 // math util
143 void scale(Math::Matrix4 &m, const Math::Vector2d &v);
144 Math::Vector2d operator*(const Math::Vector2d &v, float f);
145 float distance(const Math::Vector2d &p1, const Math::Vector2d &p2);
146 float distanceSquared(const Math::Vector2d &p1, const Math::Vector2d &p2);
147 float distanceToSegment(const Math::Vector2d &p, const Math::Vector2d &v, const Math::Vector2d &w);
148 float dot(const Math::Vector2d &u, const Math::Vector2d &v);
149 float length(const Math::Vector2d &v);
150 bool 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: str.h:59
Definition: util.h:48
Definition: array.h:52
In find(In first, In last, const T &v)
Definition: algorithm.h:225
Definition: rect.h:144
Definition: func.h:527
Definition: ptr.h:572
Graphics::Surface * scale(const Graphics::Surface &srcImage, int xSize, int ySize)
bool empty() const
Definition: array.h:351
Definition: hashmap.h:85
Definition: algorithm.h:29
size_type size() const
Definition: array.h:315
Definition: ptr.h:159
Definition: func.h:43
Definition: achievements_tables.h:27