Ember
A C++ 20 'game engine' built with SDL3 with wide platform support.
engine.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "core/engine_config.h"
4 #include "core/io/file_system.h"
7 #include "systems/time_manager.h"
8 
9 #pragma region ENGINE_SYSTEMS
12 #pragma endregion
13 #include <random>
14 
15 class Renderer;
16 class OpenglShader;
17 class OpenglRenderer;
18 
19 
20 constexpr float PIXELS_PER_METER = 32.0f;
21 constexpr float METERS_PER_PIXEL = 1.0f / PIXELS_PER_METER;
22 
28 class Engine {
29 public:
30  Engine();
31 
32  struct {
33  float width = 0;
34  float height = 0;
35  const SDL_DisplayMode* data = nullptr;
36  int bbWidth = 0, bbHeight = 0; // backbuffer
37  SDL_Window* handle{};
38  } Window;
39 
41 
42  struct {
43  SDL_AudioDeviceID device_id = 0;
44  SDL_AudioSpec spec{};
45  float global_volume = 1.f;
46  } Audio;
47 
49 
50  bool is_running = false;
51 
58  void resize_window(int w, int h);
59 
65  [[nodiscard]] Renderer* get_renderer() const;
66 
72  [[nodiscard]] InputManager* input_manager() const;
73 
79  [[nodiscard]] TimeManager* time_manager() const;
80 
86  void shutdown();
87 
88 
96  void set_vsync(bool enabled);
97 
115  bool initialize(int width, int height, Backend type, Uint64 flags = 0);
116 
124  void update(double delta_time = 0);
125 
131  [[nodiscard]] b2WorldId get_physics_world() const;
132 
140  template <typename T>
141  T* get_system();
142 
143 private:
144  double _physics_accumulator = 0.0;
145 
146  std::vector<std::unique_ptr<EngineManager>> _systems{};
147 
148  Renderer* _renderer = nullptr;
149  InputManager* _input_manager = nullptr;
150  TimeManager* _time_manager = nullptr;
151 
152  b2WorldId _world;
153 
165  Renderer* _create_renderer_internal(SDL_Window* window, int view_width, int view_height, Backend type);
166 
177  Renderer* _create_renderer_gl(SDL_Window* window, int view_width, int view_height);
178 
189  Renderer* _create_renderer_metal(SDL_Window* window, int view_width, int view_height);
190 };
191 
192 template <typename T>
194 
195  for (auto& sys : _systems) {
196  if (T* casted = dynamic_cast<T*>(sys.get())) {
197  return casted;
198  }
199  }
200 
201  return nullptr;
202 }
203 
204 // Global engine instance
205 extern std::unique_ptr<Engine> GEngine;
206 
207 // Global audio engine instance (Miniaudio)
208 extern ma_engine audio_engine;
209 
216 b2Vec2 pixels_to_world(const glm::vec2& pixel_pos);
217 
223 glm::vec2 world_to_pixels(const b2Vec2& world_pos);
224 
228 template <typename T>
229 T random(T min, T max) {
230  static std::random_device rd;
231  static std::mt19937 gen(rd());
232  if constexpr (std::is_integral_v<T>) {
233  std::uniform_int_distribution<T> dist(min, max);
234  return dist(gen);
235  } else if constexpr (std::is_floating_point_v<T>) {
236  std::uniform_real_distribution<T> dist(min, max);
237  return dist(gen);
238  } else {
239  static_assert("Unsupported type for random function");
240  }
241 
242  return T{0};
243 }
Core Engine singleton.
Definition: engine.h:28
T * get_system()
Definition: engine.h:193
Ember_VFS VirtualFileSystem
Definition: engine.h:48
struct Engine::@0 Window
void resize_window(int w, int h)
Resize the SDL window.
Definition: engine.cpp:371
float global_volume
Definition: engine.h:45
struct Engine::@1 Audio
SDL_AudioSpec spec
Definition: engine.h:44
b2WorldId get_physics_world() const
Get the created physics world.
Definition: engine.cpp:36
Renderer * get_renderer() const
Get the renderer instance.
Definition: engine.cpp:379
int bbWidth
Definition: engine.h:36
void shutdown()
Deinitialize window, renderer, and modules.
Definition: engine.cpp:289
SDL_Window * handle
Definition: engine.h:37
SDL_AudioDeviceID device_id
Definition: engine.h:43
EngineConfig Config
Definition: engine.h:40
void update(double delta_time=0)
Update all engine systems.
Definition: engine.cpp:17
TimeManager * time_manager() const
Get the time manager.
Definition: engine.cpp:387
int bbHeight
Definition: engine.h:36
Engine()
Definition: engine.cpp:365
const SDL_DisplayMode * data
Definition: engine.h:35
float width
Definition: engine.h:33
bool is_running
Definition: engine.h:50
void set_vsync(bool enabled)
Disable or enable vertical synchronization (VSync).
Definition: engine.cpp:59
InputManager * input_manager() const
Get the input manager.
Definition: engine.cpp:383
bool initialize(int width, int height, Backend type, Uint64 flags=0)
Initialize the engine: SDL window, renderer, audio, fonts.
Definition: engine.cpp:70
float height
Definition: engine.h:34
Definition: input_manager.h:117
Opengl Renderer implementation.
Definition: ember_gl.h:25
Opengl Shader implementation.
Definition: shader_gl.h:18
Base class for all renderers.
Definition: ember_core.h:109
TimeManager class.
Definition: time_manager.h:11
T random(T min, T max)
Generate a random number between min and max.
Definition: engine.h:229
b2Vec2 pixels_to_world(const glm::vec2 &pixel_pos)
Convert pixel coordinates to Physics world coordinates.
Definition: engine.cpp:502
ma_engine audio_engine
Definition: engine.cpp:12
std::unique_ptr< Engine > GEngine
Definition: engine.cpp:10
constexpr float METERS_PER_PIXEL
Definition: engine.h:21
glm::vec2 world_to_pixels(const b2Vec2 &world_pos)
Convert pixel coordinates to Physics world coordinates.
Definition: engine.cpp:507
constexpr float PIXELS_PER_METER
Definition: engine.h:20
Backend
Supported rendering backends for the engine.
Definition: engine_config.h:11
Engine virtual file system.
Definition: file_system.h:43
Definition: engine_config.h:138