2 #include "Interface.hpp"
4 #include "../app/Assets.hpp"
5 #include "../app/Environment.hpp"
6 #include "../app/FrameCounter.hpp"
7 #include "../app/init.hpp"
8 #include "../audio/Audio.hpp"
9 #include "../graphics/Font.hpp"
10 #include "../graphics/Viewport.hpp"
11 #include "../model/shapes.hpp"
12 #include "../world/World.hpp"
18 #include <glm/gtc/matrix_transform.hpp>
19 #include <glm/gtx/io.hpp>
24 HUD::HUD(const BlockTypeRegistry &types, const Font &font)
29 , block_transform(1.0f)
31 , block_visible(false)
33 block_transform = glm::translate(block_transform, glm::vec3(50.0f, 50.0f, 0.0f));
34 block_transform = glm::scale(block_transform, glm::vec3(50.0f));
35 block_transform = glm::rotate(block_transform, 3.5f, glm::vec3(1.0f, 0.0f, 0.0f));
36 block_transform = glm::rotate(block_transform, 0.35f, glm::vec3(0.0f, 1.0f, 0.0f));
38 OutlineModel::Buffer buf;
39 buf.vertices = std::vector<glm::vec3>({
40 { -10.0f, 0.0f, 0.0f }, { 10.0f, 0.0f, 0.0f },
41 { 0.0f, -10.0f, 0.0f }, { 0.0f, 10.0f, 0.0f },
43 buf.indices = std::vector<OutlineModel::Index>({
46 buf.colors.resize(4, { 10.0f, 10.0f, 10.0f });
47 crosshair.Update(buf);
50 glm::vec3(50.0f, 85.0f, 0.0f),
54 block_label.Foreground(glm::vec4(1.0f));
55 block_label.Background(glm::vec4(0.5f));
59 void HUD::Display(const Block &b) {
60 const BlockType &type = types.Get(b.type);
63 type.FillEntityModel(block_buf, b.Transform());
64 block.Update(block_buf);
66 block_label.Set(font, type.label);
68 block_visible = type.visible;
72 void HUD::Render(Viewport &viewport) noexcept {
73 viewport.ClearDepth();
75 DirectionalLighting &world_prog = viewport.HUDProgram();
76 world_prog.SetLightDirection({ 1.0f, 3.0f, 5.0f });
77 // disable distance fog
78 world_prog.SetFogDensity(0.0f);
80 viewport.EnableInvertBlending();
81 viewport.SetCursor(glm::vec3(0.0f), Gravity::CENTER);
82 world_prog.SetM(viewport.Cursor());
86 viewport.DisableBlending();
87 world_prog.SetM(block_transform);
89 block_label.Render(viewport);
100 , ctrl(world.Player())
101 , hud(world.BlockTypes(), env.assets.small_ui_font)
102 , aim{{ 0, 0, 0 }, { 0, 0, -1 }}
107 , outline_transform(1.0f)
109 , messages(env.assets.small_ui_font)
116 , place_sound(env.assets.LoadSound("thump"))
117 , remove_sound(env.assets.LoadSound("plop"))
121 counter_text.Position(glm::vec3(-25.0f, 25.0f, 0.0f), Gravity::NORTH_EAST);
122 counter_text.Foreground(glm::vec4(1.0f));
123 counter_text.Background(glm::vec4(0.5f));
124 position_text.Hide();
125 position_text.Position(glm::vec3(-25.0f, 25.0f + env.assets.small_ui_font.LineSkip(), 0.0f), Gravity::NORTH_EAST);
126 position_text.Foreground(glm::vec4(1.0f));
127 position_text.Background(glm::vec4(0.5f));
128 messages.Position(glm::vec3(25.0f, -25.0f, 0.0f), Gravity::SOUTH_WEST);
129 messages.Foreground(glm::vec4(1.0f));
130 messages.Background(glm::vec4(0.5f));
131 hud.Display(selection);
135 void Interface::HandlePress(const SDL_KeyboardEvent &event) {
136 if (config.keyboard_disabled) return;
138 switch (event.keysym.sym) {
179 PrintSelectionInfo();
194 void Interface::HandleRelease(const SDL_KeyboardEvent &event) {
195 if (config.keyboard_disabled) return;
197 switch (event.keysym.sym) {
219 void Interface::FaceBlock() {
220 selection.SetFace(Block::Face((selection.GetFace() + 1) % Block::FACE_COUNT));
221 hud.Display(selection);
224 void Interface::TurnBlock() {
225 selection.SetTurn(Block::Turn((selection.GetTurn() + 1) % Block::TURN_COUNT));
226 hud.Display(selection);
229 void Interface::ToggleCollision() {
230 ctrl.Controlled().WorldCollidable(!ctrl.Controlled().WorldCollidable());
231 if (ctrl.Controlled().WorldCollidable()) {
232 PostMessage("collision on");
234 PostMessage("collision off");
238 void Interface::PrintBlockInfo() {
239 std::cout << std::endl;
241 PostMessage("not looking at any block");
242 Ray aim = ctrl.Aim();
244 s << "aim ray: " << aim.orig << ", " << aim.dir;
245 PostMessage(s.str());
249 s << "looking at block " << aim_block
250 << " " << Chunk::ToCoords(aim_block)
251 << " of chunk " << aim_chunk->Position()
253 PostMessage(s.str());
254 Print(aim_chunk->BlockAt(aim_block));
257 void Interface::PrintChunkInfo() {
258 std::cout << std::endl;
260 PostMessage("not looking at any block");
264 s << "looking at chunk " << aim_chunk->Position();
265 PostMessage(s.str());
267 PostMessage(" neighbors:");
268 if (aim_chunk->HasNeighbor(Block::FACE_LEFT)) {
270 s << " left " << aim_chunk->GetNeighbor(Block::FACE_LEFT).Position();
271 PostMessage(s.str());
273 if (aim_chunk->HasNeighbor(Block::FACE_RIGHT)) {
275 s << " right " << aim_chunk->GetNeighbor(Block::FACE_RIGHT).Position();
276 PostMessage(s.str());
278 if (aim_chunk->HasNeighbor(Block::FACE_UP)) {
280 s << " up " << aim_chunk->GetNeighbor(Block::FACE_UP).Position();
281 PostMessage(s.str());
283 if (aim_chunk->HasNeighbor(Block::FACE_DOWN)) {
285 s << " down " << aim_chunk->GetNeighbor(Block::FACE_DOWN).Position();
286 PostMessage(s.str());
288 if (aim_chunk->HasNeighbor(Block::FACE_FRONT)) {
290 s << " front " << aim_chunk->GetNeighbor(Block::FACE_FRONT).Position();
291 PostMessage(s.str());
293 if (aim_chunk->HasNeighbor(Block::FACE_BACK)) {
295 s << " back " << aim_chunk->GetNeighbor(Block::FACE_BACK).Position();
296 PostMessage(s.str());
298 std::cout << std::endl;
301 void Interface::PrintLightInfo() {
304 << "light level " << world.PlayerChunk().GetLight(world.Player().Position())
305 << " at position " << world.Player().Position()
307 PostMessage(s.str());
310 void Interface::PrintSelectionInfo() {
311 std::cout << std::endl;
315 void Interface::Print(const Block &block) {
317 s << "type: " << block.type
318 << ", face: " << block.GetFace()
319 << ", turn: " << block.GetTurn()
321 PostMessage(s.str());
324 void Interface::ToggleAudio() {
325 config.audio_disabled = !config.audio_disabled;
326 if (config.audio_disabled) {
327 PostMessage("audio off");
329 PostMessage("audio on");
333 void Interface::ToggleVisual() {
334 config.visual_disabled = !config.visual_disabled;
335 if (config.visual_disabled) {
336 PostMessage("visual off");
338 PostMessage("visual on");
342 void Interface::ToggleDebug() {
343 counter_text.Toggle();
344 position_text.Toggle();
345 if (counter_text.Visible()) {
351 void Interface::UpdateCounter() {
353 s << std::setprecision(3) <<
354 "avg: " << env.counter.Average().running << "ms, "
355 "peak: " << env.counter.Peak().running << "ms";
356 std::string text = s.str();
357 counter_text.Set(env.assets.small_ui_font, text);
360 void Interface::UpdatePosition() {
362 s << std::setprecision(3) << "pos: " << ctrl.Controlled().AbsolutePosition();
363 position_text.Set(env.assets.small_ui_font, s.str());
367 void Interface::Handle(const SDL_MouseMotionEvent &event) {
368 if (config.mouse_disabled) return;
369 ctrl.RotateYaw(event.xrel * config.yaw_sensitivity);
370 ctrl.RotatePitch(event.yrel * config.pitch_sensitivity);
373 void Interface::HandlePress(const SDL_MouseButtonEvent &event) {
374 if (config.mouse_disabled) return;
376 if (event.button == SDL_BUTTON_LEFT) {
378 remove_timer.Start();
379 } else if (event.button == SDL_BUTTON_MIDDLE) {
381 } else if (event.button == SDL_BUTTON_RIGHT) {
387 void Interface::HandleRelease(const SDL_MouseButtonEvent &event) {
388 if (config.mouse_disabled) return;
390 if (event.button == SDL_BUTTON_LEFT) {
392 } else if (event.button == SDL_BUTTON_RIGHT) {
397 void Interface::PickBlock() {
398 if (!aim_chunk) return;
399 selection = aim_chunk->BlockAt(aim_block);
400 hud.Display(selection);
403 void Interface::PlaceBlock() {
404 if (!aim_chunk) return;
405 Chunk *mod_chunk = aim_chunk;
406 glm::vec3 next_pos = Chunk::ToCoords(aim_block) + aim_normal;
407 if (!Chunk::InBounds(next_pos)) {
408 mod_chunk = &world.Next(*aim_chunk, aim_normal);
409 next_pos -= aim_normal * glm::vec3(Chunk::Extent());
411 mod_chunk->SetBlock(next_pos, selection);
413 if (config.audio_disabled) return;
414 const Entity &player = ctrl.Controlled();
417 mod_chunk->ToSceneCoords(player.ChunkCoords(), next_pos)
421 void Interface::RemoveBlock() noexcept {
422 if (!aim_chunk) return;
423 aim_chunk->SetBlock(aim_block, remove);
425 if (config.audio_disabled) return;
426 const Entity &player = ctrl.Controlled();
429 aim_chunk->ToSceneCoords(player.ChunkCoords(), Chunk::ToCoords(aim_block))
434 void Interface::Handle(const SDL_MouseWheelEvent &event) {
435 if (config.mouse_disabled) return;
439 } else if (event.y > 0) {
444 void Interface::SelectNext() {
446 if (size_t(selection.type) >= world.BlockTypes().Size()) {
449 hud.Display(selection);
452 void Interface::SelectPrevious() {
454 if (selection.type <= 0) {
455 selection.type = world.BlockTypes().Size() - 1;
457 hud.Display(selection);
461 void Interface::PostMessage(const char *msg) {
462 messages.PushLine(msg);
465 std::cout << msg << std::endl;
469 void Interface::Update(int dt) {
470 ctrl.Velocity(glm::vec3(fwd - rev) * config.move_velocity);
473 msg_timer.Update(dt);
474 place_timer.Update(dt);
475 remove_timer.Update(dt);
480 if (msg_timer.HitOnce()) {
484 if (remove_timer.Hit()) {
489 if (place_timer.Hit()) {
494 if (counter_text.Visible() && env.counter.Changed()) {
497 if (position_text.Visible()) {
504 OutlineModel::Buffer outl_buf;
508 void Interface::CheckAim() {
510 if (world.Intersection(aim, glm::mat4(1.0f), aim_chunk, aim_block, dist, aim_normal)) {
512 aim_chunk->Type(aim_chunk->BlockAt(aim_block)).FillOutlineModel(outl_buf);
513 outline.Update(outl_buf);
514 outline_transform = glm::scale(glm::vec3(1.0002f));
515 outline_transform *= aim_chunk->Transform(world.Player().ChunkCoords());
516 outline_transform *= aim_chunk->ToTransform(Chunk::ToPos(aim_block), aim_block);
523 void Interface::Render(Viewport &viewport) noexcept {
524 if (config.visual_disabled) return;
527 DirectionalLighting &world_prog = viewport.EntityProgram();
528 world_prog.SetM(outline_transform);
532 if (counter_text.Visible()) {
533 counter_text.Render(viewport);
535 if (position_text.Visible()) {
536 position_text.Render(viewport);
539 if (msg_timer.Running()) {
540 messages.Render(viewport);
543 hud.Render(viewport);