]> git.localhorst.tv Git - blank.git/blob - src/ui/ui.cpp
implemented font redering
[blank.git] / src / ui / ui.cpp
1 #include "HUD.hpp"
2 #include "Interface.hpp"
3
4 #include "../app/init.hpp"
5 #include "../graphics/DirectionalLighting.hpp"
6 #include "../model/shapes.hpp"
7 #include "../world/World.hpp"
8
9 #include <iostream>
10 #include <glm/gtc/matrix_transform.hpp>
11 #include <glm/gtx/io.hpp>
12
13
14 namespace blank {
15
16 HUD::HUD(const BlockTypeRegistry &types)
17 : types(types)
18 , block()
19 , block_buf()
20 , block_transform(1.0f)
21 , block_visible(false)
22 , crosshair()
23 , crosshair_transform(1.0f)
24 , near(100.0f)
25 , far(-100.0f)
26 , projection(glm::ortho(0.0f, 1.0f, 1.0f, 0.0f, near, far))
27 , view(glm::translate(glm::mat4(1.0f), glm::vec3(-0.5f, -0.5f, 0))) {
28         block_transform = glm::translate(block_transform, glm::vec3(50.0f, 50.0f, 0.0f));
29         block_transform = glm::scale(block_transform, glm::vec3(50.0f));
30         block_transform = glm::rotate(block_transform, 3.5f, glm::vec3(1.0f, 0.0f, 0.0f));
31         block_transform = glm::rotate(block_transform, 0.35f, glm::vec3(0.0f, 1.0f, 0.0f));
32
33         crosshair.vertices = std::vector<glm::vec3>({
34                 { -10.0f,   0.0f, 0.0f }, { 10.0f,  0.0f, 0.0f },
35                 {   0.0f, -10.0f, 0.0f }, {  0.0f, 10.0f, 0.0f },
36         });
37         crosshair.indices = std::vector<OutlineModel::Index>({
38                 0, 1, 2, 3
39         });
40         crosshair.colors.resize(4, { 10.0f, 10.0f, 10.0f });
41         crosshair.Invalidate();
42 }
43
44
45 void HUD::Viewport(float width, float height) noexcept {
46         Viewport(0, 0, width, height);
47 }
48
49 void HUD::Viewport(float x, float y, float width, float height) noexcept {
50         projection = glm::ortho(x, width, height, y, near, far);
51         crosshair_transform = glm::translate(glm::mat4(1.0f), glm::vec3(width * 0.5f, height * 0.5f, 0.0f));
52 }
53
54
55 void HUD::Display(const Block &b) {
56         const BlockType &type = types.Get(b.type);
57
58         block_buf.Clear();
59         type.FillModel(block_buf, b.Transform());
60         block.Update(block_buf);
61         block_visible = type.visible;
62 }
63
64
65 void HUD::Render(DirectionalLighting &program) noexcept {
66         program.SetLightDirection({ 1.0f, 3.0f, 5.0f });
67         // disable distance fog
68         program.SetFogDensity(0.0f);
69         GLContext::ClearDepthBuffer();
70
71         program.SetVP(view, projection);
72
73         if (block_visible) {
74                 program.SetM(block_transform);
75                 block.Draw();
76         }
77
78         program.SetM(crosshair_transform);
79         crosshair.Draw();
80 }
81
82
83 Interface::Interface(const Config &config, World &world)
84 : world(world)
85 , ctrl(world.Player())
86 , hud(world.BlockTypes())
87 , aim{{ 0, 0, 0 }, { 0, 0, -1 }}
88 , aim_chunk(nullptr)
89 , aim_block(0)
90 , aim_normal()
91 , outline()
92 , outline_transform(1.0f)
93 , config(config)
94 , place_timer(256)
95 , remove_timer(256)
96 , remove(0)
97 , selection(1)
98 , fwd(0)
99 , rev(0) {
100         hud.Viewport(960, 600);
101         hud.Display(selection);
102 }
103
104
105 void Interface::HandlePress(const SDL_KeyboardEvent &event) {
106         if (config.keyboard_disabled) return;
107
108         switch (event.keysym.sym) {
109                 case SDLK_w:
110                         rev.z = 1;
111                         break;
112                 case SDLK_s:
113                         fwd.z = 1;
114                         break;
115                 case SDLK_a:
116                         rev.x = 1;
117                         break;
118                 case SDLK_d:
119                         fwd.x = 1;
120                         break;
121                 case SDLK_SPACE:
122                         fwd.y = 1;
123                         break;
124                 case SDLK_LSHIFT:
125                         rev.y = 1;
126                         break;
127
128                 case SDLK_q:
129                         FaceBlock();
130                         break;
131                 case SDLK_e:
132                         TurnBlock();
133                         break;
134
135                 case SDLK_n:
136                         ToggleCollision();
137                         break;
138
139                 case SDLK_b:
140                         PrintBlockInfo();
141                         break;
142                 case SDLK_c:
143                         PrintChunkInfo();
144                         break;
145                 case SDLK_l:
146                         PrintLightInfo();
147                         break;
148                 case SDLK_p:
149                         PrintSelectionInfo();
150                         break;
151         }
152 }
153
154 void Interface::HandleRelease(const SDL_KeyboardEvent &event) {
155         if (config.keyboard_disabled) return;
156
157         switch (event.keysym.sym) {
158                 case SDLK_w:
159                         rev.z = 0;
160                         break;
161                 case SDLK_s:
162                         fwd.z = 0;
163                         break;
164                 case SDLK_a:
165                         rev.x = 0;
166                         break;
167                 case SDLK_d:
168                         fwd.x = 0;
169                         break;
170                 case SDLK_SPACE:
171                         fwd.y = 0;
172                         break;
173                 case SDLK_LSHIFT:
174                         rev.y = 0;
175                         break;
176         }
177 }
178
179 void Interface::FaceBlock() {
180         selection.SetFace(Block::Face((selection.GetFace() + 1) % Block::FACE_COUNT));
181         hud.Display(selection);
182 }
183
184 void Interface::TurnBlock() {
185         selection.SetTurn(Block::Turn((selection.GetTurn() + 1) % Block::TURN_COUNT));
186         hud.Display(selection);
187 }
188
189 void Interface::ToggleCollision() {
190         ctrl.Controlled().WorldCollidable(!ctrl.Controlled().WorldCollidable());
191         std::cout << "collision " << (ctrl.Controlled().WorldCollidable() ? "on" : "off") << std::endl;
192 }
193
194 void Interface::PrintBlockInfo() {
195         std::cout << std::endl;
196         if (!aim_chunk) {
197                 std::cout << "not looking at any block" << std::endl;
198                 Ray aim = ctrl.Aim();
199                 std::cout << "aim ray: " << aim.orig << ", " << aim.dir << std::endl;
200                 return;
201         }
202         std::cout << "looking at block " << aim_block
203                 << " " << Chunk::ToCoords(aim_block)
204                 << " of chunk " << aim_chunk->Position()
205                 << std::endl;
206         Print(aim_chunk->BlockAt(aim_block));
207 }
208
209 void Interface::PrintChunkInfo() {
210         std::cout << std::endl;
211         if (!aim_chunk) {
212                 std::cout << "not looking at any block" << std::endl;
213                 return;
214         }
215         std::cout << "looking at chunk "
216                 << aim_chunk->Position()
217                 << std::endl;
218
219         std::cout << "  neighbors:" << std::endl;
220         if (aim_chunk->HasNeighbor(Block::FACE_LEFT)) {
221                 std::cout << " left  " << aim_chunk->GetNeighbor(Block::FACE_LEFT).Position() << std::endl;
222         }
223         if (aim_chunk->HasNeighbor(Block::FACE_RIGHT)) {
224                 std::cout << " right " << aim_chunk->GetNeighbor(Block::FACE_RIGHT).Position() << std::endl;
225         }
226         if (aim_chunk->HasNeighbor(Block::FACE_UP)) {
227                 std::cout << " up    " << aim_chunk->GetNeighbor(Block::FACE_UP).Position() << std::endl;
228         }
229         if (aim_chunk->HasNeighbor(Block::FACE_DOWN)) {
230                 std::cout << " down  " << aim_chunk->GetNeighbor(Block::FACE_DOWN).Position() << std::endl;
231         }
232         if (aim_chunk->HasNeighbor(Block::FACE_FRONT)) {
233                 std::cout << " front " << aim_chunk->GetNeighbor(Block::FACE_FRONT).Position() << std::endl;
234         }
235         if (aim_chunk->HasNeighbor(Block::FACE_BACK)) {
236                 std::cout << " back  " << aim_chunk->GetNeighbor(Block::FACE_BACK).Position() << std::endl;
237         }
238         std::cout << std::endl;
239 }
240
241 void Interface::PrintLightInfo() {
242         std::cout
243                 << "light level " << world.PlayerChunk().GetLight(world.Player().Position())
244                 << " at position " << world.Player().Position()
245                 << std::endl;
246 }
247
248 void Interface::PrintSelectionInfo() {
249         std::cout << std::endl;
250         Print(selection);
251 }
252
253 void Interface::Print(const Block &block) {
254         std::cout << "type: " << block.type
255                 << ", face: " << block.GetFace()
256                 << ", turn: " << block.GetTurn()
257                 << std::endl;
258 }
259
260
261 void Interface::Handle(const SDL_MouseMotionEvent &event) {
262         if (config.mouse_disabled) return;
263         ctrl.RotateYaw(event.xrel * config.yaw_sensitivity);
264         ctrl.RotatePitch(event.yrel * config.pitch_sensitivity);
265 }
266
267 void Interface::HandlePress(const SDL_MouseButtonEvent &event) {
268         if (config.mouse_disabled) return;
269
270         if (event.button == SDL_BUTTON_LEFT) {
271                 RemoveBlock();
272                 remove_timer.Start();
273         } else if (event.button == SDL_BUTTON_MIDDLE) {
274                 PickBlock();
275         } else if (event.button == SDL_BUTTON_RIGHT) {
276                 PlaceBlock();
277                 place_timer.Start();
278         }
279 }
280
281 void Interface::HandleRelease(const SDL_MouseButtonEvent &event) {
282         if (config.mouse_disabled) return;
283
284         if (event.button == SDL_BUTTON_LEFT) {
285                 remove_timer.Stop();
286         } else if (event.button == SDL_BUTTON_RIGHT) {
287                 place_timer.Stop();
288         }
289 }
290
291 void Interface::PickBlock() {
292         if (!aim_chunk) return;
293         selection = aim_chunk->BlockAt(aim_block);
294         hud.Display(selection);
295 }
296
297 void Interface::PlaceBlock() {
298         if (!aim_chunk) return;
299         Chunk *mod_chunk = aim_chunk;
300         glm::vec3 next_pos = Chunk::ToCoords(aim_block) + aim_normal;
301         if (!Chunk::InBounds(next_pos)) {
302                 mod_chunk = &world.Next(*aim_chunk, aim_normal);
303                 next_pos -= aim_normal * glm::vec3(Chunk::Extent());
304         }
305         mod_chunk->SetBlock(next_pos, selection);
306         mod_chunk->Invalidate();
307 }
308
309 void Interface::RemoveBlock() noexcept {
310         if (!aim_chunk) return;
311         aim_chunk->SetBlock(aim_block, remove);
312         aim_chunk->Invalidate();
313 }
314
315
316 void Interface::Handle(const SDL_MouseWheelEvent &event) {
317         if (config.mouse_disabled) return;
318
319         if (event.y < 0) {
320                 SelectNext();
321         } else if (event.y > 0) {
322                 SelectPrevious();
323         }
324 }
325
326 void Interface::SelectNext() {
327         ++selection.type;
328         if (size_t(selection.type) >= world.BlockTypes().Size()) {
329                 selection.type = 1;
330         }
331         hud.Display(selection);
332 }
333
334 void Interface::SelectPrevious() {
335         --selection.type;
336         if (selection.type <= 0) {
337                 selection.type = world.BlockTypes().Size() - 1;
338         }
339         hud.Display(selection);
340 }
341
342 void Interface::Handle(const SDL_WindowEvent &event) noexcept {
343         if (event.event == SDL_WINDOWEVENT_RESIZED) {
344                 hud.Viewport(event.data1, event.data2);
345         }
346 }
347
348
349 void Interface::Update(int dt) {
350         ctrl.Velocity(glm::vec3(fwd - rev) * config.move_velocity);
351         ctrl.Update(dt);
352
353         place_timer.Update(dt);
354         remove_timer.Update(dt);
355
356         aim = ctrl.Aim();
357         CheckAim();
358
359         if (remove_timer.Hit()) {
360                 RemoveBlock();
361                 CheckAim();
362         }
363
364         if (place_timer.Hit()) {
365                 PlaceBlock();
366                 CheckAim();
367         }
368 }
369
370 void Interface::CheckAim() {
371         float dist;
372         if (world.Intersection(aim, glm::mat4(1.0f), aim_chunk, aim_block, dist, aim_normal)) {
373                 outline.Clear();
374                 aim_chunk->Type(aim_chunk->BlockAt(aim_block)).FillOutlineModel(outline);
375                 outline_transform = glm::scale(glm::vec3(1.0002f));
376                 outline_transform *= aim_chunk->Transform(world.Player().ChunkCoords());
377                 outline_transform *= aim_chunk->ToTransform(Chunk::ToPos(aim_block), aim_block);
378         } else {
379                 aim_chunk = nullptr;
380         }
381 }
382
383
384 void Interface::Render(DirectionalLighting &program) noexcept {
385         if (config.visual_disabled) return;
386
387         if (aim_chunk) {
388                 program.SetM(outline_transform);
389                 outline.Draw();
390         }
391
392         hud.Render(program);
393 }
394
395 }