Fx2D
A C++20 2D rigid-body physics engine
Loading...
Searching...
No Matches
Renderer.h
Go to the documentation of this file.
1#pragma once
2
3// Raylib + rlImGui (Dear ImGui binding for raylib)
4#define RAYLIB_QUIET // Suppress raylib logging
5#include <raylib.h>
6
7#define IMGUI_DEFINE_MATH_OPERATORS
8#include "imgui.h"
9#include "rlImGui.h"
10#include <rlgl.h>
11
12#include <functional>
13#include <memory>
14#include <string>
15#include <unordered_map>
16
17#include "Fx2D/Math.h"
18#include "Fx2D/Scene.h"
19
20// Fx2D colour to raylib colour.
21inline Color to_rl_color(const FxVec4ui8& c) {
22 return Color{c[0], c[1], c[2], c[3]};
23}
24
26 protected:
28 unsigned int m_scale;
29 unsigned int m_display_w, m_display_h;
30 // Fixed timestep variables
31 double m_fixed_dt = 1.0 / 60.0;
32 double m_dt_accumulator = 0.0;
33 double m_simulation_time = 0.0; // total time elaspsed from start in seconds
34 // Real-time factor variables
35 double m_real_time_factor = 1.0;
36 double m_max_time_step = 0.06;
37 double m_min_time_step = 1e-3;
38 bool m_play = true; // run-state
39 // background settings
40 Color m_backgroundColor{50, 50, 50, 255};
41 Texture2D m_backgroundTexture{0, 0, 0, 0, 0};
43 // texture cache for entity textures
44 std::unordered_map<std::string, Texture2D> m_textureCache;
45 Texture2D get_or_load_texture(const std::string& path);
46 // user overlay drawn after the scene, before the UI
47 std::function<void(FxRylbRenderer&)> m_func_draw_callback;
48 // helpers
49 void init(int fps = 60);
50 void draw_ui(double curr_rt_factor);
51 void draw_scene();
52 // Polls keyboard and mouse into m_scene.input(), once per rendered frame.
53 void poll_input();
54
55 public:
56 FxRylbRenderer(FxScene& scene, int fps = 60, unsigned int scale = 100);
58
59 void run(bool play = true);
60 // One rendered frame: poll input, consume the accumulator, draw. run() loops over it; in
61 // the browser Emscripten calls it from requestAnimationFrame. Call it yourself to embed
62 // the renderer in a loop you own.
63 void frame();
64 void set_background(const FxVec4ui8& color);
65 void set_background(const std::string& filepath);
66 void set_real_time_factor(const double& rt_factor);
67 double get_real_time_factor() const { return m_real_time_factor; }
68
69 // Convert between window pixels and scene units.
70 FxVec2f screen_to_world(const FxVec2f& screen) const;
71 FxVec2f world_to_screen(const FxVec2f& world) const;
72
73 // Custom drawing over the scene, before the UI panels. Use world_to_screen() to place
74 // things in scene coordinates.
75 void set_draw_callback(const std::function<void(FxRylbRenderer&)>& callback) {
76 m_func_draw_callback = callback;
77 }
78};
Color to_rl_color(const FxVec4ui8 &c)
Definition Renderer.h:21
Runs and draws an FxScene in a raylib window.
Definition Renderer.h:25
Color m_backgroundColor
Definition Renderer.h:40
Texture2D get_or_load_texture(const std::string &path)
std::unordered_map< std::string, Texture2D > m_textureCache
Definition Renderer.h:44
FxVec2f world_to_screen(const FxVec2f &world) const
double m_max_time_step
Definition Renderer.h:36
void set_background(const std::string &filepath)
FxScene & m_scene
Definition Renderer.h:27
void set_real_time_factor(const double &rt_factor)
std::function< void(FxRylbRenderer &)> m_func_draw_callback
Definition Renderer.h:47
void init(int fps=60)
unsigned int m_scale
Definition Renderer.h:28
unsigned int m_display_w
Definition Renderer.h:29
double m_min_time_step
Definition Renderer.h:37
unsigned int m_display_h
Definition Renderer.h:29
void set_draw_callback(const std::function< void(FxRylbRenderer &)> &callback)
Definition Renderer.h:75
double m_real_time_factor
Definition Renderer.h:35
void run(bool play=true)
FxVec2f screen_to_world(const FxVec2f &screen) const
void set_background(const FxVec4ui8 &color)
double m_simulation_time
Definition Renderer.h:33
double m_dt_accumulator
Definition Renderer.h:32
Texture2D m_backgroundTexture
Definition Renderer.h:41
double m_fixed_dt
Definition Renderer.h:31
void draw_ui(double curr_rt_factor)
bool m_hasBackgroundTexture
Definition Renderer.h:42
double get_real_time_factor() const
Definition Renderer.h:67
FxRylbRenderer(FxScene &scene, int fps=60, unsigned int scale=100)
Owns a simulated world and advances it with step().
Definition Scene.h:56
Single-precision 2D vector.
Definition Math.h:47