ScummVM API documentation
common.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 ALCACHOFA_COMMON_H
23 #define ALCACHOFA_COMMON_H
24 
25 #include "common/rect.h"
26 #include "common/serializer.h"
27 #include "common/stream.h"
28 #include "common/str-enc.h"
29 #include "common/stack.h"
30 #include "math/vector2d.h"
31 #include "math/vector3d.h"
32 
33 namespace Alcachofa {
34 
35 enum class CursorType {
36  Point,
37  LeaveUp,
38  LeaveRight,
39  LeaveDown,
40  LeaveLeft,
41  WalkTo
42 };
43 
44 enum class Direction {
45  Up,
46  Right,
47  Down,
48  Left,
49 
50  // V1 has some fields with these additional directions
51  // but they are largely irrelevant so we map them to the main four
52  UpRight,
53  DownRight,
54  DownLeft,
55  UpLeft,
56 
57  Invalid = -1
58 };
59 
60 enum class MainCharacterKind {
61  None,
62  Mortadelo,
63  Filemon
64 };
65 
66 enum class EasingType {
67  Linear,
68  InOut,
69  In,
70  Out
71 };
72 
73 constexpr const int32 kDirectionCount = 4;
74 constexpr const int32 kFullDirectionCount = 8;
75 constexpr const int8 kOrderCount = 70;
76 constexpr const int8 kForegroundOrderCount = 10;
77 
78 struct Color {
79  uint8 r, g, b, a;
80 };
81 static constexpr const Color kWhite = { 255, 255, 255, 255 };
82 static constexpr const Color kBlack = { 0, 0, 0, 255 };
83 static constexpr const Color kClear = { 0, 0, 0, 0 };
84 static constexpr const Color kDebugRed = { 250, 0, 0, 70 };
85 static constexpr const Color kDebugGreen = { 0, 255, 0, 85 };
86 static constexpr const Color kDebugBlue = { 0, 0, 255, 110 };
87 static constexpr const Color kDebugLightBlue = { 80, 80, 255, 190 };
88 
93 struct FakeSemaphore {
94  FakeSemaphore(const char *name, uint initialCount = 0);
95  ~FakeSemaphore();
96 
97  inline bool isReleased() const { return _counter == 0; }
98  inline uint counter() const { return _counter; }
99 
100  static void sync(Common::Serializer &s, FakeSemaphore &semaphore);
101 private:
102  friend struct FakeLock;
103  const char *const _name;
104  uint _counter = 0;
105 };
106 
107 struct FakeLock {
108  FakeLock();
109  FakeLock(const char *name, FakeSemaphore &semaphore);
110  FakeLock(const FakeLock &other);
111  FakeLock(FakeLock &&other);
112  ~FakeLock();
113  void operator= (FakeLock &&other);
114  FakeLock &operator= (const FakeLock &other);
115  void release();
116 
117  inline bool isReleased() const { return _semaphore == nullptr; }
118 private:
119  void debug(const char *action);
120 
121  const char *_name = "<uninitialized>";
122  FakeSemaphore *_semaphore = nullptr;
123 };
124 
125 bool isPowerOfTwo(int16 x);
126 
127 float ease(float t, EasingType type);
128 
129 Common::String reencode(
130  const Common::String &string,
131  Common::CodePage from = Common::CodePage::kISO8859_1, // "Western European", used for the spanish special characters
132  Common::CodePage to = Common::CodePage::kUtf8);
133 
134 Math::Vector3d as3D(const Math::Vector2d &v);
135 Math::Vector3d as3D(Common::Point p);
136 Math::Vector2d as2D(const Math::Vector3d &v);
137 Math::Vector2d as2D(Common::Point p);
138 
139 bool readBool(Common::ReadStream &stream);
140 Common::Point readPoint16(Common::ReadStream &stream);
141 Common::Point readPoint32(Common::ReadStream &stream);
142 Common::String readVarString(Common::ReadStream &stream);
143 void skipVarString(Common::SeekableReadStream &stream);
144 void syncPoint(Common::Serializer &serializer, Common::Point &point);
145 Direction intToDirection(int32 value);
146 Direction readDirection(Common::ReadStream &stream);
147 
148 template<typename T>
149 inline void syncArray(Common::Serializer &serializer, Common::Array<T> &array, void (*serializeFunction)(Common::Serializer &, T &)) {
150  auto size = array.size();
151  serializer.syncAsUint32LE(size);
152  array.resize(size);
153  serializer.syncArray(array.data(), size, serializeFunction);
154 }
155 
156 template<typename T>
157 inline void syncStack(
158  Common::Serializer &serializer,
159  Common::Stack<T> &stack,
160  void (*serializeFunction)(Common::Serializer &, T &),
161  Common::Serializer::Version minVersion = 0) {
162  if (serializer.getVersion() < minVersion)
163  return;
164 
165  auto size = stack.size();
166  serializer.syncAsUint32LE(size);
167  if (serializer.isLoading()) {
168  for (uint i = 0; i < size; i++) {
169  T value = {};
170  serializeFunction(serializer, value);
171  stack.push(value);
172  }
173  } else {
174  for (uint i = 0; i < size; i++)
175  serializeFunction(serializer, stack[i]);
176  }
177 }
178 
179 template<typename T>
180 inline void syncEnum(Common::Serializer &serializer, T &enumValue) {
181  // syncAs does not have a cast for saving
182  int32 intValue = static_cast<int32>(enumValue);
183  serializer.syncAsSint32LE(intValue);
184  enumValue = static_cast<T>(intValue);
185 }
186 
198  Common::String _path;
199  uint32
200  _fileIndex = UINT32_MAX,
201  _position = UINT32_MAX,
202  _size = 0;
203 
204  GameFileReference() {}
205 
206  explicit GameFileReference(const Common::String &path)
207  : _path(path) {}
208 
209  // in this case, path is only for debugging purposes
210  GameFileReference(const Common::String &path, uint32 fileIndex, int64 position, uint32 size)
211  : _path(path)
212  , _fileIndex(fileIndex)
213  , _position(position)
214  , _size(size) {}
215 
216  inline bool isValid() const {
217  return !_path.empty() || _fileIndex != UINT32_MAX;
218  }
219 
220  inline bool isEmbedded() const {
221  return _fileIndex != UINT32_MAX;
222  }
223 };
224 
225 }
226 
227 #endif // ALCACHOFA_COMMON_H
Definition: alcachofa.h:45
Definition: common.h:78
Definition: str.h:59
const T * data() const
Definition: array.h:208
Definition: array.h:52
Definition: display_client.h:58
Definition: stream.h:745
Definition: serializer.h:79
void debug(MSVC_PRINTF const char *s,...) GCC_PRINTF(1
References a game file either as path or as embedded byte range.
Definition: common.h:197
Definition: rect.h:144
size_type size() const
Definition: array.h:316
Version getVersion() const
Definition: serializer.h:172
void resize(size_type newSize)
Definition: array.h:412
Definition: stream.h:385
This fake semaphore does not work in multi-threaded scenarios It is used as a safer option for a simp...
Definition: common.h:93
Definition: stack.h:102
Definition: common.h:107