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::DisplayNone() {
63 block_visible = false;
66 void HUD::Display(const Block &b) {
67 const BlockType &type = types.Get(b.type);
70 type.FillEntityModel(block_buf, b.Transform());
71 block.Update(block_buf);
73 block_label.Set(font, type.label);
75 block_visible = type.visible;
79 void HUD::Render(Viewport &viewport) noexcept {
80 viewport.ClearDepth();
82 PlainColor &outline_prog = viewport.HUDOutlineProgram();
83 viewport.EnableInvertBlending();
84 viewport.SetCursor(glm::vec3(0.0f), Gravity::CENTER);
85 outline_prog.SetM(viewport.Cursor());
89 DirectionalLighting &world_prog = viewport.HUDProgram();
90 world_prog.SetLightDirection({ 1.0f, 3.0f, 5.0f });
91 // disable distance fog
92 world_prog.SetFogDensity(0.0f);
94 viewport.DisableBlending();
95 world_prog.SetM(block_transform);
97 block_label.Render(viewport);
102 Interface::Interface(
103 const Config &config,
108 , ctrl(world.Player())
109 , hud(world.BlockTypes(), env.assets.small_ui_font)
110 , aim{{ 0, 0, 0 }, { 0, 0, -1 }}
114 , outline_transform(1.0f)
120 , last_entity(nullptr)
121 , messages(env.assets.small_ui_font)
128 , place_sound(env.loader.LoadSound("thump"))
129 , remove_sound(env.loader.LoadSound("plop"))
133 counter_text.Position(glm::vec3(-25.0f, 25.0f, 0.0f), Gravity::NORTH_EAST);
134 counter_text.Foreground(glm::vec4(1.0f));
135 counter_text.Background(glm::vec4(0.5f));
136 position_text.Position(glm::vec3(-25.0f, 25.0f + env.assets.small_ui_font.LineSkip(), 0.0f), Gravity::NORTH_EAST);
137 position_text.Foreground(glm::vec4(1.0f));
138 position_text.Background(glm::vec4(0.5f));
139 orientation_text.Position(glm::vec3(-25.0f, 25.0f + 2 * env.assets.small_ui_font.LineSkip(), 0.0f), Gravity::NORTH_EAST);
140 orientation_text.Foreground(glm::vec4(1.0f));
141 orientation_text.Background(glm::vec4(0.5f));
142 block_text.Position(glm::vec3(-25.0f, 25.0f + 4 * env.assets.small_ui_font.LineSkip(), 0.0f), Gravity::NORTH_EAST);
143 block_text.Foreground(glm::vec4(1.0f));
144 block_text.Background(glm::vec4(0.5f));
145 block_text.Set(env.assets.small_ui_font, "Block: none");
146 entity_text.Position(glm::vec3(-25.0f, 25.0f + 4 * env.assets.small_ui_font.LineSkip(), 0.0f), Gravity::NORTH_EAST);
147 entity_text.Foreground(glm::vec4(1.0f));
148 entity_text.Background(glm::vec4(0.5f));
149 entity_text.Set(env.assets.small_ui_font, "Entity: none");
150 messages.Position(glm::vec3(25.0f, -25.0f, 0.0f), Gravity::SOUTH_WEST);
151 messages.Foreground(glm::vec4(1.0f));
152 messages.Background(glm::vec4(0.5f));
157 void Interface::HandlePress(const SDL_KeyboardEvent &event) {
158 if (config.keyboard_disabled) return;
160 switch (env.keymap.Lookup(event)) {
161 case Keymap::MOVE_FORWARD:
164 case Keymap::MOVE_BACKWARD:
167 case Keymap::MOVE_LEFT:
170 case Keymap::MOVE_RIGHT:
173 case Keymap::MOVE_UP:
176 case Keymap::MOVE_DOWN:
180 case Keymap::BLOCK_FACE:
183 case Keymap::BLOCK_TURN:
186 case Keymap::BLOCK_NEXT:
189 case Keymap::BLOCK_PREV:
193 case Keymap::BLOCK_PLACE:
196 case Keymap::BLOCK_PICK:
199 case Keymap::BLOCK_REMOVE:
203 case Keymap::TOGGLE_COLLISION:
207 case Keymap::PRINT_BLOCK:
210 case Keymap::PRINT_CHUNK:
213 case Keymap::PRINT_LIGHT:
216 case Keymap::PRINT_SELECTION:
217 PrintSelectionInfo();
220 case Keymap::TOGGLE_VISUAL:
223 case Keymap::TOGGLE_DEBUG:
226 case Keymap::TOGGLE_AUDIO:
239 void Interface::HandleRelease(const SDL_KeyboardEvent &event) {
240 if (config.keyboard_disabled) return;
242 switch (env.keymap.Lookup(event)) {
243 case Keymap::MOVE_FORWARD:
246 case Keymap::MOVE_BACKWARD:
249 case Keymap::MOVE_LEFT:
252 case Keymap::MOVE_RIGHT:
255 case Keymap::MOVE_UP:
258 case Keymap::MOVE_DOWN:
267 void Interface::FaceBlock() {
268 selection.SetFace(Block::Face((selection.GetFace() + 1) % Block::FACE_COUNT));
269 hud.Display(selection);
272 void Interface::TurnBlock() {
273 selection.SetTurn(Block::Turn((selection.GetTurn() + 1) % Block::TURN_COUNT));
274 hud.Display(selection);
277 void Interface::ToggleCollision() {
278 ctrl.Controlled().WorldCollidable(!ctrl.Controlled().WorldCollidable());
279 if (ctrl.Controlled().WorldCollidable()) {
280 PostMessage("collision on");
282 PostMessage("collision off");
286 void Interface::PrintBlockInfo() {
287 std::cout << std::endl;
289 PostMessage("not looking at any block");
290 Ray aim = ctrl.Aim();
292 s << "aim ray: " << aim.orig << ", " << aim.dir;
293 PostMessage(s.str());
297 s << "looking at block " << aim_world.block
298 << " " << aim_world.BlockCoords()
299 << " of chunk " << aim_world.GetChunk().Position()
301 PostMessage(s.str());
302 Print(aim_world.GetBlock());
305 void Interface::PrintChunkInfo() {
306 std::cout << std::endl;
308 PostMessage("not looking at any block");
312 s << "looking at chunk " << aim_world.GetChunk().Position();
313 PostMessage(s.str());
315 PostMessage(" neighbors:");
316 if (aim_world.GetChunk().HasNeighbor(Block::FACE_LEFT)) {
318 s << " left " << aim_world.GetChunk().GetNeighbor(Block::FACE_LEFT).Position();
319 PostMessage(s.str());
321 if (aim_world.GetChunk().HasNeighbor(Block::FACE_RIGHT)) {
323 s << " right " << aim_world.GetChunk().GetNeighbor(Block::FACE_RIGHT).Position();
324 PostMessage(s.str());
326 if (aim_world.GetChunk().HasNeighbor(Block::FACE_UP)) {
328 s << " up " << aim_world.GetChunk().GetNeighbor(Block::FACE_UP).Position();
329 PostMessage(s.str());
331 if (aim_world.GetChunk().HasNeighbor(Block::FACE_DOWN)) {
333 s << " down " << aim_world.GetChunk().GetNeighbor(Block::FACE_DOWN).Position();
334 PostMessage(s.str());
336 if (aim_world.GetChunk().HasNeighbor(Block::FACE_FRONT)) {
338 s << " front " << aim_world.GetChunk().GetNeighbor(Block::FACE_FRONT).Position();
339 PostMessage(s.str());
341 if (aim_world.GetChunk().HasNeighbor(Block::FACE_BACK)) {
343 s << " back " << aim_world.GetChunk().GetNeighbor(Block::FACE_BACK).Position();
344 PostMessage(s.str());
346 std::cout << std::endl;
349 void Interface::PrintLightInfo() {
352 << "light level " << world.PlayerChunk().GetLight(world.Player().Position())
353 << " at position " << world.Player().Position()
355 PostMessage(s.str());
358 void Interface::PrintSelectionInfo() {
359 std::cout << std::endl;
363 void Interface::Print(const Block &block) {
365 s << "type: " << block.type
366 << ", face: " << block.GetFace()
367 << ", turn: " << block.GetTurn()
369 PostMessage(s.str());
372 void Interface::ToggleAudio() {
373 config.audio_disabled = !config.audio_disabled;
374 if (config.audio_disabled) {
375 PostMessage("audio off");
377 PostMessage("audio on");
381 void Interface::ToggleVisual() {
382 config.visual_disabled = !config.visual_disabled;
383 if (config.visual_disabled) {
384 PostMessage("visual off");
386 PostMessage("visual on");
390 void Interface::ToggleDebug() {
401 void Interface::UpdateCounter() {
403 s << std::setprecision(3) <<
404 "avg: " << env.counter.Average().running << "ms, "
405 "peak: " << env.counter.Peak().running << "ms";
406 std::string text = s.str();
407 counter_text.Set(env.assets.small_ui_font, text);
410 void Interface::UpdatePosition() {
412 s << std::setprecision(3) << "pos: " << ctrl.Controlled().AbsolutePosition();
413 position_text.Set(env.assets.small_ui_font, s.str());
416 void Interface::UpdateOrientation() {
418 s << std::setprecision(3) << "pitch: " << rad2deg(ctrl.Pitch())
419 << ", yaw: " << rad2deg(ctrl.Yaw());
420 orientation_text.Set(env.assets.small_ui_font, s.str());
423 void Interface::UpdateBlockInfo() {
425 const Block &block = aim_world.GetBlock();
426 if (last_block != block) {
429 << aim_world.GetType().label
430 << ", face: " << block.GetFace()
431 << ", turn: " << block.GetTurn();
432 block_text.Set(env.assets.small_ui_font, s.str());
436 if (last_block != Block()) {
439 block_text.Set(env.assets.small_ui_font, s.str());
440 last_block = Block();
445 void Interface::UpdateEntityInfo() {
447 if (last_entity != aim_entity.entity) {
449 s << "Entity: " << aim_entity.entity->Name();
450 entity_text.Set(env.assets.small_ui_font, s.str());
451 last_entity = aim_entity.entity;
457 void Interface::Handle(const SDL_MouseMotionEvent &event) {
458 if (config.mouse_disabled) return;
459 ctrl.RotateYaw(event.xrel * config.yaw_sensitivity);
460 ctrl.RotatePitch(event.yrel * config.pitch_sensitivity);
463 void Interface::HandlePress(const SDL_MouseButtonEvent &event) {
464 if (config.mouse_disabled) return;
466 if (event.button == SDL_BUTTON_LEFT) {
468 remove_timer.Start();
469 } else if (event.button == SDL_BUTTON_MIDDLE) {
471 } else if (event.button == SDL_BUTTON_RIGHT) {
477 void Interface::HandleRelease(const SDL_MouseButtonEvent &event) {
478 if (config.mouse_disabled) return;
480 if (event.button == SDL_BUTTON_LEFT) {
482 } else if (event.button == SDL_BUTTON_RIGHT) {
487 void Interface::PickBlock() {
488 if (!aim_world) return;
489 selection = aim_world.GetBlock();
490 hud.Display(selection);
493 void Interface::PlaceBlock() {
494 if (!aim_world) return;
496 glm::vec3 next_pos = aim_world.BlockCoords() + aim_world.normal;
497 BlockLookup next_block(&aim_world.GetChunk(), next_pos);
500 next_block.SetBlock(selection);
502 if (config.audio_disabled) return;
503 const Entity &player = ctrl.Controlled();
506 aim_world.GetChunk().ToSceneCoords(player.ChunkCoords(), next_pos)
510 void Interface::RemoveBlock() noexcept {
511 if (!aim_world) return;
512 aim_world.SetBlock(remove);
514 if (config.audio_disabled) return;
515 const Entity &player = ctrl.Controlled();
518 aim_world.GetChunk().ToSceneCoords(player.ChunkCoords(), aim_world.BlockCoords())
523 void Interface::Handle(const SDL_MouseWheelEvent &event) {
524 if (config.mouse_disabled) return;
528 } else if (event.y > 0) {
533 void Interface::SelectNext() {
535 if (size_t(selection.type) >= world.BlockTypes().Size()) {
538 hud.Display(selection);
541 void Interface::SelectPrevious() {
543 if (selection.type <= 0) {
544 selection.type = world.BlockTypes().Size() - 1;
546 hud.Display(selection);
550 void Interface::PostMessage(const char *msg) {
551 messages.PushLine(msg);
554 std::cout << msg << std::endl;
558 void Interface::Update(int dt) {
559 ctrl.Velocity(glm::vec3(fwd - rev) * config.move_velocity);
562 msg_timer.Update(dt);
563 place_timer.Update(dt);
564 remove_timer.Update(dt);
569 if (msg_timer.HitOnce()) {
573 if (remove_timer.Hit()) {
578 if (place_timer.Hit()) {
584 if (env.counter.Changed()) {
594 OutlineModel::Buffer outl_buf;
598 void Interface::CheckAim() {
599 if (!world.Intersection(aim, glm::mat4(1.0f), ctrl.Controlled().ChunkCoords(), aim_world)) {
600 aim_world = WorldCollision();
602 if (!world.Intersection(aim, glm::mat4(1.0f), ctrl.Controlled(), aim_entity)) {
603 aim_entity = EntityCollision();
605 if (aim_world && aim_entity) {
606 // got both, pick the closest one
607 if (aim_world.depth < aim_entity.depth) {
609 aim_entity = EntityCollision();
611 aim_world = WorldCollision();
613 } else if (aim_world) {
622 void Interface::UpdateOutline() {
624 aim_world.GetType().FillOutlineModel(outl_buf);
625 outline.Update(outl_buf);
626 outline_transform = aim_world.GetChunk().Transform(world.Player().ChunkCoords());
627 outline_transform *= aim_world.BlockTransform();
628 outline_transform *= glm::scale(glm::vec3(1.005f));
632 void Interface::Render(Viewport &viewport) noexcept {
633 if (config.visual_disabled) return;
636 PlainColor &outline_prog = viewport.WorldOutlineProgram();
637 outline_prog.SetM(outline_transform);
642 counter_text.Render(viewport);
643 position_text.Render(viewport);
644 orientation_text.Render(viewport);
646 block_text.Render(viewport);
647 } else if (aim_entity) {
648 entity_text.Render(viewport);
652 if (msg_timer.Running()) {
653 messages.Render(viewport);
656 hud.Render(viewport);
665 void Keymap::Map(SDL_Scancode scancode, Action action) {
666 if (scancode > MAX_SCANCODE) {
667 throw std::runtime_error("refusing to map scancode: too damn high");
669 codemap[scancode] = action;
672 Keymap::Action Keymap::Lookup(SDL_Scancode scancode) {
673 if (scancode < NUM_SCANCODES) {
674 return codemap[scancode];
681 void Keymap::LoadDefault() {
682 Map(SDL_SCANCODE_UP, MOVE_FORWARD);
683 Map(SDL_SCANCODE_W, MOVE_FORWARD);
684 Map(SDL_SCANCODE_DOWN, MOVE_BACKWARD);
685 Map(SDL_SCANCODE_S, MOVE_BACKWARD);
686 Map(SDL_SCANCODE_LEFT, MOVE_LEFT);
687 Map(SDL_SCANCODE_A, MOVE_LEFT);
688 Map(SDL_SCANCODE_RIGHT, MOVE_RIGHT);
689 Map(SDL_SCANCODE_D, MOVE_RIGHT);
690 Map(SDL_SCANCODE_SPACE, MOVE_UP);
691 Map(SDL_SCANCODE_RSHIFT, MOVE_UP);
692 Map(SDL_SCANCODE_LSHIFT, MOVE_DOWN);
693 Map(SDL_SCANCODE_LCTRL, MOVE_DOWN);
694 Map(SDL_SCANCODE_RCTRL, MOVE_DOWN);
696 Map(SDL_SCANCODE_Q, BLOCK_FACE);
697 Map(SDL_SCANCODE_E, BLOCK_TURN);
698 Map(SDL_SCANCODE_TAB, BLOCK_NEXT);
699 Map(SDL_SCANCODE_RIGHTBRACKET, BLOCK_NEXT);
700 Map(SDL_SCANCODE_LEFTBRACKET, BLOCK_PREV);
702 Map(SDL_SCANCODE_INSERT, BLOCK_PLACE);
703 Map(SDL_SCANCODE_RETURN, BLOCK_PLACE);
704 Map(SDL_SCANCODE_MENU, BLOCK_PICK);
705 Map(SDL_SCANCODE_DELETE, BLOCK_REMOVE);
706 Map(SDL_SCANCODE_BACKSPACE, BLOCK_REMOVE);
708 Map(SDL_SCANCODE_N, TOGGLE_COLLISION);
709 Map(SDL_SCANCODE_F1, TOGGLE_VISUAL);
710 Map(SDL_SCANCODE_F3, TOGGLE_DEBUG);
711 Map(SDL_SCANCODE_F4, TOGGLE_AUDIO);
713 Map(SDL_SCANCODE_B, PRINT_BLOCK);
714 Map(SDL_SCANCODE_C, PRINT_CHUNK);
715 Map(SDL_SCANCODE_L, PRINT_LIGHT);
716 Map(SDL_SCANCODE_P, PRINT_SELECTION);
718 Map(SDL_SCANCODE_ESCAPE, EXIT);
722 void Keymap::Load(std::istream &is) {
723 TokenStreamReader in(is);
724 std::string key_name;
725 std::string action_name;
728 while (in.HasMore()) {
729 if (in.Peek().type == Token::STRING) {
730 in.ReadString(key_name);
731 key = SDL_GetScancodeFromName(key_name.c_str());
733 key = SDL_Scancode(in.GetInt());
735 in.Skip(Token::EQUALS);
736 in.ReadIdentifier(action_name);
737 action = StringToAction(action_name);
738 if (in.HasMore() && in.Peek().type == Token::SEMICOLON) {
739 in.Skip(Token::SEMICOLON);
745 void Keymap::Save(std::ostream &out) {
746 for (unsigned int i = 0; i < NUM_SCANCODES; ++i) {
747 if (codemap[i] == NONE) continue;
749 const char *str = SDL_GetScancodeName(SDL_Scancode(i));
765 out << " = " << ActionToString(codemap[i]) << std::endl;;
770 const char *Keymap::ActionToString(Action action) {
776 return "move_forward";
778 return "move_backward";
796 return "block_place";
800 return "block_remove";
801 case TOGGLE_COLLISION:
802 return "toggle_collision";
804 return "toggle_audio";
806 return "toggle_visual";
808 return "toggle_debug";
810 return "print_block";
812 return "print_chunk";
814 return "print_light";
815 case PRINT_SELECTION:
816 return "print_selection";
822 Keymap::Action Keymap::StringToAction(const std::string &str) {
823 if (str == "move_forward") {
825 } else if (str == "move_backward") {
826 return MOVE_BACKWARD;
827 } else if (str == "move_left") {
829 } else if (str == "move_right") {
831 } else if (str == "move_up") {
833 } else if (str == "move_down") {
835 } else if (str == "block_face") {
837 } else if (str == "block_turn") {
839 } else if (str == "block_next") {
841 } else if (str == "block_prev") {
843 } else if (str == "block_place") {
845 } else if (str == "block_pick") {
847 } else if (str == "block_remove") {
849 } else if (str == "toggle_collision") {
850 return TOGGLE_COLLISION;
851 } else if (str == "toggle_audio") {
853 } else if (str == "toggle_visual") {
854 return TOGGLE_VISUAL;
855 } else if (str == "toggle_debug") {
857 } else if (str == "print_block") {
859 } else if (str == "print_chunk") {
861 } else if (str == "print_light") {
863 } else if (str == "print_selection") {
864 return PRINT_SELECTION;
865 } else if (str == "exit") {