/
FontScene.h
219 lines (187 loc) · 5.18 KB
/
FontScene.h
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#pragma once
#include "Math.h"
#include "Scene.h"
#include "RenderEngine.h"
#include "Text.h"
#define WORD_COUNT 2048
class FontScene : public Scene {
public:
FontScene(RenderEngine& render_engine) : render_engine_(render_engine) {}
void OnQuit() {
if (startup_) {
text_.Unregister();
}
}
void OnEntry() {
if (!startup_) {
startup_ = true;
render_pass_ = render_engine_.CreateRenderPass();
text_.Register(render_pass_);
}
}
void OnExit() {
}
void Update(std::array<bool, SDL_NUM_SCANCODES>& key_state, bool mouse_capture, int mouse_x, int mouse_y) {
}
bool EventHandler(const SDL_Event* event) {
return false;
}
void Render() {
uint32_t image_index;
if (!render_engine_.AcquireNextImage(image_index)) {
return;
}
text_.DrawBegin();
uint32_t window_width = render_engine_.swapchain_extent_.width;
uint32_t window_height = render_engine_.swapchain_extent_.height;
for (int i = 0; i < WORD_COUNT; i++) {
const char* word = words_[rand() % words_.size()];
uint32_t width;
uint32_t height;
text_.GetSize(word, width, height);
glm::vec3 color = {
(rand() % 256) / 255.0,
(rand() % 256) / 255.0,
(rand() % 256) / 255.0
};
glm::vec2 position = {
rand() % (window_width - width),
rand() % (window_height - height)
};
text_.Draw(color, position, word);
}
text_.DrawEnd();
VkCommandBuffer& command_buffer = render_engine_.command_buffers_[image_index];
VkCommandBufferBeginInfo begin_info = {};
begin_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
if (vkBeginCommandBuffer(command_buffer, &begin_info) != VK_SUCCESS) {
throw std::runtime_error("failed to begin recording command buffer");
}
VkRenderPassBeginInfo render_pass_info = {};
render_pass_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
render_pass_info.renderPass = render_pass_->render_pass_;
render_pass_info.framebuffer = render_pass_->framebuffers_[image_index];
render_pass_info.renderArea.offset = {0, 0};
render_pass_info.renderArea.extent = render_engine_.swapchain_extent_;
std::array<VkClearValue, 2> clear_values = {};
clear_values[0].color = {0.0f, 0.0f, 0.0f, 1.0f};
clear_values[1].depthStencil = {1.0f, 0};
render_pass_info.clearValueCount = static_cast<uint32_t>(clear_values.size());
render_pass_info.pClearValues = clear_values.data();
vkCmdBeginRenderPass(command_buffer, &render_pass_info, VK_SUBPASS_CONTENTS_INLINE);
text_.Render(command_buffer, image_index);
vkCmdEndRenderPass(command_buffer);
if (vkEndCommandBuffer(command_buffer) != VK_SUCCESS) {
throw std::runtime_error("failed to record command buffer");
}
render_engine_.SubmitDrawCommands(image_index);
render_engine_.PresentImage(image_index);
}
private:
RenderEngine& render_engine_;
bool startup_ = false;
std::shared_ptr<RenderEngine::RenderPass> render_pass_{};
Text text_{render_engine_};
std::vector<const char*> words_ = {
"acceptable",
"accessible",
"adhesive",
"admire",
"advise",
"appliance",
"arrogant",
"bawdy",
"behave",
"bell",
"best",
"breath",
"cable",
"cake",
"carve",
"cemetery",
"comb",
"comfortable",
"crown",
"curve",
"decorate",
"depend",
"disagreeable",
"disastrous",
"discover",
"discreet",
"disillusioned",
"dog",
"draconian",
"endurable",
"entertain",
"ethereal",
"expect",
"fang",
"fax",
"fertile",
"first",
"fish",
"front",
"grey",
"grouchy",
"hilarious",
"hug",
"impress",
"injure",
"ink",
"invent",
"irritate",
"join",
"knife",
"lamentable",
"lick",
"likeable",
"lying",
"marked",
"mist",
"mouth",
"nebulous",
"noise",
"numerous",
"occur",
"old",
"overrated",
"payment",
"peel",
"prepare",
"preserve",
"public",
"punishment",
"quarter",
"quizzical",
"rainy",
"rightful",
"salt",
"scare",
"scream",
"short",
"sick",
"signal",
"sock",
"sofa",
"soup",
"stiff",
"stingy",
"strip",
"supply",
"suspect",
"table",
"tawdry",
"temporary",
"tenuous",
"texture",
"thunder",
"trade",
"treatment",
"two",
"wax",
"wire",
"wish",
"wistful"
};
};