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 crosshair.vertices = std::vector<glm::vec3>({
38 { -10.0f, 0.0f, 0.0f }, { 10.0f, 0.0f, 0.0f },
39 { 0.0f, -10.0f, 0.0f }, { 0.0f, 10.0f, 0.0f },
41 crosshair.indices = std::vector<OutlineModel::Index>({
44 crosshair.colors.resize(4, { 10.0f, 10.0f, 10.0f });
45 crosshair.Invalidate();
48 glm::vec3(50.0f, 85.0f, 0.0f),
52 block_label.Foreground(glm::vec4(1.0f));
53 block_label.Background(glm::vec4(0.5f));
57 void HUD::Display(const Block &b) {
58 const BlockType &type = types.Get(b.type);
61 type.FillModel(block_buf, b.Transform());
62 block.Update(block_buf);
64 block_label.Set(font, type.label);
66 block_visible = type.visible;
70 void HUD::Render(Viewport &viewport) noexcept {
71 viewport.ClearDepth();
73 DirectionalLighting &world_prog = viewport.HUDProgram();
74 world_prog.SetLightDirection({ 1.0f, 3.0f, 5.0f });
75 // disable distance fog
76 world_prog.SetFogDensity(0.0f);
78 viewport.EnableInvertBlending();
79 viewport.SetCursor(glm::vec3(0.0f), Gravity::CENTER);
80 world_prog.SetM(viewport.Cursor());
84 viewport.DisableBlending();
85 world_prog.SetM(block_transform);
87 block_label.Render(viewport);
96 const FrameCounter &counter,
101 , ctrl(world.Player())
102 , font(assets.LoadFont("DejaVuSans", 16))
103 , hud(world.BlockTypes(), font)
104 , aim{{ 0, 0, 0 }, { 0, 0, -1 }}
109 , outline_transform(1.0f)
118 , place_sound(assets.LoadSound("thump"))
119 , remove_sound(assets.LoadSound("plop"))
123 counter_text.Position(glm::vec3(-25.0f, 25.0f, 0.0f), Gravity::NORTH_EAST);
124 counter_text.Foreground(glm::vec4(1.0f));
125 counter_text.Background(glm::vec4(0.5f));
126 messages.Position(glm::vec3(25.0f, -25.0f, 0.0f), Gravity::SOUTH_WEST);
127 messages.Foreground(glm::vec4(1.0f));
128 messages.Background(glm::vec4(0.5f));
129 hud.Display(selection);
133 void Interface::HandlePress(const SDL_KeyboardEvent &event) {
134 if (config.keyboard_disabled) return;
136 switch (event.keysym.sym) {
177 PrintSelectionInfo();
192 void Interface::HandleRelease(const SDL_KeyboardEvent &event) {
193 if (config.keyboard_disabled) return;
195 switch (event.keysym.sym) {
217 void Interface::FaceBlock() {
218 selection.SetFace(Block::Face((selection.GetFace() + 1) % Block::FACE_COUNT));
219 hud.Display(selection);
222 void Interface::TurnBlock() {
223 selection.SetTurn(Block::Turn((selection.GetTurn() + 1) % Block::TURN_COUNT));
224 hud.Display(selection);
227 void Interface::ToggleCollision() {
228 ctrl.Controlled().WorldCollidable(!ctrl.Controlled().WorldCollidable());
229 if (ctrl.Controlled().WorldCollidable()) {
230 PostMessage("collision on");
232 PostMessage("collision off");
236 void Interface::PrintBlockInfo() {
237 std::cout << std::endl;
239 PostMessage("not looking at any block");
240 Ray aim = ctrl.Aim();
242 s << "aim ray: " << aim.orig << ", " << aim.dir;
243 PostMessage(s.str());
247 s << "looking at block " << aim_block
248 << " " << Chunk::ToCoords(aim_block)
249 << " of chunk " << aim_chunk->Position()
251 PostMessage(s.str());
252 Print(aim_chunk->BlockAt(aim_block));
255 void Interface::PrintChunkInfo() {
256 std::cout << std::endl;
258 PostMessage("not looking at any block");
262 s << "looking at chunk " << aim_chunk->Position();
263 PostMessage(s.str());
265 PostMessage(" neighbors:");
266 if (aim_chunk->HasNeighbor(Block::FACE_LEFT)) {
268 s << " left " << aim_chunk->GetNeighbor(Block::FACE_LEFT).Position();
269 PostMessage(s.str());
271 if (aim_chunk->HasNeighbor(Block::FACE_RIGHT)) {
273 s << " right " << aim_chunk->GetNeighbor(Block::FACE_RIGHT).Position();
274 PostMessage(s.str());
276 if (aim_chunk->HasNeighbor(Block::FACE_UP)) {
278 s << " up " << aim_chunk->GetNeighbor(Block::FACE_UP).Position();
279 PostMessage(s.str());
281 if (aim_chunk->HasNeighbor(Block::FACE_DOWN)) {
283 s << " down " << aim_chunk->GetNeighbor(Block::FACE_DOWN).Position();
284 PostMessage(s.str());
286 if (aim_chunk->HasNeighbor(Block::FACE_FRONT)) {
288 s << " front " << aim_chunk->GetNeighbor(Block::FACE_FRONT).Position();
289 PostMessage(s.str());
291 if (aim_chunk->HasNeighbor(Block::FACE_BACK)) {
293 s << " back " << aim_chunk->GetNeighbor(Block::FACE_BACK).Position();
294 PostMessage(s.str());
296 std::cout << std::endl;
299 void Interface::PrintLightInfo() {
302 << "light level " << world.PlayerChunk().GetLight(world.Player().Position())
303 << " at position " << world.Player().Position()
305 PostMessage(s.str());
308 void Interface::PrintSelectionInfo() {
309 std::cout << std::endl;
313 void Interface::Print(const Block &block) {
315 s << "type: " << block.type
316 << ", face: " << block.GetFace()
317 << ", turn: " << block.GetTurn()
319 PostMessage(s.str());
322 void Interface::ToggleAudio() {
323 config.audio_disabled = !config.audio_disabled;
324 if (config.audio_disabled) {
325 PostMessage("audio off");
327 PostMessage("audio on");
331 void Interface::ToggleVisual() {
332 config.visual_disabled = !config.visual_disabled;
333 if (config.visual_disabled) {
334 PostMessage("visual off");
336 PostMessage("visual on");
340 void Interface::ToggleCounter() {
341 counter_text.Toggle();
342 if (counter_text.Visible()) {
347 void Interface::UpdateCounter() {
349 s << std::setprecision(3) <<
350 "avg: " << counter.Average().running << "ms, "
351 "peak: " << counter.Peak().running << "ms";
352 std::string text = s.str();
353 counter_text.Set(font, text);
357 void Interface::Handle(const SDL_MouseMotionEvent &event) {
358 if (config.mouse_disabled) return;
359 ctrl.RotateYaw(event.xrel * config.yaw_sensitivity);
360 ctrl.RotatePitch(event.yrel * config.pitch_sensitivity);
363 void Interface::HandlePress(const SDL_MouseButtonEvent &event) {
364 if (config.mouse_disabled) return;
366 if (event.button == SDL_BUTTON_LEFT) {
368 remove_timer.Start();
369 } else if (event.button == SDL_BUTTON_MIDDLE) {
371 } else if (event.button == SDL_BUTTON_RIGHT) {
377 void Interface::HandleRelease(const SDL_MouseButtonEvent &event) {
378 if (config.mouse_disabled) return;
380 if (event.button == SDL_BUTTON_LEFT) {
382 } else if (event.button == SDL_BUTTON_RIGHT) {
387 void Interface::PickBlock() {
388 if (!aim_chunk) return;
389 selection = aim_chunk->BlockAt(aim_block);
390 hud.Display(selection);
393 void Interface::PlaceBlock() {
394 if (!aim_chunk) return;
395 Chunk *mod_chunk = aim_chunk;
396 glm::vec3 next_pos = Chunk::ToCoords(aim_block) + aim_normal;
397 if (!Chunk::InBounds(next_pos)) {
398 mod_chunk = &world.Next(*aim_chunk, aim_normal);
399 next_pos -= aim_normal * glm::vec3(Chunk::Extent());
401 mod_chunk->SetBlock(next_pos, selection);
402 mod_chunk->Invalidate();
404 if (config.audio_disabled) return;
405 const Entity &player = ctrl.Controlled();
408 mod_chunk->ToSceneCoords(player.ChunkCoords(), next_pos)
412 void Interface::RemoveBlock() noexcept {
413 if (!aim_chunk) return;
414 aim_chunk->SetBlock(aim_block, remove);
415 aim_chunk->Invalidate();
417 if (config.audio_disabled) return;
418 const Entity &player = ctrl.Controlled();
421 aim_chunk->ToSceneCoords(player.ChunkCoords(), Chunk::ToCoords(aim_block))
426 void Interface::Handle(const SDL_MouseWheelEvent &event) {
427 if (config.mouse_disabled) return;
431 } else if (event.y > 0) {
436 void Interface::SelectNext() {
438 if (size_t(selection.type) >= world.BlockTypes().Size()) {
441 hud.Display(selection);
444 void Interface::SelectPrevious() {
446 if (selection.type <= 0) {
447 selection.type = world.BlockTypes().Size() - 1;
449 hud.Display(selection);
453 void Interface::PostMessage(const char *msg) {
454 messages.PushLine(msg);
457 std::cout << msg << std::endl;
461 void Interface::Update(int dt) {
462 ctrl.Velocity(glm::vec3(fwd - rev) * config.move_velocity);
465 msg_timer.Update(dt);
466 place_timer.Update(dt);
467 remove_timer.Update(dt);
472 if (msg_timer.HitOnce()) {
476 if (remove_timer.Hit()) {
481 if (place_timer.Hit()) {
486 if (counter_text.Visible() && counter.Changed()) {
491 void Interface::CheckAim() {
493 if (world.Intersection(aim, glm::mat4(1.0f), aim_chunk, aim_block, dist, aim_normal)) {
495 aim_chunk->Type(aim_chunk->BlockAt(aim_block)).FillOutlineModel(outline);
496 outline_transform = glm::scale(glm::vec3(1.0002f));
497 outline_transform *= aim_chunk->Transform(world.Player().ChunkCoords());
498 outline_transform *= aim_chunk->ToTransform(Chunk::ToPos(aim_block), aim_block);
505 void Interface::Render(Viewport &viewport) noexcept {
506 if (config.visual_disabled) return;
509 DirectionalLighting &world_prog = viewport.EntityProgram();
510 world_prog.SetM(outline_transform);
514 if (counter_text.Visible()) {
515 counter_text.Render(viewport);
518 if (msg_timer.Running()) {
519 messages.Render(viewport);
522 hud.Render(viewport);