ScummVM API documentation
gfx.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 FREESCAPE_GFX_H
23 #define FREESCAPE_GFX_H
24 
25 #include "common/hashmap.h"
26 #include "common/rendermode.h"
27 #include "common/rect.h"
28 
29 #include "graphics/pixelformat.h"
30 #include "graphics/managed_surface.h"
31 #include "graphics/renderer.h"
32 #include "math/frustum.h"
33 #include "math/vector3d.h"
34 
35 namespace Freescape {
36 
37 #define kVertexArraySize 128
38 #define kCoordsArraySize 4
39 
40 typedef Common::Array<byte *> ColorMap;
41 typedef Common::HashMap<int, int> ColorReMap;
42 
43 class Renderer;
44 
45 const Graphics::PixelFormat getRGBAPixelFormat();
46 
47 class Texture {
48 public:
49  Texture(){ _width = 0; _height = 0; };
50  virtual ~Texture(){};
51 
52  uint _width;
53  uint _height;
54  Graphics::PixelFormat _format;
55 
56  virtual void update(const Graphics::Surface *surface) = 0;
57  virtual void updatePartial(const Graphics::Surface *surface, const Common::Rect &rect) = 0;
58 };
59 
60 class Renderer {
61 public:
62  Renderer(int screenW, int screenH, Common::RenderMode renderMode, bool authenticGraphics);
63  virtual ~Renderer();
64 
65  Graphics::PixelFormat _currentPixelFormat;
66  Graphics::PixelFormat _palettePixelFormat;
67  Graphics::PixelFormat _texturePixelFormat;
68  bool _isAccelerated;
69  bool _authenticGraphics;
70 
71  virtual void init() = 0;
72  virtual void setViewport(const Common::Rect &rect) = 0;
73 
77  virtual void flipBuffer() {}
78  virtual void useColor(uint8 r, uint8 g, uint8 b) = 0;
79  virtual void depthTesting(bool enabled) {};
80  virtual void polygonOffset(bool enabled) = 0;
81 
82  virtual Texture *createTexture(const Graphics::Surface *surface, bool is3D = false) = 0;
83  Graphics::Surface *convertImageFormatIfNecessary(Graphics::ManagedSurface *surface);
84 
85  virtual void freeTexture(Texture *texture) = 0;
86  virtual void drawTexturedRect2D(const Common::Rect &screenRect, const Common::Rect &textureRect, Texture *texture) = 0;
87 
88  virtual void renderSensorShoot(byte color, const Math::Vector3d sensor, const Math::Vector3d player, const Common::Rect viewPort) = 0;
89  virtual void renderPlayerShootBall(byte color, const Common::Point position, int frame, const Common::Rect viewPort) = 0;
90  virtual void renderPlayerShootRay(byte color, const Common::Point position, const Common::Rect viewPort) = 0;
91 
92  virtual void renderCrossair(const Common::Point crossairPosition) = 0;
93 
94  virtual void renderCube(const Math::Vector3d &position, const Math::Vector3d &size, Common::Array<uint8> *colours, Common::Array<uint8> *ecolours, float offset = 0.0);
95  virtual void renderRectangle(const Math::Vector3d &position, const Math::Vector3d &size, Common::Array<uint8> *colours, Common::Array<uint8> *ecolours, float offset = 0.0);
96  virtual void renderPolygon(const Math::Vector3d &origin, const Math::Vector3d &size, const Common::Array<float> *ordinates, Common::Array<uint8> *colours, Common::Array<uint8> *ecolours, float offset = 0.0);
97  virtual void renderPyramid(const Math::Vector3d &origin, const Math::Vector3d &size, const Common::Array<float> *ordinates, Common::Array<uint8> *colours, Common::Array<uint8> *ecolours, int type);
98  virtual void renderFace(const Common::Array<Math::Vector3d> &vertices) = 0;
99 
100  void setColorRemaps(ColorReMap *colorRemaps);
101  virtual void clear(uint8 r, uint8 g, uint8 b, bool ignoreViewport = false) = 0;
102  virtual void drawFloor(uint8 color) = 0;
103  virtual void drawBackground(uint8 color);
104 
105  void drawEclipse(uint8 color1, uint8 color2, float difference);
106  virtual void drawSkybox(Texture *texture, Math::Vector3d camera) {};
107  virtual void drawCelestialBody(Math::Vector3d position, float radius, uint8 color) {};
108 
109  Common::Rect viewport() const;
110  virtual Common::Point nativeResolution() { return Common::Point(_screenW, _screenH); }
111 
112  // palette
113  void readFromPalette(uint8 index, uint8 &r, uint8 &g, uint8 &b);
114  void setPaletteValue(uint8 index, uint8 r, uint8 g, uint8 b);
115  uint8 indexFromColor(uint8 r, uint8 g, uint8 b);
116  uint8 mapEGAColor(uint8 index);
117 
118  bool getRGBAt(uint8 index, uint8 ecolor, uint8 &r1, uint8 &g1, uint8 &b1, uint8 &r2, uint8 &g2, uint8 &b2, byte *&stipple);
119  bool getRGBAtC64(uint8 index, uint8 &r1, uint8 &g1, uint8 &b1, uint8 &r2, uint8 &g2, uint8 &b2);
120  bool getRGBAtCGA(uint8 index, uint8 &r1, uint8 &g1, uint8 &b1, uint8 &r2, uint8 &g2, uint8 &b2, byte *&stipple);
121  bool getRGBAtCPC(uint8 index, uint8 &r1, uint8 &g1, uint8 &b1, uint8 &r2, uint8 &g2, uint8 &b2, byte *&stipple);
122  bool getRGBAtEGA(uint8 index, uint8 &r1, uint8 &g1, uint8 &b1, uint8 &r2, uint8 &g2, uint8 &b2);
123  bool getRGBAtZX(uint8 index, uint8 &r1, uint8 &g1, uint8 &b1, uint8 &r2, uint8 &g2, uint8 &b2, byte *&stipple);
124  bool getRGBAtHercules(uint8 index, uint8 &r1, uint8 &g1, uint8 &b1, uint8 &r2, uint8 &g2, uint8 &b2, byte *&stipple);
125  void extractCPCIndexes(uint8 cm1, uint8 cm2, uint8 &i1, uint8 &i2);
126  void extractC64Indexes(uint8 cm1, uint8 cm2, uint8 &i1, uint8 &i2);
127 
128  void selectColorFromFourColorPalette(uint8 index, uint8 &r1, uint8 &g1, uint8 &b1);
129 
130  // Stipple
131  virtual void setStippleData(byte *data) {};
132  virtual void useStipple(bool enabled) {};
133  void scaleStipplePattern(byte originalPattern[128], byte newPattern[128]);
134 
135  byte _defaultStippleArray[128] = {
136  0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
137  0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
138  0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
139  0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
140  0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
141  0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
142  0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
143  0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
144  0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
145  0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
146  0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
147  0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
148  0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
149  0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
150  0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
151  0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
152  };
153 
154  byte _defaultStippleArraySmall[128] = {
155  0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
156  0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
157  0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
158  0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
159  0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
160  0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
161  0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
162  0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
163  0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
164  0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
165  0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
166  0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
167  0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
168  0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
169  0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
170  0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
171  };
172 
173  byte *_variableStippleArray;
174 
175  float _skyNormals[16][3] = {
176  { 0.0, 0.0, 1.0 }, //front //0
177  { 0.0, 0.0, 1.0 }, //1
178  { 0.0, 0.0, 1.0 }, //2
179  { 0.0, 0.0, 1.0 }, //3
180  { 0.0, 0.0, -1.0 }, //back //0
181  { 0.0, 0.0, -1.0 }, //1
182  { 0.0, 0.0, -1.0 }, //2
183  { 0.0, 0.0, -1.0 }, //3
184  { -1.0, 0.0, 0.0 }, //left
185  { -1.0, 0.0, 0.0 },
186  { -1.0, 0.0, 0.0 },
187  { -1.0, 0.0, 0.0 },
188  { 1.0, 0.0, 0.0 }, //right
189  { 1.0, 0.0, 0.0 },
190  { 1.0, 0.0, 0.0 },
191  { 1.0, 0.0, 0.0 }
192  };
193 
194  float _skyUvs1008[16][2] = {
195  { 0.0f, 0.0f }, //1
196  { 0.0f, 2.0f }, //2
197  { 0.4f, 2.0f }, //3
198  { 0.4f, 0.0f }, //front //4
199 
200  { 0.0f, 2.0f }, //back //1
201  { 0.4f, 2.0f }, //2
202  { 0.4f, 0.0f }, //3
203  { 0.0f, 0.0f }, //4
204 
205  { 0.0f, 0.0f }, //left //1
206  { 0.4f, 0.0f }, //2
207  { 0.4f, 2.0f }, //3
208  { 0.0f, 2.0f }, //4
209 
210  { 0.4f, 0.0f }, //right //1
211  { 0.0f, 0.0f }, //2
212  { 0.0f, 2.0f }, //3
213  { 0.4f, 2.0f }, //4
214  };
215 
216  float _skyUvs128[16][2] = {
217  { 0.0f, 0.0f }, //1
218  { 0.0f, 2.0f }, //2
219  { 2.5f, 2.0f }, //3
220  { 2.5f, 0.0f }, //front //4
221 
222  { 0.0f, 2.0f }, //back //1
223  { 2.5f, 2.0f }, //2
224  { 2.5f, 0.0f }, //3
225  { 0.0f, 0.0f }, //4
226 
227  { 0.0f, 0.0f }, //left //1
228  { 2.5f, 0.0f }, //2
229  { 2.5f, 2.0f }, //3
230  { 0.0f, 2.0f }, //4
231 
232  { 2.5f, 0.0f }, //right //1
233  { 0.0f, 0.0f }, //2
234  { 0.0f, 2.0f }, //3
235  { 2.5f, 2.0f }, //4
236  };
237 
238  float _skyVertices[16][3] = {
239  { -81280.0, 8128.0, 81280.0 }, //1 // Vertex #0 front
240  { -81280.0, -8128.0, 81280.0 }, //2 // Vertex #1
241  { 81280.0, -8128.0, 81280.0 }, //3 // Vertex #2
242  { 81280.0, 8128.0, 81280.0 }, //4 // Vertex #3
243 
244  { 81280.0f, -8128.0f, -81280.0f }, // 1
245  { -81280.0f, -8128.0f, -81280.0f }, // 2
246  { -81280.0f, 8128.0f, -81280.0f }, // 3
247  { 81280.0f, 8128.0f, -81280.0f }, // 4
248 
249  { -81280.0f, 8128.0f, 81280.0f }, //left //1
250  { -81280.0f, 8128.0f, -81280.0f }, //2
251  { -81280.0f, -8128.0f, -81280.0f }, //3
252  { -81280.0f, -8128.0f, 81280.0f }, //4
253 
254  { 81280.0f, 8128.0f, -81280.0f }, //right //1
255  { 81280.0f, 8128.0f, 81280.0f }, //2
256  { 81280.0f, -8128.0f, 81280.0f },//3
257  { 81280.0f, -8128.0f, -81280.0f },//4
258  };
259 
260  byte *_palette;
261  void setColorMap(ColorMap *colorMap_);
262  ColorMap *_colorMap;
263  ColorReMap *_colorRemaps;
264  void clearColorPairArray();
265  void fillColorPairArray();
266  byte _colorPair[16];
267  int _keyColor;
268  int _inkColor;
269  int _paperColor;
270  int _underFireBackgroundColor;
271  Common::Point _shakeOffset;
272  byte _stipples[16][128];
273 
274  int _scale;
275 
282  virtual void positionCamera(const Math::Vector3d &pos, const Math::Vector3d &interest) = 0;
283  virtual void updateProjectionMatrix(float fov, float aspectRatio, float nearClipPlane, float farClipPlane) = 0;
284 
285  Math::Matrix4 getMvpMatrix() const { return _mvpMatrix; }
286  virtual Graphics::Surface *getScreenshot() = 0;
287  void flipVertical(Graphics::Surface *s);
288 
289  int _screenW;
290  int _screenH;
291  Common::RenderMode _renderMode;
292 
293  bool computeScreenViewport();
294 
295 protected:
296  Common::Rect _screenViewport;
297  Common::Rect _viewport;
298  Common::Rect _unscaledViewport;
299 
300  Math::Matrix4 _projectionMatrix;
301  Math::Matrix4 _modelViewMatrix;
302  Math::Matrix4 _mvpMatrix;
303 
304  Math::Frustum _frustum;
305 
306  Math::Matrix4 makeProjectionMatrix(float fov, float nearClipPlane, float farClipPlane) const;
307 };
308 
309 Graphics::RendererType determinateRenderType();
310 Renderer *CreateGfxOpenGL(int screenW, int screenH, Common::RenderMode renderMode, bool authenticGraphics);
311 Renderer *CreateGfxOpenGLShader(int screenW, int screenH, Common::RenderMode renderMode, bool authenticGraphics);
312 Renderer *CreateGfxTinyGL(int screenW, int screenH, Common::RenderMode renderMode);
313 Renderer *createRenderer(int screenW, int screenH, Common::RenderMode renderMode, bool authenticGraphics);
314 
315 } // End of namespace Freescape
316 
317 #endif // FREESCAPE_GFX_H
Definition: managed_surface.h:51
virtual void flipBuffer()
Definition: gfx.h:77
Definition: surface.h:67
RendererType
Definition: renderer.h:45
Definition: area.h:36
Definition: pixelformat.h:138
RenderMode
Definition: rendermode.h:48
Definition: rect.h:144
Definition: gfx.h:60
Definition: rect.h:45
Definition: gfx.h:47