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 drawCelestialBody(Math::Vector3d position, float radius, uint8 color) {};
106 
107  Common::Rect viewport() const;
108  virtual Common::Point nativeResolution() { return Common::Point(_screenW, _screenH); }
109 
110  // palette
111  void readFromPalette(uint8 index, uint8 &r, uint8 &g, uint8 &b);
112  void setPaletteValue(uint8 index, uint8 r, uint8 g, uint8 b);
113  uint8 indexFromColor(uint8 r, uint8 g, uint8 b);
114  uint8 mapEGAColor(uint8 index);
115 
116  bool getRGBAt(uint8 index, uint8 ecolor, uint8 &r1, uint8 &g1, uint8 &b1, uint8 &r2, uint8 &g2, uint8 &b2, byte *&stipple);
117  bool getRGBAtC64(uint8 index, uint8 &r1, uint8 &g1, uint8 &b1, uint8 &r2, uint8 &g2, uint8 &b2, byte *&stipple);
118  bool getRGBAtCGA(uint8 index, uint8 &r1, uint8 &g1, uint8 &b1, uint8 &r2, uint8 &g2, uint8 &b2, byte *&stipple);
119  bool getRGBAtCPC(uint8 index, uint8 &r1, uint8 &g1, uint8 &b1, uint8 &r2, uint8 &g2, uint8 &b2, byte *&stipple);
120  bool getRGBAtEGA(uint8 index, uint8 &r1, uint8 &g1, uint8 &b1, uint8 &r2, uint8 &g2, uint8 &b2);
121  bool getRGBAtZX(uint8 index, uint8 &r1, uint8 &g1, uint8 &b1, uint8 &r2, uint8 &g2, uint8 &b2, byte *&stipple);
122  bool getRGBAtHercules(uint8 index, uint8 &r1, uint8 &g1, uint8 &b1, uint8 &r2, uint8 &g2, uint8 &b2, byte *&stipple);
123  void extractCPCIndexes(uint8 cm1, uint8 cm2, uint8 &i1, uint8 &i2);
124  void extractC64Indexes(uint8 cm1, uint8 cm2, uint8 &i1, uint8 &i2);
125 
126  void selectColorFromFourColorPalette(uint8 index, uint8 &r1, uint8 &g1, uint8 &b1);
127 
128  // Stipple
129  virtual void setStippleData(byte *data) {};
130  virtual void useStipple(bool enabled) {};
131  void scaleStipplePattern(byte originalPattern[128], byte newPattern[128]);
132 
133  byte _defaultStippleArray[128] = {
134  0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
135  0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
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  };
151 
152  byte *_variableStippleArray;
153 
154  float _skyNormals[16][3] = {
155  { 0.0, 0.0, 1.0 }, //front //0
156  { 0.0, 0.0, 1.0 }, //1
157  { 0.0, 0.0, 1.0 }, //2
158  { 0.0, 0.0, 1.0 }, //3
159  { 0.0, 0.0, -1.0 }, //back //0
160  { 0.0, 0.0, -1.0 }, //1
161  { 0.0, 0.0, -1.0 }, //2
162  { 0.0, 0.0, -1.0 }, //3
163  { -1.0, 0.0, 0.0 }, //left
164  { -1.0, 0.0, 0.0 },
165  { -1.0, 0.0, 0.0 },
166  { -1.0, 0.0, 0.0 },
167  { 1.0, 0.0, 0.0 }, //right
168  { 1.0, 0.0, 0.0 },
169  { 1.0, 0.0, 0.0 },
170  { 1.0, 0.0, 0.0 }
171  };
172 
173  float _skyUvs1008[16][2] = {
174  { 0.0f, 0.0f }, //1
175  { 0.0f, 2.0f }, //2
176  { 0.4f, 2.0f }, //3
177  { 0.4f, 0.0f }, //front //4
178 
179  { 0.0f, 2.0f }, //back //1
180  { 0.4f, 2.0f }, //2
181  { 0.4f, 0.0f }, //3
182  { 0.0f, 0.0f }, //4
183 
184  { 0.0f, 0.0f }, //left //1
185  { 0.4f, 0.0f }, //2
186  { 0.4f, 2.0f }, //3
187  { 0.0f, 2.0f }, //4
188 
189  { 0.4f, 0.0f }, //right //1
190  { 0.0f, 0.0f }, //2
191  { 0.0f, 2.0f }, //3
192  { 0.4f, 2.0f }, //4
193  };
194 
195  float _skyUvs128[16][2] = {
196  { 0.0f, 0.0f }, //1
197  { 0.0f, 2.0f }, //2
198  { 2.5f, 2.0f }, //3
199  { 2.5f, 0.0f }, //front //4
200 
201  { 0.0f, 2.0f }, //back //1
202  { 2.5f, 2.0f }, //2
203  { 2.5f, 0.0f }, //3
204  { 0.0f, 0.0f }, //4
205 
206  { 0.0f, 0.0f }, //left //1
207  { 2.5f, 0.0f }, //2
208  { 2.5f, 2.0f }, //3
209  { 0.0f, 2.0f }, //4
210 
211  { 2.5f, 0.0f }, //right //1
212  { 0.0f, 0.0f }, //2
213  { 0.0f, 2.0f }, //3
214  { 2.5f, 2.0f }, //4
215  };
216 
217  float _skyVertices[16][3] = {
218  { -81280.0, 8128.0, 81280.0 }, //1 // Vertex #0 front
219  { -81280.0, -8128.0, 81280.0 }, //2 // Vertex #1
220  { 81280.0, -8128.0, 81280.0 }, //3 // Vertex #2
221  { 81280.0, 8128.0, 81280.0 }, //4 // Vertex #3
222 
223  { 81280.0f, -8128.0f, -81280.0f }, // 1
224  { -81280.0f, -8128.0f, -81280.0f }, // 2
225  { -81280.0f, 8128.0f, -81280.0f }, // 3
226  { 81280.0f, 8128.0f, -81280.0f }, // 4
227 
228  { -81280.0f, 8128.0f, 81280.0f }, //left //1
229  { -81280.0f, 8128.0f, -81280.0f }, //2
230  { -81280.0f, -8128.0f, -81280.0f }, //3
231  { -81280.0f, -8128.0f, 81280.0f }, //4
232 
233  { 81280.0f, 8128.0f, -81280.0f }, //right //1
234  { 81280.0f, 8128.0f, 81280.0f }, //2
235  { 81280.0f, -8128.0f, 81280.0f },//3
236  { 81280.0f, -8128.0f, -81280.0f },//4
237  };
238 
239  unsigned int _skyIndices[24] = {
240  0, 1, 2, // front
241  0, 2, 3,
242 
243  4, 5, 6, // back
244  4, 6, 7,
245 
246  8, 9, 10, // left
247  8, 10, 11,
248 
249  12, 13, 14, // right
250  12, 14, 15,
251  };
252 
253  byte *_palette;
254  void setColorMap(ColorMap *colorMap_);
255  ColorMap *_colorMap;
256  ColorReMap *_colorRemaps;
257  void clearColorPairArray();
258  void fillColorPairArray();
259  byte _colorPair[16];
260  int _keyColor;
261  int _inkColor;
262  int _paperColor;
263  int _underFireBackgroundColor;
264  Common::Point _shakeOffset;
265  byte _stipples[16][128];
266 
267  int _scale;
268 
275  virtual void positionCamera(const Math::Vector3d &pos, const Math::Vector3d &interest, float rollAngle = 0.0f) = 0;
276  virtual void updateProjectionMatrix(float fov, float aspectRatio, float nearClipPlane, float farClipPlane) = 0;
277 
278  Math::Matrix4 getMvpMatrix() const { return _mvpMatrix; }
279  virtual Graphics::Surface *getScreenshot() = 0;
280  void flipVertical(Graphics::Surface *s);
281 
282  int _screenW;
283  int _screenH;
284  Common::RenderMode _renderMode;
285 
286  bool computeScreenViewport();
287 
288 protected:
289  Common::Rect _screenViewport;
290  Common::Rect _viewport;
291  Common::Rect _unscaledViewport;
292 
293  Math::Matrix4 _projectionMatrix;
294  Math::Matrix4 _modelViewMatrix;
295  Math::Matrix4 _mvpMatrix;
296 
297  Math::Frustum _frustum;
298 
299  Math::Matrix4 makeProjectionMatrix(float fov, float nearClipPlane, float farClipPlane) const;
300 };
301 
302 Graphics::RendererType determinateRenderType();
303 Renderer *CreateGfxOpenGL(int screenW, int screenH, Common::RenderMode renderMode, bool authenticGraphics);
304 Renderer *CreateGfxOpenGLShader(int screenW, int screenH, Common::RenderMode renderMode, bool authenticGraphics);
305 Renderer *CreateGfxTinyGL(int screenW, int screenH, Common::RenderMode renderMode);
306 Renderer *createRenderer(int screenW, int screenH, Common::RenderMode renderMode, bool authenticGraphics);
307 
308 } // End of namespace Freescape
309 
310 #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