aboutsummaryrefslogtreecommitdiff
path: root/src/renderer/Renderer.hpp
blob: fc037d6637078d6e02890624e283377fc93d11a2 (plain)
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#pragma once

#include <common/math.hpp>
#include <common/Font.hpp>
#include <common/shapes.hpp>

#include <SDL3/SDL.h>
#include <SDL3/SDL_video.h>
#include <imgui.h>

#include <vector>
#include <memory>


class RSoftwareBackend;
class Renderer;

extern Renderer g_renderer;


enum REntityType : int32_t {
    REntityType_Rectangle,
    REntityType_MonoBitmap,
    REntityType_Circle,
};

struct REntity_MonoBitmap {
    REntityType type;
    V3F32 pos;
    MonoBitmap& bitmap;
    Color color;
};

struct REntity_Rectangle {
    REntityType type;
    Rectangle rect;
    float z;
    Color color;
};

struct REntity_Circle {
    REntityType type;
    Circle circle;
    float z;
    Color color;
};

union REntity {
    REntityType type;
    REntity_MonoBitmap bitmap;
    REntity_Rectangle rect;
    REntity_Circle circle;
};

struct RSortEntry {
    float z;
    size_t entity_index;
};


class Renderer {
public:
    void Init(SDL_Window* window);


    /* core functions */

    void Draw();
    void Reset();

    void Clear(Color color);
    void PushRectangle(Rectangle rect, float z, Color color);
    void PushMonoBitmap(MonoBitmap& bitmap, V3F32 pos, Color color);
    void PushCircle(Circle circle, float z, Color color);


    /* helper functions */

    void SetScreenSize(int32_t w, int32_t h);
    void SetCameraSize(float w, float h);


    int32_t WorldXToScreenX(float x);
    int32_t WorldYToScreenY(float y);

    int32_t WorldWidthToScreenWidth(float w);
    int32_t WorldHeightToScreenHeight(float h);


    /* temporary helper functions (from old RGroup api) */

    float GetScale();
    V2F32 ViewPosToScreenPos(V2F32 view_pos);
    V2F32 ViewSizeToScreenSize(V2F32 view_size);
    ImVec2 ViewPosToScreenPosImGui(V2F32 view_pos);
    ImVec2 ViewSizeToScreenSizeImGui(V2F32 view_size);



public:
    int32_t m_screen_w;
    int32_t m_screen_h;

    float m_camera_w;
    float m_camera_h;

    Color m_clear_color {};
    std::vector<REntity> m_render_entities;
    std::vector<RSortEntry> m_sort_entries;

    std::unique_ptr<RSoftwareBackend> m_backend;


    friend class RSoftwareBackend;
};