2 #include "Interface.hpp"
5 #include "../app/Assets.hpp"
6 #include "../app/Environment.hpp"
7 #include "../app/FrameCounter.hpp"
8 #include "../app/init.hpp"
9 #include "../audio/Audio.hpp"
10 #include "../graphics/Font.hpp"
11 #include "../graphics/Viewport.hpp"
12 #include "../io/TokenStreamReader.hpp"
13 #include "../model/shapes.hpp"
14 #include "../world/World.hpp"
20 #include <glm/gtc/matrix_transform.hpp>
21 #include <glm/gtx/io.hpp>
26 HUD::HUD(const BlockTypeRegistry &types, const Font &font)
31 , block_transform(1.0f)
33 , block_visible(false)
35 block_transform = glm::translate(block_transform, glm::vec3(50.0f, 50.0f, 0.0f));
36 block_transform = glm::scale(block_transform, glm::vec3(50.0f));
37 block_transform = glm::rotate(block_transform, 3.5f, glm::vec3(1.0f, 0.0f, 0.0f));
38 block_transform = glm::rotate(block_transform, 0.35f, glm::vec3(0.0f, 1.0f, 0.0f));
40 OutlineModel::Buffer buf;
41 buf.vertices = std::vector<glm::vec3>({
42 { -10.0f, 0.0f, 0.0f }, { 10.0f, 0.0f, 0.0f },
43 { 0.0f, -10.0f, 0.0f }, { 0.0f, 10.0f, 0.0f },
45 buf.indices = std::vector<OutlineModel::Index>({
48 buf.colors.resize(4, { 10.0f, 10.0f, 10.0f });
49 crosshair.Update(buf);
52 glm::vec3(50.0f, 85.0f, 0.0f),
56 block_label.Foreground(glm::vec4(1.0f));
57 block_label.Background(glm::vec4(0.5f));
61 void HUD::Display(const Block &b) {
62 const BlockType &type = types.Get(b.type);
65 type.FillEntityModel(block_buf, b.Transform());
66 block.Update(block_buf);
68 block_label.Set(font, type.label);
70 block_visible = type.visible;
74 void HUD::Render(Viewport &viewport) noexcept {
75 viewport.ClearDepth();
77 PlainColor &outline_prog = viewport.HUDOutlineProgram();
78 viewport.EnableInvertBlending();
79 viewport.SetCursor(glm::vec3(0.0f), Gravity::CENTER);
80 outline_prog.SetM(viewport.Cursor());
84 DirectionalLighting &world_prog = viewport.HUDProgram();
85 world_prog.SetLightDirection({ 1.0f, 3.0f, 5.0f });
86 // disable distance fog
87 world_prog.SetFogDensity(0.0f);
89 viewport.DisableBlending();
90 world_prog.SetM(block_transform);
92 block_label.Render(viewport);
103 , ctrl(world.Player())
104 , hud(world.BlockTypes(), env.assets.small_ui_font)
105 , aim{{ 0, 0, 0 }, { 0, 0, -1 }}
110 , outline_transform(1.0f)
116 , messages(env.assets.small_ui_font)
123 , place_sound(env.assets.LoadSound("thump"))
124 , remove_sound(env.assets.LoadSound("plop"))
128 counter_text.Position(glm::vec3(-25.0f, 25.0f, 0.0f), Gravity::NORTH_EAST);
129 counter_text.Foreground(glm::vec4(1.0f));
130 counter_text.Background(glm::vec4(0.5f));
131 position_text.Position(glm::vec3(-25.0f, 25.0f + env.assets.small_ui_font.LineSkip(), 0.0f), Gravity::NORTH_EAST);
132 position_text.Foreground(glm::vec4(1.0f));
133 position_text.Background(glm::vec4(0.5f));
134 orientation_text.Position(glm::vec3(-25.0f, 25.0f + 2 * env.assets.small_ui_font.LineSkip(), 0.0f), Gravity::NORTH_EAST);
135 orientation_text.Foreground(glm::vec4(1.0f));
136 orientation_text.Background(glm::vec4(0.5f));
137 block_text.Position(glm::vec3(-25.0f, 25.0f + 4 * env.assets.small_ui_font.LineSkip(), 0.0f), Gravity::NORTH_EAST);
138 block_text.Foreground(glm::vec4(1.0f));
139 block_text.Background(glm::vec4(0.5f));
140 block_text.Set(env.assets.small_ui_font, "Block: none");
141 messages.Position(glm::vec3(25.0f, -25.0f, 0.0f), Gravity::SOUTH_WEST);
142 messages.Foreground(glm::vec4(1.0f));
143 messages.Background(glm::vec4(0.5f));
144 hud.Display(selection);
148 void Interface::HandlePress(const SDL_KeyboardEvent &event) {
149 if (config.keyboard_disabled) return;
151 switch (env.keymap.Lookup(event)) {
152 case Keymap::MOVE_FORWARD:
155 case Keymap::MOVE_BACKWARD:
158 case Keymap::MOVE_LEFT:
161 case Keymap::MOVE_RIGHT:
164 case Keymap::MOVE_UP:
167 case Keymap::MOVE_DOWN:
171 case Keymap::BLOCK_FACE:
174 case Keymap::BLOCK_TURN:
177 case Keymap::BLOCK_NEXT:
180 case Keymap::BLOCK_PREV:
184 case Keymap::BLOCK_PLACE:
187 case Keymap::BLOCK_PICK:
190 case Keymap::BLOCK_REMOVE:
194 case Keymap::TOGGLE_COLLISION:
198 case Keymap::PRINT_BLOCK:
201 case Keymap::PRINT_CHUNK:
204 case Keymap::PRINT_LIGHT:
207 case Keymap::PRINT_SELECTION:
208 PrintSelectionInfo();
211 case Keymap::TOGGLE_VISUAL:
214 case Keymap::TOGGLE_DEBUG:
217 case Keymap::TOGGLE_AUDIO:
230 void Interface::HandleRelease(const SDL_KeyboardEvent &event) {
231 if (config.keyboard_disabled) return;
233 switch (env.keymap.Lookup(event)) {
234 case Keymap::MOVE_FORWARD:
237 case Keymap::MOVE_BACKWARD:
240 case Keymap::MOVE_LEFT:
243 case Keymap::MOVE_RIGHT:
246 case Keymap::MOVE_UP:
249 case Keymap::MOVE_DOWN:
258 void Interface::FaceBlock() {
259 selection.SetFace(Block::Face((selection.GetFace() + 1) % Block::FACE_COUNT));
260 hud.Display(selection);
263 void Interface::TurnBlock() {
264 selection.SetTurn(Block::Turn((selection.GetTurn() + 1) % Block::TURN_COUNT));
265 hud.Display(selection);
268 void Interface::ToggleCollision() {
269 ctrl.Controlled().WorldCollidable(!ctrl.Controlled().WorldCollidable());
270 if (ctrl.Controlled().WorldCollidable()) {
271 PostMessage("collision on");
273 PostMessage("collision off");
277 void Interface::PrintBlockInfo() {
278 std::cout << std::endl;
280 PostMessage("not looking at any block");
281 Ray aim = ctrl.Aim();
283 s << "aim ray: " << aim.orig << ", " << aim.dir;
284 PostMessage(s.str());
288 s << "looking at block " << aim_block
289 << " " << Chunk::ToCoords(aim_block)
290 << " of chunk " << aim_chunk->Position()
292 PostMessage(s.str());
293 Print(aim_chunk->BlockAt(aim_block));
296 void Interface::PrintChunkInfo() {
297 std::cout << std::endl;
299 PostMessage("not looking at any block");
303 s << "looking at chunk " << aim_chunk->Position();
304 PostMessage(s.str());
306 PostMessage(" neighbors:");
307 if (aim_chunk->HasNeighbor(Block::FACE_LEFT)) {
309 s << " left " << aim_chunk->GetNeighbor(Block::FACE_LEFT).Position();
310 PostMessage(s.str());
312 if (aim_chunk->HasNeighbor(Block::FACE_RIGHT)) {
314 s << " right " << aim_chunk->GetNeighbor(Block::FACE_RIGHT).Position();
315 PostMessage(s.str());
317 if (aim_chunk->HasNeighbor(Block::FACE_UP)) {
319 s << " up " << aim_chunk->GetNeighbor(Block::FACE_UP).Position();
320 PostMessage(s.str());
322 if (aim_chunk->HasNeighbor(Block::FACE_DOWN)) {
324 s << " down " << aim_chunk->GetNeighbor(Block::FACE_DOWN).Position();
325 PostMessage(s.str());
327 if (aim_chunk->HasNeighbor(Block::FACE_FRONT)) {
329 s << " front " << aim_chunk->GetNeighbor(Block::FACE_FRONT).Position();
330 PostMessage(s.str());
332 if (aim_chunk->HasNeighbor(Block::FACE_BACK)) {
334 s << " back " << aim_chunk->GetNeighbor(Block::FACE_BACK).Position();
335 PostMessage(s.str());
337 std::cout << std::endl;
340 void Interface::PrintLightInfo() {
343 << "light level " << world.PlayerChunk().GetLight(world.Player().Position())
344 << " at position " << world.Player().Position()
346 PostMessage(s.str());
349 void Interface::PrintSelectionInfo() {
350 std::cout << std::endl;
354 void Interface::Print(const Block &block) {
356 s << "type: " << block.type
357 << ", face: " << block.GetFace()
358 << ", turn: " << block.GetTurn()
360 PostMessage(s.str());
363 void Interface::ToggleAudio() {
364 config.audio_disabled = !config.audio_disabled;
365 if (config.audio_disabled) {
366 PostMessage("audio off");
368 PostMessage("audio on");
372 void Interface::ToggleVisual() {
373 config.visual_disabled = !config.visual_disabled;
374 if (config.visual_disabled) {
375 PostMessage("visual off");
377 PostMessage("visual on");
381 void Interface::ToggleDebug() {
391 void Interface::UpdateCounter() {
393 s << std::setprecision(3) <<
394 "avg: " << env.counter.Average().running << "ms, "
395 "peak: " << env.counter.Peak().running << "ms";
396 std::string text = s.str();
397 counter_text.Set(env.assets.small_ui_font, text);
400 void Interface::UpdatePosition() {
402 s << std::setprecision(3) << "pos: " << ctrl.Controlled().AbsolutePosition();
403 position_text.Set(env.assets.small_ui_font, s.str());
406 void Interface::UpdateOrientation() {
408 s << std::setprecision(3) << "pitch: " << rad2deg(ctrl.Pitch())
409 << ", yaw: " << rad2deg(ctrl.Yaw());
410 orientation_text.Set(env.assets.small_ui_font, s.str());
413 void Interface::UpdateBlockInfo() {
415 const Block &block = aim_chunk->BlockAt(aim_block);
416 if (last_displayed != block) {
419 << aim_chunk->Type(block).label
420 << ", face: " << block.GetFace()
421 << ", turn: " << block.GetTurn();
422 block_text.Set(env.assets.small_ui_font, s.str());
423 last_displayed = block;
426 if (last_displayed != Block()) {
429 block_text.Set(env.assets.small_ui_font, s.str());
430 last_displayed = Block();
436 void Interface::Handle(const SDL_MouseMotionEvent &event) {
437 if (config.mouse_disabled) return;
438 ctrl.RotateYaw(event.xrel * config.yaw_sensitivity);
439 ctrl.RotatePitch(event.yrel * config.pitch_sensitivity);
442 void Interface::HandlePress(const SDL_MouseButtonEvent &event) {
443 if (config.mouse_disabled) return;
445 if (event.button == SDL_BUTTON_LEFT) {
447 remove_timer.Start();
448 } else if (event.button == SDL_BUTTON_MIDDLE) {
450 } else if (event.button == SDL_BUTTON_RIGHT) {
456 void Interface::HandleRelease(const SDL_MouseButtonEvent &event) {
457 if (config.mouse_disabled) return;
459 if (event.button == SDL_BUTTON_LEFT) {
461 } else if (event.button == SDL_BUTTON_RIGHT) {
466 void Interface::PickBlock() {
467 if (!aim_chunk) return;
468 selection = aim_chunk->BlockAt(aim_block);
469 hud.Display(selection);
472 void Interface::PlaceBlock() {
473 if (!aim_chunk) return;
474 Chunk *mod_chunk = aim_chunk;
475 glm::vec3 next_pos = Chunk::ToCoords(aim_block) + aim_normal;
476 if (!Chunk::InBounds(next_pos)) {
477 mod_chunk = &world.Next(*aim_chunk, aim_normal);
478 next_pos -= aim_normal * glm::vec3(Chunk::Extent());
480 mod_chunk->SetBlock(next_pos, selection);
482 if (config.audio_disabled) return;
483 const Entity &player = ctrl.Controlled();
486 mod_chunk->ToSceneCoords(player.ChunkCoords(), next_pos)
490 void Interface::RemoveBlock() noexcept {
491 if (!aim_chunk) return;
492 aim_chunk->SetBlock(aim_block, remove);
494 if (config.audio_disabled) return;
495 const Entity &player = ctrl.Controlled();
498 aim_chunk->ToSceneCoords(player.ChunkCoords(), Chunk::ToCoords(aim_block))
503 void Interface::Handle(const SDL_MouseWheelEvent &event) {
504 if (config.mouse_disabled) return;
508 } else if (event.y > 0) {
513 void Interface::SelectNext() {
515 if (size_t(selection.type) >= world.BlockTypes().Size()) {
518 hud.Display(selection);
521 void Interface::SelectPrevious() {
523 if (selection.type <= 0) {
524 selection.type = world.BlockTypes().Size() - 1;
526 hud.Display(selection);
530 void Interface::PostMessage(const char *msg) {
531 messages.PushLine(msg);
534 std::cout << msg << std::endl;
538 void Interface::Update(int dt) {
539 ctrl.Velocity(glm::vec3(fwd - rev) * config.move_velocity);
542 msg_timer.Update(dt);
543 place_timer.Update(dt);
544 remove_timer.Update(dt);
549 if (msg_timer.HitOnce()) {
553 if (remove_timer.Hit()) {
558 if (place_timer.Hit()) {
564 if (env.counter.Changed()) {
574 OutlineModel::Buffer outl_buf;
578 void Interface::CheckAim() {
580 glm::vec3 chunk_normal;
581 if (world.Intersection(aim, glm::mat4(1.0f), aim_chunk, aim_block, chunk_dist, chunk_normal)) {
586 glm::vec3 entity_normal;
587 if (!world.Intersection(aim, glm::mat4(1.0f), aim_entity, entity_dist, entity_normal)) {
588 aim_entity = nullptr;
590 if (aim_chunk && aim_entity) {
591 // got both, pick the closest one
592 if (chunk_dist < entity_dist) {
593 aim_normal = chunk_normal;
595 aim_entity = nullptr;
597 aim_normal = entity_normal;
600 } else if (aim_chunk) {
601 aim_normal = chunk_normal;
603 } else if (aim_entity) {
604 aim_normal = entity_normal;
611 void Interface::UpdateOutline() {
613 aim_chunk->Type(aim_chunk->BlockAt(aim_block)).FillOutlineModel(outl_buf);
614 outline.Update(outl_buf);
615 outline_transform = aim_chunk->Transform(world.Player().ChunkCoords());
616 outline_transform *= aim_chunk->ToTransform(Chunk::ToPos(aim_block), aim_block);
617 outline_transform *= glm::scale(glm::vec3(1.005f));
621 void Interface::Render(Viewport &viewport) noexcept {
622 if (config.visual_disabled) return;
625 PlainColor &outline_prog = viewport.WorldOutlineProgram();
626 outline_prog.SetM(outline_transform);
631 counter_text.Render(viewport);
632 position_text.Render(viewport);
633 orientation_text.Render(viewport);
634 block_text.Render(viewport);
637 if (msg_timer.Running()) {
638 messages.Render(viewport);
641 hud.Render(viewport);
650 void Keymap::Map(SDL_Scancode scancode, Action action) {
651 if (scancode > MAX_SCANCODE) {
652 throw std::runtime_error("refusing to map scancode: too damn high");
654 codemap[scancode] = action;
657 Keymap::Action Keymap::Lookup(SDL_Scancode scancode) {
658 if (scancode < NUM_SCANCODES) {
659 return codemap[scancode];
666 void Keymap::LoadDefault() {
667 Map(SDL_SCANCODE_UP, MOVE_FORWARD);
668 Map(SDL_SCANCODE_W, MOVE_FORWARD);
669 Map(SDL_SCANCODE_DOWN, MOVE_BACKWARD);
670 Map(SDL_SCANCODE_S, MOVE_BACKWARD);
671 Map(SDL_SCANCODE_LEFT, MOVE_LEFT);
672 Map(SDL_SCANCODE_A, MOVE_LEFT);
673 Map(SDL_SCANCODE_RIGHT, MOVE_RIGHT);
674 Map(SDL_SCANCODE_D, MOVE_RIGHT);
675 Map(SDL_SCANCODE_SPACE, MOVE_UP);
676 Map(SDL_SCANCODE_RSHIFT, MOVE_UP);
677 Map(SDL_SCANCODE_LSHIFT, MOVE_DOWN);
678 Map(SDL_SCANCODE_LCTRL, MOVE_DOWN);
679 Map(SDL_SCANCODE_RCTRL, MOVE_DOWN);
681 Map(SDL_SCANCODE_Q, BLOCK_FACE);
682 Map(SDL_SCANCODE_E, BLOCK_TURN);
683 Map(SDL_SCANCODE_TAB, BLOCK_NEXT);
684 Map(SDL_SCANCODE_RIGHTBRACKET, BLOCK_NEXT);
685 Map(SDL_SCANCODE_LEFTBRACKET, BLOCK_PREV);
687 Map(SDL_SCANCODE_INSERT, BLOCK_PLACE);
688 Map(SDL_SCANCODE_RETURN, BLOCK_PLACE);
689 Map(SDL_SCANCODE_MENU, BLOCK_PICK);
690 Map(SDL_SCANCODE_DELETE, BLOCK_REMOVE);
691 Map(SDL_SCANCODE_BACKSPACE, BLOCK_REMOVE);
693 Map(SDL_SCANCODE_N, TOGGLE_COLLISION);
694 Map(SDL_SCANCODE_F1, TOGGLE_VISUAL);
695 Map(SDL_SCANCODE_F3, TOGGLE_DEBUG);
696 Map(SDL_SCANCODE_F4, TOGGLE_AUDIO);
698 Map(SDL_SCANCODE_B, PRINT_BLOCK);
699 Map(SDL_SCANCODE_C, PRINT_CHUNK);
700 Map(SDL_SCANCODE_L, PRINT_LIGHT);
701 Map(SDL_SCANCODE_P, PRINT_SELECTION);
703 Map(SDL_SCANCODE_ESCAPE, EXIT);
707 void Keymap::Load(std::istream &is) {
708 TokenStreamReader in(is);
709 std::string key_name;
710 std::string action_name;
713 while (in.HasMore()) {
714 if (in.Peek().type == Token::STRING) {
715 in.ReadString(key_name);
716 key = SDL_GetScancodeFromName(key_name.c_str());
718 key = SDL_Scancode(in.GetInt());
720 in.Skip(Token::EQUALS);
721 in.ReadIdentifier(action_name);
722 action = StringToAction(action_name);
723 if (in.HasMore() && in.Peek().type == Token::SEMICOLON) {
724 in.Skip(Token::SEMICOLON);
730 void Keymap::Save(std::ostream &out) {
731 for (unsigned int i = 0; i < NUM_SCANCODES; ++i) {
732 if (codemap[i] == NONE) continue;
734 const char *str = SDL_GetScancodeName(SDL_Scancode(i));
750 out << " = " << ActionToString(codemap[i]) << std::endl;;
755 const char *Keymap::ActionToString(Action action) {
761 return "move_forward";
763 return "move_backward";
781 return "block_place";
785 return "block_remove";
786 case TOGGLE_COLLISION:
787 return "toggle_collision";
789 return "toggle_audio";
791 return "toggle_visual";
793 return "toggle_debug";
795 return "print_block";
797 return "print_chunk";
799 return "print_light";
800 case PRINT_SELECTION:
801 return "print_selection";
807 Keymap::Action Keymap::StringToAction(const std::string &str) {
808 if (str == "move_forward") {
810 } else if (str == "move_backward") {
811 return MOVE_BACKWARD;
812 } else if (str == "move_left") {
814 } else if (str == "move_right") {
816 } else if (str == "move_up") {
818 } else if (str == "move_down") {
820 } else if (str == "block_face") {
822 } else if (str == "block_turn") {
824 } else if (str == "block_next") {
826 } else if (str == "block_prev") {
828 } else if (str == "block_place") {
830 } else if (str == "block_pick") {
832 } else if (str == "block_remove") {
834 } else if (str == "toggle_collision") {
835 return TOGGLE_COLLISION;
836 } else if (str == "toggle_audio") {
838 } else if (str == "toggle_visual") {
839 return TOGGLE_VISUAL;
840 } else if (str == "toggle_debug") {
842 } else if (str == "print_block") {
844 } else if (str == "print_chunk") {
846 } else if (str == "print_light") {
848 } else if (str == "print_selection") {
849 return PRINT_SELECTION;
850 } else if (str == "exit") {