aboutsummaryrefslogtreecommitdiff
path: root/src/renderer/Renderer.hpp
blob: 4fdf5240d278401a27449734215ec46dca0455fe (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
#pragma once

#include <basic/math.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,
};

struct REntity_Rectangle {
    REntityType type;
    float x0;
    float y0;
    float x1;
    float y1;
    float z;
    Color color;
};

struct REntity_MonoBitmap {
    REntityType type;
    float x;
    float y;
    int32_t w;
    int32_t h;
    float z;
    void *data;
};

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

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(RectF32 rect, float z, Color color);
    void PushMonoBitmap(V3F32 pos, int w, int h, void* bitmap);


    /* 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);



    // Todo: make this private
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;
};