ScummVM API documentation
xplib.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 XPLIB_XPLIB_H
23 #define XPLIB_XPLIB_H
24 
25 #include "audio/audiostream.h"
26 #include "audio/mixer.h"
27 
28 #include "common/events.h"
29 #include "common/file.h"
30 #include "common/mutex.h"
31 
32 #include "graphics/paletteman.h"
33 
34 #include "bolt/bolt.h"
35 
36 namespace Bolt {
37 
38 // RENDER FLAGS
39 
40 #define RF_OVERLAY_ACTIVE 0x01 // RLE overlay source stored on front surface
41 #define RF_DOUBLE_BUFFER 0x02 // Double-buffer compositing enabled
42 #define RF_FULL_REDRAW 0x04 // Full-screen overwrite, skip dirty tracking
43 #define RF_FRONT_DIRTY 0x08 // Front surface pixels changed
44 #define RF_BACK_DIRTY 0x10 // Back surface pixels changed
45 #define RF_FRONT_PAL_DIRTY 0x20 // Front surface palette changed
46 #define RF_BACK_PAL_DIRTY 0x40 // Back surface palette changed
47 #define RF_CURSOR_VISIBLE 0x80 // Cursor should be drawn this frame
48 
49 struct DisplaySpecs;
50 class BoltEngine;
51 
52 struct XPCycleState {
53  int16 startIndex;
54  int16 endIndex;
55  int16 delay; // ms between rotations
56  uint32 nextFire; // getMillis() deadline for next rotation
57  bool active;
58 
59  XPCycleState() {
60  startIndex = 0;
61  endIndex = 0;
62  delay = 0;
63  nextFire = 0;
64  active = false;
65  }
66 };
67 
68 typedef struct XPPicDesc {
69  byte *pixelData;
70  int16 width;
71  int16 height;
72  byte *palette;
73  int16 paletteStart;
74  int16 paletteCount;
75  int16 flags;
76 
77  XPPicDesc() {
78  pixelData = nullptr;
79  width = 0;
80  height = 0;
81  palette = nullptr;
82  paletteStart = 0;
83  paletteCount = 0;
84  flags = 0;
85  }
86 } XPPicDesc;
87 
88 enum XPSurfaceType : int {
89  stFront = 0,
90  stBack
91 };
92 
93 typedef struct XPSurface {
94  XPPicDesc mainPic;
95  XPPicDesc overlayPic;
96  int16 dirtyPalStart;
97  int16 dirtyPalEnd;
98 
99  XPSurface() {
100  dirtyPalStart = 0;
101  dirtyPalEnd = 0;
102  }
103 } XPSurface;
104 
105 enum XPEventTypes : int16 {
106  etEmpty = 0,
107  etTimer = 1,
108  etMouseMove = 2,
109  etMouseDown = 3,
110  etMouseUp = 4,
111  etJoystick = 5,
112  etSound = 6,
113  etInactivity = 7,
114  etTrigger = 8
115 };
116 
117 enum XPEventKeyStates : int16 {
118  eksInputOff = 0,
119  eksMouseMode = 1,
120  eksJoystickMode = 2
121 };
122 
123 static const Common::EventType EVENT_TIMER = Common::EVENT_USER_FIRST_AVAILABLE;
124 
125 typedef struct XPEvent {
126  XPEvent *prev;
127  XPEvent *next;
128  XPEventTypes type;
129  uint32 payload;
130  byte *payloadPtr;
131 
132  XPEvent() {
133  prev = nullptr;
134  next = nullptr;
135  type = etEmpty;
136  payload = 0;
137  payloadPtr = nullptr;
138  }
139 } XPEvent;
140 
141 typedef struct XPTimer {
142  uint32 id;
143  uint32 deadline;
144  bool active;
145 
146  XPTimer() {
147  id = 0;
148  deadline = 0;
149  active = false;
150  }
151 } XPTimer;
152 
153 class XpLib {
154 friend class BoltEngine;
155 
156 public:
157  XpLib(BoltEngine *bolt);
158  ~XpLib();
159 
160  bool initialize();
161  void terminate();
162 
163  // Blit
164  void blit(byte *src, uint16 srcStride, byte *dst, uint16 dstStride, uint16 width, uint16 height);
165  void maskBlit(byte *src, uint16 srcStride, byte *dst, uint16 dstStride, uint16 width, uint16 height);
166 
167  // Palette
168  void getPalette(int16 startIndex, int16 count, byte *destBuf);
169  void setPalette(int16 count, int16 startIndex, byte *srcBuf);
170  bool startCycle(XPCycleState *specs);
171  void cycleColors();
172  void stopCycle();
173  void setScreenBrightness(uint8 percent);
174 
175  // Cursor
176  bool readCursor(uint16 *outButtons, int16 *outX, int16 *outY);
177  void setCursorPos(int16 x, int16 y);
178  void setCursorImage(byte *bitmap, int16 hotspotX, int16 hotspotY);
179  void setCursorColor(byte r, byte g, byte b);
180  bool showCursor();
181  void hideCursor();
182  void updateCursorPosition();
183 
184  // Events
185  int16 getEvent(int16 filter, uint32 *outData, byte **outPtrData = nullptr);
186  int16 peekEvent(int16 filter, uint32 *outData, byte **outPtrData = nullptr);
187  void postEvent(XPEventTypes type, uint32 data, byte *ptrData = nullptr);
188  int16 setInactivityTimer(int16 seconds);
189  int16 setScreenSaverTimer(int16 seconds);
190  bool enableController();
191  void disableController();
192 
193  // Display
194  bool setDisplaySpec(int *outMode, DisplaySpecs *spec);
195  void setCoordSpec(int16 x, int16 y, int16 width, int16 height);
196  void displayPic(XPPicDesc *pic, int16 x, int16 y, int16 page);
197  void setFrameRate(int16 fps);
198  void updateDisplay();
199  void setTransparency(bool toggle);
200  void fillDisplay(byte color, int16 page);
201 
202  // Random
203  int16 getRandom(int16 range);
204  void randomize();
205 
206  // File
207  Common::File *openFile(const char *fileName, int16 flags);
208  void closeFile(Common::File *handle);
209  bool readFile(Common::File *handle, void *buffer, uint32 *size);
210  bool setFilePos(Common::File *handle, int32 offset, int32 origin);
211  void *allocMem(uint32 size);
212  void *tryAllocMem(uint32 size);
213  void freeMem(void *mem);
214 
215  // Sound
216  bool playSound(byte *data, uint32 size, int16 sampleRate);
217  bool pauseSound();
218  bool resumeSound();
219  bool stopSound();
220 
221  // Timer
222  uint32 startTimer(int16 delay);
223  void updateTimers();
224  bool killTimer(uint32 timerId);
225 
226 protected:
227  BoltEngine *_bolt = nullptr;
228  bool _xpInitialized = false;
229 
230  // Blit
231  void dirtyBlit(byte *src, byte *dst, uint16 width, uint16 height, byte *dirtyFlags);
232  void compositeBlit(byte *src, byte *background, byte *dst, uint16 stride, uint16 width, uint16 height);
233  void rleBlit(byte *src, uint16 srcStride, byte *dst, uint16 dstStride, uint16 width, uint16 height);
234  void rleMaskBlit(byte *src, uint16 srcStride, byte *dst, uint16 dstStride, uint16 width, uint16 height);
235  void rleCompositeBlit(byte *rle, byte *background, byte *dst, uint16 width, uint16 height, byte *dirtyFlags);
236  uint16 rleDataSize(byte *rleData, uint16 height);
237  void markCursorPixels(byte *buffer, uint32 count);
238 
239  // Palette
240  XPCycleState _cycleStates[4];
241  byte _paletteBuffer[3 * 256];
242  byte _shiftedPaletteBuffer[3 * 256];
243  byte _cycleTempPalette[3 * 20];
244  uint32 _cycleTimerIds[4];
245  int16 _brightnessShift = 0;
246 
247  // Cursor
248  bool initCursor();
249  void shutdownCursor();
250  void readJoystick(int16 *outX, int16 *outY);
251 
252  byte _cursorBuffer[16 * 16];
253  int16 _cursorHotspotX = 0;
254  int16 _cursorHotspotY = 0;
255  int16 _lastCursorX = 0;
256  int16 _lastCursorY = 0;
257  int16 _cursorViewportWidth = 0;
258  int16 _cursorViewportHeight = 0;
259  int16 _cursorHidden = 1;
260 
261  // Events
262  bool initEvents();
263  void shutdownEvents();
264  void unlinkEvent(XPEvent *node);
265  void enqueueEvent(XPEvent *node);
266  void pumpMessages();
267  void handleTimer(uint32 timerId);
268  void handleMouseMove(bool *mouseMoved);
269  void handleMouseButton(int16 down, int16 button);
270  void handleKey(Common::KeyCode vkey, int16 down);
271  void postJoystickEvent(int16 source, int16 dx, int16 dy);
272  bool canDropEvent(int16 type);
273  void activateScreenSaver();
274  void resetInactivity();
275  void enableMouse();
276  void disableMouse();
277  int16 getButtonState();
278 
279  XPEvent _events[40];
280 
281  int8 _keyStateLeft = 0;
282  int8 _keyStateRight = 0;
283  int8 _keyStateUp = 0;
284  int8 _keyStateDown = 0;
285  int16 _lastJoystickX = 0;
286  int16 _lastJoystickY = 0;
287  int16 _mouseButtonPrev = 0;
288  int16 _mouseButtonState = 0;
289  int16 _eventMouseMoved = 0;
290  int16 _eventKeyStates = 0;
291  uint32 _inactivityDeadline = 0;
292 
293  XPEvent *_eventQueueHead = nullptr;
294  XPEvent *_eventQueueTail = nullptr;
295  XPEvent *_eventFreeList = nullptr;
296 
297  uint32 _lastMouseEventData = 0;
298  Common::Point _lastRegisteredMousePos;
299 
300  // Display
301  bool initDisplay();
302  void shutdownDisplay();
303  bool createSurface(XPSurface *surf);
304  void freeSurface(XPSurface *surf);
305  void virtualToScreen(int16 *x, int16 *y);
306  void screenToVirtual(int16 *x, int16 *y);
307  void dispatchBlit(int16 mode, byte *src, uint16 srcStride, byte *dst, uint16 dstStride, uint16 width, uint16 height);
308  bool clipAndBlit(XPPicDesc *src, XPPicDesc *dest, int16 x, int16 y, Common::Rect *outClip);
309  void addDirtyRect(Common::Rect *rect);
310  void waitForFrameRate();
311  void handlePaletteTransitions();
312  void flushPalette();
313  void overlayComposite();
314  void compositeToScreen();
315  void mergeDirtyRects();
316  void blitDirtyRects(Common::Rect *rects, int16 count);
317  void compositeDirtyRects(Common::Rect *rects, int16 count);
318  void applyCursorPalette();
319  void prepareBackSurface();
320 
321  XPSurface _surfaces[2];
322 
323  int16 _virtualWidth = 0;
324  int16 _virtualHeight = 0;
325  int16 _currentDisplayPage = 0;
326  int16 _prevRenderFlags = 0;
327  int16 _renderFlags = 0;
328  int16 _frameRateFPS = 0;
329  int16 _overlayCount = 0;
330  int16 _prevDirtyCount = 0;
331  int16 _prevDirtyValid = 0;
332  int16 _surfaceWidth = 0;
333  int16 _surfaceHeight = 0;
334  int16 _viewportOffsetX = 0;
335  int16 _viewportOffsetY = 0;
336  uint32 _nextFrameTime = 0;
337 
338  Common::Rect _dirtyRects[30];
339  Common::Rect _prevDirtyRects[30];
340  Common::Rect _cursorRect;
341  Common::Rect _prevCursorRect;
342  Common::Rect _overlayCursorRect;
343  Common::Rect _prevOverlayCursorRect;
344 
345  byte *_vgaFramebuffer = nullptr;
346  byte *_rowDirtyFlags = nullptr;
347  byte _cursorBackgroundSaveBuffer[16 * 16];
348 
349  XPPicDesc _cursorBackgroundSave;
350  XPPicDesc _cursorSprite;
351 
352  // File
353  void fileError(const char *message);
354 
355  // Sound
356  bool pollSound(byte **outData);
357  bool initSound();
358  void shutdownSound();
359 
360  Audio::QueuingAudioStream *_audioStream = nullptr;
361  Audio::SoundHandle _soundHandle;
362  Common::Queue<uint32> _durationQueue;
363  uint32 _nextSoundDeadlineMs = 0;
364  uint32 _pauseTimeMs = 0;
365  int16 _sndPlayState = 0;
366  int16 _sndQueued = 0;
367  int _sndCompletedCount = 0;
368  bool _sndPaused = false;
369  int16 _sndSampleRate = 22050;
370  uint32 _sndNextDeadline = 0;
371  uint32 _sndBufferQueueTime = 0;
372  Common::Queue<byte *> _bufferSourceQueue;
373 
374  // Timer
375  bool initTimer();
376  void shutdownTimer();
377 
378  uint32 _inactivityTimerId = 0;
379  int16 _inactivityCountdown = 0;
380  int16 _inactivityTimerValue = 0;
381  int16 _screensaverCountdown = 0;
382  int16 _screenSaverTimerValue = 0;
383  int16 _inactivityTimeout = 0;
384 
385  bool _timerInitialized = false;
386 
387  XPTimer _timers[128];
388  uint16 _nextTimerId = 0;
389 };
390 
391 } // End of namespace Bolt
392 
393 #endif // XPLIB_XPLIB_H
Definition: bolt.h:40
Definition: xplib.h:52
EventType
Definition: events.h:49
Definition: bolt.h:549
Definition: rect.h:524
Definition: xplib.h:68
Definition: mixer.h:49
Definition: xplib.h:125
Definition: file.h:47
Definition: xplib.h:93
Definition: rect.h:144
Definition: xplib.h:153
Definition: audiostream.h:370
bool maskBlit(byte *dst, const byte *src, const byte *mask, const uint dstPitch, const uint srcPitch, const uint maskPitch, const uint w, const uint h, const uint bytesPerPixel)
Definition: bolt.h:50
Definition: xplib.h:141
Definition: events.h:128