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)
30 , label_transform(1.0f)
31 , label_color{0xFF, 0xFF, 0xFF, 0xFF}
32 , block_visible(false)
34 block_transform = glm::translate(block_transform, glm::vec3(50.0f, 50.0f, 0.0f));
35 block_transform = glm::scale(block_transform, glm::vec3(50.0f));
36 block_transform = glm::rotate(block_transform, 3.5f, glm::vec3(1.0f, 0.0f, 0.0f));
37 block_transform = glm::rotate(block_transform, 0.35f, glm::vec3(0.0f, 1.0f, 0.0f));
39 crosshair.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 crosshair.indices = std::vector<OutlineModel::Index>({
46 crosshair.colors.resize(4, { 10.0f, 10.0f, 10.0f });
47 crosshair.Invalidate();
51 void HUD::Display(const Block &b) {
52 const BlockType &type = types.Get(b.type);
55 type.FillModel(block_buf, b.Transform());
56 block.Update(block_buf);
58 font.Render(type.label.c_str(), label_color, block_label);
59 glm::vec2 size(font.TextSize(type.label.c_str()));
60 label_sprite.LoadRect(size.x, size.y);
61 label_transform = glm::translate(glm::vec3(
62 std::max(5.0f, 50.0f - std::round(size.x * 0.5f)),
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 world_prog.SetM(viewport.CenterTransform());
84 viewport.DisableBlending();
85 world_prog.SetM(block_transform);
88 BlendedSprite &sprite_prog = viewport.SpriteProgram();
89 sprite_prog.SetM(label_transform);
90 sprite_prog.SetTexture(block_label);
99 const FrameCounter &counter,
103 , ctrl(world.Player())
104 , font(assets.LoadFont("DejaVuSans", 16))
105 , hud(world.BlockTypes(), font)
106 , aim{{ 0, 0, 0 }, { 0, 0, -1 }}
111 , outline_transform(1.0f)
112 , show_counter(false)
115 , counter_transform(1.0f)
117 , counter_color{0xFF, 0xFF, 0xFF, 0xFF}
125 hud.Display(selection);
129 void Interface::HandlePress(const SDL_KeyboardEvent &event) {
130 if (config.keyboard_disabled) return;
132 switch (event.keysym.sym) {
173 PrintSelectionInfo();
182 void Interface::HandleRelease(const SDL_KeyboardEvent &event) {
183 if (config.keyboard_disabled) return;
185 switch (event.keysym.sym) {
207 void Interface::FaceBlock() {
208 selection.SetFace(Block::Face((selection.GetFace() + 1) % Block::FACE_COUNT));
209 hud.Display(selection);
212 void Interface::TurnBlock() {
213 selection.SetTurn(Block::Turn((selection.GetTurn() + 1) % Block::TURN_COUNT));
214 hud.Display(selection);
217 void Interface::ToggleCollision() {
218 ctrl.Controlled().WorldCollidable(!ctrl.Controlled().WorldCollidable());
219 std::cout << "collision " << (ctrl.Controlled().WorldCollidable() ? "on" : "off") << std::endl;
222 void Interface::PrintBlockInfo() {
223 std::cout << std::endl;
225 std::cout << "not looking at any block" << std::endl;
226 Ray aim = ctrl.Aim();
227 std::cout << "aim ray: " << aim.orig << ", " << aim.dir << std::endl;
230 std::cout << "looking at block " << aim_block
231 << " " << Chunk::ToCoords(aim_block)
232 << " of chunk " << aim_chunk->Position()
234 Print(aim_chunk->BlockAt(aim_block));
237 void Interface::PrintChunkInfo() {
238 std::cout << std::endl;
240 std::cout << "not looking at any block" << std::endl;
243 std::cout << "looking at chunk "
244 << aim_chunk->Position()
247 std::cout << " neighbors:" << std::endl;
248 if (aim_chunk->HasNeighbor(Block::FACE_LEFT)) {
249 std::cout << " left " << aim_chunk->GetNeighbor(Block::FACE_LEFT).Position() << std::endl;
251 if (aim_chunk->HasNeighbor(Block::FACE_RIGHT)) {
252 std::cout << " right " << aim_chunk->GetNeighbor(Block::FACE_RIGHT).Position() << std::endl;
254 if (aim_chunk->HasNeighbor(Block::FACE_UP)) {
255 std::cout << " up " << aim_chunk->GetNeighbor(Block::FACE_UP).Position() << std::endl;
257 if (aim_chunk->HasNeighbor(Block::FACE_DOWN)) {
258 std::cout << " down " << aim_chunk->GetNeighbor(Block::FACE_DOWN).Position() << std::endl;
260 if (aim_chunk->HasNeighbor(Block::FACE_FRONT)) {
261 std::cout << " front " << aim_chunk->GetNeighbor(Block::FACE_FRONT).Position() << std::endl;
263 if (aim_chunk->HasNeighbor(Block::FACE_BACK)) {
264 std::cout << " back " << aim_chunk->GetNeighbor(Block::FACE_BACK).Position() << std::endl;
266 std::cout << std::endl;
269 void Interface::PrintLightInfo() {
271 << "light level " << world.PlayerChunk().GetLight(world.Player().Position())
272 << " at position " << world.Player().Position()
276 void Interface::PrintSelectionInfo() {
277 std::cout << std::endl;
281 void Interface::Print(const Block &block) {
282 std::cout << "type: " << block.type
283 << ", face: " << block.GetFace()
284 << ", turn: " << block.GetTurn()
288 void Interface::ToggleCounter() {
289 if ((show_counter = !show_counter)) {
294 void Interface::UpdateCounter() {
296 s << std::setprecision(3) << counter.AvgRunning() << "ms";
297 std::string text = s.str();
298 font.Render(text.c_str(), counter_color, counter_tex);
299 glm::vec2 size(font.TextSize(text.c_str()));
300 counter_sprite.LoadRect(size.x, size.y);
301 counter_transform = glm::translate(glm::vec3(
309 void Interface::Handle(const SDL_MouseMotionEvent &event) {
310 if (config.mouse_disabled) return;
311 ctrl.RotateYaw(event.xrel * config.yaw_sensitivity);
312 ctrl.RotatePitch(event.yrel * config.pitch_sensitivity);
315 void Interface::HandlePress(const SDL_MouseButtonEvent &event) {
316 if (config.mouse_disabled) return;
318 if (event.button == SDL_BUTTON_LEFT) {
320 remove_timer.Start();
321 } else if (event.button == SDL_BUTTON_MIDDLE) {
323 } else if (event.button == SDL_BUTTON_RIGHT) {
329 void Interface::HandleRelease(const SDL_MouseButtonEvent &event) {
330 if (config.mouse_disabled) return;
332 if (event.button == SDL_BUTTON_LEFT) {
334 } else if (event.button == SDL_BUTTON_RIGHT) {
339 void Interface::PickBlock() {
340 if (!aim_chunk) return;
341 selection = aim_chunk->BlockAt(aim_block);
342 hud.Display(selection);
345 void Interface::PlaceBlock() {
346 if (!aim_chunk) return;
347 Chunk *mod_chunk = aim_chunk;
348 glm::vec3 next_pos = Chunk::ToCoords(aim_block) + aim_normal;
349 if (!Chunk::InBounds(next_pos)) {
350 mod_chunk = &world.Next(*aim_chunk, aim_normal);
351 next_pos -= aim_normal * glm::vec3(Chunk::Extent());
353 mod_chunk->SetBlock(next_pos, selection);
354 mod_chunk->Invalidate();
357 void Interface::RemoveBlock() noexcept {
358 if (!aim_chunk) return;
359 aim_chunk->SetBlock(aim_block, remove);
360 aim_chunk->Invalidate();
364 void Interface::Handle(const SDL_MouseWheelEvent &event) {
365 if (config.mouse_disabled) return;
369 } else if (event.y > 0) {
374 void Interface::SelectNext() {
376 if (size_t(selection.type) >= world.BlockTypes().Size()) {
379 hud.Display(selection);
382 void Interface::SelectPrevious() {
384 if (selection.type <= 0) {
385 selection.type = world.BlockTypes().Size() - 1;
387 hud.Display(selection);
391 void Interface::Resize(const Viewport &viewport) {
392 counter_x = viewport.Width() - 25.0f;
396 void Interface::Update(int dt) {
397 ctrl.Velocity(glm::vec3(fwd - rev) * config.move_velocity);
400 place_timer.Update(dt);
401 remove_timer.Update(dt);
406 if (remove_timer.Hit()) {
411 if (place_timer.Hit()) {
416 if (show_counter && counter.Changed()) {
421 void Interface::CheckAim() {
423 if (world.Intersection(aim, glm::mat4(1.0f), aim_chunk, aim_block, dist, aim_normal)) {
425 aim_chunk->Type(aim_chunk->BlockAt(aim_block)).FillOutlineModel(outline);
426 outline_transform = glm::scale(glm::vec3(1.0002f));
427 outline_transform *= aim_chunk->Transform(world.Player().ChunkCoords());
428 outline_transform *= aim_chunk->ToTransform(Chunk::ToPos(aim_block), aim_block);
435 void Interface::Render(Viewport &viewport) noexcept {
436 if (config.visual_disabled) return;
439 DirectionalLighting &world_prog = viewport.EntityProgram();
440 world_prog.SetM(outline_transform);
445 BlendedSprite &sprite_prog = viewport.SpriteProgram();
446 sprite_prog.SetM(counter_transform);
447 sprite_prog.SetTexture(counter_tex);
448 counter_sprite.Draw();
451 hud.Render(viewport);