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  Invalid = -1
51 };
52 
53 enum class MainCharacterKind {
54  None,
55  Mortadelo,
56  Filemon
57 };
58 
59 enum class EasingType {
60  Linear,
61  InOut,
62  In,
63  Out
64 };
65 
66 constexpr const int32 kDirectionCount = 4;
67 constexpr const int8 kOrderCount = 70;
68 constexpr const int8 kForegroundOrderCount = 10;
69 
70 struct Color {
71  uint8 r, g, b, a;
72 };
73 static constexpr const Color kWhite = { 255, 255, 255, 255 };
74 static constexpr const Color kBlack = { 0, 0, 0, 255 };
75 static constexpr const Color kClear = { 0, 0, 0, 0 };
76 static constexpr const Color kDebugRed = { 250, 0, 0, 70 };
77 static constexpr const Color kDebugGreen = { 0, 255, 0, 85 };
78 static constexpr const Color kDebugBlue = { 0, 0, 255, 110 };
79 static constexpr const Color kDebugLightBlue = { 80, 80, 255, 190 };
80 
85 struct FakeSemaphore {
86  FakeSemaphore(const char *name, uint initialCount = 0);
87  ~FakeSemaphore();
88 
89  inline bool isReleased() const { return _counter == 0; }
90  inline uint counter() const { return _counter; }
91 
92  static void sync(Common::Serializer &s, FakeSemaphore &semaphore);
93 private:
94  friend struct FakeLock;
95  const char *const _name;
96  uint _counter = 0;
97 };
98 
99 struct FakeLock {
100  FakeLock();
101  FakeLock(const char *name, FakeSemaphore &semaphore);
102  FakeLock(const FakeLock &other);
103  FakeLock(FakeLock &&other);
104  ~FakeLock();
105  void operator= (FakeLock &&other);
106  FakeLock &operator= (const FakeLock &other);
107  void release();
108 
109  inline bool isReleased() const { return _semaphore == nullptr; }
110 private:
111  void debug(const char *action);
112 
113  const char *_name = "<uninitialized>";
114  FakeSemaphore *_semaphore = nullptr;
115 };
116 
117 bool isPowerOfTwo(int16 x);
118 
119 float ease(float t, EasingType type);
120 
121 Common::String reencode(
122  const Common::String &string,
123  Common::CodePage from = Common::CodePage::kISO8859_1, // "Western European", used for the spanish special characters
124  Common::CodePage to = Common::CodePage::kUtf8);
125 
126 Math::Vector3d as3D(const Math::Vector2d &v);
127 Math::Vector3d as3D(Common::Point p);
128 Math::Vector2d as2D(const Math::Vector3d &v);
129 Math::Vector2d as2D(Common::Point p);
130 
131 bool readBool(Common::ReadStream &stream);
132 Common::Point readPoint(Common::ReadStream &stream);
133 Common::String readVarString(Common::ReadStream &stream);
134 void skipVarString(Common::SeekableReadStream &stream);
135 void syncPoint(Common::Serializer &serializer, Common::Point &point);
136 
137 template<typename T>
138 inline void syncArray(Common::Serializer &serializer, Common::Array<T> &array, void (*serializeFunction)(Common::Serializer &, T &)) {
139  auto size = array.size();
140  serializer.syncAsUint32LE(size);
141  array.resize(size);
142  serializer.syncArray(array.data(), size, serializeFunction);
143 }
144 
145 template<typename T>
146 inline void syncStack(Common::Serializer &serializer, Common::Stack<T> &stack, void (*serializeFunction)(Common::Serializer &, T &)) {
147  auto size = stack.size();
148  serializer.syncAsUint32LE(size);
149  if (serializer.isLoading()) {
150  for (uint i = 0; i < size; i++) {
151  T value;
152  serializeFunction(serializer, value);
153  stack.push(value);
154  }
155  } else {
156  for (uint i = 0; i < size; i++)
157  serializeFunction(serializer, stack[i]);
158  }
159 }
160 
161 template<typename T>
162 inline void syncEnum(Common::Serializer &serializer, T &enumValue) {
163  // syncAs does not have a cast for saving
164  int32 intValue = static_cast<int32>(enumValue);
165  serializer.syncAsSint32LE(intValue);
166  enumValue = static_cast<T>(intValue);
167 }
168 
169 }
170 
171 #endif // ALCACHOFA_COMMON_H
Definition: alcachofa.h:45
Definition: common.h:70
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
Definition: rect.h:144
size_type size() const
Definition: array.h:316
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:85
Definition: stack.h:102
Definition: common.h:99