VoxelEngine
 
Loading...
Searching...
No Matches
Engine.h
1#pragma once
2
3#include <memory>
4#include <variant>
5#include <vector>
6
7#include "RenderContext.h"
8#include "RenderPassRegistry.h"
9#include "Window.h"
10
11
12namespace engine {
13 class Updateable;
14 class Renderable;
15 class Tickable;
16 class Sun;
17 class InputSystem;
18
19 struct RenderStats {
20 uint32_t drawCalls = 0;
21 uint32_t vertices = 0;
22 uint32_t triangles = 0;
23 double frameTime = 0.0;
24 double _frameStart = 0.0;
25
26 void reset() {
27 drawCalls = 0;
28 vertices = 0;
29 triangles = 0;
30 frameTime = 0.0;
31 }
32
33 std::string toString() const;
34 };
35
36 class Engine {
37 public:
38 static inline float TickRate = 1.0f / 30.0f;
39
40 Engine(std::unique_ptr<Window> window);
41 ~Engine() = default;
42
43 void submitRender(RenderContext&& ctx, bool immediate = false);
44 void submitRender(GroupRenderContext&& ctx, bool immediate = false);
45
49 virtual void flush();
50
51 void subscribeUpdate(std::shared_ptr<Updateable> updateable);
52 void subscribeTick(std::shared_ptr<Tickable> tickable);
53 void fireUpdate(float dt);
54 void gameloop();
55
56 virtual void beforeRender() {};
57 virtual void render(double dt) = 0;
58 virtual void afterRender() {};
59
60 Window* window() const { return m_window.get(); }
61 InputSystem* inputSystem() const { return m_inputSystem.get(); }
62
63 void setDirectionalLightSource(std::shared_ptr<Sun> lightSource, uint8_t passPosition = 0);
64 std::shared_ptr<Sun> directionalLightSource() const { return m_directionalLightSource; }
65
66 protected:
67 std::unique_ptr<InputSystem> m_inputSystem;
68 std::shared_ptr<Sun> m_directionalLightSource;
69
70 RenderPassRegistry* m_passRegistry;
71 std::vector<std::variant<RenderContext, GroupRenderContext>> m_renderQueue;
72 std::unique_ptr<Window> m_window;
73
74 void beginFrame();
75 void endFrame();
76
77 private:
78 void initUtilityShaders();
79
80 std::vector<std::weak_ptr<Updateable>> m_updateSubscribers;
81 std::vector<std::weak_ptr<Tickable>> m_tickSubscribers;
82 float m_tickAccumulator = 0.0f;
83
84
85 void render(RenderContext& ctx, const RenderPass* renderPass) const;
86 void render(GroupRenderContext& ctx, const RenderPass* renderPass) const;
87 mutable RenderStats m_renderStats;
88 };
89} // namespace engine
Definition Engine.h:36
virtual void flush()
Flushes the render queue; loops over all render passes and renders all contexts.
Definition Engine.cpp:114
Definition InputSystem.h:16
Definition RenderPassRegistry.h:9
Definition RenderPass.h:19
Definition Window.h:9
Definition RenderContext.h:82
Definition RenderContext.h:45
Definition Engine.h:19