ScummVM API documentation
zbasic.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 FOOL_ZBASIC_H
23 #define FOOL_ZBASIC_H
24 
25 #include "common/memorypool.h"
26 #include "common/memstream.h"
27 #include "common/ptr.h"
28 #include "common/str.h"
29 #include "common/stream.h"
30 #include "common/ustr.h"
31 #include "graphics/macgui/macwindow.h"
32 #include "graphics/mactoolbox/toolbox.h"
33 
34 namespace Fool {
35 
36 union DoubleBits {
37  uint64 i;
38  double d;
39 };
40 
41 struct ZBasicBCD {
42  uint16 exponent = 0;
43  uint64 mantissaBits = 0;
44  uint8 mantissaSize = 0;
45 
46  ZBasicBCD() {}
47 
48  ZBasicBCD(uint16 e, uint64 mb, uint8 ms) {
49  this->exponent = e;
50  this->mantissaBits = mb;
51  this->mantissaSize = ms;
52  }
53 
54  ZBasicBCD(const ZBasicBCD &obj) {
55  this->exponent = obj.exponent;
56  this->mantissaBits = obj.mantissaBits;
57  this->mantissaSize = obj.mantissaSize;
58  }
59 
60  static ZBasicBCD read(Common::SeekableReadStream *stream) {
61  ZBasicBCD bcd = { 0, 0, 0 };
62  uint8 size = stream->readByte();
63  if (size == 0) {
64  // pass
65  } else if (size == 4) {
66  bcd.exponent = stream->readUint16BE();
67  bcd.mantissaBits = stream->readUint16BE();
68  bcd.mantissaSize = 16;
69  } else if (size == 6) {
70  bcd.exponent = stream->readUint16BE();
71  bcd.mantissaBits = stream->readUint32BE();
72  bcd.mantissaSize = 32;
73  } else if (size == 8) {
74  bcd.exponent = stream->readUint16BE();
75  bcd.mantissaBits = stream->readUint32BE() << 16;
76  bcd.mantissaBits += stream->readUint16BE();
77  bcd.mantissaSize = 48;
78  } else {
79  warning("ZBasicBCD::read: unexpected size for bcd %d", size);
80  stream->skip(size);
81  }
82  return bcd;
83  }
84 
85  double toDouble() {
86  DoubleBits result = { 0 };
87  int64 expSigned = this->exponent <= 0x3fff ? (int64)this->exponent : ((int64)this->exponent - 0x8000);
88  if (expSigned > 0x7ff) {
89  result.d = INFINITY;
90  } else if (expSigned < -0x800) {
91  result.d = -INFINITY;
92  } else {
93  int sign = expSigned < 0 ? -1 : 1;
94  uint64 mantissaRaw = this->mantissaBits;
95  for (int i = 0; i < this->mantissaSize; i += 4) {
96  if (i != 0) {
97  result.d /= 10;
98  }
99  result.d += (double)(mantissaRaw & 0xf);
100  mantissaRaw >>= 4;
101  }
102  result.d *= sign * pow((double)10, (double)expSigned);
103  }
104  return result.d;
105  }
106 };
107 
108 enum ZBasicTextFont {
109  kFontSystem = 0,
110  kFontApplication = 1,
111  kFontNewYork = 2,
112  kFontGeneva = 3,
113  kFontMonaco = 4,
114  kFontVenice = 5,
115  kFontLondon = 6,
116  kFontAthens = 7,
117  kFontSanFrancisco = 8,
118  kFontToronto = 9,
119  kFontSeattle = 10,
120  kFontCairo = 11,
121  kFontLosAngeles = 12
122 };
123 
124 enum ZBasicDatumType {
125  kDatumNULL = 0,
126  kDatumINT = 1,
127  kDatumDBLINT = 2,
128  kDatumBCD = 4,
129  kDatumSTR = 8,
130 };
131 
132 enum ZBasicWindowType {
133  kWindowDocument = 1,
134  kWindowDialogTwoLine = 2,
135  kWindowDialogOneLine = 3,
136  kWindowShadow = 4,
137  kWindowDocumentFixed = 5,
138 };
139 
140 struct ZBasicDatum {
141  ZBasicDatumType type = kDatumNULL;
142  uint32 offset = 0;
143  union {
144  ZBasicBCD *bcd = 0;
145  int16 i16;
146  int32 i32;
147  Common::U32String *str;
148  } data;
149 
150  ZBasicDatum() {}
151 
152  ~ZBasicDatum() {
153  if (this->type == kDatumBCD) {
154  delete this->data.bcd;
155  this->data.bcd = 0;
156  } else if (this->type == kDatumSTR) {
157  delete this->data.str;
158  this->data.str = 0;
159  }
160  }
161 
162  static Common::SharedPtr<ZBasicDatum> newNull(int32 offset) {
163  ZBasicDatum *result = new ZBasicDatum();
164  result->type = kDatumNULL;
165  result->offset = offset;
166  result->data.bcd = 0;
167  return Common::SharedPtr<ZBasicDatum>(result);
168  }
169 
170  static Common::SharedPtr<ZBasicDatum> newInt(uint32 offset, int16 value) {
171  ZBasicDatum *result = new ZBasicDatum();
172  result->type = kDatumINT;
173  result->offset = offset;
174  result->data.i16 = value;
175  return Common::SharedPtr<ZBasicDatum>(result);
176  }
177 
178  static Common::SharedPtr<ZBasicDatum> newDblInt(uint32 offset, int32 value) {
179  ZBasicDatum *result = new ZBasicDatum();
180  result->type = kDatumDBLINT;
181  result->offset = offset;
182  result->data.i32 = value;
183  return Common::SharedPtr<ZBasicDatum>(result);
184  }
185 
186  static Common::SharedPtr<ZBasicDatum> newBcd(uint32 offset, const ZBasicBCD &value) {
187  ZBasicDatum *result = new ZBasicDatum();
188  result->type = kDatumBCD;
189  result->offset = offset;
190  result->data.bcd = new ZBasicBCD(value);
191  return Common::SharedPtr<ZBasicDatum>(result);
192  }
193 
194  static Common::SharedPtr<ZBasicDatum> newStr(uint32 offset, const Common::U32String &value) {
195  ZBasicDatum *result = new ZBasicDatum();
196  result->type = kDatumSTR;
197  result->offset = offset;
198  result->data.str = new Common::U32String(value);
199  return Common::SharedPtr<ZBasicDatum>(result);
200  }
201 };
202 
203 class ZBasic {
204 
205 private:
207  uint32 _dataPtr;
209 
211  Common::MemoryPool *_memPool;
212  int16 _fileId = -1;
213  Graphics::MacWindow *_window;
214  Graphics::MacMenu *_menu;
215 
216  Graphics::MacToolbox::GrafPtr _defaultPort;
217 
222  Common::HashMap<int16, uint32> _fileLineSize;
224 
225  uint32 _fileType;
226  uint32 _fileCreator;
227 
228 public:
229 
231  ~ZBasic();
232 
233  void loadProgram(const Common::Path &path);
234  void loadSCOT();
235  void loadFonts();
236 
237  int16 asc(const Common::U32String &str);
238  void bufferFlush(const Common::U32String &str);
239  void blockMove(void *srcptr, void *destptr, uint16 size)
240 ;
241  int16 castInt(const Common::String &str);
242  Common::U32String chr(uint16 code);
243  void close(int16 fileNo);
244  void coordinateWindow();
245  void defOpen(const Common::String &str);
246  void get(int16 x1, int16 y1, int16 x2, int16 y2, Graphics::MacToolbox::BitMap &dest, bool preserveDims = false);
247  int16 instr(int16 expression, const Common::U32String &string1, const Common::U32String &string2);
248  int16 instr(int16 expression, const Common::String &string1, const Common::String &string2);
249  Common::U32String files(int16 expression, const Common::U32String &prompt, const Common::U32String &defaultFilename, int16 &volume);
250  int finderInfo(int16 &count, Common::U32String &var, Graphics::MacToolbox::OSType &type, uint16 volume);
251  bool maybe();
252  uint32 mem(int16 index);
253  void menu(uint16 menuNo, uint16 itemNo, uint16 state, const Common::U32String &title);
254  Common::U32String leftStr(const Common::U32String &str, int16 expression);
255  Common::String leftStr(const Common::String &str, int16 expression);
256  Common::U32String rightStr(const Common::U32String &str, int16 expression);
257  Common::String rightStr(const Common::String &str, int16 expression);
258  Common::U32String midStr(const Common::U32String &str, int16 expr1, int16 expr2);
259  void midStrSet(Common::U32String &target, int16 expr1, int16 expr2, const Common::U32String &src);
260  Common::String midStr(const Common::String &str, int16 expr1, int16 expr2);
261  void midStrSet(Common::String &target, int16 expr1, int16 expr2, const Common::String &src);
262  void openR(int16 fileNo, const Common::U32String &fileName, uint32 lineSize, int16 volNo);
263  void openW(int16 fileNo, const Common::U32String &fileName, uint32 lineSize, int16 volNo);
264  void picture(int16 x, int16 y, Graphics::MacToolbox::PicHandle &src);
265  void picture(int16 x1, int16 y1, int16 x2, int16 y2, Graphics::MacToolbox::PicHandle &src);
266  void put(int16 x, int16 y, Graphics::MacToolbox::BitMap &src, Graphics::MacToolbox::SourceMode mode);
267  void put(int16 x1, int16 y1, int16 x2, int16 y2, Graphics::MacToolbox::BitMap &src, Graphics::MacToolbox::SourceMode mode);
268  int16 readDataInt();
269  int32 readDataDblInt();
270  Common::U32String readDataStr();
271  Graphics::MacToolbox::Handle readFile(int16 fileNo, uint32 length);
272  uint32 readFile(int16 fileNo, byte *dest, uint32 length);
273  int32 readFileDblInt(int16 fileNo);
274  int16 readFileInt(int16 fileNo);
275  Common::String readFileStr(int16 fileNo, int16 length);
276  void record(int16 fileNo, int16 recordNo, int16 location);
277  void restore(uint32 index);
278  int16 rndInt(int16 max);
279  Common::U32String space(int16 count);
280  void swapInt(int16 &a, int16 &b);
281  void swapStr(Common::U32String &a, Common::U32String &b);
282  void text(uint16 font, uint16 size, uint16 face, Graphics::MacToolbox::SourceMode mode);
283  void window(int16 windowNumber, const Common::String &title, int16 x1, int16 y1, int16 x2, int16 y2, ZBasicWindowType type);
284  void writeFileStr(int16 fileNo, const Common::String &str);
285  void writeFileInt(int16 fileNo, int16 data);
286  void writeFileDblInt(int16 fileNo, int32 data);
287  Common::U32String ucase(const Common::U32String &str);
288 
289  const Common::U32String str(size_t index);
290  const Common::String strRaw(size_t index);
291 
292 
293  void unk_4(); // possibly quit?
294  int16 soundBusy();
295  void sound(int16 frequency, int32 duration, int16 volume, int16 voice);
296  void unk_8();
297  void unk_10();
298  void unk_11(int16 unk1);
299  void unk_20();
300  bool incrAndCheck(int16 &a0, int16 d1, int16 d0);
301  void unk_44(int16 unk1);
302  Common::String encodeInt(uint16 data);
303  void stringCopy(Common::U32String &dest, const Common::U32String &src);
304  void unk_130(int16 unk1);
305  void unk_158();
306  uint16 decodeInt(const Common::String &data);
307  void unk_331(uint16 unk1, int16 unk2);
308  void indexClear(int16 table);
309  void indexSet(const Common::U32String &value, int16 table, int16 index);
310  void indexRawSet(const Common::String &value, int16 table, int16 index);
311  Common::U32String index(int16 table, int16 index);
312  Common::String indexRaw(int16 table, int16 index);
313 
314  int16 getFileId() { return _fileId; }
315  void injectFOND(const byte *data, const size_t size, const Common::String &name);
316  void setMenuFont(uint16 font, uint16 size);
317 
318 };
319 
320 } // namespace Fool
321 
322 #endif
Definition: zbasic.h:41
Definition: str.h:59
Definition: memorypool.h:49
void warning(MSVC_PRINTF const char *s,...) GCC_PRINTF(1
Definition: array.h:52
Definition: macwindow.h:205
Definition: zbasic.h:36
Definition: toolbox.h:573
Definition: path.h:52
Definition: stream.h:745
Definition: toolbox.h:452
byte readByte()
Definition: stream.h:434
Definition: ustr.h:57
Definition: zbasic.h:140
Definition: macmenu.h:95
Definition: detection.h:27
Definition: zbasic.h:203
uint32 readUint32BE()
Definition: stream.h:515
uint16 readUint16BE()
Definition: stream.h:501
Definition: ptr.h:159
virtual bool skip(uint32 offset)
Definition: stream.h:793