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  * Additional copyright for this file:
8  * Copyright (C) 1995-1997 Presto Studios, Inc.
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program. If not, see <http://www.gnu.org/licenses/>.
22  *
23  */
24 
25 #ifndef PEGASUS_UTIL_H
26 #define PEGASUS_UTIL_H
27 
28 #include "common/stream.h"
29 
30 #include "pegasus/types.h"
31 
32 namespace Common {
33  class RandomSource;
34 }
35 
36 namespace Pegasus {
37 
38 class IDObject {
39 public:
40  IDObject(const int32 id);
41  ~IDObject();
42 
43  int32 getObjectID() const;
44 
45 private:
46  int32 _objectID;
47 };
48 
49 #define NUM_FLAGS (sizeof(Unit) * 8)
50 #define BIT_INDEX_SHIFT (sizeof(Unit) + 2 - (sizeof(Unit)) / 3)
51 #define BIT_INDEX_MASK (NUM_FLAGS - 1)
52 
53 template <typename Unit, uint32 kNumFlags>
54 class FlagsArray {
55 public:
56  FlagsArray() { clearAllFlags(); }
57  void clearAllFlags() { memset(_flags, 0, sizeof(_flags)); }
58  void setAllFlags() { memset(_flags, ~((Unit)0), sizeof(_flags)); }
59  void setFlag(uint32 flag) { _flags[flag >> BIT_INDEX_SHIFT] |= 1 << (flag & BIT_INDEX_MASK); }
60  void clearFlag(uint32 flag) { _flags[flag >> BIT_INDEX_SHIFT] &= ~(1 << (flag & BIT_INDEX_MASK)); }
61  void setFlag(uint32 flag, bool val) { if (val) setFlag(flag); else clearFlag(flag); }
62  bool getFlag(uint32 flag) { return (_flags[flag >> BIT_INDEX_SHIFT] & (1 << (flag & BIT_INDEX_MASK))) != 0; }
63  bool anyFlagSet() {
64  for (uint32 i = 0; i < sizeof(_flags); i++)
65  if (_flags[i] != 0)
66  return true;
67  return false;
68  }
69 
70  void readFromStream(Common::ReadStream *stream) {
71  // Shortcut
72  if (sizeof(Unit) == 1) {
73  stream->read(_flags, sizeof(_flags));
74  return;
75  }
76 
77  for (uint32 i = 0; i < ARRAYSIZE(_flags); i++) {
78  if (sizeof(Unit) == 2)
79  _flags[i] = stream->readUint16BE();
80  else /* if (sizeof(Unit) == 4) */
81  _flags[i] = stream->readUint32BE();
82  }
83  }
84 
85  void writeToStream(Common::WriteStream *stream) {
86  // Shortcut
87  if (sizeof(Unit) == 1) {
88  stream->write(_flags, sizeof(_flags));
89  return;
90  }
91 
92  for (uint32 i = 0; i < ARRAYSIZE(_flags); i++) {
93  if (sizeof(Unit) == 2)
94  stream->writeUint16BE(_flags[i]);
95  else /* if (sizeof(Unit) == 4) */
96  stream->writeUint32BE(_flags[i]);
97  }
98  }
99 
100 private:
101  Unit _flags[(kNumFlags - 1) / NUM_FLAGS + 1];
102 };
103 
104 #undef NUM_FLAGS
105 #undef BIT_INDEX_SHIFT
106 #undef BIT_INDEX_MASK
107 
108 int32 linearInterp(const int32 start1, const int32 stop1, const int32 current1, const int32 start2, const int32 stop2);
109 
110 int32 pegasusRound(const int32 a, const int32 b);
111 
112 uint32 tickCount();
113 
114 } // End of namespace Pegasus
115 
116 #endif
#define ARRAYSIZE(x)
Definition: util.h:91
Definition: stream.h:77
void writeUint16BE(uint16 value)
Definition: stream.h:173
void writeUint32BE(uint32 value)
Definition: stream.h:180
Definition: util.h:54
Definition: util.h:38
Definition: algorithm.h:29
uint32 readUint32BE()
Definition: stream.h:515
uint16 readUint16BE()
Definition: stream.h:501
Definition: stream.h:385
virtual uint32 write(const void *dataPtr, uint32 dataSize)=0
Definition: ai_action.h:33
virtual uint32 read(void *dataPtr, uint32 dataSize)=0