2 #include "Interface.hpp"
4 #include "../app/Assets.hpp"
5 #include "../app/Environment.hpp"
6 #include "../app/FrameCounter.hpp"
7 #include "../app/init.hpp"
8 #include "../audio/Audio.hpp"
9 #include "../graphics/Font.hpp"
10 #include "../graphics/Viewport.hpp"
11 #include "../model/shapes.hpp"
12 #include "../world/World.hpp"
18 #include <glm/gtc/matrix_transform.hpp>
19 #include <glm/gtx/io.hpp>
24 HUD::HUD(const BlockTypeRegistry &types, const Font &font)
29 , block_transform(1.0f)
31 , block_visible(false)
33 block_transform = glm::translate(block_transform, glm::vec3(50.0f, 50.0f, 0.0f));
34 block_transform = glm::scale(block_transform, glm::vec3(50.0f));
35 block_transform = glm::rotate(block_transform, 3.5f, glm::vec3(1.0f, 0.0f, 0.0f));
36 block_transform = glm::rotate(block_transform, 0.35f, glm::vec3(0.0f, 1.0f, 0.0f));
38 OutlineModel::Buffer buf;
39 buf.vertices = std::vector<glm::vec3>({
40 { -10.0f, 0.0f, 0.0f }, { 10.0f, 0.0f, 0.0f },
41 { 0.0f, -10.0f, 0.0f }, { 0.0f, 10.0f, 0.0f },
43 buf.indices = std::vector<OutlineModel::Index>({
46 buf.colors.resize(4, { 10.0f, 10.0f, 10.0f });
47 crosshair.Update(buf);
50 glm::vec3(50.0f, 85.0f, 0.0f),
54 block_label.Foreground(glm::vec4(1.0f));
55 block_label.Background(glm::vec4(0.5f));
59 void HUD::Display(const Block &b) {
60 const BlockType &type = types.Get(b.type);
63 type.FillEntityModel(block_buf, b.Transform());
64 block.Update(block_buf);
66 block_label.Set(font, type.label);
68 block_visible = type.visible;
72 void HUD::Render(Viewport &viewport) noexcept {
73 viewport.ClearDepth();
75 DirectionalLighting &world_prog = viewport.HUDProgram();
76 world_prog.SetLightDirection({ 1.0f, 3.0f, 5.0f });
77 // disable distance fog
78 world_prog.SetFogDensity(0.0f);
80 viewport.EnableInvertBlending();
81 viewport.SetCursor(glm::vec3(0.0f), Gravity::CENTER);
82 world_prog.SetM(viewport.Cursor());
86 viewport.DisableBlending();
87 world_prog.SetM(block_transform);
89 block_label.Render(viewport);
100 , ctrl(world.Player())
101 , font(env.assets.LoadFont("DejaVuSans", 16))
102 , hud(world.BlockTypes(), font)
103 , aim{{ 0, 0, 0 }, { 0, 0, -1 }}
108 , outline_transform(1.0f)
117 , place_sound(env.assets.LoadSound("thump"))
118 , remove_sound(env.assets.LoadSound("plop"))
122 counter_text.Position(glm::vec3(-25.0f, 25.0f, 0.0f), Gravity::NORTH_EAST);
123 counter_text.Foreground(glm::vec4(1.0f));
124 counter_text.Background(glm::vec4(0.5f));
125 position_text.Hide();
126 position_text.Position(glm::vec3(-25.0f, 25.0f + font.LineSkip(), 0.0f), Gravity::NORTH_EAST);
127 position_text.Foreground(glm::vec4(1.0f));
128 position_text.Background(glm::vec4(0.5f));
129 messages.Position(glm::vec3(25.0f, -25.0f, 0.0f), Gravity::SOUTH_WEST);
130 messages.Foreground(glm::vec4(1.0f));
131 messages.Background(glm::vec4(0.5f));
132 hud.Display(selection);
136 void Interface::HandlePress(const SDL_KeyboardEvent &event) {
137 if (config.keyboard_disabled) return;
139 switch (event.keysym.sym) {
180 PrintSelectionInfo();
195 void Interface::HandleRelease(const SDL_KeyboardEvent &event) {
196 if (config.keyboard_disabled) return;
198 switch (event.keysym.sym) {
220 void Interface::FaceBlock() {
221 selection.SetFace(Block::Face((selection.GetFace() + 1) % Block::FACE_COUNT));
222 hud.Display(selection);
225 void Interface::TurnBlock() {
226 selection.SetTurn(Block::Turn((selection.GetTurn() + 1) % Block::TURN_COUNT));
227 hud.Display(selection);
230 void Interface::ToggleCollision() {
231 ctrl.Controlled().WorldCollidable(!ctrl.Controlled().WorldCollidable());
232 if (ctrl.Controlled().WorldCollidable()) {
233 PostMessage("collision on");
235 PostMessage("collision off");
239 void Interface::PrintBlockInfo() {
240 std::cout << std::endl;
242 PostMessage("not looking at any block");
243 Ray aim = ctrl.Aim();
245 s << "aim ray: " << aim.orig << ", " << aim.dir;
246 PostMessage(s.str());
250 s << "looking at block " << aim_block
251 << " " << Chunk::ToCoords(aim_block)
252 << " of chunk " << aim_chunk->Position()
254 PostMessage(s.str());
255 Print(aim_chunk->BlockAt(aim_block));
258 void Interface::PrintChunkInfo() {
259 std::cout << std::endl;
261 PostMessage("not looking at any block");
265 s << "looking at chunk " << aim_chunk->Position();
266 PostMessage(s.str());
268 PostMessage(" neighbors:");
269 if (aim_chunk->HasNeighbor(Block::FACE_LEFT)) {
271 s << " left " << aim_chunk->GetNeighbor(Block::FACE_LEFT).Position();
272 PostMessage(s.str());
274 if (aim_chunk->HasNeighbor(Block::FACE_RIGHT)) {
276 s << " right " << aim_chunk->GetNeighbor(Block::FACE_RIGHT).Position();
277 PostMessage(s.str());
279 if (aim_chunk->HasNeighbor(Block::FACE_UP)) {
281 s << " up " << aim_chunk->GetNeighbor(Block::FACE_UP).Position();
282 PostMessage(s.str());
284 if (aim_chunk->HasNeighbor(Block::FACE_DOWN)) {
286 s << " down " << aim_chunk->GetNeighbor(Block::FACE_DOWN).Position();
287 PostMessage(s.str());
289 if (aim_chunk->HasNeighbor(Block::FACE_FRONT)) {
291 s << " front " << aim_chunk->GetNeighbor(Block::FACE_FRONT).Position();
292 PostMessage(s.str());
294 if (aim_chunk->HasNeighbor(Block::FACE_BACK)) {
296 s << " back " << aim_chunk->GetNeighbor(Block::FACE_BACK).Position();
297 PostMessage(s.str());
299 std::cout << std::endl;
302 void Interface::PrintLightInfo() {
305 << "light level " << world.PlayerChunk().GetLight(world.Player().Position())
306 << " at position " << world.Player().Position()
308 PostMessage(s.str());
311 void Interface::PrintSelectionInfo() {
312 std::cout << std::endl;
316 void Interface::Print(const Block &block) {
318 s << "type: " << block.type
319 << ", face: " << block.GetFace()
320 << ", turn: " << block.GetTurn()
322 PostMessage(s.str());
325 void Interface::ToggleAudio() {
326 config.audio_disabled = !config.audio_disabled;
327 if (config.audio_disabled) {
328 PostMessage("audio off");
330 PostMessage("audio on");
334 void Interface::ToggleVisual() {
335 config.visual_disabled = !config.visual_disabled;
336 if (config.visual_disabled) {
337 PostMessage("visual off");
339 PostMessage("visual on");
343 void Interface::ToggleDebug() {
344 counter_text.Toggle();
345 position_text.Toggle();
346 if (counter_text.Visible()) {
352 void Interface::UpdateCounter() {
354 s << std::setprecision(3) <<
355 "avg: " << env.counter.Average().running << "ms, "
356 "peak: " << env.counter.Peak().running << "ms";
357 std::string text = s.str();
358 counter_text.Set(font, text);
361 void Interface::UpdatePosition() {
363 s << std::setprecision(3) << "pos: " << ctrl.Controlled().AbsolutePosition();
364 position_text.Set(font, s.str());
368 void Interface::Handle(const SDL_MouseMotionEvent &event) {
369 if (config.mouse_disabled) return;
370 ctrl.RotateYaw(event.xrel * config.yaw_sensitivity);
371 ctrl.RotatePitch(event.yrel * config.pitch_sensitivity);
374 void Interface::HandlePress(const SDL_MouseButtonEvent &event) {
375 if (config.mouse_disabled) return;
377 if (event.button == SDL_BUTTON_LEFT) {
379 remove_timer.Start();
380 } else if (event.button == SDL_BUTTON_MIDDLE) {
382 } else if (event.button == SDL_BUTTON_RIGHT) {
388 void Interface::HandleRelease(const SDL_MouseButtonEvent &event) {
389 if (config.mouse_disabled) return;
391 if (event.button == SDL_BUTTON_LEFT) {
393 } else if (event.button == SDL_BUTTON_RIGHT) {
398 void Interface::PickBlock() {
399 if (!aim_chunk) return;
400 selection = aim_chunk->BlockAt(aim_block);
401 hud.Display(selection);
404 void Interface::PlaceBlock() {
405 if (!aim_chunk) return;
406 Chunk *mod_chunk = aim_chunk;
407 glm::vec3 next_pos = Chunk::ToCoords(aim_block) + aim_normal;
408 if (!Chunk::InBounds(next_pos)) {
409 mod_chunk = &world.Next(*aim_chunk, aim_normal);
410 next_pos -= aim_normal * glm::vec3(Chunk::Extent());
412 mod_chunk->SetBlock(next_pos, selection);
414 if (config.audio_disabled) return;
415 const Entity &player = ctrl.Controlled();
418 mod_chunk->ToSceneCoords(player.ChunkCoords(), next_pos)
422 void Interface::RemoveBlock() noexcept {
423 if (!aim_chunk) return;
424 aim_chunk->SetBlock(aim_block, remove);
426 if (config.audio_disabled) return;
427 const Entity &player = ctrl.Controlled();
430 aim_chunk->ToSceneCoords(player.ChunkCoords(), Chunk::ToCoords(aim_block))
435 void Interface::Handle(const SDL_MouseWheelEvent &event) {
436 if (config.mouse_disabled) return;
440 } else if (event.y > 0) {
445 void Interface::SelectNext() {
447 if (size_t(selection.type) >= world.BlockTypes().Size()) {
450 hud.Display(selection);
453 void Interface::SelectPrevious() {
455 if (selection.type <= 0) {
456 selection.type = world.BlockTypes().Size() - 1;
458 hud.Display(selection);
462 void Interface::PostMessage(const char *msg) {
463 messages.PushLine(msg);
466 std::cout << msg << std::endl;
470 void Interface::Update(int dt) {
471 ctrl.Velocity(glm::vec3(fwd - rev) * config.move_velocity);
474 msg_timer.Update(dt);
475 place_timer.Update(dt);
476 remove_timer.Update(dt);
481 if (msg_timer.HitOnce()) {
485 if (remove_timer.Hit()) {
490 if (place_timer.Hit()) {
495 if (counter_text.Visible() && env.counter.Changed()) {
498 if (position_text.Visible()) {
505 OutlineModel::Buffer outl_buf;
509 void Interface::CheckAim() {
511 if (world.Intersection(aim, glm::mat4(1.0f), aim_chunk, aim_block, dist, aim_normal)) {
513 aim_chunk->Type(aim_chunk->BlockAt(aim_block)).FillOutlineModel(outl_buf);
514 outline.Update(outl_buf);
515 outline_transform = glm::scale(glm::vec3(1.0002f));
516 outline_transform *= aim_chunk->Transform(world.Player().ChunkCoords());
517 outline_transform *= aim_chunk->ToTransform(Chunk::ToPos(aim_block), aim_block);
524 void Interface::Render(Viewport &viewport) noexcept {
525 if (config.visual_disabled) return;
528 DirectionalLighting &world_prog = viewport.EntityProgram();
529 world_prog.SetM(outline_transform);
533 if (counter_text.Visible()) {
534 counter_text.Render(viewport);
536 if (position_text.Visible()) {
537 position_text.Render(viewport);
540 if (msg_timer.Running()) {
541 messages.Render(viewport);
544 hud.Render(viewport);