2 #include "Interface.hpp"
4 #include "../app/Assets.hpp"
5 #include "../app/FrameCounter.hpp"
6 #include "../app/init.hpp"
7 #include "../audio/Audio.hpp"
8 #include "../graphics/Font.hpp"
9 #include "../graphics/Viewport.hpp"
10 #include "../model/shapes.hpp"
11 #include "../world/World.hpp"
17 #include <glm/gtc/matrix_transform.hpp>
18 #include <glm/gtx/io.hpp>
23 HUD::HUD(const BlockTypeRegistry &types, const Font &font)
28 , block_transform(1.0f)
30 , block_visible(false)
32 block_transform = glm::translate(block_transform, glm::vec3(50.0f, 50.0f, 0.0f));
33 block_transform = glm::scale(block_transform, glm::vec3(50.0f));
34 block_transform = glm::rotate(block_transform, 3.5f, glm::vec3(1.0f, 0.0f, 0.0f));
35 block_transform = glm::rotate(block_transform, 0.35f, glm::vec3(0.0f, 1.0f, 0.0f));
37 OutlineModel::Buffer buf;
38 buf.vertices = std::vector<glm::vec3>({
39 { -10.0f, 0.0f, 0.0f }, { 10.0f, 0.0f, 0.0f },
40 { 0.0f, -10.0f, 0.0f }, { 0.0f, 10.0f, 0.0f },
42 buf.indices = std::vector<OutlineModel::Index>({
45 buf.colors.resize(4, { 10.0f, 10.0f, 10.0f });
46 crosshair.Update(buf);
49 glm::vec3(50.0f, 85.0f, 0.0f),
53 block_label.Foreground(glm::vec4(1.0f));
54 block_label.Background(glm::vec4(0.5f));
58 void HUD::Display(const Block &b) {
59 const BlockType &type = types.Get(b.type);
62 type.FillEntityModel(block_buf, b.Transform());
63 block.Update(block_buf);
65 block_label.Set(font, type.label);
67 block_visible = type.visible;
71 void HUD::Render(Viewport &viewport) noexcept {
72 viewport.ClearDepth();
74 DirectionalLighting &world_prog = viewport.HUDProgram();
75 world_prog.SetLightDirection({ 1.0f, 3.0f, 5.0f });
76 // disable distance fog
77 world_prog.SetFogDensity(0.0f);
79 viewport.EnableInvertBlending();
80 viewport.SetCursor(glm::vec3(0.0f), Gravity::CENTER);
81 world_prog.SetM(viewport.Cursor());
85 viewport.DisableBlending();
86 world_prog.SetM(block_transform);
88 block_label.Render(viewport);
97 const FrameCounter &counter,
102 , ctrl(world.Player())
103 , font(assets.LoadFont("DejaVuSans", 16))
104 , hud(world.BlockTypes(), font)
105 , aim{{ 0, 0, 0 }, { 0, 0, -1 }}
110 , outline_transform(1.0f)
119 , place_sound(assets.LoadSound("thump"))
120 , remove_sound(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 messages.Position(glm::vec3(25.0f, -25.0f, 0.0f), Gravity::SOUTH_WEST);
128 messages.Foreground(glm::vec4(1.0f));
129 messages.Background(glm::vec4(0.5f));
130 hud.Display(selection);
134 void Interface::HandlePress(const SDL_KeyboardEvent &event) {
135 if (config.keyboard_disabled) return;
137 switch (event.keysym.sym) {
178 PrintSelectionInfo();
193 void Interface::HandleRelease(const SDL_KeyboardEvent &event) {
194 if (config.keyboard_disabled) return;
196 switch (event.keysym.sym) {
218 void Interface::FaceBlock() {
219 selection.SetFace(Block::Face((selection.GetFace() + 1) % Block::FACE_COUNT));
220 hud.Display(selection);
223 void Interface::TurnBlock() {
224 selection.SetTurn(Block::Turn((selection.GetTurn() + 1) % Block::TURN_COUNT));
225 hud.Display(selection);
228 void Interface::ToggleCollision() {
229 ctrl.Controlled().WorldCollidable(!ctrl.Controlled().WorldCollidable());
230 if (ctrl.Controlled().WorldCollidable()) {
231 PostMessage("collision on");
233 PostMessage("collision off");
237 void Interface::PrintBlockInfo() {
238 std::cout << std::endl;
240 PostMessage("not looking at any block");
241 Ray aim = ctrl.Aim();
243 s << "aim ray: " << aim.orig << ", " << aim.dir;
244 PostMessage(s.str());
248 s << "looking at block " << aim_block
249 << " " << Chunk::ToCoords(aim_block)
250 << " of chunk " << aim_chunk->Position()
252 PostMessage(s.str());
253 Print(aim_chunk->BlockAt(aim_block));
256 void Interface::PrintChunkInfo() {
257 std::cout << std::endl;
259 PostMessage("not looking at any block");
263 s << "looking at chunk " << aim_chunk->Position();
264 PostMessage(s.str());
266 PostMessage(" neighbors:");
267 if (aim_chunk->HasNeighbor(Block::FACE_LEFT)) {
269 s << " left " << aim_chunk->GetNeighbor(Block::FACE_LEFT).Position();
270 PostMessage(s.str());
272 if (aim_chunk->HasNeighbor(Block::FACE_RIGHT)) {
274 s << " right " << aim_chunk->GetNeighbor(Block::FACE_RIGHT).Position();
275 PostMessage(s.str());
277 if (aim_chunk->HasNeighbor(Block::FACE_UP)) {
279 s << " up " << aim_chunk->GetNeighbor(Block::FACE_UP).Position();
280 PostMessage(s.str());
282 if (aim_chunk->HasNeighbor(Block::FACE_DOWN)) {
284 s << " down " << aim_chunk->GetNeighbor(Block::FACE_DOWN).Position();
285 PostMessage(s.str());
287 if (aim_chunk->HasNeighbor(Block::FACE_FRONT)) {
289 s << " front " << aim_chunk->GetNeighbor(Block::FACE_FRONT).Position();
290 PostMessage(s.str());
292 if (aim_chunk->HasNeighbor(Block::FACE_BACK)) {
294 s << " back " << aim_chunk->GetNeighbor(Block::FACE_BACK).Position();
295 PostMessage(s.str());
297 std::cout << std::endl;
300 void Interface::PrintLightInfo() {
303 << "light level " << world.PlayerChunk().GetLight(world.Player().Position())
304 << " at position " << world.Player().Position()
306 PostMessage(s.str());
309 void Interface::PrintSelectionInfo() {
310 std::cout << std::endl;
314 void Interface::Print(const Block &block) {
316 s << "type: " << block.type
317 << ", face: " << block.GetFace()
318 << ", turn: " << block.GetTurn()
320 PostMessage(s.str());
323 void Interface::ToggleAudio() {
324 config.audio_disabled = !config.audio_disabled;
325 if (config.audio_disabled) {
326 PostMessage("audio off");
328 PostMessage("audio on");
332 void Interface::ToggleVisual() {
333 config.visual_disabled = !config.visual_disabled;
334 if (config.visual_disabled) {
335 PostMessage("visual off");
337 PostMessage("visual on");
341 void Interface::ToggleCounter() {
342 counter_text.Toggle();
343 if (counter_text.Visible()) {
348 void Interface::UpdateCounter() {
350 s << std::setprecision(3) <<
351 "avg: " << counter.Average().running << "ms, "
352 "peak: " << counter.Peak().running << "ms";
353 std::string text = s.str();
354 counter_text.Set(font, text);
358 void Interface::Handle(const SDL_MouseMotionEvent &event) {
359 if (config.mouse_disabled) return;
360 ctrl.RotateYaw(event.xrel * config.yaw_sensitivity);
361 ctrl.RotatePitch(event.yrel * config.pitch_sensitivity);
364 void Interface::HandlePress(const SDL_MouseButtonEvent &event) {
365 if (config.mouse_disabled) return;
367 if (event.button == SDL_BUTTON_LEFT) {
369 remove_timer.Start();
370 } else if (event.button == SDL_BUTTON_MIDDLE) {
372 } else if (event.button == SDL_BUTTON_RIGHT) {
378 void Interface::HandleRelease(const SDL_MouseButtonEvent &event) {
379 if (config.mouse_disabled) return;
381 if (event.button == SDL_BUTTON_LEFT) {
383 } else if (event.button == SDL_BUTTON_RIGHT) {
388 void Interface::PickBlock() {
389 if (!aim_chunk) return;
390 selection = aim_chunk->BlockAt(aim_block);
391 hud.Display(selection);
394 void Interface::PlaceBlock() {
395 if (!aim_chunk) return;
396 Chunk *mod_chunk = aim_chunk;
397 glm::vec3 next_pos = Chunk::ToCoords(aim_block) + aim_normal;
398 if (!Chunk::InBounds(next_pos)) {
399 mod_chunk = &world.Next(*aim_chunk, aim_normal);
400 next_pos -= aim_normal * glm::vec3(Chunk::Extent());
402 mod_chunk->SetBlock(next_pos, selection);
403 mod_chunk->Invalidate();
405 if (config.audio_disabled) return;
406 const Entity &player = ctrl.Controlled();
409 mod_chunk->ToSceneCoords(player.ChunkCoords(), next_pos)
413 void Interface::RemoveBlock() noexcept {
414 if (!aim_chunk) return;
415 aim_chunk->SetBlock(aim_block, remove);
416 aim_chunk->Invalidate();
418 if (config.audio_disabled) return;
419 const Entity &player = ctrl.Controlled();
422 aim_chunk->ToSceneCoords(player.ChunkCoords(), Chunk::ToCoords(aim_block))
427 void Interface::Handle(const SDL_MouseWheelEvent &event) {
428 if (config.mouse_disabled) return;
432 } else if (event.y > 0) {
437 void Interface::SelectNext() {
439 if (size_t(selection.type) >= world.BlockTypes().Size()) {
442 hud.Display(selection);
445 void Interface::SelectPrevious() {
447 if (selection.type <= 0) {
448 selection.type = world.BlockTypes().Size() - 1;
450 hud.Display(selection);
454 void Interface::PostMessage(const char *msg) {
455 messages.PushLine(msg);
458 std::cout << msg << std::endl;
462 void Interface::Update(int dt) {
463 ctrl.Velocity(glm::vec3(fwd - rev) * config.move_velocity);
466 msg_timer.Update(dt);
467 place_timer.Update(dt);
468 remove_timer.Update(dt);
473 if (msg_timer.HitOnce()) {
477 if (remove_timer.Hit()) {
482 if (place_timer.Hit()) {
487 if (counter_text.Visible() && counter.Changed()) {
494 OutlineModel::Buffer outl_buf;
498 void Interface::CheckAim() {
500 if (world.Intersection(aim, glm::mat4(1.0f), aim_chunk, aim_block, dist, aim_normal)) {
502 aim_chunk->Type(aim_chunk->BlockAt(aim_block)).FillOutlineModel(outl_buf);
503 outline.Update(outl_buf);
504 outline_transform = glm::scale(glm::vec3(1.0002f));
505 outline_transform *= aim_chunk->Transform(world.Player().ChunkCoords());
506 outline_transform *= aim_chunk->ToTransform(Chunk::ToPos(aim_block), aim_block);
513 void Interface::Render(Viewport &viewport) noexcept {
514 if (config.visual_disabled) return;
517 DirectionalLighting &world_prog = viewport.EntityProgram();
518 world_prog.SetM(outline_transform);
522 if (counter_text.Visible()) {
523 counter_text.Render(viewport);
526 if (msg_timer.Running()) {
527 messages.Render(viewport);
530 hud.Render(viewport);