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, byte *&stipple);
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 *_variableStippleArray;
155 
156  float _skyNormals[16][3] = {
157  { 0.0, 0.0, 1.0 }, //front //0
158  { 0.0, 0.0, 1.0 }, //1
159  { 0.0, 0.0, 1.0 }, //2
160  { 0.0, 0.0, 1.0 }, //3
161  { 0.0, 0.0, -1.0 }, //back //0
162  { 0.0, 0.0, -1.0 }, //1
163  { 0.0, 0.0, -1.0 }, //2
164  { 0.0, 0.0, -1.0 }, //3
165  { -1.0, 0.0, 0.0 }, //left
166  { -1.0, 0.0, 0.0 },
167  { -1.0, 0.0, 0.0 },
168  { -1.0, 0.0, 0.0 },
169  { 1.0, 0.0, 0.0 }, //right
170  { 1.0, 0.0, 0.0 },
171  { 1.0, 0.0, 0.0 },
172  { 1.0, 0.0, 0.0 }
173  };
174 
175  float _skyUvs1008[16][2] = {
176  { 0.0f, 0.0f }, //1
177  { 0.0f, 2.0f }, //2
178  { 0.4f, 2.0f }, //3
179  { 0.4f, 0.0f }, //front //4
180 
181  { 0.0f, 2.0f }, //back //1
182  { 0.4f, 2.0f }, //2
183  { 0.4f, 0.0f }, //3
184  { 0.0f, 0.0f }, //4
185 
186  { 0.0f, 0.0f }, //left //1
187  { 0.4f, 0.0f }, //2
188  { 0.4f, 2.0f }, //3
189  { 0.0f, 2.0f }, //4
190 
191  { 0.4f, 0.0f }, //right //1
192  { 0.0f, 0.0f }, //2
193  { 0.0f, 2.0f }, //3
194  { 0.4f, 2.0f }, //4
195  };
196 
197  float _skyUvs128[16][2] = {
198  { 0.0f, 0.0f }, //1
199  { 0.0f, 2.0f }, //2
200  { 2.5f, 2.0f }, //3
201  { 2.5f, 0.0f }, //front //4
202 
203  { 0.0f, 2.0f }, //back //1
204  { 2.5f, 2.0f }, //2
205  { 2.5f, 0.0f }, //3
206  { 0.0f, 0.0f }, //4
207 
208  { 0.0f, 0.0f }, //left //1
209  { 2.5f, 0.0f }, //2
210  { 2.5f, 2.0f }, //3
211  { 0.0f, 2.0f }, //4
212 
213  { 2.5f, 0.0f }, //right //1
214  { 0.0f, 0.0f }, //2
215  { 0.0f, 2.0f }, //3
216  { 2.5f, 2.0f }, //4
217  };
218 
219  float _skyVertices[16][3] = {
220  { -81280.0, 8128.0, 81280.0 }, //1 // Vertex #0 front
221  { -81280.0, -8128.0, 81280.0 }, //2 // Vertex #1
222  { 81280.0, -8128.0, 81280.0 }, //3 // Vertex #2
223  { 81280.0, 8128.0, 81280.0 }, //4 // Vertex #3
224 
225  { 81280.0f, -8128.0f, -81280.0f }, // 1
226  { -81280.0f, -8128.0f, -81280.0f }, // 2
227  { -81280.0f, 8128.0f, -81280.0f }, // 3
228  { 81280.0f, 8128.0f, -81280.0f }, // 4
229 
230  { -81280.0f, 8128.0f, 81280.0f }, //left //1
231  { -81280.0f, 8128.0f, -81280.0f }, //2
232  { -81280.0f, -8128.0f, -81280.0f }, //3
233  { -81280.0f, -8128.0f, 81280.0f }, //4
234 
235  { 81280.0f, 8128.0f, -81280.0f }, //right //1
236  { 81280.0f, 8128.0f, 81280.0f }, //2
237  { 81280.0f, -8128.0f, 81280.0f },//3
238  { 81280.0f, -8128.0f, -81280.0f },//4
239  };
240 
241  unsigned int _skyIndices[24] = {
242  0, 1, 2, // front
243  0, 2, 3,
244 
245  4, 5, 6, // back
246  4, 6, 7,
247 
248  8, 9, 10, // left
249  8, 10, 11,
250 
251  12, 13, 14, // right
252  12, 14, 15,
253  };
254 
255  byte *_palette;
256  void setColorMap(ColorMap *colorMap_);
257  ColorMap *_colorMap;
258  ColorReMap *_colorRemaps;
259  void clearColorPairArray();
260  void fillColorPairArray();
261  byte _colorPair[16];
262  int _keyColor;
263  int _inkColor;
264  int _paperColor;
265  int _underFireBackgroundColor;
266  Common::Point _shakeOffset;
267  byte _stipples[16][128];
268 
269  int _scale;
270 
277  virtual void positionCamera(const Math::Vector3d &pos, const Math::Vector3d &interest) = 0;
278  virtual void updateProjectionMatrix(float fov, float aspectRatio, float nearClipPlane, float farClipPlane) = 0;
279 
280  Math::Matrix4 getMvpMatrix() const { return _mvpMatrix; }
281  virtual Graphics::Surface *getScreenshot() = 0;
282  void flipVertical(Graphics::Surface *s);
283 
284  int _screenW;
285  int _screenH;
286  Common::RenderMode _renderMode;
287 
288  bool computeScreenViewport();
289 
290 protected:
291  Common::Rect _screenViewport;
292  Common::Rect _viewport;
293  Common::Rect _unscaledViewport;
294 
295  Math::Matrix4 _projectionMatrix;
296  Math::Matrix4 _modelViewMatrix;
297  Math::Matrix4 _mvpMatrix;
298 
299  Math::Frustum _frustum;
300 
301  Math::Matrix4 makeProjectionMatrix(float fov, float nearClipPlane, float farClipPlane) const;
302 };
303 
304 Graphics::RendererType determinateRenderType();
305 Renderer *CreateGfxOpenGL(int screenW, int screenH, Common::RenderMode renderMode, bool authenticGraphics);
306 Renderer *CreateGfxOpenGLShader(int screenW, int screenH, Common::RenderMode renderMode, bool authenticGraphics);
307 Renderer *CreateGfxTinyGL(int screenW, int screenH, Common::RenderMode renderMode);
308 Renderer *createRenderer(int screenW, int screenH, Common::RenderMode renderMode, bool authenticGraphics);
309 
310 } // End of namespace Freescape
311 
312 #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