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 , last_entity(nullptr)
117 , messages(env.assets.small_ui_font)
124 , place_sound(env.assets.LoadSound("thump"))
125 , remove_sound(env.assets.LoadSound("plop"))
129 counter_text.Position(glm::vec3(-25.0f, 25.0f, 0.0f), Gravity::NORTH_EAST);
130 counter_text.Foreground(glm::vec4(1.0f));
131 counter_text.Background(glm::vec4(0.5f));
132 position_text.Position(glm::vec3(-25.0f, 25.0f + env.assets.small_ui_font.LineSkip(), 0.0f), Gravity::NORTH_EAST);
133 position_text.Foreground(glm::vec4(1.0f));
134 position_text.Background(glm::vec4(0.5f));
135 orientation_text.Position(glm::vec3(-25.0f, 25.0f + 2 * env.assets.small_ui_font.LineSkip(), 0.0f), Gravity::NORTH_EAST);
136 orientation_text.Foreground(glm::vec4(1.0f));
137 orientation_text.Background(glm::vec4(0.5f));
138 block_text.Position(glm::vec3(-25.0f, 25.0f + 4 * env.assets.small_ui_font.LineSkip(), 0.0f), Gravity::NORTH_EAST);
139 block_text.Foreground(glm::vec4(1.0f));
140 block_text.Background(glm::vec4(0.5f));
141 block_text.Set(env.assets.small_ui_font, "Block: none");
142 entity_text.Position(glm::vec3(-25.0f, 25.0f + 4 * env.assets.small_ui_font.LineSkip(), 0.0f), Gravity::NORTH_EAST);
143 entity_text.Foreground(glm::vec4(1.0f));
144 entity_text.Background(glm::vec4(0.5f));
145 entity_text.Set(env.assets.small_ui_font, "Entity: none");
146 messages.Position(glm::vec3(25.0f, -25.0f, 0.0f), Gravity::SOUTH_WEST);
147 messages.Foreground(glm::vec4(1.0f));
148 messages.Background(glm::vec4(0.5f));
149 hud.Display(selection);
153 void Interface::HandlePress(const SDL_KeyboardEvent &event) {
154 if (config.keyboard_disabled) return;
156 switch (env.keymap.Lookup(event)) {
157 case Keymap::MOVE_FORWARD:
160 case Keymap::MOVE_BACKWARD:
163 case Keymap::MOVE_LEFT:
166 case Keymap::MOVE_RIGHT:
169 case Keymap::MOVE_UP:
172 case Keymap::MOVE_DOWN:
176 case Keymap::BLOCK_FACE:
179 case Keymap::BLOCK_TURN:
182 case Keymap::BLOCK_NEXT:
185 case Keymap::BLOCK_PREV:
189 case Keymap::BLOCK_PLACE:
192 case Keymap::BLOCK_PICK:
195 case Keymap::BLOCK_REMOVE:
199 case Keymap::TOGGLE_COLLISION:
203 case Keymap::PRINT_BLOCK:
206 case Keymap::PRINT_CHUNK:
209 case Keymap::PRINT_LIGHT:
212 case Keymap::PRINT_SELECTION:
213 PrintSelectionInfo();
216 case Keymap::TOGGLE_VISUAL:
219 case Keymap::TOGGLE_DEBUG:
222 case Keymap::TOGGLE_AUDIO:
235 void Interface::HandleRelease(const SDL_KeyboardEvent &event) {
236 if (config.keyboard_disabled) return;
238 switch (env.keymap.Lookup(event)) {
239 case Keymap::MOVE_FORWARD:
242 case Keymap::MOVE_BACKWARD:
245 case Keymap::MOVE_LEFT:
248 case Keymap::MOVE_RIGHT:
251 case Keymap::MOVE_UP:
254 case Keymap::MOVE_DOWN:
263 void Interface::FaceBlock() {
264 selection.SetFace(Block::Face((selection.GetFace() + 1) % Block::FACE_COUNT));
265 hud.Display(selection);
268 void Interface::TurnBlock() {
269 selection.SetTurn(Block::Turn((selection.GetTurn() + 1) % Block::TURN_COUNT));
270 hud.Display(selection);
273 void Interface::ToggleCollision() {
274 ctrl.Controlled().WorldCollidable(!ctrl.Controlled().WorldCollidable());
275 if (ctrl.Controlled().WorldCollidable()) {
276 PostMessage("collision on");
278 PostMessage("collision off");
282 void Interface::PrintBlockInfo() {
283 std::cout << std::endl;
285 PostMessage("not looking at any block");
286 Ray aim = ctrl.Aim();
288 s << "aim ray: " << aim.orig << ", " << aim.dir;
289 PostMessage(s.str());
293 s << "looking at block " << aim_world.block
294 << " " << aim_world.BlockCoords()
295 << " of chunk " << aim_world.GetChunk().Position()
297 PostMessage(s.str());
298 Print(aim_world.GetBlock());
301 void Interface::PrintChunkInfo() {
302 std::cout << std::endl;
304 PostMessage("not looking at any block");
308 s << "looking at chunk " << aim_world.GetChunk().Position();
309 PostMessage(s.str());
311 PostMessage(" neighbors:");
312 if (aim_world.GetChunk().HasNeighbor(Block::FACE_LEFT)) {
314 s << " left " << aim_world.GetChunk().GetNeighbor(Block::FACE_LEFT).Position();
315 PostMessage(s.str());
317 if (aim_world.GetChunk().HasNeighbor(Block::FACE_RIGHT)) {
319 s << " right " << aim_world.GetChunk().GetNeighbor(Block::FACE_RIGHT).Position();
320 PostMessage(s.str());
322 if (aim_world.GetChunk().HasNeighbor(Block::FACE_UP)) {
324 s << " up " << aim_world.GetChunk().GetNeighbor(Block::FACE_UP).Position();
325 PostMessage(s.str());
327 if (aim_world.GetChunk().HasNeighbor(Block::FACE_DOWN)) {
329 s << " down " << aim_world.GetChunk().GetNeighbor(Block::FACE_DOWN).Position();
330 PostMessage(s.str());
332 if (aim_world.GetChunk().HasNeighbor(Block::FACE_FRONT)) {
334 s << " front " << aim_world.GetChunk().GetNeighbor(Block::FACE_FRONT).Position();
335 PostMessage(s.str());
337 if (aim_world.GetChunk().HasNeighbor(Block::FACE_BACK)) {
339 s << " back " << aim_world.GetChunk().GetNeighbor(Block::FACE_BACK).Position();
340 PostMessage(s.str());
342 std::cout << std::endl;
345 void Interface::PrintLightInfo() {
348 << "light level " << world.PlayerChunk().GetLight(world.Player().Position())
349 << " at position " << world.Player().Position()
351 PostMessage(s.str());
354 void Interface::PrintSelectionInfo() {
355 std::cout << std::endl;
359 void Interface::Print(const Block &block) {
361 s << "type: " << block.type
362 << ", face: " << block.GetFace()
363 << ", turn: " << block.GetTurn()
365 PostMessage(s.str());
368 void Interface::ToggleAudio() {
369 config.audio_disabled = !config.audio_disabled;
370 if (config.audio_disabled) {
371 PostMessage("audio off");
373 PostMessage("audio on");
377 void Interface::ToggleVisual() {
378 config.visual_disabled = !config.visual_disabled;
379 if (config.visual_disabled) {
380 PostMessage("visual off");
382 PostMessage("visual on");
386 void Interface::ToggleDebug() {
397 void Interface::UpdateCounter() {
399 s << std::setprecision(3) <<
400 "avg: " << env.counter.Average().running << "ms, "
401 "peak: " << env.counter.Peak().running << "ms";
402 std::string text = s.str();
403 counter_text.Set(env.assets.small_ui_font, text);
406 void Interface::UpdatePosition() {
408 s << std::setprecision(3) << "pos: " << ctrl.Controlled().AbsolutePosition();
409 position_text.Set(env.assets.small_ui_font, s.str());
412 void Interface::UpdateOrientation() {
414 s << std::setprecision(3) << "pitch: " << rad2deg(ctrl.Pitch())
415 << ", yaw: " << rad2deg(ctrl.Yaw());
416 orientation_text.Set(env.assets.small_ui_font, s.str());
419 void Interface::UpdateBlockInfo() {
421 const Block &block = aim_world.GetBlock();
422 if (last_block != block) {
425 << aim_world.GetType().label
426 << ", face: " << block.GetFace()
427 << ", turn: " << block.GetTurn();
428 block_text.Set(env.assets.small_ui_font, s.str());
432 if (last_block != Block()) {
435 block_text.Set(env.assets.small_ui_font, s.str());
436 last_block = Block();
441 void Interface::UpdateEntityInfo() {
443 if (last_entity != aim_entity.entity) {
445 s << "Entity: " << aim_entity.entity->Name();
446 entity_text.Set(env.assets.small_ui_font, s.str());
447 last_entity = aim_entity.entity;
453 void Interface::Handle(const SDL_MouseMotionEvent &event) {
454 if (config.mouse_disabled) return;
455 ctrl.RotateYaw(event.xrel * config.yaw_sensitivity);
456 ctrl.RotatePitch(event.yrel * config.pitch_sensitivity);
459 void Interface::HandlePress(const SDL_MouseButtonEvent &event) {
460 if (config.mouse_disabled) return;
462 if (event.button == SDL_BUTTON_LEFT) {
464 remove_timer.Start();
465 } else if (event.button == SDL_BUTTON_MIDDLE) {
467 } else if (event.button == SDL_BUTTON_RIGHT) {
473 void Interface::HandleRelease(const SDL_MouseButtonEvent &event) {
474 if (config.mouse_disabled) return;
476 if (event.button == SDL_BUTTON_LEFT) {
478 } else if (event.button == SDL_BUTTON_RIGHT) {
483 void Interface::PickBlock() {
484 if (!aim_world) return;
485 selection = aim_world.GetBlock();
486 hud.Display(selection);
489 void Interface::PlaceBlock() {
490 if (!aim_world) return;
492 glm::vec3 next_pos = aim_world.BlockCoords() + aim_world.normal;
493 BlockLookup next_block(&aim_world.GetChunk(), next_pos);
496 next_block.SetBlock(selection);
498 if (config.audio_disabled) return;
499 const Entity &player = ctrl.Controlled();
502 aim_world.GetChunk().ToSceneCoords(player.ChunkCoords(), next_pos)
506 void Interface::RemoveBlock() noexcept {
507 if (!aim_world) return;
508 aim_world.SetBlock(remove);
510 if (config.audio_disabled) return;
511 const Entity &player = ctrl.Controlled();
514 aim_world.GetChunk().ToSceneCoords(player.ChunkCoords(), aim_world.BlockCoords())
519 void Interface::Handle(const SDL_MouseWheelEvent &event) {
520 if (config.mouse_disabled) return;
524 } else if (event.y > 0) {
529 void Interface::SelectNext() {
531 if (size_t(selection.type) >= world.BlockTypes().Size()) {
534 hud.Display(selection);
537 void Interface::SelectPrevious() {
539 if (selection.type <= 0) {
540 selection.type = world.BlockTypes().Size() - 1;
542 hud.Display(selection);
546 void Interface::PostMessage(const char *msg) {
547 messages.PushLine(msg);
550 std::cout << msg << std::endl;
554 void Interface::Update(int dt) {
555 ctrl.Velocity(glm::vec3(fwd - rev) * config.move_velocity);
558 msg_timer.Update(dt);
559 place_timer.Update(dt);
560 remove_timer.Update(dt);
565 if (msg_timer.HitOnce()) {
569 if (remove_timer.Hit()) {
574 if (place_timer.Hit()) {
580 if (env.counter.Changed()) {
590 OutlineModel::Buffer outl_buf;
594 void Interface::CheckAim() {
595 if (!world.Intersection(aim, glm::mat4(1.0f), aim_world)) {
596 aim_world = WorldCollision();
598 if (!world.Intersection(aim, glm::mat4(1.0f), aim_entity)) {
599 aim_entity = EntityCollision();
601 if (aim_world && aim_entity) {
602 // got both, pick the closest one
603 if (aim_world.depth < aim_entity.depth) {
605 aim_entity = EntityCollision();
607 aim_world = WorldCollision();
609 } else if (aim_world) {
618 void Interface::UpdateOutline() {
620 aim_world.GetType().FillOutlineModel(outl_buf);
621 outline.Update(outl_buf);
622 outline_transform = aim_world.GetChunk().Transform(world.Player().ChunkCoords());
623 outline_transform *= aim_world.BlockTransform();
624 outline_transform *= glm::scale(glm::vec3(1.005f));
628 void Interface::Render(Viewport &viewport) noexcept {
629 if (config.visual_disabled) return;
632 PlainColor &outline_prog = viewport.WorldOutlineProgram();
633 outline_prog.SetM(outline_transform);
638 counter_text.Render(viewport);
639 position_text.Render(viewport);
640 orientation_text.Render(viewport);
642 block_text.Render(viewport);
643 } else if (aim_entity) {
644 entity_text.Render(viewport);
648 if (msg_timer.Running()) {
649 messages.Render(viewport);
652 hud.Render(viewport);
661 void Keymap::Map(SDL_Scancode scancode, Action action) {
662 if (scancode > MAX_SCANCODE) {
663 throw std::runtime_error("refusing to map scancode: too damn high");
665 codemap[scancode] = action;
668 Keymap::Action Keymap::Lookup(SDL_Scancode scancode) {
669 if (scancode < NUM_SCANCODES) {
670 return codemap[scancode];
677 void Keymap::LoadDefault() {
678 Map(SDL_SCANCODE_UP, MOVE_FORWARD);
679 Map(SDL_SCANCODE_W, MOVE_FORWARD);
680 Map(SDL_SCANCODE_DOWN, MOVE_BACKWARD);
681 Map(SDL_SCANCODE_S, MOVE_BACKWARD);
682 Map(SDL_SCANCODE_LEFT, MOVE_LEFT);
683 Map(SDL_SCANCODE_A, MOVE_LEFT);
684 Map(SDL_SCANCODE_RIGHT, MOVE_RIGHT);
685 Map(SDL_SCANCODE_D, MOVE_RIGHT);
686 Map(SDL_SCANCODE_SPACE, MOVE_UP);
687 Map(SDL_SCANCODE_RSHIFT, MOVE_UP);
688 Map(SDL_SCANCODE_LSHIFT, MOVE_DOWN);
689 Map(SDL_SCANCODE_LCTRL, MOVE_DOWN);
690 Map(SDL_SCANCODE_RCTRL, MOVE_DOWN);
692 Map(SDL_SCANCODE_Q, BLOCK_FACE);
693 Map(SDL_SCANCODE_E, BLOCK_TURN);
694 Map(SDL_SCANCODE_TAB, BLOCK_NEXT);
695 Map(SDL_SCANCODE_RIGHTBRACKET, BLOCK_NEXT);
696 Map(SDL_SCANCODE_LEFTBRACKET, BLOCK_PREV);
698 Map(SDL_SCANCODE_INSERT, BLOCK_PLACE);
699 Map(SDL_SCANCODE_RETURN, BLOCK_PLACE);
700 Map(SDL_SCANCODE_MENU, BLOCK_PICK);
701 Map(SDL_SCANCODE_DELETE, BLOCK_REMOVE);
702 Map(SDL_SCANCODE_BACKSPACE, BLOCK_REMOVE);
704 Map(SDL_SCANCODE_N, TOGGLE_COLLISION);
705 Map(SDL_SCANCODE_F1, TOGGLE_VISUAL);
706 Map(SDL_SCANCODE_F3, TOGGLE_DEBUG);
707 Map(SDL_SCANCODE_F4, TOGGLE_AUDIO);
709 Map(SDL_SCANCODE_B, PRINT_BLOCK);
710 Map(SDL_SCANCODE_C, PRINT_CHUNK);
711 Map(SDL_SCANCODE_L, PRINT_LIGHT);
712 Map(SDL_SCANCODE_P, PRINT_SELECTION);
714 Map(SDL_SCANCODE_ESCAPE, EXIT);
718 void Keymap::Load(std::istream &is) {
719 TokenStreamReader in(is);
720 std::string key_name;
721 std::string action_name;
724 while (in.HasMore()) {
725 if (in.Peek().type == Token::STRING) {
726 in.ReadString(key_name);
727 key = SDL_GetScancodeFromName(key_name.c_str());
729 key = SDL_Scancode(in.GetInt());
731 in.Skip(Token::EQUALS);
732 in.ReadIdentifier(action_name);
733 action = StringToAction(action_name);
734 if (in.HasMore() && in.Peek().type == Token::SEMICOLON) {
735 in.Skip(Token::SEMICOLON);
741 void Keymap::Save(std::ostream &out) {
742 for (unsigned int i = 0; i < NUM_SCANCODES; ++i) {
743 if (codemap[i] == NONE) continue;
745 const char *str = SDL_GetScancodeName(SDL_Scancode(i));
761 out << " = " << ActionToString(codemap[i]) << std::endl;;
766 const char *Keymap::ActionToString(Action action) {
772 return "move_forward";
774 return "move_backward";
792 return "block_place";
796 return "block_remove";
797 case TOGGLE_COLLISION:
798 return "toggle_collision";
800 return "toggle_audio";
802 return "toggle_visual";
804 return "toggle_debug";
806 return "print_block";
808 return "print_chunk";
810 return "print_light";
811 case PRINT_SELECTION:
812 return "print_selection";
818 Keymap::Action Keymap::StringToAction(const std::string &str) {
819 if (str == "move_forward") {
821 } else if (str == "move_backward") {
822 return MOVE_BACKWARD;
823 } else if (str == "move_left") {
825 } else if (str == "move_right") {
827 } else if (str == "move_up") {
829 } else if (str == "move_down") {
831 } else if (str == "block_face") {
833 } else if (str == "block_turn") {
835 } else if (str == "block_next") {
837 } else if (str == "block_prev") {
839 } else if (str == "block_place") {
841 } else if (str == "block_pick") {
843 } else if (str == "block_remove") {
845 } else if (str == "toggle_collision") {
846 return TOGGLE_COLLISION;
847 } else if (str == "toggle_audio") {
849 } else if (str == "toggle_visual") {
850 return TOGGLE_VISUAL;
851 } else if (str == "toggle_debug") {
853 } else if (str == "print_block") {
855 } else if (str == "print_chunk") {
857 } else if (str == "print_light") {
859 } else if (str == "print_selection") {
860 return PRINT_SELECTION;
861 } else if (str == "exit") {