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)
114 , messages(env.assets.small_ui_font)
121 , place_sound(env.assets.LoadSound("thump"))
122 , remove_sound(env.assets.LoadSound("plop"))
126 counter_text.Position(glm::vec3(-25.0f, 25.0f, 0.0f), Gravity::NORTH_EAST);
127 counter_text.Foreground(glm::vec4(1.0f));
128 counter_text.Background(glm::vec4(0.5f));
129 position_text.Position(glm::vec3(-25.0f, 25.0f + env.assets.small_ui_font.LineSkip(), 0.0f), Gravity::NORTH_EAST);
130 position_text.Foreground(glm::vec4(1.0f));
131 position_text.Background(glm::vec4(0.5f));
132 orientation_text.Position(glm::vec3(-25.0f, 25.0f + 2 * env.assets.small_ui_font.LineSkip(), 0.0f), Gravity::NORTH_EAST);
133 orientation_text.Foreground(glm::vec4(1.0f));
134 orientation_text.Background(glm::vec4(0.5f));
135 messages.Position(glm::vec3(25.0f, -25.0f, 0.0f), Gravity::SOUTH_WEST);
136 messages.Foreground(glm::vec4(1.0f));
137 messages.Background(glm::vec4(0.5f));
138 hud.Display(selection);
142 void Interface::HandlePress(const SDL_KeyboardEvent &event) {
143 if (config.keyboard_disabled) return;
145 switch (env.keymap.Lookup(event)) {
146 case Keymap::MOVE_FORWARD:
149 case Keymap::MOVE_BACKWARD:
152 case Keymap::MOVE_LEFT:
155 case Keymap::MOVE_RIGHT:
158 case Keymap::MOVE_UP:
161 case Keymap::MOVE_DOWN:
165 case Keymap::BLOCK_FACE:
168 case Keymap::BLOCK_TURN:
171 case Keymap::BLOCK_NEXT:
174 case Keymap::BLOCK_PREV:
178 case Keymap::BLOCK_PLACE:
181 case Keymap::BLOCK_PICK:
184 case Keymap::BLOCK_REMOVE:
188 case Keymap::TOGGLE_COLLISION:
192 case Keymap::PRINT_BLOCK:
195 case Keymap::PRINT_CHUNK:
198 case Keymap::PRINT_LIGHT:
201 case Keymap::PRINT_SELECTION:
202 PrintSelectionInfo();
205 case Keymap::TOGGLE_VISUAL:
208 case Keymap::TOGGLE_DEBUG:
211 case Keymap::TOGGLE_AUDIO:
224 void Interface::HandleRelease(const SDL_KeyboardEvent &event) {
225 if (config.keyboard_disabled) return;
227 switch (env.keymap.Lookup(event)) {
228 case Keymap::MOVE_FORWARD:
231 case Keymap::MOVE_BACKWARD:
234 case Keymap::MOVE_LEFT:
237 case Keymap::MOVE_RIGHT:
240 case Keymap::MOVE_UP:
243 case Keymap::MOVE_DOWN:
252 void Interface::FaceBlock() {
253 selection.SetFace(Block::Face((selection.GetFace() + 1) % Block::FACE_COUNT));
254 hud.Display(selection);
257 void Interface::TurnBlock() {
258 selection.SetTurn(Block::Turn((selection.GetTurn() + 1) % Block::TURN_COUNT));
259 hud.Display(selection);
262 void Interface::ToggleCollision() {
263 ctrl.Controlled().WorldCollidable(!ctrl.Controlled().WorldCollidable());
264 if (ctrl.Controlled().WorldCollidable()) {
265 PostMessage("collision on");
267 PostMessage("collision off");
271 void Interface::PrintBlockInfo() {
272 std::cout << std::endl;
274 PostMessage("not looking at any block");
275 Ray aim = ctrl.Aim();
277 s << "aim ray: " << aim.orig << ", " << aim.dir;
278 PostMessage(s.str());
282 s << "looking at block " << aim_block
283 << " " << Chunk::ToCoords(aim_block)
284 << " of chunk " << aim_chunk->Position()
286 PostMessage(s.str());
287 Print(aim_chunk->BlockAt(aim_block));
290 void Interface::PrintChunkInfo() {
291 std::cout << std::endl;
293 PostMessage("not looking at any block");
297 s << "looking at chunk " << aim_chunk->Position();
298 PostMessage(s.str());
300 PostMessage(" neighbors:");
301 if (aim_chunk->HasNeighbor(Block::FACE_LEFT)) {
303 s << " left " << aim_chunk->GetNeighbor(Block::FACE_LEFT).Position();
304 PostMessage(s.str());
306 if (aim_chunk->HasNeighbor(Block::FACE_RIGHT)) {
308 s << " right " << aim_chunk->GetNeighbor(Block::FACE_RIGHT).Position();
309 PostMessage(s.str());
311 if (aim_chunk->HasNeighbor(Block::FACE_UP)) {
313 s << " up " << aim_chunk->GetNeighbor(Block::FACE_UP).Position();
314 PostMessage(s.str());
316 if (aim_chunk->HasNeighbor(Block::FACE_DOWN)) {
318 s << " down " << aim_chunk->GetNeighbor(Block::FACE_DOWN).Position();
319 PostMessage(s.str());
321 if (aim_chunk->HasNeighbor(Block::FACE_FRONT)) {
323 s << " front " << aim_chunk->GetNeighbor(Block::FACE_FRONT).Position();
324 PostMessage(s.str());
326 if (aim_chunk->HasNeighbor(Block::FACE_BACK)) {
328 s << " back " << aim_chunk->GetNeighbor(Block::FACE_BACK).Position();
329 PostMessage(s.str());
331 std::cout << std::endl;
334 void Interface::PrintLightInfo() {
337 << "light level " << world.PlayerChunk().GetLight(world.Player().Position())
338 << " at position " << world.Player().Position()
340 PostMessage(s.str());
343 void Interface::PrintSelectionInfo() {
344 std::cout << std::endl;
348 void Interface::Print(const Block &block) {
350 s << "type: " << block.type
351 << ", face: " << block.GetFace()
352 << ", turn: " << block.GetTurn()
354 PostMessage(s.str());
357 void Interface::ToggleAudio() {
358 config.audio_disabled = !config.audio_disabled;
359 if (config.audio_disabled) {
360 PostMessage("audio off");
362 PostMessage("audio on");
366 void Interface::ToggleVisual() {
367 config.visual_disabled = !config.visual_disabled;
368 if (config.visual_disabled) {
369 PostMessage("visual off");
371 PostMessage("visual on");
375 void Interface::ToggleDebug() {
384 void Interface::UpdateCounter() {
386 s << std::setprecision(3) <<
387 "avg: " << env.counter.Average().running << "ms, "
388 "peak: " << env.counter.Peak().running << "ms";
389 std::string text = s.str();
390 counter_text.Set(env.assets.small_ui_font, text);
393 void Interface::UpdatePosition() {
395 s << std::setprecision(3) << "pos: " << ctrl.Controlled().AbsolutePosition();
396 position_text.Set(env.assets.small_ui_font, s.str());
399 void Interface::UpdateOrientation() {
401 s << std::setprecision(3) << "pitch: " << rad2deg(ctrl.Pitch())
402 << ", yaw: " << rad2deg(ctrl.Yaw());
403 orientation_text.Set(env.assets.small_ui_font, s.str());
407 void Interface::Handle(const SDL_MouseMotionEvent &event) {
408 if (config.mouse_disabled) return;
409 ctrl.RotateYaw(event.xrel * config.yaw_sensitivity);
410 ctrl.RotatePitch(event.yrel * config.pitch_sensitivity);
413 void Interface::HandlePress(const SDL_MouseButtonEvent &event) {
414 if (config.mouse_disabled) return;
416 if (event.button == SDL_BUTTON_LEFT) {
418 remove_timer.Start();
419 } else if (event.button == SDL_BUTTON_MIDDLE) {
421 } else if (event.button == SDL_BUTTON_RIGHT) {
427 void Interface::HandleRelease(const SDL_MouseButtonEvent &event) {
428 if (config.mouse_disabled) return;
430 if (event.button == SDL_BUTTON_LEFT) {
432 } else if (event.button == SDL_BUTTON_RIGHT) {
437 void Interface::PickBlock() {
438 if (!aim_chunk) return;
439 selection = aim_chunk->BlockAt(aim_block);
440 hud.Display(selection);
443 void Interface::PlaceBlock() {
444 if (!aim_chunk) return;
445 Chunk *mod_chunk = aim_chunk;
446 glm::vec3 next_pos = Chunk::ToCoords(aim_block) + aim_normal;
447 if (!Chunk::InBounds(next_pos)) {
448 mod_chunk = &world.Next(*aim_chunk, aim_normal);
449 next_pos -= aim_normal * glm::vec3(Chunk::Extent());
451 mod_chunk->SetBlock(next_pos, selection);
453 if (config.audio_disabled) return;
454 const Entity &player = ctrl.Controlled();
457 mod_chunk->ToSceneCoords(player.ChunkCoords(), next_pos)
461 void Interface::RemoveBlock() noexcept {
462 if (!aim_chunk) return;
463 aim_chunk->SetBlock(aim_block, remove);
465 if (config.audio_disabled) return;
466 const Entity &player = ctrl.Controlled();
469 aim_chunk->ToSceneCoords(player.ChunkCoords(), Chunk::ToCoords(aim_block))
474 void Interface::Handle(const SDL_MouseWheelEvent &event) {
475 if (config.mouse_disabled) return;
479 } else if (event.y > 0) {
484 void Interface::SelectNext() {
486 if (size_t(selection.type) >= world.BlockTypes().Size()) {
489 hud.Display(selection);
492 void Interface::SelectPrevious() {
494 if (selection.type <= 0) {
495 selection.type = world.BlockTypes().Size() - 1;
497 hud.Display(selection);
501 void Interface::PostMessage(const char *msg) {
502 messages.PushLine(msg);
505 std::cout << msg << std::endl;
509 void Interface::Update(int dt) {
510 ctrl.Velocity(glm::vec3(fwd - rev) * config.move_velocity);
513 msg_timer.Update(dt);
514 place_timer.Update(dt);
515 remove_timer.Update(dt);
520 if (msg_timer.HitOnce()) {
524 if (remove_timer.Hit()) {
529 if (place_timer.Hit()) {
535 if (env.counter.Changed()) {
545 OutlineModel::Buffer outl_buf;
549 void Interface::CheckAim() {
551 if (world.Intersection(aim, glm::mat4(1.0f), aim_chunk, aim_block, dist, aim_normal)) {
553 aim_chunk->Type(aim_chunk->BlockAt(aim_block)).FillOutlineModel(outl_buf);
554 outline.Update(outl_buf);
555 outline_transform = aim_chunk->Transform(world.Player().ChunkCoords());
556 outline_transform *= aim_chunk->ToTransform(Chunk::ToPos(aim_block), aim_block);
557 outline_transform *= glm::scale(glm::vec3(1.005f));
564 void Interface::Render(Viewport &viewport) noexcept {
565 if (config.visual_disabled) return;
568 PlainColor &outline_prog = viewport.WorldOutlineProgram();
569 outline_prog.SetM(outline_transform);
574 counter_text.Render(viewport);
575 position_text.Render(viewport);
576 orientation_text.Render(viewport);
579 if (msg_timer.Running()) {
580 messages.Render(viewport);
583 hud.Render(viewport);
592 void Keymap::Map(SDL_Scancode scancode, Action action) {
593 if (scancode > MAX_SCANCODE) {
594 throw std::runtime_error("refusing to map scancode: too damn high");
596 codemap[scancode] = action;
599 Keymap::Action Keymap::Lookup(SDL_Scancode scancode) {
600 if (scancode < NUM_SCANCODES) {
601 return codemap[scancode];
608 void Keymap::LoadDefault() {
609 Map(SDL_SCANCODE_UP, MOVE_FORWARD);
610 Map(SDL_SCANCODE_W, MOVE_FORWARD);
611 Map(SDL_SCANCODE_DOWN, MOVE_BACKWARD);
612 Map(SDL_SCANCODE_S, MOVE_BACKWARD);
613 Map(SDL_SCANCODE_LEFT, MOVE_LEFT);
614 Map(SDL_SCANCODE_A, MOVE_LEFT);
615 Map(SDL_SCANCODE_RIGHT, MOVE_RIGHT);
616 Map(SDL_SCANCODE_D, MOVE_RIGHT);
617 Map(SDL_SCANCODE_SPACE, MOVE_UP);
618 Map(SDL_SCANCODE_RSHIFT, MOVE_UP);
619 Map(SDL_SCANCODE_LSHIFT, MOVE_DOWN);
620 Map(SDL_SCANCODE_LCTRL, MOVE_DOWN);
621 Map(SDL_SCANCODE_RCTRL, MOVE_DOWN);
623 Map(SDL_SCANCODE_Q, BLOCK_FACE);
624 Map(SDL_SCANCODE_E, BLOCK_TURN);
625 Map(SDL_SCANCODE_TAB, BLOCK_NEXT);
626 Map(SDL_SCANCODE_RIGHTBRACKET, BLOCK_NEXT);
627 Map(SDL_SCANCODE_LEFTBRACKET, BLOCK_PREV);
629 Map(SDL_SCANCODE_INSERT, BLOCK_PLACE);
630 Map(SDL_SCANCODE_RETURN, BLOCK_PLACE);
631 Map(SDL_SCANCODE_MENU, BLOCK_PICK);
632 Map(SDL_SCANCODE_DELETE, BLOCK_REMOVE);
633 Map(SDL_SCANCODE_BACKSPACE, BLOCK_REMOVE);
635 Map(SDL_SCANCODE_N, TOGGLE_COLLISION);
636 Map(SDL_SCANCODE_F1, TOGGLE_VISUAL);
637 Map(SDL_SCANCODE_F3, TOGGLE_DEBUG);
638 Map(SDL_SCANCODE_F4, TOGGLE_AUDIO);
640 Map(SDL_SCANCODE_B, PRINT_BLOCK);
641 Map(SDL_SCANCODE_C, PRINT_CHUNK);
642 Map(SDL_SCANCODE_L, PRINT_LIGHT);
643 Map(SDL_SCANCODE_P, PRINT_SELECTION);
645 Map(SDL_SCANCODE_ESCAPE, EXIT);
649 void Keymap::Load(std::istream &is) {
650 TokenStreamReader in(is);
651 std::string key_name;
652 std::string action_name;
655 while (in.HasMore()) {
656 if (in.Peek().type == Token::STRING) {
657 in.ReadString(key_name);
658 key = SDL_GetScancodeFromName(key_name.c_str());
660 key = SDL_Scancode(in.GetInt());
662 in.Skip(Token::EQUALS);
663 in.ReadIdentifier(action_name);
664 action = StringToAction(action_name);
665 if (in.HasMore() && in.Peek().type == Token::SEMICOLON) {
666 in.Skip(Token::SEMICOLON);
672 void Keymap::Save(std::ostream &out) {
673 for (unsigned int i = 0; i < NUM_SCANCODES; ++i) {
674 if (codemap[i] == NONE) continue;
676 const char *str = SDL_GetScancodeName(SDL_Scancode(i));
692 out << " = " << ActionToString(codemap[i]) << std::endl;;
697 const char *Keymap::ActionToString(Action action) {
703 return "move_forward";
705 return "move_backward";
723 return "block_place";
727 return "block_remove";
728 case TOGGLE_COLLISION:
729 return "toggle_collision";
731 return "toggle_audio";
733 return "toggle_visual";
735 return "toggle_debug";
737 return "print_block";
739 return "print_chunk";
741 return "print_light";
742 case PRINT_SELECTION:
743 return "print_selection";
749 Keymap::Action Keymap::StringToAction(const std::string &str) {
750 if (str == "move_forward") {
752 } else if (str == "move_backward") {
753 return MOVE_BACKWARD;
754 } else if (str == "move_left") {
756 } else if (str == "move_right") {
758 } else if (str == "move_up") {
760 } else if (str == "move_down") {
762 } else if (str == "block_face") {
764 } else if (str == "block_turn") {
766 } else if (str == "block_next") {
768 } else if (str == "block_prev") {
770 } else if (str == "block_place") {
772 } else if (str == "block_pick") {
774 } else if (str == "block_remove") {
776 } else if (str == "toggle_collision") {
777 return TOGGLE_COLLISION;
778 } else if (str == "toggle_audio") {
780 } else if (str == "toggle_visual") {
781 return TOGGLE_VISUAL;
782 } else if (str == "toggle_debug") {
784 } else if (str == "print_block") {
786 } else if (str == "print_chunk") {
788 } else if (str == "print_light") {
790 } else if (str == "print_selection") {
791 return PRINT_SELECTION;
792 } else if (str == "exit") {