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 PlainColor &outline_prog = viewport.HUDOutlineProgram();
76 viewport.EnableInvertBlending();
77 viewport.SetCursor(glm::vec3(0.0f), Gravity::CENTER);
78 outline_prog.SetM(viewport.Cursor());
82 DirectionalLighting &world_prog = viewport.HUDProgram();
83 world_prog.SetLightDirection({ 1.0f, 3.0f, 5.0f });
84 // disable distance fog
85 world_prog.SetFogDensity(0.0f);
87 viewport.DisableBlending();
88 world_prog.SetM(block_transform);
90 block_label.Render(viewport);
101 , ctrl(world.Player())
102 , hud(world.BlockTypes(), env.assets.small_ui_font)
103 , aim{{ 0, 0, 0 }, { 0, 0, -1 }}
108 , outline_transform(1.0f)
112 , messages(env.assets.small_ui_font)
119 , place_sound(env.assets.LoadSound("thump"))
120 , remove_sound(env.assets.LoadSound("plop"))
124 counter_text.Position(glm::vec3(-25.0f, 25.0f, 0.0f), Gravity::NORTH_EAST);
125 counter_text.Foreground(glm::vec4(1.0f));
126 counter_text.Background(glm::vec4(0.5f));
127 position_text.Position(glm::vec3(-25.0f, 25.0f + env.assets.small_ui_font.LineSkip(), 0.0f), Gravity::NORTH_EAST);
128 position_text.Foreground(glm::vec4(1.0f));
129 position_text.Background(glm::vec4(0.5f));
130 orientation_text.Position(glm::vec3(-25.0f, 25.0f + 2 * env.assets.small_ui_font.LineSkip(), 0.0f), Gravity::NORTH_EAST);
131 orientation_text.Foreground(glm::vec4(1.0f));
132 orientation_text.Background(glm::vec4(0.5f));
133 messages.Position(glm::vec3(25.0f, -25.0f, 0.0f), Gravity::SOUTH_WEST);
134 messages.Foreground(glm::vec4(1.0f));
135 messages.Background(glm::vec4(0.5f));
136 hud.Display(selection);
140 void Interface::HandlePress(const SDL_KeyboardEvent &event) {
141 if (config.keyboard_disabled) return;
143 switch (event.keysym.sym) {
184 PrintSelectionInfo();
199 void Interface::HandleRelease(const SDL_KeyboardEvent &event) {
200 if (config.keyboard_disabled) return;
202 switch (event.keysym.sym) {
224 void Interface::FaceBlock() {
225 selection.SetFace(Block::Face((selection.GetFace() + 1) % Block::FACE_COUNT));
226 hud.Display(selection);
229 void Interface::TurnBlock() {
230 selection.SetTurn(Block::Turn((selection.GetTurn() + 1) % Block::TURN_COUNT));
231 hud.Display(selection);
234 void Interface::ToggleCollision() {
235 ctrl.Controlled().WorldCollidable(!ctrl.Controlled().WorldCollidable());
236 if (ctrl.Controlled().WorldCollidable()) {
237 PostMessage("collision on");
239 PostMessage("collision off");
243 void Interface::PrintBlockInfo() {
244 std::cout << std::endl;
246 PostMessage("not looking at any block");
247 Ray aim = ctrl.Aim();
249 s << "aim ray: " << aim.orig << ", " << aim.dir;
250 PostMessage(s.str());
254 s << "looking at block " << aim_block
255 << " " << Chunk::ToCoords(aim_block)
256 << " of chunk " << aim_chunk->Position()
258 PostMessage(s.str());
259 Print(aim_chunk->BlockAt(aim_block));
262 void Interface::PrintChunkInfo() {
263 std::cout << std::endl;
265 PostMessage("not looking at any block");
269 s << "looking at chunk " << aim_chunk->Position();
270 PostMessage(s.str());
272 PostMessage(" neighbors:");
273 if (aim_chunk->HasNeighbor(Block::FACE_LEFT)) {
275 s << " left " << aim_chunk->GetNeighbor(Block::FACE_LEFT).Position();
276 PostMessage(s.str());
278 if (aim_chunk->HasNeighbor(Block::FACE_RIGHT)) {
280 s << " right " << aim_chunk->GetNeighbor(Block::FACE_RIGHT).Position();
281 PostMessage(s.str());
283 if (aim_chunk->HasNeighbor(Block::FACE_UP)) {
285 s << " up " << aim_chunk->GetNeighbor(Block::FACE_UP).Position();
286 PostMessage(s.str());
288 if (aim_chunk->HasNeighbor(Block::FACE_DOWN)) {
290 s << " down " << aim_chunk->GetNeighbor(Block::FACE_DOWN).Position();
291 PostMessage(s.str());
293 if (aim_chunk->HasNeighbor(Block::FACE_FRONT)) {
295 s << " front " << aim_chunk->GetNeighbor(Block::FACE_FRONT).Position();
296 PostMessage(s.str());
298 if (aim_chunk->HasNeighbor(Block::FACE_BACK)) {
300 s << " back " << aim_chunk->GetNeighbor(Block::FACE_BACK).Position();
301 PostMessage(s.str());
303 std::cout << std::endl;
306 void Interface::PrintLightInfo() {
309 << "light level " << world.PlayerChunk().GetLight(world.Player().Position())
310 << " at position " << world.Player().Position()
312 PostMessage(s.str());
315 void Interface::PrintSelectionInfo() {
316 std::cout << std::endl;
320 void Interface::Print(const Block &block) {
322 s << "type: " << block.type
323 << ", face: " << block.GetFace()
324 << ", turn: " << block.GetTurn()
326 PostMessage(s.str());
329 void Interface::ToggleAudio() {
330 config.audio_disabled = !config.audio_disabled;
331 if (config.audio_disabled) {
332 PostMessage("audio off");
334 PostMessage("audio on");
338 void Interface::ToggleVisual() {
339 config.visual_disabled = !config.visual_disabled;
340 if (config.visual_disabled) {
341 PostMessage("visual off");
343 PostMessage("visual on");
347 void Interface::ToggleDebug() {
356 void Interface::UpdateCounter() {
358 s << std::setprecision(3) <<
359 "avg: " << env.counter.Average().running << "ms, "
360 "peak: " << env.counter.Peak().running << "ms";
361 std::string text = s.str();
362 counter_text.Set(env.assets.small_ui_font, text);
365 void Interface::UpdatePosition() {
367 s << std::setprecision(3) << "pos: " << ctrl.Controlled().AbsolutePosition();
368 position_text.Set(env.assets.small_ui_font, s.str());
371 void Interface::UpdateOrientation() {
373 s << std::setprecision(3) << "pitch: " << rad2deg(ctrl.Pitch())
374 << ", yaw: " << rad2deg(ctrl.Yaw());
375 orientation_text.Set(env.assets.small_ui_font, s.str());
379 void Interface::Handle(const SDL_MouseMotionEvent &event) {
380 if (config.mouse_disabled) return;
381 ctrl.RotateYaw(event.xrel * config.yaw_sensitivity);
382 ctrl.RotatePitch(event.yrel * config.pitch_sensitivity);
385 void Interface::HandlePress(const SDL_MouseButtonEvent &event) {
386 if (config.mouse_disabled) return;
388 if (event.button == SDL_BUTTON_LEFT) {
390 remove_timer.Start();
391 } else if (event.button == SDL_BUTTON_MIDDLE) {
393 } else if (event.button == SDL_BUTTON_RIGHT) {
399 void Interface::HandleRelease(const SDL_MouseButtonEvent &event) {
400 if (config.mouse_disabled) return;
402 if (event.button == SDL_BUTTON_LEFT) {
404 } else if (event.button == SDL_BUTTON_RIGHT) {
409 void Interface::PickBlock() {
410 if (!aim_chunk) return;
411 selection = aim_chunk->BlockAt(aim_block);
412 hud.Display(selection);
415 void Interface::PlaceBlock() {
416 if (!aim_chunk) return;
417 Chunk *mod_chunk = aim_chunk;
418 glm::vec3 next_pos = Chunk::ToCoords(aim_block) + aim_normal;
419 if (!Chunk::InBounds(next_pos)) {
420 mod_chunk = &world.Next(*aim_chunk, aim_normal);
421 next_pos -= aim_normal * glm::vec3(Chunk::Extent());
423 mod_chunk->SetBlock(next_pos, selection);
425 if (config.audio_disabled) return;
426 const Entity &player = ctrl.Controlled();
429 mod_chunk->ToSceneCoords(player.ChunkCoords(), next_pos)
433 void Interface::RemoveBlock() noexcept {
434 if (!aim_chunk) return;
435 aim_chunk->SetBlock(aim_block, remove);
437 if (config.audio_disabled) return;
438 const Entity &player = ctrl.Controlled();
441 aim_chunk->ToSceneCoords(player.ChunkCoords(), Chunk::ToCoords(aim_block))
446 void Interface::Handle(const SDL_MouseWheelEvent &event) {
447 if (config.mouse_disabled) return;
451 } else if (event.y > 0) {
456 void Interface::SelectNext() {
458 if (size_t(selection.type) >= world.BlockTypes().Size()) {
461 hud.Display(selection);
464 void Interface::SelectPrevious() {
466 if (selection.type <= 0) {
467 selection.type = world.BlockTypes().Size() - 1;
469 hud.Display(selection);
473 void Interface::PostMessage(const char *msg) {
474 messages.PushLine(msg);
477 std::cout << msg << std::endl;
481 void Interface::Update(int dt) {
482 ctrl.Velocity(glm::vec3(fwd - rev) * config.move_velocity);
485 msg_timer.Update(dt);
486 place_timer.Update(dt);
487 remove_timer.Update(dt);
492 if (msg_timer.HitOnce()) {
496 if (remove_timer.Hit()) {
501 if (place_timer.Hit()) {
507 if (env.counter.Changed()) {
517 OutlineModel::Buffer outl_buf;
521 void Interface::CheckAim() {
523 if (world.Intersection(aim, glm::mat4(1.0f), aim_chunk, aim_block, dist, aim_normal)) {
525 aim_chunk->Type(aim_chunk->BlockAt(aim_block)).FillOutlineModel(outl_buf);
526 outline.Update(outl_buf);
527 outline_transform = aim_chunk->Transform(world.Player().ChunkCoords());
528 outline_transform *= aim_chunk->ToTransform(Chunk::ToPos(aim_block), aim_block);
529 outline_transform *= glm::scale(glm::vec3(1.005f));
536 void Interface::Render(Viewport &viewport) noexcept {
537 if (config.visual_disabled) return;
540 PlainColor &outline_prog = viewport.WorldOutlineProgram();
541 outline_prog.SetM(outline_transform);
546 counter_text.Render(viewport);
547 position_text.Render(viewport);
548 orientation_text.Render(viewport);
551 if (msg_timer.Running()) {
552 messages.Render(viewport);
555 hud.Render(viewport);