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
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (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, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #ifndef ASYLUM_SYSTEM_SCREEN_H
24 #define ASYLUM_SYSTEM_SCREEN_H
25 
26 #include "common/array.h"
27 #include "common/rect.h"
28 
29 #include "graphics/palette.h"
30 #include "graphics/surface.h"
31 
32 #include "asylum/shared.h"
33 
34 namespace Asylum {
35 
36 #define PALETTE_SIZE 256 * 3
37 
38 class AsylumEngine;
39 class GraphicResource;
40 class ResourcePack;
41 
42 struct GraphicFrame;
43 
44 enum GraphicItemType {
45 	kGraphicItemNormal = 1,
46 	kGraphicItemMasked = 5
47 };
48 
49 typedef struct GraphicQueueItem {
50 	int32 priority;
51 
52 	GraphicItemType type;
53 	ResourceId resourceId;
54 	uint32 frameIndex;
55 	Common::Point source;
56 	ResourceId resourceIdDestination;
57 	Common::Point destination;
58 	DrawFlags flags;
59 	int32 transTableNum;
60 
GraphicQueueItemGraphicQueueItem61 	GraphicQueueItem() {
62 		priority = 0;
63 
64 		type = kGraphicItemNormal;
65 		resourceId = kResourceNone;
66 		frameIndex = 0;
67 		resourceIdDestination = kResourceNone;
68 		flags = kDrawFlagNone;
69 		transTableNum = 0;
70 	}
71 } GraphicQueueItem;
72 
73 class Screen {
74 public:
75 	Screen(AsylumEngine *_vm);
76 	~Screen();
77 
78 	// Drawing
79 	void draw(ResourceId resourceId);
80 	void draw(ResourceId resourceId, uint32 frameIndex, const Common::Point &source, DrawFlags flags = kDrawFlagNone, bool colorKey = true);
81 	void draw(ResourceId resourceId, uint32 frameIndex, const int16 (*srcPtr)[2], DrawFlags flags = kDrawFlagNone, bool colorKey = true);
82 	void draw(GraphicResource *resource, uint32 frameIndex, const Common::Point &source, DrawFlags flags= kDrawFlagNone, bool colorKey = true);
83 	void drawTransparent(ResourceId resourceId, uint32 frameIndex, const Common::Point &source, DrawFlags flags, uint32 transTableNum);
84 	void drawTransparent(GraphicResource *resource, uint32 frameIndex, const Common::Point &source, DrawFlags flags, uint32 transTableNum);
85 	void draw(ResourceId resourceId, uint32 frameIndex, const Common::Point &source, DrawFlags flags, ResourceId resourceId2, const Common::Point &destination, bool colorKey = true);
86 
87 	// Misc
88 	void clear();
clearDefaultColor()89 	void clearDefaultColor() { memset(_mainPalette, 0, 3); setupPalette(NULL, 0, 0); }
90 	void drawWideScreenBars(int16 barSize) const;
91 	void fillRect(int16 x, int16 y, int16 x2, int16 y2, uint32 color);
92 	void copyBackBufferToScreen();
setFlag(int16 val)93 	void setFlag(int16 val) { _flag = (val < -1) ? -1 : val; }
getFlag()94 	int16 getFlag() { return _flag; }
95 
96 	// Palette
97 	void setPalette(ResourceId id);
98 	void setMainPalette(const byte *data);
99 	void loadGrayPalette();
100 	void updatePalette();
101 	void updatePalette(int32 param);
102 	void setupPalette(byte *buffer, int start, int count);
103 
104 	void startPaletteFade(ResourceId resourceId, int32 ticksWait, int32 delta);
105 	void paletteFade(uint32 start, int32 ticksWait, int32 delta);
106 	void stopPaletteFade(char red, char green, char blue);
107 	void stopPaletteFadeAndSet(ResourceId id, int32 ticksWait, int32 delta);
108 
109 	// Gamma
110 	void setPaletteGamma(ResourceId id);
111 	void setGammaLevel(ResourceId id);
112 
113 	// Transparency tables
114 	void setupTransTable(ResourceId resourceId);
115 	void setupTransTables(uint32 count, ...);
116 	void selectTransTable(uint32 index);
117 
118 	// Graphic queue
119 	void addGraphicToQueue(ResourceId resourceId, uint32 frameIndex, const Common::Point &point, DrawFlags flags, int32 transTableNum, int32 priority);
120 	void addGraphicToQueue(ResourceId resourceId, uint32 frameIndex, const int16 (*pointPtr)[2], DrawFlags flags, int32 transTableNum, int32 priority);
121 	void addGraphicToQueueCrossfade(ResourceId resourceId, uint32 frameIndex, const Common::Point &source, int32 objectResourceId, const Common::Point &destination, uint32 transTableNum);
122 	void addGraphicToQueueMasked(ResourceId resourceId, uint32 frameIndex, const Common::Point &source, int32 objectResourceId, const Common::Point &destination, DrawFlags flags, int32 priority);
123 	void addGraphicToQueue(GraphicQueueItem const &item);
124 	void drawGraphicsInQueue();
125 	void clearGraphicsInQueue();
126 	void deleteGraphicFromQueue(ResourceId resourceId);
127 
128 	// Used by Video
129 	void copyToBackBuffer(const byte *buffer, int32 pitch, int16 x, int16 y, uint16 width, uint16 height, bool mirrored = false);
130 
131 	// Debug
132 	void drawLine(const Common::Point &source, const Common::Point &destination, uint32 color = 0xFF);
133 	void drawLine(const int16 (*srcPtr)[2], const int16 (*dstPtr)[2], uint32 color = 0xFF);
134 	void drawRect(const Common::Rect &rect, uint32 color = 0xFF);
135 	void copyToBackBufferClipped(Graphics::Surface *surface, int16 x, int16 y);
136 
137 	// Used by Writings puzzle
getSurface()138 	const Graphics::Surface *getSurface() const { return &_backBuffer; };
139 
140 protected:
141 	// Palette fading Timer
142 	static void paletteFadeTimer(void *ptr);
143 	void handlePaletteFadeTimer();
144 
145 private:
146 	AsylumEngine *_vm;
147 
148 	Graphics::Surface _backBuffer;
149 	Common::Rect _clipRect;
150 	Common::Array<GraphicQueueItem> _queueItems;
151 
152 	int16 _flag;
153 	bool _useColorKey;
154 
155 	// Transparency tables
156 	uint32 _transTableCount;
157 	byte *_transTable;
158 	byte *_transTableBuffer;
159 	void clearTransTables();
160 
161 	// Palette
162 	byte _currentPalette[PALETTE_SIZE];
163 	byte _mainPalette[PALETTE_SIZE];
164 	bool _isFading;
165 	bool _fadeStop;
166 	ResourceId _fadeResourceId;
167 	int32 _fadeTicksWait;
168 	int32 _fadeDelta;
169 
170 
171 	byte *getPaletteData(ResourceId id);
172 	void setPaletteGamma(byte *data, byte *target = NULL);
173 
174 	void paletteFadeWorker(ResourceId id, int32 ticksWait, int32 delta);
175 	void stopPaletteFadeTimer();
176 
177 	// Graphic queue
178 	static bool graphicQueueItemComparator(const GraphicQueueItem &item1, const GraphicQueueItem &item2);
179 
180 	// Misc
181 	void clip(Common::Rect *source, Common::Rect *destination, int32 flags) const;
182 
183 	void draw(GraphicResource *resource, uint32 frameIndex, const Common::Point &source, DrawFlags flags, ResourceId resourceId2, const Common::Point &destination, bool colorKey = true);
184 
185 	// Screen blitting
186 	void blit(GraphicFrame *frame, Common::Rect *source, Common::Rect *destination, int32 flags);
187 	void blitMasked(GraphicFrame *frame, Common::Rect *source, byte *maskData, Common::Rect *sourceMask, Common::Rect *destMask, uint16 maskWidth, Common::Rect *destination, int32 flags);
188 	void blitTranstable        (byte *dstBuffer, byte *srcBuffer, int16 height, int16 width, uint16 srcPitch, uint16 dstPitch) const;
189 	void blitTranstableMirrored(byte *dstBuffer, byte *srcBuffer, int16 height, int16 width, uint16 srcPitch, uint16 dstPitch) const;
190 	void blitMirrored          (byte *dstBuffer, byte *srcBuffer, int16 height, int16 width, uint16 srcPitch, uint16 dstPitch) const;
191 	void blitMirroredColorKey  (byte *dstBuffer, byte *srcBuffer, int16 height, int16 width, uint16 srcPitch, uint16 dstPitch) const;
192 	void blitRaw               (byte *dstBuffer, byte *srcBuffer, int16 height, int16 width, uint16 srcPitch, uint16 dstPitch) const;
193 	void blitRawColorKey       (byte *dstBuffer, byte *srcBuffer, int16 height, int16 width, uint16 srcPitch, uint16 dstPitch) const;
194 	void blitCrossfade         (byte *dstBuffer, byte *srcBuffer, byte *objectBuffer, int16 height, int16 width, uint16 srcPitch, uint16 dstPitch, uint16 objectPitch) const;
195 	void bltMasked             (byte *srcBuffer, byte *maskBuffer, int16 height, int16 width, uint16 srcPitch, uint16 maskPitch, byte zoom, byte *dstBuffer, uint16 dstPitch) const;
196 
197 	// DirectDraw-equivalent functions
198 	void blt(Common::Rect *dest, GraphicFrame* frame, Common::Rect *source, int32 flags);
199 	void bltFast(int16 dX, int16 dY, GraphicFrame* frame, Common::Rect *source);
200 
201 	void copyToBackBufferWithTransparency(byte *buffer, int32 pitch, int16 x, int16 y, uint16 width, uint16 height, bool mirrored = false);
202 
203 	// Debug
204 	void drawZoomedMask(byte *mask, uint16 height, uint16 width, uint16 maskPitch);
205 };
206 
207 } // end of namespace Asylum
208 
209 #endif // ASYLUM_SYSTEM_SCREEN_H
210