summaryrefslogtreecommitdiff
path: root/ui.hpp
blob: 2b671ae59bf2c599b80c45adc5b46a146786a3c7 (plain) (blame)
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
#ifndef ui_hpp
#define ui_hpp

#include "maths.hpp"
#include "video.hpp"

struct Arena;
struct UI {
	struct Vertex {
		float x, y, u, v, r, g, b, a;
	};

	struct Colour {
		uint8_t r, g, b, a;
		Colour(unsigned rgb, uint8_t a = 0xff);
		float r_f() { return (float)r / 255.0f; };
		float g_f() { return (float)g / 255.0f; };
		float b_f() { return (float)b / 255.0f; };
		float a_f() { return (float)a / 255.0f; };
	};

	struct Rect {
		int x, y, w, h;

		Rect() = default;
		Rect(int x, int y, int w, int h);
	};

	struct Vertex_Buffer {
		Rect clip;
		bool dirty;
		Staged_Buffer buf;
		Buffer_Id indices;
		int start, usage;

		void init(Device* dev);
		void init_indices(Device* dev);
		void destroy(UI* ui);
		void update_buffer(Context& ctx);
		void reset(const Rect& clip);
		void set_clip(const Rect& clip);

		void add_quad(
			UI* ui,
			int x,
			int y,
			int w,
			int h,
			float u0,
			float v0,
			float u1,
			float v1,
			Colour col
		);
		void add_rect(
			UI* ui,
			int x,
			int y,
			int w,
			int h,
			Colour col
		);
		void add_char(UI* ui, int x, int y, char ch, Colour col);
		void add_text(
			UI* ui,
			int x,
			int y,
			const char* txt,
			Colour col
		);
		void draw(
			UI* ui,
			Context& ctx,
			Pipeline& pip,
			Render_Pass& rp
		);
		Vertex_Buffer* next;
	};

	Heap* heap;
	Device* device;
	Texture_Id atlas;
	Shader_Id shader;
	Vertex_Format_Id vertex_format;
	Sampler_Id sampler;
	Staged_Buffer cbuffer;
	Vertex_Buffer mesh;
	Pipeline* pipeline;
	Render_Pass* render_pass;

	struct UI_CBuffer {
		m4f projection;
	};
	struct {
		int vert_binding;
		int atlas_binding;
		int config_binding;
	} shader_info;

	static int text_width(const char* t);
	static int text_height(const char* t);

	static UI* create(Device* dev, Arena* a, Shader_Id sh);

	void init(
		Device* dev,
		Heap* h,
		Texture_Id atlas,
		Shader_Id sh
	);
	void destroy();
	void update(Arena* s);
	void render(Arena* s, Texture_Id target);
};

#endif