2 #include "Interface.hpp"
4 #include "../app/Assets.hpp"
5 #include "../app/FrameCounter.hpp"
6 #include "../app/init.hpp"
7 #include "../graphics/Font.hpp"
8 #include "../graphics/Viewport.hpp"
9 #include "../model/shapes.hpp"
10 #include "../world/World.hpp"
16 #include <glm/gtc/matrix_transform.hpp>
17 #include <glm/gtx/io.hpp>
22 HUD::HUD(const BlockTypeRegistry &types, const Font &font)
27 , block_transform(1.0f)
29 , block_visible(false)
31 block_transform = glm::translate(block_transform, glm::vec3(50.0f, 50.0f, 0.0f));
32 block_transform = glm::scale(block_transform, glm::vec3(50.0f));
33 block_transform = glm::rotate(block_transform, 3.5f, glm::vec3(1.0f, 0.0f, 0.0f));
34 block_transform = glm::rotate(block_transform, 0.35f, glm::vec3(0.0f, 1.0f, 0.0f));
36 crosshair.vertices = std::vector<glm::vec3>({
37 { -10.0f, 0.0f, 0.0f }, { 10.0f, 0.0f, 0.0f },
38 { 0.0f, -10.0f, 0.0f }, { 0.0f, 10.0f, 0.0f },
40 crosshair.indices = std::vector<OutlineModel::Index>({
43 crosshair.colors.resize(4, { 10.0f, 10.0f, 10.0f });
44 crosshair.Invalidate();
47 glm::vec3(50.0f, 85.0f, 0.0f),
51 block_label.Foreground(glm::vec4(1.0f));
52 block_label.Background(glm::vec4(0.5f));
56 void HUD::Display(const Block &b) {
57 const BlockType &type = types.Get(b.type);
60 type.FillModel(block_buf, b.Transform());
61 block.Update(block_buf);
63 block_label.Set(font, type.label);
65 block_visible = type.visible;
69 void HUD::Render(Viewport &viewport) noexcept {
70 viewport.ClearDepth();
72 DirectionalLighting &world_prog = viewport.HUDProgram();
73 world_prog.SetLightDirection({ 1.0f, 3.0f, 5.0f });
74 // disable distance fog
75 world_prog.SetFogDensity(0.0f);
77 viewport.EnableInvertBlending();
78 viewport.SetCursor(glm::vec3(0.0f), Gravity::CENTER);
79 world_prog.SetM(viewport.Cursor());
83 viewport.DisableBlending();
84 world_prog.SetM(block_transform);
86 block_label.Render(viewport);
94 const FrameCounter &counter,
98 , ctrl(world.Player())
99 , font(assets.LoadFont("DejaVuSans", 16))
100 , hud(world.BlockTypes(), font)
101 , aim{{ 0, 0, 0 }, { 0, 0, -1 }}
106 , outline_transform(1.0f)
118 counter_text.Position(glm::vec3(-25.0f, 25.0f, 0.0f), Gravity::NORTH_EAST);
119 counter_text.Foreground(glm::vec4(1.0f));
120 counter_text.Background(glm::vec4(0.5f));
121 messages.Position(glm::vec3(25.0f, -25.0f, 0.0f), Gravity::SOUTH_WEST);
122 messages.Foreground(glm::vec4(1.0f));
123 messages.Background(glm::vec4(0.5f));
124 hud.Display(selection);
128 void Interface::HandlePress(const SDL_KeyboardEvent &event) {
129 if (config.keyboard_disabled) return;
131 switch (event.keysym.sym) {
172 PrintSelectionInfo();
181 void Interface::HandleRelease(const SDL_KeyboardEvent &event) {
182 if (config.keyboard_disabled) return;
184 switch (event.keysym.sym) {
206 void Interface::FaceBlock() {
207 selection.SetFace(Block::Face((selection.GetFace() + 1) % Block::FACE_COUNT));
208 hud.Display(selection);
211 void Interface::TurnBlock() {
212 selection.SetTurn(Block::Turn((selection.GetTurn() + 1) % Block::TURN_COUNT));
213 hud.Display(selection);
216 void Interface::ToggleCollision() {
217 ctrl.Controlled().WorldCollidable(!ctrl.Controlled().WorldCollidable());
218 if (ctrl.Controlled().WorldCollidable()) {
219 PostMessage("collision on");
221 PostMessage("collision off");
225 void Interface::PrintBlockInfo() {
226 std::cout << std::endl;
228 PostMessage("not looking at any block");
229 Ray aim = ctrl.Aim();
231 s << "aim ray: " << aim.orig << ", " << aim.dir;
232 PostMessage(s.str());
236 s << "looking at block " << aim_block
237 << " " << Chunk::ToCoords(aim_block)
238 << " of chunk " << aim_chunk->Position()
240 PostMessage(s.str());
241 Print(aim_chunk->BlockAt(aim_block));
244 void Interface::PrintChunkInfo() {
245 std::cout << std::endl;
247 PostMessage("not looking at any block");
251 s << "looking at chunk " << aim_chunk->Position();
252 PostMessage(s.str());
254 PostMessage(" neighbors:");
255 if (aim_chunk->HasNeighbor(Block::FACE_LEFT)) {
257 s << " left " << aim_chunk->GetNeighbor(Block::FACE_LEFT).Position();
258 PostMessage(s.str());
260 if (aim_chunk->HasNeighbor(Block::FACE_RIGHT)) {
262 s << " right " << aim_chunk->GetNeighbor(Block::FACE_RIGHT).Position();
263 PostMessage(s.str());
265 if (aim_chunk->HasNeighbor(Block::FACE_UP)) {
267 s << " up " << aim_chunk->GetNeighbor(Block::FACE_UP).Position();
268 PostMessage(s.str());
270 if (aim_chunk->HasNeighbor(Block::FACE_DOWN)) {
272 s << " down " << aim_chunk->GetNeighbor(Block::FACE_DOWN).Position();
273 PostMessage(s.str());
275 if (aim_chunk->HasNeighbor(Block::FACE_FRONT)) {
277 s << " front " << aim_chunk->GetNeighbor(Block::FACE_FRONT).Position();
278 PostMessage(s.str());
280 if (aim_chunk->HasNeighbor(Block::FACE_BACK)) {
282 s << " back " << aim_chunk->GetNeighbor(Block::FACE_BACK).Position();
283 PostMessage(s.str());
285 std::cout << std::endl;
288 void Interface::PrintLightInfo() {
291 << "light level " << world.PlayerChunk().GetLight(world.Player().Position())
292 << " at position " << world.Player().Position()
294 PostMessage(s.str());
297 void Interface::PrintSelectionInfo() {
298 std::cout << std::endl;
302 void Interface::Print(const Block &block) {
304 s << "type: " << block.type
305 << ", face: " << block.GetFace()
306 << ", turn: " << block.GetTurn()
308 PostMessage(s.str());
311 void Interface::ToggleCounter() {
312 counter_text.Toggle();
313 if (counter_text.Visible()) {
318 void Interface::UpdateCounter() {
320 s << std::setprecision(3) <<
321 "avg: " << counter.Average().running << "ms, "
322 "peak: " << counter.Peak().running << "ms";
323 std::string text = s.str();
324 counter_text.Set(font, text);
328 void Interface::Handle(const SDL_MouseMotionEvent &event) {
329 if (config.mouse_disabled) return;
330 ctrl.RotateYaw(event.xrel * config.yaw_sensitivity);
331 ctrl.RotatePitch(event.yrel * config.pitch_sensitivity);
334 void Interface::HandlePress(const SDL_MouseButtonEvent &event) {
335 if (config.mouse_disabled) return;
337 if (event.button == SDL_BUTTON_LEFT) {
339 remove_timer.Start();
340 } else if (event.button == SDL_BUTTON_MIDDLE) {
342 } else if (event.button == SDL_BUTTON_RIGHT) {
348 void Interface::HandleRelease(const SDL_MouseButtonEvent &event) {
349 if (config.mouse_disabled) return;
351 if (event.button == SDL_BUTTON_LEFT) {
353 } else if (event.button == SDL_BUTTON_RIGHT) {
358 void Interface::PickBlock() {
359 if (!aim_chunk) return;
360 selection = aim_chunk->BlockAt(aim_block);
361 hud.Display(selection);
364 void Interface::PlaceBlock() {
365 if (!aim_chunk) return;
366 Chunk *mod_chunk = aim_chunk;
367 glm::vec3 next_pos = Chunk::ToCoords(aim_block) + aim_normal;
368 if (!Chunk::InBounds(next_pos)) {
369 mod_chunk = &world.Next(*aim_chunk, aim_normal);
370 next_pos -= aim_normal * glm::vec3(Chunk::Extent());
372 mod_chunk->SetBlock(next_pos, selection);
373 mod_chunk->Invalidate();
376 void Interface::RemoveBlock() noexcept {
377 if (!aim_chunk) return;
378 aim_chunk->SetBlock(aim_block, remove);
379 aim_chunk->Invalidate();
383 void Interface::Handle(const SDL_MouseWheelEvent &event) {
384 if (config.mouse_disabled) return;
388 } else if (event.y > 0) {
393 void Interface::SelectNext() {
395 if (size_t(selection.type) >= world.BlockTypes().Size()) {
398 hud.Display(selection);
401 void Interface::SelectPrevious() {
403 if (selection.type <= 0) {
404 selection.type = world.BlockTypes().Size() - 1;
406 hud.Display(selection);
410 void Interface::PostMessage(const char *msg) {
411 messages.PushLine(msg);
414 std::cout << msg << std::endl;
418 void Interface::Update(int dt) {
419 ctrl.Velocity(glm::vec3(fwd - rev) * config.move_velocity);
422 msg_timer.Update(dt);
423 place_timer.Update(dt);
424 remove_timer.Update(dt);
429 if (msg_timer.HitOnce()) {
433 if (remove_timer.Hit()) {
438 if (place_timer.Hit()) {
443 if (counter_text.Visible() && counter.Changed()) {
448 void Interface::CheckAim() {
450 if (world.Intersection(aim, glm::mat4(1.0f), aim_chunk, aim_block, dist, aim_normal)) {
452 aim_chunk->Type(aim_chunk->BlockAt(aim_block)).FillOutlineModel(outline);
453 outline_transform = glm::scale(glm::vec3(1.0002f));
454 outline_transform *= aim_chunk->Transform(world.Player().ChunkCoords());
455 outline_transform *= aim_chunk->ToTransform(Chunk::ToPos(aim_block), aim_block);
462 void Interface::Render(Viewport &viewport) noexcept {
463 if (config.visual_disabled) return;
466 DirectionalLighting &world_prog = viewport.EntityProgram();
467 world_prog.SetM(outline_transform);
471 if (counter_text.Visible()) {
472 counter_text.Render(viewport);
475 if (msg_timer.Running()) {
476 messages.Render(viewport);
479 hud.Render(viewport);