1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
#pragma once
#include "vertex_array.h"
#include "shader.h"
#include <glm/glm.hpp>
#include <memory>
namespace Donut
{
template<typename T>
using Scope = std::unique_ptr<T>;
template<typename T, typename ... Args>
constexpr Scope<T> create_scope(Args&& ... args)
{
return std::make_unique<T>(std::forward<Args>(args)...);
}
class RendererAPI
{
public:
enum class API
{
None = 0,
OpenGL = 1,
Vulkan = 2,
};
public:
virtual ~RendererAPI() = default;
virtual void init() = 0;
virtual void set_viewport(uint32_t x, uint32_t y, uint32_t width, uint32_t height) = 0;
virtual void set_clear_color(const glm::vec4& color) = 0;
virtual void clear() = 0;
virtual void draw_indexed(const std::shared_ptr<VertexArray>& vertex_array, uint32_t index_count = 0) = 0;
inline static API get_api() { return s_api; }
static Scope<RendererAPI> create();
private:
static API s_api;
};
class RenderCommand
{
public:
inline static void init() {
s_renderer_api->init();
}
inline static void set_viewport(uint32_t x, uint32_t y, uint32_t width, uint32_t height) {
s_renderer_api->set_viewport(x, y, width, height);
}
inline static void set_clear_color(const glm::vec4& color) {
s_renderer_api->set_clear_color(color);
}
inline static void clear() {
s_renderer_api->clear();
}
inline static void draw_indexed(const std::shared_ptr<VertexArray>& vertex_array, uint32_t index_count = 0) {
s_renderer_api->draw_indexed(vertex_array, index_count);
}
private:
static Scope<RendererAPI> s_renderer_api;
};
class Renderer
{
public:
static void init();
static void shutdown();
static void on_window_resize(uint32_t width, uint32_t height);
static void begin_scene();
static void end_scene();
static void submit(const std::shared_ptr<Shader>& shader,
const std::shared_ptr<VertexArray>& vertex_array,
const glm::mat4& transform = glm::mat4(1.0f));
static void set_clear_color(const glm::vec4& color);
static void clear();
inline static RendererAPI::API get_api() { return RendererAPI::get_api(); }
private:
struct SceneData
{
glm::mat4 ViewProjectionMatrix;
};
static SceneData* s_scene_data;
};
};
|