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
33namespace Alcachofa {
34
35enum class CursorType {
36 Point,
37 LeaveUp,
38 LeaveRight,
39 LeaveDown,
40 LeaveLeft,
41 WalkTo
42};
43
44enum 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
60enum class MainCharacterKind {
61 None,
62 Mortadelo,
63 Filemon
64};
65
66enum class EasingType {
67 Linear,
68 InOut,
69 In,
70 Out
71};
72
73constexpr const int32 kDirectionCount = 4;
74constexpr const int32 kFullDirectionCount = 8;
75constexpr const int8 kOrderCount = 70;
76constexpr const int8 kForegroundOrderCount = 10;
77
78struct Color {
79 uint8 r, g, b, a;
80};
81static constexpr const Color kWhite = { 255, 255, 255, 255 };
82static constexpr const Color kBlack = { 0, 0, 0, 255 };
83static constexpr const Color kClear = { 0, 0, 0, 0 };
84static constexpr const Color kDebugRed = { 250, 0, 0, 70 };
85static constexpr const Color kDebugGreen = { 0, 255, 0, 85 };
86static constexpr const Color kDebugBlue = { 0, 0, 255, 110 };
87static constexpr const Color kDebugLightBlue = { 80, 80, 255, 190 };
88
94 FakeSemaphore(const char *name, uint initialCount = 0);
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);
101private:
102 friend struct FakeLock;
103 const char *const _name;
104 uint _counter = 0;
105};
106
107struct 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; }
118private:
119 void debug(const char *action);
120
121 const char *_name = "<uninitialized>";
122 FakeSemaphore *_semaphore = nullptr;
123};
124
125bool isPowerOfTwo(int16 x);
126
127float ease(float t, EasingType type);
128
129Common::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
134Math::Vector3d as3D(const Math::Vector2d &v);
135Math::Vector3d as3D(Common::Point p);
136Math::Vector2d as2D(const Math::Vector3d &v);
137Math::Vector2d as2D(Common::Point p);
138
139bool readBool(Common::ReadStream &stream);
140Common::Point readPoint16(Common::ReadStream &stream);
141Common::Point readPoint32(Common::ReadStream &stream);
142Common::String readVarString(Common::ReadStream &stream);
143void skipVarString(Common::SeekableReadStream &stream);
144void syncPoint(Common::Serializer &serializer, Common::Point &point);
145Direction intToDirection(int32 value);
146Direction readDirection(Common::ReadStream &stream);
147
148template<typename T>
149inline 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
156template<typename T>
157inline 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
179template<typename T>
180inline 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
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: array.h:52
const T * data() const
Definition: array.h:208
size_type size() const
Definition: array.h:316
void resize(size_type newSize)
Definition: array.h:412
Definition: stream.h:385
Definition: stream.h:745
Definition: serializer.h:80
Version getVersion() const
Definition: serializer.h:173
Definition: stack.h:102
Definition: str.h:59
Definition: common.h:78
Definition: common.h:107
This fake semaphore does not work in multi-threaded scenarios It is used as a safer option for a simp...
Definition: common.h:93
References a game file either as path or as embedded byte range.
Definition: common.h:197
Definition: rect.h:144
Definition: display_client.h:58