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 _texturePixelFormat;
66  bool _isAccelerated;
67  bool _authenticGraphics;
68 
69  virtual void init() = 0;
70  virtual void setViewport(const Common::Rect &rect) = 0;
71 
75  virtual void flipBuffer() {}
76  virtual void useColor(uint8 r, uint8 g, uint8 b) = 0;
77  virtual void depthTesting(bool enabled) {};
78  virtual void polygonOffset(bool enabled) = 0;
79 
80  virtual Texture *createTexture(const Graphics::Surface *surface, bool is3D = false) = 0;
81  Graphics::Surface *convertImageFormatIfNecessary(Graphics::ManagedSurface *surface);
82 
83  virtual void freeTexture(Texture *texture) = 0;
84  virtual void drawTexturedRect2D(const Common::Rect &screenRect, const Common::Rect &textureRect, Texture *texture) = 0;
85 
86  virtual void renderSensorShoot(byte color, const Math::Vector3d sensor, const Math::Vector3d player, const Common::Rect &viewPort) = 0;
87  virtual void renderPlayerShootBall(byte color, const Common::Point &position, int frame, const Common::Rect &viewPort) = 0;
88  virtual void renderPlayerShootRay(byte color, const Common::Point &position, const Common::Rect &viewPort) = 0;
89 
90  virtual void renderCrossair(const Common::Point &crossairPosition) = 0;
91 
92  virtual void renderCube(const Math::Vector3d &position, const Math::Vector3d &size, Common::Array<uint8> *colours, Common::Array<uint8> *ecolours, float offset = 0.0);
93  virtual void renderRectangle(const Math::Vector3d &position, const Math::Vector3d &size, Common::Array<uint8> *colours, Common::Array<uint8> *ecolours, float offset = 0.0);
94  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);
95  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);
96  virtual void renderFace(const Common::Array<Math::Vector3d> &vertices) = 0;
97 
98  void setColorRemaps(ColorReMap *colorRemaps);
99  virtual void clear(uint8 r, uint8 g, uint8 b, bool ignoreViewport = false) = 0;
100  virtual void drawFloor(uint8 color) = 0;
101  virtual void drawBackground(uint8 color);
102 
103  void drawEclipse(uint8 color1, uint8 color2, float difference);
104  virtual void drawSkybox(Texture *texture, Math::Vector3d camera) {};
105  virtual void drawThunder(Texture *texture, Math::Vector3d camera, float size) {};
106  virtual void drawCelestialBody(Math::Vector3d position, float radius, uint8 color) {};
107 
108  Common::Rect viewport() const;
109  virtual Common::Point nativeResolution() { return Common::Point(_screenW, _screenH); }
110 
111  // palette
112  void readFromPalette(uint8 index, uint8 &r, uint8 &g, uint8 &b);
113  void setPaletteValue(uint8 index, uint8 r, uint8 g, uint8 b);
114  uint8 indexFromColor(uint8 r, uint8 g, uint8 b);
115  uint8 mapEGAColor(uint8 index);
116 
117  bool getRGBAt(uint8 index, uint8 ecolor, uint8 &r1, uint8 &g1, uint8 &b1, uint8 &r2, uint8 &g2, uint8 &b2, byte *&stipple);
118  bool getRGBAtC64(uint8 index, uint8 &r1, uint8 &g1, uint8 &b1, uint8 &r2, uint8 &g2, uint8 &b2, byte *&stipple);
119  bool getRGBAtCGA(uint8 index, uint8 &r1, uint8 &g1, uint8 &b1, uint8 &r2, uint8 &g2, uint8 &b2, byte *&stipple);
120  bool getRGBAtCPC(uint8 index, uint8 &r1, uint8 &g1, uint8 &b1, uint8 &r2, uint8 &g2, uint8 &b2, byte *&stipple);
121  bool getRGBAtEGA(uint8 index, uint8 &r1, uint8 &g1, uint8 &b1, uint8 &r2, uint8 &g2, uint8 &b2);
122  bool getRGBAtZX(uint8 index, uint8 &r1, uint8 &g1, uint8 &b1, uint8 &r2, uint8 &g2, uint8 &b2, byte *&stipple);
123  bool getRGBAtHercules(uint8 index, uint8 &r1, uint8 &g1, uint8 &b1, uint8 &r2, uint8 &g2, uint8 &b2, byte *&stipple);
124  void extractCPCIndexes(uint8 cm1, uint8 cm2, uint8 &i1, uint8 &i2);
125  void extractC64Indexes(uint8 cm1, uint8 cm2, uint8 &i1, uint8 &i2);
126 
127  void selectColorFromFourColorPalette(uint8 index, uint8 &r1, uint8 &g1, uint8 &b1);
128 
129  // Stipple
130  virtual void setStippleData(byte *data) {};
131  virtual void useStipple(bool enabled) {};
132  void scaleStipplePattern(byte originalPattern[128], byte newPattern[128]);
133 
134  byte _defaultStippleArray[128] = {
135  0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
136  0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
137  0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
138  0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
139  0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
140  0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
141  0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
142  0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
143  0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
144  0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
145  0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
146  0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
147  0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
148  0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
149  0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
150  0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
151  };
152 
153  byte *_variableStippleArray;
154 
155  float _skyNormals[16][3] = {
156  { 0.0, 0.0, 1.0 }, //front //0
157  { 0.0, 0.0, 1.0 }, //1
158  { 0.0, 0.0, 1.0 }, //2
159  { 0.0, 0.0, 1.0 }, //3
160  { 0.0, 0.0, -1.0 }, //back //0
161  { 0.0, 0.0, -1.0 }, //1
162  { 0.0, 0.0, -1.0 }, //2
163  { 0.0, 0.0, -1.0 }, //3
164  { -1.0, 0.0, 0.0 }, //left
165  { -1.0, 0.0, 0.0 },
166  { -1.0, 0.0, 0.0 },
167  { -1.0, 0.0, 0.0 },
168  { 1.0, 0.0, 0.0 }, //right
169  { 1.0, 0.0, 0.0 },
170  { 1.0, 0.0, 0.0 },
171  { 1.0, 0.0, 0.0 }
172  };
173 
174  float _skyUvs1008[16][2] = {
175  { 0.0f, 0.0f }, //1
176  { 0.0f, 2.0f }, //2
177  { 0.4f, 2.0f }, //3
178  { 0.4f, 0.0f }, //front //4
179 
180  { 0.0f, 2.0f }, //back //1
181  { 0.4f, 2.0f }, //2
182  { 0.4f, 0.0f }, //3
183  { 0.0f, 0.0f }, //4
184 
185  { 0.0f, 0.0f }, //left //1
186  { 0.4f, 0.0f }, //2
187  { 0.4f, 2.0f }, //3
188  { 0.0f, 2.0f }, //4
189 
190  { 0.4f, 0.0f }, //right //1
191  { 0.0f, 0.0f }, //2
192  { 0.0f, 2.0f }, //3
193  { 0.4f, 2.0f }, //4
194  };
195 
196  float _skyUvs128[16][2] = {
197  { 0.0f, 0.0f }, //1
198  { 0.0f, 2.0f }, //2
199  { 2.5f, 2.0f }, //3
200  { 2.5f, 0.0f }, //front //4
201 
202  { 0.0f, 2.0f }, //back //1
203  { 2.5f, 2.0f }, //2
204  { 2.5f, 0.0f }, //3
205  { 0.0f, 0.0f }, //4
206 
207  { 0.0f, 0.0f }, //left //1
208  { 2.5f, 0.0f }, //2
209  { 2.5f, 2.0f }, //3
210  { 0.0f, 2.0f }, //4
211 
212  { 2.5f, 0.0f }, //right //1
213  { 0.0f, 0.0f }, //2
214  { 0.0f, 2.0f }, //3
215  { 2.5f, 2.0f }, //4
216  };
217 
218  float _skyVertices[16][3] = {
219  { -81280.0, 8128.0, 81280.0 }, //1 // Vertex #0 front
220  { -81280.0, -8128.0, 81280.0 }, //2 // Vertex #1
221  { 81280.0, -8128.0, 81280.0 }, //3 // Vertex #2
222  { 81280.0, 8128.0, 81280.0 }, //4 // Vertex #3
223 
224  { 81280.0f, -8128.0f, -81280.0f }, // 1
225  { -81280.0f, -8128.0f, -81280.0f }, // 2
226  { -81280.0f, 8128.0f, -81280.0f }, // 3
227  { 81280.0f, 8128.0f, -81280.0f }, // 4
228 
229  { -81280.0f, 8128.0f, 81280.0f }, //left //1
230  { -81280.0f, 8128.0f, -81280.0f }, //2
231  { -81280.0f, -8128.0f, -81280.0f }, //3
232  { -81280.0f, -8128.0f, 81280.0f }, //4
233 
234  { 81280.0f, 8128.0f, -81280.0f }, //right //1
235  { 81280.0f, 8128.0f, 81280.0f }, //2
236  { 81280.0f, -8128.0f, 81280.0f },//3
237  { 81280.0f, -8128.0f, -81280.0f },//4
238  };
239 
240  unsigned int _skyIndices[24] = {
241  0, 1, 2, // front
242  0, 2, 3,
243 
244  4, 5, 6, // back
245  4, 6, 7,
246 
247  8, 9, 10, // left
248  8, 10, 11,
249 
250  12, 13, 14, // right
251  12, 14, 15,
252  };
253 
254  byte *_palette;
255  void setColorMap(ColorMap *colorMap_);
256  ColorMap *_colorMap;
257  ColorReMap *_colorRemaps;
258  void clearColorPairArray();
259  void fillColorPairArray();
260  byte _colorPair[16];
261  int _keyColor;
262  int _inkColor;
263  int _paperColor;
264  int _underFireBackgroundColor;
265  Common::Point _shakeOffset;
266  byte _stipples[16][128];
267 
268  int _scale;
269 
276  virtual void positionCamera(const Math::Vector3d &pos, const Math::Vector3d &interest, float rollAngle = 0.0f) = 0;
277  virtual void updateProjectionMatrix(float fov, float aspectRatio, float nearClipPlane, float farClipPlane) = 0;
278 
279  Math::Matrix4 getMvpMatrix() const { return _mvpMatrix; }
280  virtual Graphics::Surface *getScreenshot() = 0;
281  void flipVertical(Graphics::Surface *s);
282 
283  int _screenW;
284  int _screenH;
285  Common::RenderMode _renderMode;
286 
287  bool computeScreenViewport();
288 
289 protected:
290  Common::Rect _screenViewport;
291  Common::Rect _viewport;
292  Common::Rect _unscaledViewport;
293 
294  Math::Matrix4 _projectionMatrix;
295  Math::Matrix4 _modelViewMatrix;
296  Math::Matrix4 _mvpMatrix;
297 
298  Math::Frustum _frustum;
299 
300  Math::Matrix4 makeProjectionMatrix(float fov, float nearClipPlane, float farClipPlane) const;
301 };
302 
303 Graphics::RendererType determinateRenderType();
304 Renderer *CreateGfxOpenGL(int screenW, int screenH, Common::RenderMode renderMode, bool authenticGraphics);
305 Renderer *CreateGfxOpenGLShader(int screenW, int screenH, Common::RenderMode renderMode, bool authenticGraphics);
306 Renderer *CreateGfxTinyGL(int screenW, int screenH, Common::RenderMode renderMode);
307 Renderer *createRenderer(int screenW, int screenH, Common::RenderMode renderMode, bool authenticGraphics);
308 
309 } // End of namespace Freescape
310 
311 #endif // FREESCAPE_GFX_H
Definition: managed_surface.h:51
virtual void flipBuffer()
Definition: gfx.h:75
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:524
Definition: gfx.h:60
Definition: rect.h:144
Definition: gfx.h:47