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 position_text.Hide();
128 position_text.Position(glm::vec3(-25.0f, 25.0f + font.LineSkip(), 0.0f), Gravity::NORTH_EAST);
129 position_text.Foreground(glm::vec4(1.0f));
130 position_text.Background(glm::vec4(0.5f));
131 messages.Position(glm::vec3(25.0f, -25.0f, 0.0f), Gravity::SOUTH_WEST);
132 messages.Foreground(glm::vec4(1.0f));
133 messages.Background(glm::vec4(0.5f));
134 hud.Display(selection);
138 void Interface::HandlePress(const SDL_KeyboardEvent &event) {
139 if (config.keyboard_disabled) return;
141 switch (event.keysym.sym) {
182 PrintSelectionInfo();
197 void Interface::HandleRelease(const SDL_KeyboardEvent &event) {
198 if (config.keyboard_disabled) return;
200 switch (event.keysym.sym) {
222 void Interface::FaceBlock() {
223 selection.SetFace(Block::Face((selection.GetFace() + 1) % Block::FACE_COUNT));
224 hud.Display(selection);
227 void Interface::TurnBlock() {
228 selection.SetTurn(Block::Turn((selection.GetTurn() + 1) % Block::TURN_COUNT));
229 hud.Display(selection);
232 void Interface::ToggleCollision() {
233 ctrl.Controlled().WorldCollidable(!ctrl.Controlled().WorldCollidable());
234 if (ctrl.Controlled().WorldCollidable()) {
235 PostMessage("collision on");
237 PostMessage("collision off");
241 void Interface::PrintBlockInfo() {
242 std::cout << std::endl;
244 PostMessage("not looking at any block");
245 Ray aim = ctrl.Aim();
247 s << "aim ray: " << aim.orig << ", " << aim.dir;
248 PostMessage(s.str());
252 s << "looking at block " << aim_block
253 << " " << Chunk::ToCoords(aim_block)
254 << " of chunk " << aim_chunk->Position()
256 PostMessage(s.str());
257 Print(aim_chunk->BlockAt(aim_block));
260 void Interface::PrintChunkInfo() {
261 std::cout << std::endl;
263 PostMessage("not looking at any block");
267 s << "looking at chunk " << aim_chunk->Position();
268 PostMessage(s.str());
270 PostMessage(" neighbors:");
271 if (aim_chunk->HasNeighbor(Block::FACE_LEFT)) {
273 s << " left " << aim_chunk->GetNeighbor(Block::FACE_LEFT).Position();
274 PostMessage(s.str());
276 if (aim_chunk->HasNeighbor(Block::FACE_RIGHT)) {
278 s << " right " << aim_chunk->GetNeighbor(Block::FACE_RIGHT).Position();
279 PostMessage(s.str());
281 if (aim_chunk->HasNeighbor(Block::FACE_UP)) {
283 s << " up " << aim_chunk->GetNeighbor(Block::FACE_UP).Position();
284 PostMessage(s.str());
286 if (aim_chunk->HasNeighbor(Block::FACE_DOWN)) {
288 s << " down " << aim_chunk->GetNeighbor(Block::FACE_DOWN).Position();
289 PostMessage(s.str());
291 if (aim_chunk->HasNeighbor(Block::FACE_FRONT)) {
293 s << " front " << aim_chunk->GetNeighbor(Block::FACE_FRONT).Position();
294 PostMessage(s.str());
296 if (aim_chunk->HasNeighbor(Block::FACE_BACK)) {
298 s << " back " << aim_chunk->GetNeighbor(Block::FACE_BACK).Position();
299 PostMessage(s.str());
301 std::cout << std::endl;
304 void Interface::PrintLightInfo() {
307 << "light level " << world.PlayerChunk().GetLight(world.Player().Position())
308 << " at position " << world.Player().Position()
310 PostMessage(s.str());
313 void Interface::PrintSelectionInfo() {
314 std::cout << std::endl;
318 void Interface::Print(const Block &block) {
320 s << "type: " << block.type
321 << ", face: " << block.GetFace()
322 << ", turn: " << block.GetTurn()
324 PostMessage(s.str());
327 void Interface::ToggleAudio() {
328 config.audio_disabled = !config.audio_disabled;
329 if (config.audio_disabled) {
330 PostMessage("audio off");
332 PostMessage("audio on");
336 void Interface::ToggleVisual() {
337 config.visual_disabled = !config.visual_disabled;
338 if (config.visual_disabled) {
339 PostMessage("visual off");
341 PostMessage("visual on");
345 void Interface::ToggleDebug() {
346 counter_text.Toggle();
347 position_text.Toggle();
348 if (counter_text.Visible()) {
354 void Interface::UpdateCounter() {
356 s << std::setprecision(3) <<
357 "avg: " << counter.Average().running << "ms, "
358 "peak: " << counter.Peak().running << "ms";
359 std::string text = s.str();
360 counter_text.Set(font, text);
363 void Interface::UpdatePosition() {
365 s << std::setprecision(3) << "pos: " << ctrl.Controlled().AbsolutePosition();
366 position_text.Set(font, s.str());
370 void Interface::Handle(const SDL_MouseMotionEvent &event) {
371 if (config.mouse_disabled) return;
372 ctrl.RotateYaw(event.xrel * config.yaw_sensitivity);
373 ctrl.RotatePitch(event.yrel * config.pitch_sensitivity);
376 void Interface::HandlePress(const SDL_MouseButtonEvent &event) {
377 if (config.mouse_disabled) return;
379 if (event.button == SDL_BUTTON_LEFT) {
381 remove_timer.Start();
382 } else if (event.button == SDL_BUTTON_MIDDLE) {
384 } else if (event.button == SDL_BUTTON_RIGHT) {
390 void Interface::HandleRelease(const SDL_MouseButtonEvent &event) {
391 if (config.mouse_disabled) return;
393 if (event.button == SDL_BUTTON_LEFT) {
395 } else if (event.button == SDL_BUTTON_RIGHT) {
400 void Interface::PickBlock() {
401 if (!aim_chunk) return;
402 selection = aim_chunk->BlockAt(aim_block);
403 hud.Display(selection);
406 void Interface::PlaceBlock() {
407 if (!aim_chunk) return;
408 Chunk *mod_chunk = aim_chunk;
409 glm::vec3 next_pos = Chunk::ToCoords(aim_block) + aim_normal;
410 if (!Chunk::InBounds(next_pos)) {
411 mod_chunk = &world.Next(*aim_chunk, aim_normal);
412 next_pos -= aim_normal * glm::vec3(Chunk::Extent());
414 mod_chunk->SetBlock(next_pos, selection);
415 mod_chunk->Invalidate();
417 if (config.audio_disabled) return;
418 const Entity &player = ctrl.Controlled();
421 mod_chunk->ToSceneCoords(player.ChunkCoords(), next_pos)
425 void Interface::RemoveBlock() noexcept {
426 if (!aim_chunk) return;
427 aim_chunk->SetBlock(aim_block, remove);
428 aim_chunk->Invalidate();
430 if (config.audio_disabled) return;
431 const Entity &player = ctrl.Controlled();
434 aim_chunk->ToSceneCoords(player.ChunkCoords(), Chunk::ToCoords(aim_block))
439 void Interface::Handle(const SDL_MouseWheelEvent &event) {
440 if (config.mouse_disabled) return;
444 } else if (event.y > 0) {
449 void Interface::SelectNext() {
451 if (size_t(selection.type) >= world.BlockTypes().Size()) {
454 hud.Display(selection);
457 void Interface::SelectPrevious() {
459 if (selection.type <= 0) {
460 selection.type = world.BlockTypes().Size() - 1;
462 hud.Display(selection);
466 void Interface::PostMessage(const char *msg) {
467 messages.PushLine(msg);
470 std::cout << msg << std::endl;
474 void Interface::Update(int dt) {
475 ctrl.Velocity(glm::vec3(fwd - rev) * config.move_velocity);
478 msg_timer.Update(dt);
479 place_timer.Update(dt);
480 remove_timer.Update(dt);
485 if (msg_timer.HitOnce()) {
489 if (remove_timer.Hit()) {
494 if (place_timer.Hit()) {
499 if (counter_text.Visible() && counter.Changed()) {
502 if (position_text.Visible()) {
509 OutlineModel::Buffer outl_buf;
513 void Interface::CheckAim() {
515 if (world.Intersection(aim, glm::mat4(1.0f), aim_chunk, aim_block, dist, aim_normal)) {
517 aim_chunk->Type(aim_chunk->BlockAt(aim_block)).FillOutlineModel(outl_buf);
518 outline.Update(outl_buf);
519 outline_transform = glm::scale(glm::vec3(1.0002f));
520 outline_transform *= aim_chunk->Transform(world.Player().ChunkCoords());
521 outline_transform *= aim_chunk->ToTransform(Chunk::ToPos(aim_block), aim_block);
528 void Interface::Render(Viewport &viewport) noexcept {
529 if (config.visual_disabled) return;
532 DirectionalLighting &world_prog = viewport.EntityProgram();
533 world_prog.SetM(outline_transform);
537 if (counter_text.Visible()) {
538 counter_text.Render(viewport);
540 if (position_text.Visible()) {
541 position_text.Render(viewport);
544 if (msg_timer.Running()) {
545 messages.Render(viewport);
548 hud.Render(viewport);