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 TWP_GFX_H
23 #define TWP_GFX_H
24 
25 #include "common/array.h"
26 #include "common/hashmap.h"
27 #include "common/rect.h"
28 #include "graphics/surface.h"
29 #include "graphics/opengl/shader.h"
30 #include "math/vector2d.h"
31 #include "math/matrix4.h"
32 
33 namespace Twp {
34 
35 struct Color {
36  union {
37  float v[4];
38  struct {
39  float r;
40  float g;
41  float b;
42  float a;
43  } rgba;
44  };
45 
46  Color(float red = 1.0f, float green = 1.0f, float blue = 1.0f, float alpha = 1.0f) {
47  rgba.r = red;
48  rgba.g = green;
49  rgba.b = blue;
50  rgba.a = alpha;
51  }
52 
53  static Color withAlpha(const Color &c, float alpha = 1.0f) {
54  Color result = c;
55  result.rgba.a = alpha;
56  return result;
57  }
58 
59  static Color rgb(int c) {
60  return create((uint8)((c >> 16) & 0xFF), (uint8)((c >> 8) & 0xFF), (uint8)(c & 0xFF), 0xFF);
61  }
62 
63  static Color fromRgba(int c) {
64  return create((uint8)((c >> 16) & 0xFF), (uint8)((c >> 8) & 0xFF), (uint8)(c & 0xFF), (uint8)((c >> 24) & 0xFF));
65  }
66 
67  static Color create(uint8 red, uint8 green, uint8 blue, uint8 alpha = 0xFF) {
68  return Color(red / 255.f, green / 255.f, blue / 255.f, alpha / 255.f);
69  }
70 
71  Common::String toStr() {
72  return Common::String::format("rgba(%f,%f,%f,%f)", rgba.r, rgba.g, rgba.b, rgba.a);
73  }
74 
75  Color operator-(const Color &c) {
76  return Color(rgba.r - c.rgba.r, rgba.g - c.rgba.g, rgba.b - c.rgba.b, rgba.a - c.rgba.a);
77  }
78 
79  Color operator+(const Color &c) {
80  return Color(rgba.r + c.rgba.r, rgba.g + c.rgba.g, rgba.b + c.rgba.b, rgba.a + c.rgba.a);
81  }
82 
83  Color operator*(float f) {
84  return Color(rgba.r * f, rgba.g * f, rgba.b * f, rgba.a * f);
85  }
86 
87  int toInt() const;
88 };
89 
90 // This is a point in 2D with a color and texture coordinates
91 struct Vertex {
92 public:
93  Math::Vector2d pos;
94  Color color;
95  Math::Vector2d texCoords;
96 
97  Vertex();
98  Vertex(const Math::Vector2d &p, const Color &c = Color(), const Math::Vector2d &t = Math::Vector2d());
99 };
100 
101 class Texture {
102 public:
103  Texture() {}
104  virtual ~Texture() {}
105 
106  void load(const Graphics::Surface &surface);
107  static void bind(const Texture *texture);
108  void capture(Common::Array<byte> &data);
109 
110 public:
111  uint id = 0;
112  int width = 0, height = 0;
113  uint fbo = 0;
114 };
115 
116 class RenderTexture : public Texture {
117 public:
118  explicit RenderTexture(const Math::Vector2d &size);
119  ~RenderTexture() override;
120 };
121 
122 struct TextureSlot {
123  int id;
124  Texture texture;
125 };
126 
127 class Shader {
128 public:
129  friend class Gfx;
130 
131 public:
132  Shader();
133  virtual ~Shader();
134 
135  void init(const char *name, const char *vertex, const char *fragment);
136 
137  int getUniformLocation(const char *name) const;
138 
139  void setUniform(const char *name, int value);
140  void setUniform(const char *name, float value);
141  void setUniform(const char *name, float *value, size_t count);
142  void setUniform2(const char *name, float *value, size_t count);
143  void setUniform3(const char *name, float *value, size_t count);
144 
145  void setUniform(const char *name, Math::Vector2d value);
146  void setUniform3(const char *name, Color value);
147  void setUniform4(const char *name, Color value);
148 
149  virtual void applyUniforms() {}
150  virtual int getNumTextures() { return 0; };
151  virtual int getTexture(int index) { return 0; };
152  virtual int getTextureLoc(int index) { return 0; };
153 
154 private:
156  OpenGL::Shader _shader;
157 };
158 
160 
161 class Gfx {
162 public:
163  friend class Shader;
164 
165 public:
166  void init();
167 
168  void camera(const Math::Vector2d &size);
169  Math::Vector2d camera() const;
170  Math::Vector2d cameraPos() const { return _cameraPos; }
171  void cameraPos(const Math::Vector2d &pos) { _cameraPos = pos; }
172 
173  Shader *getShader() { return _shader; }
174  void use(Shader *shader);
175  void setRenderTarget(RenderTexture *target);
176 
177  void clear(const Color &color);
178  void drawPrimitives(uint32 primitivesType, Vertex *vertices, int v_size, const Math::Matrix4 &transf = Math::Matrix4(), Texture *texture = NULL);
179  void drawPrimitives(uint32 primitivesType, Vertex *vertices, int v_size, uint32 *indices, int i_size, const Math::Matrix4 &transf = Math::Matrix4(), Texture *texture = NULL);
180  void drawLines(Vertex *vertices, int count, const Math::Matrix4 &trsf = Math::Matrix4());
181  void drawLinesLoop(Vertex *vertices, int count, const Math::Matrix4 &trsf = Math::Matrix4());
182  void draw(Vertex *vertices, int v_size, uint32 *indices, int i_size, const Math::Matrix4 &trsf = Math::Matrix4(), Texture *texture = NULL);
183  void drawQuad(const Math::Vector2d &size, const Color &color = Color(), const Math::Matrix4 &trsf = Math::Matrix4());
184  void drawSprite(const Common::Rect &textRect, Texture &texture, const Color &color = Color(), const Math::Matrix4 &trsf = Math::Matrix4(), bool flipX = false, bool flipY = false);
185  void drawSprite(Texture &texture, const Color &color = Color(), const Math::Matrix4 &trsf = Math::Matrix4(), bool flipX = false, bool flipY = false);
186 
187 private:
188  Math::Matrix4 getFinalTransform(const Math::Matrix4 &trsf);
189  void noTexture();
190 
191 private:
192  Texture _emptyTexture;
193  uint _vbo = 0, _ebo = 0;
194  Shader _defaultShader;
195  Shader *_shader = nullptr;
196  Math::Matrix4 _mvp;
197  Math::Vector2d _cameraPos;
198  Math::Vector2d _cameraSize;
199  Textures _textures;
200  Texture *_texture = nullptr;
201  int _oldFbo = 0;
202 };
203 } // namespace Twp
204 
205 #endif
Definition: str.h:59
Definition: gfx.h:116
Definition: surface.h:67
static String format(MSVC_PRINTF const char *fmt,...) GCC_PRINTF(1
Definition: rect.h:144
Definition: gfx.h:91
Definition: shader.h:56
Definition: gfx.h:35
Definition: gfx.h:122
Definition: gfx.h:127
Definition: gfx.h:101
Definition: achievements_tables.h:27
Definition: gfx.h:161