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 // let's assume this succeeds and hope for the best for now
109 , ctrl(*world.AddPlayer(config.player_name))
110 , hud(world.BlockTypes(), env.assets.small_ui_font)
111 , aim{{ 0, 0, 0 }, { 0, 0, -1 }}
115 , outline_transform(1.0f)
121 , last_entity(nullptr)
122 , messages(env.assets.small_ui_font)
129 , place_sound(env.loader.LoadSound("thump"))
130 , remove_sound(env.loader.LoadSound("plop"))
134 counter_text.Position(glm::vec3(-25.0f, 25.0f, 0.0f), Gravity::NORTH_EAST);
135 counter_text.Foreground(glm::vec4(1.0f));
136 counter_text.Background(glm::vec4(0.5f));
137 position_text.Position(glm::vec3(-25.0f, 25.0f + env.assets.small_ui_font.LineSkip(), 0.0f), Gravity::NORTH_EAST);
138 position_text.Foreground(glm::vec4(1.0f));
139 position_text.Background(glm::vec4(0.5f));
140 orientation_text.Position(glm::vec3(-25.0f, 25.0f + 2 * env.assets.small_ui_font.LineSkip(), 0.0f), Gravity::NORTH_EAST);
141 orientation_text.Foreground(glm::vec4(1.0f));
142 orientation_text.Background(glm::vec4(0.5f));
143 block_text.Position(glm::vec3(-25.0f, 25.0f + 4 * env.assets.small_ui_font.LineSkip(), 0.0f), Gravity::NORTH_EAST);
144 block_text.Foreground(glm::vec4(1.0f));
145 block_text.Background(glm::vec4(0.5f));
146 block_text.Set(env.assets.small_ui_font, "Block: none");
147 entity_text.Position(glm::vec3(-25.0f, 25.0f + 4 * env.assets.small_ui_font.LineSkip(), 0.0f), Gravity::NORTH_EAST);
148 entity_text.Foreground(glm::vec4(1.0f));
149 entity_text.Background(glm::vec4(0.5f));
150 entity_text.Set(env.assets.small_ui_font, "Entity: none");
151 messages.Position(glm::vec3(25.0f, -25.0f, 0.0f), Gravity::SOUTH_WEST);
152 messages.Foreground(glm::vec4(1.0f));
153 messages.Background(glm::vec4(0.5f));
158 void Interface::HandlePress(const SDL_KeyboardEvent &event) {
159 if (config.keyboard_disabled) return;
161 switch (env.keymap.Lookup(event)) {
162 case Keymap::MOVE_FORWARD:
165 case Keymap::MOVE_BACKWARD:
168 case Keymap::MOVE_LEFT:
171 case Keymap::MOVE_RIGHT:
174 case Keymap::MOVE_UP:
177 case Keymap::MOVE_DOWN:
181 case Keymap::BLOCK_FACE:
184 case Keymap::BLOCK_TURN:
187 case Keymap::BLOCK_NEXT:
190 case Keymap::BLOCK_PREV:
194 case Keymap::BLOCK_PLACE:
197 case Keymap::BLOCK_PICK:
200 case Keymap::BLOCK_REMOVE:
204 case Keymap::TOGGLE_COLLISION:
208 case Keymap::TOGGLE_VISUAL:
211 case Keymap::TOGGLE_DEBUG:
214 case Keymap::TOGGLE_AUDIO:
227 void Interface::HandleRelease(const SDL_KeyboardEvent &event) {
228 if (config.keyboard_disabled) return;
230 switch (env.keymap.Lookup(event)) {
231 case Keymap::MOVE_FORWARD:
234 case Keymap::MOVE_BACKWARD:
237 case Keymap::MOVE_LEFT:
240 case Keymap::MOVE_RIGHT:
243 case Keymap::MOVE_UP:
246 case Keymap::MOVE_DOWN:
255 void Interface::FaceBlock() {
256 selection.SetFace(Block::Face((selection.GetFace() + 1) % Block::FACE_COUNT));
257 hud.Display(selection);
260 void Interface::TurnBlock() {
261 selection.SetTurn(Block::Turn((selection.GetTurn() + 1) % Block::TURN_COUNT));
262 hud.Display(selection);
265 void Interface::ToggleCollision() {
266 ctrl.Controlled().WorldCollidable(!ctrl.Controlled().WorldCollidable());
267 if (ctrl.Controlled().WorldCollidable()) {
268 PostMessage("collision on");
270 PostMessage("collision off");
274 void Interface::ToggleAudio() {
275 config.audio_disabled = !config.audio_disabled;
276 if (config.audio_disabled) {
277 PostMessage("audio off");
279 PostMessage("audio on");
283 void Interface::ToggleVisual() {
284 config.visual_disabled = !config.visual_disabled;
285 if (config.visual_disabled) {
286 PostMessage("visual off");
288 PostMessage("visual on");
292 void Interface::ToggleDebug() {
303 void Interface::UpdateCounter() {
305 s << std::setprecision(3) <<
306 "avg: " << env.counter.Average().running << "ms, "
307 "peak: " << env.counter.Peak().running << "ms";
308 std::string text = s.str();
309 counter_text.Set(env.assets.small_ui_font, text);
312 void Interface::UpdatePosition() {
314 s << std::setprecision(3) << "pos: " << ctrl.Controlled().AbsolutePosition();
315 position_text.Set(env.assets.small_ui_font, s.str());
318 void Interface::UpdateOrientation() {
320 s << std::setprecision(3) << "pitch: " << rad2deg(ctrl.Pitch())
321 << ", yaw: " << rad2deg(ctrl.Yaw());
322 orientation_text.Set(env.assets.small_ui_font, s.str());
325 void Interface::UpdateBlockInfo() {
327 const Block &block = aim_world.GetBlock();
328 if (last_block != block) {
331 << aim_world.GetType().label
332 << ", face: " << block.GetFace()
333 << ", turn: " << block.GetTurn();
334 block_text.Set(env.assets.small_ui_font, s.str());
338 if (last_block != Block()) {
341 block_text.Set(env.assets.small_ui_font, s.str());
342 last_block = Block();
347 void Interface::UpdateEntityInfo() {
349 if (last_entity != aim_entity.entity) {
351 s << "Entity: " << aim_entity.entity->Name();
352 entity_text.Set(env.assets.small_ui_font, s.str());
353 last_entity = aim_entity.entity;
359 void Interface::Handle(const SDL_MouseMotionEvent &event) {
360 if (config.mouse_disabled) return;
361 ctrl.RotateYaw(event.xrel * config.yaw_sensitivity);
362 ctrl.RotatePitch(event.yrel * config.pitch_sensitivity);
365 void Interface::HandlePress(const SDL_MouseButtonEvent &event) {
366 if (config.mouse_disabled) return;
368 if (event.button == SDL_BUTTON_LEFT) {
370 remove_timer.Start();
371 } else if (event.button == SDL_BUTTON_MIDDLE) {
373 } else if (event.button == SDL_BUTTON_RIGHT) {
379 void Interface::HandleRelease(const SDL_MouseButtonEvent &event) {
380 if (config.mouse_disabled) return;
382 if (event.button == SDL_BUTTON_LEFT) {
384 } else if (event.button == SDL_BUTTON_RIGHT) {
389 void Interface::PickBlock() {
390 if (!aim_world) return;
391 selection = aim_world.GetBlock();
392 hud.Display(selection);
395 void Interface::PlaceBlock() {
396 if (!aim_world) return;
398 glm::vec3 next_pos = aim_world.BlockCoords() + aim_world.normal;
399 BlockLookup next_block(&aim_world.GetChunk(), next_pos);
402 next_block.SetBlock(selection);
404 if (config.audio_disabled) return;
405 const Entity &player = ctrl.Controlled();
408 aim_world.GetChunk().ToSceneCoords(player.ChunkCoords(), next_pos)
412 void Interface::RemoveBlock() noexcept {
413 if (!aim_world) return;
414 aim_world.SetBlock(remove);
416 if (config.audio_disabled) return;
417 const Entity &player = ctrl.Controlled();
420 aim_world.GetChunk().ToSceneCoords(player.ChunkCoords(), aim_world.BlockCoords())
425 void Interface::Handle(const SDL_MouseWheelEvent &event) {
426 if (config.mouse_disabled) return;
430 } else if (event.y > 0) {
435 void Interface::SelectNext() {
437 if (size_t(selection.type) >= world.BlockTypes().Size()) {
440 hud.Display(selection);
443 void Interface::SelectPrevious() {
445 if (selection.type <= 0) {
446 selection.type = world.BlockTypes().Size() - 1;
448 hud.Display(selection);
452 void Interface::PostMessage(const char *msg) {
453 messages.PushLine(msg);
456 std::cout << msg << std::endl;
460 void Interface::Update(int dt) {
461 ctrl.Velocity(glm::vec3(fwd - rev) * config.move_velocity);
464 msg_timer.Update(dt);
465 place_timer.Update(dt);
466 remove_timer.Update(dt);
471 if (msg_timer.HitOnce()) {
475 if (remove_timer.Hit()) {
480 if (place_timer.Hit()) {
486 if (env.counter.Changed()) {
496 OutlineModel::Buffer outl_buf;
500 void Interface::CheckAim() {
501 if (!world.Intersection(aim, glm::mat4(1.0f), ctrl.Controlled().ChunkCoords(), aim_world)) {
502 aim_world = WorldCollision();
504 if (!world.Intersection(aim, glm::mat4(1.0f), ctrl.Controlled(), aim_entity)) {
505 aim_entity = EntityCollision();
507 if (aim_world && aim_entity) {
508 // got both, pick the closest one
509 if (aim_world.depth < aim_entity.depth) {
511 aim_entity = EntityCollision();
513 aim_world = WorldCollision();
515 } else if (aim_world) {
524 void Interface::UpdateOutline() {
526 aim_world.GetType().FillOutlineModel(outl_buf);
527 outline.Update(outl_buf);
528 outline_transform = aim_world.GetChunk().Transform(Player().ChunkCoords());
529 outline_transform *= aim_world.BlockTransform();
530 outline_transform *= glm::scale(glm::vec3(1.005f));
534 void Interface::Render(Viewport &viewport) noexcept {
535 if (config.visual_disabled) return;
538 PlainColor &outline_prog = viewport.WorldOutlineProgram();
539 outline_prog.SetM(outline_transform);
544 counter_text.Render(viewport);
545 position_text.Render(viewport);
546 orientation_text.Render(viewport);
548 block_text.Render(viewport);
549 } else if (aim_entity) {
550 entity_text.Render(viewport);
554 if (msg_timer.Running()) {
555 messages.Render(viewport);
558 hud.Render(viewport);
567 void Keymap::Map(SDL_Scancode scancode, Action action) {
568 if (scancode > MAX_SCANCODE) {
569 throw std::runtime_error("refusing to map scancode: too damn high");
571 codemap[scancode] = action;
574 Keymap::Action Keymap::Lookup(SDL_Scancode scancode) {
575 if (scancode < NUM_SCANCODES) {
576 return codemap[scancode];
583 void Keymap::LoadDefault() {
584 Map(SDL_SCANCODE_UP, MOVE_FORWARD);
585 Map(SDL_SCANCODE_W, MOVE_FORWARD);
586 Map(SDL_SCANCODE_DOWN, MOVE_BACKWARD);
587 Map(SDL_SCANCODE_S, MOVE_BACKWARD);
588 Map(SDL_SCANCODE_LEFT, MOVE_LEFT);
589 Map(SDL_SCANCODE_A, MOVE_LEFT);
590 Map(SDL_SCANCODE_RIGHT, MOVE_RIGHT);
591 Map(SDL_SCANCODE_D, MOVE_RIGHT);
592 Map(SDL_SCANCODE_SPACE, MOVE_UP);
593 Map(SDL_SCANCODE_RSHIFT, MOVE_UP);
594 Map(SDL_SCANCODE_LSHIFT, MOVE_DOWN);
595 Map(SDL_SCANCODE_LCTRL, MOVE_DOWN);
596 Map(SDL_SCANCODE_RCTRL, MOVE_DOWN);
598 Map(SDL_SCANCODE_Q, BLOCK_FACE);
599 Map(SDL_SCANCODE_E, BLOCK_TURN);
600 Map(SDL_SCANCODE_TAB, BLOCK_NEXT);
601 Map(SDL_SCANCODE_RIGHTBRACKET, BLOCK_NEXT);
602 Map(SDL_SCANCODE_LEFTBRACKET, BLOCK_PREV);
604 Map(SDL_SCANCODE_INSERT, BLOCK_PLACE);
605 Map(SDL_SCANCODE_RETURN, BLOCK_PLACE);
606 Map(SDL_SCANCODE_MENU, BLOCK_PICK);
607 Map(SDL_SCANCODE_DELETE, BLOCK_REMOVE);
608 Map(SDL_SCANCODE_BACKSPACE, BLOCK_REMOVE);
610 Map(SDL_SCANCODE_N, TOGGLE_COLLISION);
611 Map(SDL_SCANCODE_F1, TOGGLE_VISUAL);
612 Map(SDL_SCANCODE_F3, TOGGLE_DEBUG);
613 Map(SDL_SCANCODE_F4, TOGGLE_AUDIO);
615 Map(SDL_SCANCODE_ESCAPE, EXIT);
619 void Keymap::Load(std::istream &is) {
620 TokenStreamReader in(is);
621 std::string key_name;
622 std::string action_name;
625 while (in.HasMore()) {
626 if (in.Peek().type == Token::STRING) {
627 in.ReadString(key_name);
628 key = SDL_GetScancodeFromName(key_name.c_str());
630 key = SDL_Scancode(in.GetInt());
632 in.Skip(Token::EQUALS);
633 in.ReadIdentifier(action_name);
634 action = StringToAction(action_name);
635 if (in.HasMore() && in.Peek().type == Token::SEMICOLON) {
636 in.Skip(Token::SEMICOLON);
642 void Keymap::Save(std::ostream &out) {
643 for (unsigned int i = 0; i < NUM_SCANCODES; ++i) {
644 if (codemap[i] == NONE) continue;
646 const char *str = SDL_GetScancodeName(SDL_Scancode(i));
662 out << " = " << ActionToString(codemap[i]) << std::endl;;
667 const char *Keymap::ActionToString(Action action) {
673 return "move_forward";
675 return "move_backward";
693 return "block_place";
697 return "block_remove";
698 case TOGGLE_COLLISION:
699 return "toggle_collision";
701 return "toggle_audio";
703 return "toggle_visual";
705 return "toggle_debug";
711 Keymap::Action Keymap::StringToAction(const std::string &str) {
712 if (str == "move_forward") {
714 } else if (str == "move_backward") {
715 return MOVE_BACKWARD;
716 } else if (str == "move_left") {
718 } else if (str == "move_right") {
720 } else if (str == "move_up") {
722 } else if (str == "move_down") {
724 } else if (str == "block_face") {
726 } else if (str == "block_turn") {
728 } else if (str == "block_next") {
730 } else if (str == "block_prev") {
732 } else if (str == "block_place") {
734 } else if (str == "block_pick") {
736 } else if (str == "block_remove") {
738 } else if (str == "toggle_collision") {
739 return TOGGLE_COLLISION;
740 } else if (str == "toggle_audio") {
742 } else if (str == "toggle_visual") {
743 return TOGGLE_VISUAL;
744 } else if (str == "toggle_debug") {
746 } else if (str == "exit") {