Mountain  1.0.0
Simple C++ 2D Game Framework
draw.hpp
1 #pragma once
2 
3 #include "Mountain/core.hpp"
4 
5 #include <magic_enum/magic_enum.hpp>
6 
7 #include <Maths/vector2.hpp>
8 
9 #include "Mountain/rendering/gpu_buffer.hpp"
10 #include "Mountain/rendering/gpu_vertex_array.hpp"
11 #include "Mountain/rendering/render_target.hpp"
14 #include "Mountain/utils/color.hpp"
15 #include "Mountain/utils/list.hpp"
17 #include "Mountain/utils/rectangle.hpp"
18 
19 namespace Mountain
20 {
21  enum class DrawTextureFlipping : uint8_t
22  {
23  None = 0,
24 
25  Horizontal = 1 << 0,
26  Vertical = 1 << 1,
27  Diagonal = 1 << 2,
28  AntiDiagonal = 1 << 3
29  };
30 
32  class Draw
33  {
34  STATIC_CLASS(Draw)
35 
36  public:
39  MOUNTAIN_API static void Clear(const Color& color);
40 
41  MOUNTAIN_API static void Point(Vector2 position, const Color& color = Color::White());
42 
47  MOUNTAIN_API static void Line(Vector2 point1, Vector2 point2, const Color& color = Color::White());
53  MOUNTAIN_API static void Line(Vector2 point1, Vector2 point2, const Color& color1, const Color& color2);
54 
60  MOUNTAIN_API static void Triangle(Vector2 point1, Vector2 point2, Vector2 point3, const Color& color = Color::White());
68  MOUNTAIN_API static void Triangle(Vector2 point1, Vector2 point2, Vector2 point3, const Color& color1, const Color& color2, const Color& color3);
74  MOUNTAIN_API static void TriangleFilled(Vector2 point1, Vector2 point2, Vector2 point3, const Color& color = Color::White());
82  MOUNTAIN_API static void TriangleFilled(Vector2 point1, Vector2 point2, Vector2 point3, const Color& color1, const Color& color2, const Color& color3);
83 
88  MOUNTAIN_API static void Rectangle(Vector2 position, Vector2 size, const Color& color = Color::White());
92  MOUNTAIN_API static void Rectangle(const Mountain::Rectangle& rectangle, const Color& color = Color::White());
97  MOUNTAIN_API static void RectangleFilled(Vector2 position, Vector2 size, const Color& color = Color::White());
101  MOUNTAIN_API static void RectangleFilled(const Mountain::Rectangle& rectangle, const Color& color = Color::White());
102 
108  MOUNTAIN_API static void Circle(Vector2 center, float_t radius, Vector2 scale = Vector2::One(), const Color& color = Color::White());
109 
115  MOUNTAIN_API static void CircleFilled(Vector2 center, float_t radius, Vector2 scale = Vector2::One(), const Color& color = Color::White());
116 
124  MOUNTAIN_API static void Arc(Vector2 center, float_t radius, float_t startingAngle, float_t deltaAngle, Vector2 scale = Vector2::One(), const Color& color = Color::White());
125 
133  MOUNTAIN_API static void ArcFilled(Vector2 center, float_t radius, float_t startingAngle, float_t deltaAngle, Vector2 scale = Vector2::One(), const Color& color = Color::White());
134 
146  MOUNTAIN_API static void Texture(
147  const Texture& texture,
148  Vector2 position,
149  Vector2 scale = Vector2::One(),
150  float_t rotation = 0.f,
151  Vector2 origin = Vector2::Zero(),
152  Vector2 uv0 = Vector2::Zero(),
153  Vector2 uv1 = Vector2::One(),
154  const Color& color = Color::White(),
155  Meta::UnderlyingEnumType<DrawTextureFlipping> flipFlags = static_cast<Meta::UnderlyingEnumType<DrawTextureFlipping>>(DrawTextureFlipping::None)
156  );
157 
164  MOUNTAIN_API static void Text(const Font& font, const std::string& text, Vector2 position, float_t scale = 1.f, const Color& color = Color::White());
165 
176  MOUNTAIN_API static void RenderTarget(
177  const RenderTarget& renderTarget,
178  Vector2 position = Vector2::Zero(),
179  Vector2 scale = Vector2::One(),
180  float_t rotation = 0,
181  Vector2 uv0 = Vector2::Zero(),
182  Vector2 uv1 = Vector2::One(),
183  const Color& color = Color::White(),
184  Meta::UnderlyingEnumType<DrawTextureFlipping> flipFlags = static_cast<Meta::UnderlyingEnumType<DrawTextureFlipping>>(DrawTextureFlipping::None)
185  );
186 
190  MOUNTAIN_API static void Flush();
191 
192  private:
193  struct PointData
194  {
195  Vector2 position;
196  Color color;
197  };
198 
199  struct LineData
200  {
201  Vector2 p1, p2;
202  Color color;
203  };
204 
205  struct LineColoredData
206  {
207  Vector2 p1, p2;
208  Color c1, c2;
209  };
210 
211  struct TriangleData
212  {
213  Vector2 p1, p2, p3;
214  Color color;
215  };
216 
217  struct TriangleColoredData
218  {
219  Vector2 p1, p2, p3;
220  Color c1, c2, c3;
221  };
222 
223  struct RectangleData
224  {
225  Matrix transformation;
226  Color color;
227  };
228 
229  struct CircleData
230  {
231  Matrix transformation;
232  Vector2 center;
233  float_t radius;
234  Vector2 scale;
235  Color color;
236  int32_t filled; // 32-bit boolean for GLSL
237  };
238 
239  struct ArcData
240  {
241  Matrix transformation;
242  Vector2 center;
243  float_t radius;
244  float_t startingAngle;
245  float_t deltaAngle;
246  Vector2 scale;
247  Color color;
248  int32_t filled; // 32-bit boolean for GLSL
249  };
250 
251  struct TextureData
252  {
253  Matrix transformation, uvProjection;
254  Color color;
255  };
256 
257  struct TextData
258  {
259  const Font* font;
260  std::string text;
261  Vector2 position;
262  float_t scale;
263  Color color;
264  };
265 
266  struct RenderTargetData
267  {
268  const Mountain::RenderTarget* renderTarget;
269  Matrix transformation, uvProjection;
270  Vector2 scale;
271  Color color;
272  };
273 
274  enum class DrawDataType : uint8_t
275  {
276  Point,
277  Line,
278  LineColored,
279  Triangle,
280  TriangleColored,
281  TriangleFilled,
282  TriangleColoredFilled,
283  Rectangle,
284  RectangleFilled,
285  Circle,
286  Arc,
287  Texture,
288  Text,
289  RenderTarget
290  };
291 
292  struct CommandData
293  {
294  DrawDataType type;
295  size_t count;
296  };
297 
298  class DrawList
299  {
300  public:
301  List<PointData> point;
302  List<LineData> line;
303  List<LineColoredData> lineColored;
304  List<TriangleData> triangle;
305  List<TriangleColoredData> triangleColored;
306  List<TriangleData> triangleFilled;
307  List<TriangleColoredData> triangleColoredFilled;
308  List<RectangleData> rectangle;
309  List<RectangleData> rectangleFilled;
310  List<CircleData> circle;
311  List<ArcData> arc;
312  List<TextureData> texture;
313  List<uint32_t> textureId;
314  List<TextData> text;
315  List<RenderTargetData> renderTarget;
316 
317  List<CommandData> commands;
318 
319  void AddCommand(DrawDataType type);
320  void Clear();
321  };
322 
323  static inline Pointer<Shader> m_PointShader;
324  static inline Pointer<Shader> m_LineShader;
325  static inline Pointer<Shader> m_LineColoredShader;
326  static inline Pointer<Shader> m_TriangleShader;
327  static inline Pointer<Shader> m_TriangleColoredShader;
328  static inline Pointer<Shader> m_RectangleShader;
329  static inline Pointer<Shader> m_CircleShader;
330  static inline Pointer<Shader> m_ArcShader;
331 
332  static inline Pointer<Shader> m_TextureShader, m_TextShader, m_RenderTargetShader;
333 
334  static inline Graphics::GpuBuffer m_RectangleEbo, m_Vbo, m_RectangleVbo, m_TextureVbo, m_TextVbo, m_RenderTargetVbo, m_RenderTargetSsbo;
335  static inline Graphics::GpuVertexArray m_PointVao, m_LineVao, m_LineColoredVao, m_TriangleVao, m_TriangleColoredVao, m_RectangleVao, m_CircleVao, m_ArcVao, m_TextureVao, m_TextVao, m_RenderTargetVao;
336 
337  static inline Matrix m_ProjectionMatrix;
338 
339  static inline Matrix m_CameraMatrix = Matrix::Identity();
340  static inline Vector2 m_CameraScale = Vector2::One();
341 
342  static inline DrawList m_DrawList;
343 
344  static void Initialize();
345  static void LoadResources();
346  static void Shutdown();
347 
348  static void InitializePointBuffers();
349  static void InitializeLineBuffers();
350  static void InitializeLineColoredBuffers();
351  static void InitializeTriangleBuffers();
352  static void InitializeTriangleColoredBuffers();
353  static void InitializeRectangleBuffers();
354  static void InitializeCircleBuffers();
355  static void InitializeArcBuffers();
356  static void InitializeTextureBuffers();
357  static void InitializeTextBuffers();
358  static void InitializeRenderTargetBuffers();
359 
360  static void SetProjectionMatrix(const Matrix& newProjectionMatrix);
361  static void SetCamera(const Matrix& newCameraMatrix, Vector2 newCameraScale);
362  static void UpdateShaderMatrices();
363 
364  static void CircleInternal(Vector2 center, float_t radius, bool_t filled, Vector2 scale, const Color& color);
365  static void ArcInternal(Vector2 center, float_t radius, float_t startingAngle, float_t deltaAngle, bool_t filled, Vector2 scale, const Color& color);
366 
367  static void RenderPointData(const List<PointData>& points, size_t index, size_t count);
368  static void RenderLineData(const List<LineData>& lines, size_t index, size_t count);
369  static void RenderLineColoredData(const List<LineColoredData>& linesColored, size_t index, size_t count);
370  static void RenderTriangleData(const List<TriangleData>& triangles, bool_t filled, size_t index, size_t count);
371  static void RenderTriangleColoredData(const List<TriangleColoredData>& trianglesColored, bool_t filled, size_t index, size_t count);
372  static void RenderRectangleData(const List<RectangleData>& rectangles, bool_t filled, size_t index, size_t count);
373  static void RenderCircleData(const List<CircleData>& circles, size_t index, size_t count);
374  static void RenderArcData(const List<ArcData>& arcs, size_t index, size_t count);
375  static void RenderTextureData(const List<TextureData>& textures, uint32_t textureId, size_t index, size_t count);
376  static void RenderTextData(const List<TextData>& texts, size_t index, size_t count);
377  static void RenderRenderTargetData(const List<RenderTargetData>& renderTargets, size_t index, size_t count);
378 
379  friend class Renderer;
380  friend class RenderTarget;
381  friend class ParticleSystem;
382  };
383 }
384 
385 ENUM_FLAGS(Mountain::DrawTextureFlipping)
Defines the Mountain::List class.
Defines the Mountain::Shader class.
static constexpr Vector2 Zero() noexcept
Defines multiple color structs.
Represents an image in memory.
Definition: texture.hpp:18
Custom Mountain smart pointer. Represents both a std::shared_ptr and a std::weak_ptr.
Defines the Mountain::Texture class.
static constexpr Vector2 One() noexcept
static constexpr Color White()
Constant for White.
Defines utilities for meta programming and template manipulation.
The Color struct represents a color in RGBA color space.
Definition: color.hpp:26
Low-level interface for OpenGL vertex arrays.
Holds the necessary information to draw text using a Font.
Definition: font.hpp:15
Low-level interface for OpenGL buffers.
Definition: gpu_buffer.hpp:12
The Draw class contains static functions to draw various things on screen.
Definition: draw.hpp:32
static constexpr Matrix Identity() noexcept
Contains all declarations of the Mountain Framework.
Definition: audio.hpp:22