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)
116 counter_text.Position(glm::vec3(-25.0f, 25.0f, 0.0f), Gravity::NORTH_EAST);
117 counter_text.Foreground(glm::vec4(1.0f));
118 counter_text.Background(glm::vec4(0.5f));
119 hud.Display(selection);
123 void Interface::HandlePress(const SDL_KeyboardEvent &event) {
124 if (config.keyboard_disabled) return;
126 switch (event.keysym.sym) {
167 PrintSelectionInfo();
176 void Interface::HandleRelease(const SDL_KeyboardEvent &event) {
177 if (config.keyboard_disabled) return;
179 switch (event.keysym.sym) {
201 void Interface::FaceBlock() {
202 selection.SetFace(Block::Face((selection.GetFace() + 1) % Block::FACE_COUNT));
203 hud.Display(selection);
206 void Interface::TurnBlock() {
207 selection.SetTurn(Block::Turn((selection.GetTurn() + 1) % Block::TURN_COUNT));
208 hud.Display(selection);
211 void Interface::ToggleCollision() {
212 ctrl.Controlled().WorldCollidable(!ctrl.Controlled().WorldCollidable());
213 std::cout << "collision " << (ctrl.Controlled().WorldCollidable() ? "on" : "off") << std::endl;
216 void Interface::PrintBlockInfo() {
217 std::cout << std::endl;
219 std::cout << "not looking at any block" << std::endl;
220 Ray aim = ctrl.Aim();
221 std::cout << "aim ray: " << aim.orig << ", " << aim.dir << std::endl;
224 std::cout << "looking at block " << aim_block
225 << " " << Chunk::ToCoords(aim_block)
226 << " of chunk " << aim_chunk->Position()
228 Print(aim_chunk->BlockAt(aim_block));
231 void Interface::PrintChunkInfo() {
232 std::cout << std::endl;
234 std::cout << "not looking at any block" << std::endl;
237 std::cout << "looking at chunk "
238 << aim_chunk->Position()
241 std::cout << " neighbors:" << std::endl;
242 if (aim_chunk->HasNeighbor(Block::FACE_LEFT)) {
243 std::cout << " left " << aim_chunk->GetNeighbor(Block::FACE_LEFT).Position() << std::endl;
245 if (aim_chunk->HasNeighbor(Block::FACE_RIGHT)) {
246 std::cout << " right " << aim_chunk->GetNeighbor(Block::FACE_RIGHT).Position() << std::endl;
248 if (aim_chunk->HasNeighbor(Block::FACE_UP)) {
249 std::cout << " up " << aim_chunk->GetNeighbor(Block::FACE_UP).Position() << std::endl;
251 if (aim_chunk->HasNeighbor(Block::FACE_DOWN)) {
252 std::cout << " down " << aim_chunk->GetNeighbor(Block::FACE_DOWN).Position() << std::endl;
254 if (aim_chunk->HasNeighbor(Block::FACE_FRONT)) {
255 std::cout << " front " << aim_chunk->GetNeighbor(Block::FACE_FRONT).Position() << std::endl;
257 if (aim_chunk->HasNeighbor(Block::FACE_BACK)) {
258 std::cout << " back " << aim_chunk->GetNeighbor(Block::FACE_BACK).Position() << std::endl;
260 std::cout << std::endl;
263 void Interface::PrintLightInfo() {
265 << "light level " << world.PlayerChunk().GetLight(world.Player().Position())
266 << " at position " << world.Player().Position()
270 void Interface::PrintSelectionInfo() {
271 std::cout << std::endl;
275 void Interface::Print(const Block &block) {
276 std::cout << "type: " << block.type
277 << ", face: " << block.GetFace()
278 << ", turn: " << block.GetTurn()
282 void Interface::ToggleCounter() {
283 counter_text.Toggle();
284 if (counter_text.Visible()) {
289 void Interface::UpdateCounter() {
291 s << std::setprecision(3) << counter.AvgRunning() << "ms";
292 std::string text = s.str();
293 counter_text.Set(font, text);
297 void Interface::Handle(const SDL_MouseMotionEvent &event) {
298 if (config.mouse_disabled) return;
299 ctrl.RotateYaw(event.xrel * config.yaw_sensitivity);
300 ctrl.RotatePitch(event.yrel * config.pitch_sensitivity);
303 void Interface::HandlePress(const SDL_MouseButtonEvent &event) {
304 if (config.mouse_disabled) return;
306 if (event.button == SDL_BUTTON_LEFT) {
308 remove_timer.Start();
309 } else if (event.button == SDL_BUTTON_MIDDLE) {
311 } else if (event.button == SDL_BUTTON_RIGHT) {
317 void Interface::HandleRelease(const SDL_MouseButtonEvent &event) {
318 if (config.mouse_disabled) return;
320 if (event.button == SDL_BUTTON_LEFT) {
322 } else if (event.button == SDL_BUTTON_RIGHT) {
327 void Interface::PickBlock() {
328 if (!aim_chunk) return;
329 selection = aim_chunk->BlockAt(aim_block);
330 hud.Display(selection);
333 void Interface::PlaceBlock() {
334 if (!aim_chunk) return;
335 Chunk *mod_chunk = aim_chunk;
336 glm::vec3 next_pos = Chunk::ToCoords(aim_block) + aim_normal;
337 if (!Chunk::InBounds(next_pos)) {
338 mod_chunk = &world.Next(*aim_chunk, aim_normal);
339 next_pos -= aim_normal * glm::vec3(Chunk::Extent());
341 mod_chunk->SetBlock(next_pos, selection);
342 mod_chunk->Invalidate();
345 void Interface::RemoveBlock() noexcept {
346 if (!aim_chunk) return;
347 aim_chunk->SetBlock(aim_block, remove);
348 aim_chunk->Invalidate();
352 void Interface::Handle(const SDL_MouseWheelEvent &event) {
353 if (config.mouse_disabled) return;
357 } else if (event.y > 0) {
362 void Interface::SelectNext() {
364 if (size_t(selection.type) >= world.BlockTypes().Size()) {
367 hud.Display(selection);
370 void Interface::SelectPrevious() {
372 if (selection.type <= 0) {
373 selection.type = world.BlockTypes().Size() - 1;
375 hud.Display(selection);
379 void Interface::Update(int dt) {
380 ctrl.Velocity(glm::vec3(fwd - rev) * config.move_velocity);
383 place_timer.Update(dt);
384 remove_timer.Update(dt);
389 if (remove_timer.Hit()) {
394 if (place_timer.Hit()) {
399 if (counter_text.Visible() && counter.Changed()) {
404 void Interface::CheckAim() {
406 if (world.Intersection(aim, glm::mat4(1.0f), aim_chunk, aim_block, dist, aim_normal)) {
408 aim_chunk->Type(aim_chunk->BlockAt(aim_block)).FillOutlineModel(outline);
409 outline_transform = glm::scale(glm::vec3(1.0002f));
410 outline_transform *= aim_chunk->Transform(world.Player().ChunkCoords());
411 outline_transform *= aim_chunk->ToTransform(Chunk::ToPos(aim_block), aim_block);
418 void Interface::Render(Viewport &viewport) noexcept {
419 if (config.visual_disabled) return;
422 DirectionalLighting &world_prog = viewport.EntityProgram();
423 world_prog.SetM(outline_transform);
427 if (counter_text.Visible()) {
428 counter_text.Render(viewport);
431 hud.Render(viewport);