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/stack.h"
29 #include "math/vector2d.h"
30 #include "math/vector3d.h"
31 
32 namespace Alcachofa {
33 
34 enum class CursorType {
35  Point,
36  LeaveUp,
37  LeaveRight,
38  LeaveDown,
39  LeaveLeft,
40  WalkTo
41 };
42 
43 enum class Direction {
44  Up,
45  Right,
46  Down,
47  Left,
48 
49  Invalid = -1
50 };
51 
52 enum class MainCharacterKind {
53  None,
54  Mortadelo,
55  Filemon
56 };
57 
58 enum class EasingType {
59  Linear,
60  InOut,
61  In,
62  Out
63 };
64 
65 constexpr const int32 kDirectionCount = 4;
66 constexpr const int8 kOrderCount = 70;
67 constexpr const int8 kForegroundOrderCount = 10;
68 
69 struct Color {
70  uint8 r, g, b, a;
71 };
72 static constexpr const Color kWhite = { 255, 255, 255, 255 };
73 static constexpr const Color kBlack = { 0, 0, 0, 255 };
74 static constexpr const Color kClear = { 0, 0, 0, 0 };
75 static constexpr const Color kDebugRed = { 250, 0, 0, 70 };
76 static constexpr const Color kDebugGreen = { 0, 255, 0, 85 };
77 static constexpr const Color kDebugBlue = { 0, 0, 255, 110 };
78 static constexpr const Color kDebugLightBlue = { 80, 80, 255, 190 };
79 
84 struct FakeSemaphore {
85  FakeSemaphore(const char *name, uint initialCount = 0);
86  ~FakeSemaphore();
87 
88  inline bool isReleased() const { return _counter == 0; }
89  inline uint counter() const { return _counter; }
90 
91  static void sync(Common::Serializer &s, FakeSemaphore &semaphore);
92 private:
93  friend struct FakeLock;
94  const char *const _name;
95  uint _counter = 0;
96 };
97 
98 struct FakeLock {
99  FakeLock();
100  FakeLock(const char *name, FakeSemaphore &semaphore);
101  FakeLock(const FakeLock &other);
102  FakeLock(FakeLock &&other);
103  ~FakeLock();
104  void operator= (FakeLock &&other);
105  FakeLock &operator= (const FakeLock &other);
106  void release();
107 
108  inline bool isReleased() const { return _semaphore == nullptr; }
109 private:
110  void debug(const char *action);
111 
112  const char *_name = "<uninitialized>";
113  FakeSemaphore *_semaphore = nullptr;
114 };
115 
116 float ease(float t, EasingType type);
117 
118 Math::Vector3d as3D(const Math::Vector2d &v);
119 Math::Vector3d as3D(Common::Point p);
120 Math::Vector2d as2D(const Math::Vector3d &v);
121 Math::Vector2d as2D(Common::Point p);
122 
123 bool readBool(Common::ReadStream &stream);
124 Common::Point readPoint(Common::ReadStream &stream);
125 Common::String readVarString(Common::ReadStream &stream);
126 void skipVarString(Common::SeekableReadStream &stream);
127 void syncPoint(Common::Serializer &serializer, Common::Point &point);
128 
129 template<typename T>
130 inline void syncArray(Common::Serializer &serializer, Common::Array<T> &array, void (*serializeFunction)(Common::Serializer &, T &)) {
131  auto size = array.size();
132  serializer.syncAsUint32LE(size);
133  array.resize(size);
134  serializer.syncArray(array.data(), size, serializeFunction);
135 }
136 
137 template<typename T>
138 inline void syncStack(Common::Serializer &serializer, Common::Stack<T> &stack, void (*serializeFunction)(Common::Serializer &, T &)) {
139  auto size = stack.size();
140  serializer.syncAsUint32LE(size);
141  if (serializer.isLoading()) {
142  for (uint i = 0; i < size; i++) {
143  T value;
144  serializeFunction(serializer, value);
145  stack.push(value);
146  }
147  } else {
148  for (uint i = 0; i < size; i++)
149  serializeFunction(serializer, stack[i]);
150  }
151 }
152 
153 template<typename T>
154 inline void syncEnum(Common::Serializer &serializer, T &enumValue) {
155  // syncAs does not have a cast for saving
156  int32 intValue = static_cast<int32>(enumValue);
157  serializer.syncAsSint32LE(intValue);
158  enumValue = static_cast<T>(intValue);
159 }
160 
161 }
162 
163 #endif // ALCACHOFA_COMMON_H
Definition: alcachofa.h:45
Definition: common.h:69
Definition: str.h:59
const T * data() const
Definition: array.h:210
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:318
void resize(size_type newSize)
Definition: array.h:414
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:84
Definition: stack.h:102
Definition: common.h:98