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/BlockLookup.hpp"
15 #include "../world/World.hpp"
21 #include <glm/gtc/matrix_transform.hpp>
22 #include <glm/gtx/io.hpp>
27 HUD::HUD(const BlockTypeRegistry &types, const Font &font)
32 , block_transform(1.0f)
34 , block_visible(false)
36 block_transform = glm::translate(block_transform, glm::vec3(50.0f, 50.0f, 0.0f));
37 block_transform = glm::scale(block_transform, glm::vec3(50.0f));
38 block_transform = glm::rotate(block_transform, 3.5f, glm::vec3(1.0f, 0.0f, 0.0f));
39 block_transform = glm::rotate(block_transform, 0.35f, glm::vec3(0.0f, 1.0f, 0.0f));
41 OutlineModel::Buffer buf;
42 buf.vertices = std::vector<glm::vec3>({
43 { -10.0f, 0.0f, 0.0f }, { 10.0f, 0.0f, 0.0f },
44 { 0.0f, -10.0f, 0.0f }, { 0.0f, 10.0f, 0.0f },
46 buf.indices = std::vector<OutlineModel::Index>({
49 buf.colors.resize(4, { 10.0f, 10.0f, 10.0f });
50 crosshair.Update(buf);
53 glm::vec3(50.0f, 85.0f, 0.0f),
57 block_label.Foreground(glm::vec4(1.0f));
58 block_label.Background(glm::vec4(0.5f));
62 void HUD::Display(const Block &b) {
63 const BlockType &type = types.Get(b.type);
66 type.FillEntityModel(block_buf, b.Transform());
67 block.Update(block_buf);
69 block_label.Set(font, type.label);
71 block_visible = type.visible;
75 void HUD::Render(Viewport &viewport) noexcept {
76 viewport.ClearDepth();
78 PlainColor &outline_prog = viewport.HUDOutlineProgram();
79 viewport.EnableInvertBlending();
80 viewport.SetCursor(glm::vec3(0.0f), Gravity::CENTER);
81 outline_prog.SetM(viewport.Cursor());
85 DirectionalLighting &world_prog = viewport.HUDProgram();
86 world_prog.SetLightDirection({ 1.0f, 3.0f, 5.0f });
87 // disable distance fog
88 world_prog.SetFogDensity(0.0f);
90 viewport.DisableBlending();
91 world_prog.SetM(block_transform);
93 block_label.Render(viewport);
104 , ctrl(world.Player())
105 , hud(world.BlockTypes(), env.assets.small_ui_font)
106 , 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_world.block
289 << " " << aim_world.BlockCoords()
290 << " of chunk " << aim_world.GetChunk().Position()
292 PostMessage(s.str());
293 Print(aim_world.GetBlock());
296 void Interface::PrintChunkInfo() {
297 std::cout << std::endl;
299 PostMessage("not looking at any block");
303 s << "looking at chunk " << aim_world.GetChunk().Position();
304 PostMessage(s.str());
306 PostMessage(" neighbors:");
307 if (aim_world.GetChunk().HasNeighbor(Block::FACE_LEFT)) {
309 s << " left " << aim_world.GetChunk().GetNeighbor(Block::FACE_LEFT).Position();
310 PostMessage(s.str());
312 if (aim_world.GetChunk().HasNeighbor(Block::FACE_RIGHT)) {
314 s << " right " << aim_world.GetChunk().GetNeighbor(Block::FACE_RIGHT).Position();
315 PostMessage(s.str());
317 if (aim_world.GetChunk().HasNeighbor(Block::FACE_UP)) {
319 s << " up " << aim_world.GetChunk().GetNeighbor(Block::FACE_UP).Position();
320 PostMessage(s.str());
322 if (aim_world.GetChunk().HasNeighbor(Block::FACE_DOWN)) {
324 s << " down " << aim_world.GetChunk().GetNeighbor(Block::FACE_DOWN).Position();
325 PostMessage(s.str());
327 if (aim_world.GetChunk().HasNeighbor(Block::FACE_FRONT)) {
329 s << " front " << aim_world.GetChunk().GetNeighbor(Block::FACE_FRONT).Position();
330 PostMessage(s.str());
332 if (aim_world.GetChunk().HasNeighbor(Block::FACE_BACK)) {
334 s << " back " << aim_world.GetChunk().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_world.GetBlock();
416 if (last_displayed != block) {
419 << aim_world.GetType().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_world) return;
468 selection = aim_world.GetBlock();
469 hud.Display(selection);
472 void Interface::PlaceBlock() {
473 if (!aim_world) return;
475 glm::vec3 next_pos = aim_world.BlockCoords() + aim_world.normal;
476 BlockLookup next_block(&aim_world.GetChunk(), next_pos);
479 next_block.SetBlock(selection);
481 if (config.audio_disabled) return;
482 const Entity &player = ctrl.Controlled();
485 aim_world.GetChunk().ToSceneCoords(player.ChunkCoords(), next_pos)
489 void Interface::RemoveBlock() noexcept {
490 if (!aim_world) return;
491 aim_world.SetBlock(remove);
493 if (config.audio_disabled) return;
494 const Entity &player = ctrl.Controlled();
497 aim_world.GetChunk().ToSceneCoords(player.ChunkCoords(), aim_world.BlockCoords())
502 void Interface::Handle(const SDL_MouseWheelEvent &event) {
503 if (config.mouse_disabled) return;
507 } else if (event.y > 0) {
512 void Interface::SelectNext() {
514 if (size_t(selection.type) >= world.BlockTypes().Size()) {
517 hud.Display(selection);
520 void Interface::SelectPrevious() {
522 if (selection.type <= 0) {
523 selection.type = world.BlockTypes().Size() - 1;
525 hud.Display(selection);
529 void Interface::PostMessage(const char *msg) {
530 messages.PushLine(msg);
533 std::cout << msg << std::endl;
537 void Interface::Update(int dt) {
538 ctrl.Velocity(glm::vec3(fwd - rev) * config.move_velocity);
541 msg_timer.Update(dt);
542 place_timer.Update(dt);
543 remove_timer.Update(dt);
548 if (msg_timer.HitOnce()) {
552 if (remove_timer.Hit()) {
557 if (place_timer.Hit()) {
563 if (env.counter.Changed()) {
573 OutlineModel::Buffer outl_buf;
577 void Interface::CheckAim() {
578 if (!world.Intersection(aim, glm::mat4(1.0f), aim_world)) {
579 aim_world = WorldCollision();
581 if (!world.Intersection(aim, glm::mat4(1.0f), aim_entity)) {
582 aim_entity = EntityCollision();
584 if (aim_world && aim_entity) {
585 // got both, pick the closest one
586 if (aim_world.depth < aim_entity.depth) {
588 aim_entity = EntityCollision();
590 aim_world = WorldCollision();
592 } else if (aim_world) {
600 void Interface::UpdateOutline() {
602 aim_world.GetType().FillOutlineModel(outl_buf);
603 outline.Update(outl_buf);
604 outline_transform = aim_world.GetChunk().Transform(world.Player().ChunkCoords());
605 outline_transform *= aim_world.BlockTransform();
606 outline_transform *= glm::scale(glm::vec3(1.005f));
610 void Interface::Render(Viewport &viewport) noexcept {
611 if (config.visual_disabled) return;
614 PlainColor &outline_prog = viewport.WorldOutlineProgram();
615 outline_prog.SetM(outline_transform);
620 counter_text.Render(viewport);
621 position_text.Render(viewport);
622 orientation_text.Render(viewport);
623 block_text.Render(viewport);
626 if (msg_timer.Running()) {
627 messages.Render(viewport);
630 hud.Render(viewport);
639 void Keymap::Map(SDL_Scancode scancode, Action action) {
640 if (scancode > MAX_SCANCODE) {
641 throw std::runtime_error("refusing to map scancode: too damn high");
643 codemap[scancode] = action;
646 Keymap::Action Keymap::Lookup(SDL_Scancode scancode) {
647 if (scancode < NUM_SCANCODES) {
648 return codemap[scancode];
655 void Keymap::LoadDefault() {
656 Map(SDL_SCANCODE_UP, MOVE_FORWARD);
657 Map(SDL_SCANCODE_W, MOVE_FORWARD);
658 Map(SDL_SCANCODE_DOWN, MOVE_BACKWARD);
659 Map(SDL_SCANCODE_S, MOVE_BACKWARD);
660 Map(SDL_SCANCODE_LEFT, MOVE_LEFT);
661 Map(SDL_SCANCODE_A, MOVE_LEFT);
662 Map(SDL_SCANCODE_RIGHT, MOVE_RIGHT);
663 Map(SDL_SCANCODE_D, MOVE_RIGHT);
664 Map(SDL_SCANCODE_SPACE, MOVE_UP);
665 Map(SDL_SCANCODE_RSHIFT, MOVE_UP);
666 Map(SDL_SCANCODE_LSHIFT, MOVE_DOWN);
667 Map(SDL_SCANCODE_LCTRL, MOVE_DOWN);
668 Map(SDL_SCANCODE_RCTRL, MOVE_DOWN);
670 Map(SDL_SCANCODE_Q, BLOCK_FACE);
671 Map(SDL_SCANCODE_E, BLOCK_TURN);
672 Map(SDL_SCANCODE_TAB, BLOCK_NEXT);
673 Map(SDL_SCANCODE_RIGHTBRACKET, BLOCK_NEXT);
674 Map(SDL_SCANCODE_LEFTBRACKET, BLOCK_PREV);
676 Map(SDL_SCANCODE_INSERT, BLOCK_PLACE);
677 Map(SDL_SCANCODE_RETURN, BLOCK_PLACE);
678 Map(SDL_SCANCODE_MENU, BLOCK_PICK);
679 Map(SDL_SCANCODE_DELETE, BLOCK_REMOVE);
680 Map(SDL_SCANCODE_BACKSPACE, BLOCK_REMOVE);
682 Map(SDL_SCANCODE_N, TOGGLE_COLLISION);
683 Map(SDL_SCANCODE_F1, TOGGLE_VISUAL);
684 Map(SDL_SCANCODE_F3, TOGGLE_DEBUG);
685 Map(SDL_SCANCODE_F4, TOGGLE_AUDIO);
687 Map(SDL_SCANCODE_B, PRINT_BLOCK);
688 Map(SDL_SCANCODE_C, PRINT_CHUNK);
689 Map(SDL_SCANCODE_L, PRINT_LIGHT);
690 Map(SDL_SCANCODE_P, PRINT_SELECTION);
692 Map(SDL_SCANCODE_ESCAPE, EXIT);
696 void Keymap::Load(std::istream &is) {
697 TokenStreamReader in(is);
698 std::string key_name;
699 std::string action_name;
702 while (in.HasMore()) {
703 if (in.Peek().type == Token::STRING) {
704 in.ReadString(key_name);
705 key = SDL_GetScancodeFromName(key_name.c_str());
707 key = SDL_Scancode(in.GetInt());
709 in.Skip(Token::EQUALS);
710 in.ReadIdentifier(action_name);
711 action = StringToAction(action_name);
712 if (in.HasMore() && in.Peek().type == Token::SEMICOLON) {
713 in.Skip(Token::SEMICOLON);
719 void Keymap::Save(std::ostream &out) {
720 for (unsigned int i = 0; i < NUM_SCANCODES; ++i) {
721 if (codemap[i] == NONE) continue;
723 const char *str = SDL_GetScancodeName(SDL_Scancode(i));
739 out << " = " << ActionToString(codemap[i]) << std::endl;;
744 const char *Keymap::ActionToString(Action action) {
750 return "move_forward";
752 return "move_backward";
770 return "block_place";
774 return "block_remove";
775 case TOGGLE_COLLISION:
776 return "toggle_collision";
778 return "toggle_audio";
780 return "toggle_visual";
782 return "toggle_debug";
784 return "print_block";
786 return "print_chunk";
788 return "print_light";
789 case PRINT_SELECTION:
790 return "print_selection";
796 Keymap::Action Keymap::StringToAction(const std::string &str) {
797 if (str == "move_forward") {
799 } else if (str == "move_backward") {
800 return MOVE_BACKWARD;
801 } else if (str == "move_left") {
803 } else if (str == "move_right") {
805 } else if (str == "move_up") {
807 } else if (str == "move_down") {
809 } else if (str == "block_face") {
811 } else if (str == "block_turn") {
813 } else if (str == "block_next") {
815 } else if (str == "block_prev") {
817 } else if (str == "block_place") {
819 } else if (str == "block_pick") {
821 } else if (str == "block_remove") {
823 } else if (str == "toggle_collision") {
824 return TOGGLE_COLLISION;
825 } else if (str == "toggle_audio") {
827 } else if (str == "toggle_visual") {
828 return TOGGLE_VISUAL;
829 } else if (str == "toggle_debug") {
831 } else if (str == "print_block") {
833 } else if (str == "print_chunk") {
835 } else if (str == "print_light") {
837 } else if (str == "print_selection") {
838 return PRINT_SELECTION;
839 } else if (str == "exit") {