2 #include "Interface.hpp"
4 #include "../app/Assets.hpp"
5 #include "../app/init.hpp"
6 #include "../graphics/BlendedSprite.hpp"
7 #include "../graphics/DirectionalLighting.hpp"
8 #include "../graphics/Font.hpp"
9 #include "../model/shapes.hpp"
10 #include "../world/World.hpp"
15 #include <glm/gtc/matrix_transform.hpp>
16 #include <glm/gtx/io.hpp>
21 HUD::HUD(const BlockTypeRegistry &types, const Font &font)
26 , block_transform(1.0f)
29 , label_transform(1.0f)
30 , label_color{0xFF, 0xFF, 0xFF, 0xFF}
31 , block_visible(false)
33 , crosshair_transform(1.0f)
36 , projection(glm::ortho(0.0f, 1.0f, 1.0f, 0.0f, near, far))
38 block_transform = glm::translate(block_transform, glm::vec3(50.0f, 50.0f, 0.0f));
39 block_transform = glm::scale(block_transform, glm::vec3(50.0f));
40 block_transform = glm::rotate(block_transform, 3.5f, glm::vec3(1.0f, 0.0f, 0.0f));
41 block_transform = glm::rotate(block_transform, 0.35f, glm::vec3(0.0f, 1.0f, 0.0f));
43 crosshair.vertices = std::vector<glm::vec3>({
44 { -10.0f, 0.0f, 0.0f }, { 10.0f, 0.0f, 0.0f },
45 { 0.0f, -10.0f, 0.0f }, { 0.0f, 10.0f, 0.0f },
47 crosshair.indices = std::vector<OutlineModel::Index>({
50 crosshair.colors.resize(4, { 10.0f, 10.0f, 10.0f });
51 crosshair.Invalidate();
55 void HUD::Viewport(float width, float height) noexcept {
56 Viewport(0, 0, width, height);
59 void HUD::Viewport(float x, float y, float width, float height) noexcept {
60 projection = glm::ortho(x, width, height, y, near, far);
61 crosshair_transform = glm::translate(glm::vec3(width * 0.5f, height * 0.5f, 0.0f));
65 void HUD::Display(const Block &b) {
66 const BlockType &type = types.Get(b.type);
69 type.FillModel(block_buf, b.Transform());
70 block.Update(block_buf);
72 font.Render(type.label.c_str(), label_color, block_label);
73 glm::vec2 size(font.TextSize(type.label.c_str()));
74 label_sprite.LoadRect(size.x, size.y);
75 label_transform = glm::translate(glm::vec3(
76 std::max(5.0f, 50.0f - std::round(size.x * 0.5f)),
81 block_visible = type.visible;
85 void HUD::Render(DirectionalLighting &world_prog, BlendedSprite &sprite_prog) noexcept {
86 world_prog.Activate();
87 world_prog.SetLightDirection({ 1.0f, 3.0f, 5.0f });
88 // disable distance fog
89 world_prog.SetFogDensity(0.0f);
90 GLContext::ClearDepthBuffer();
92 GLContext::EnableInvertBlending();
93 world_prog.SetMVP(crosshair_transform, view, projection);
97 GLContext::DisableBlending();
98 world_prog.SetM(block_transform);
101 sprite_prog.Activate();
102 sprite_prog.SetMVP(label_transform, view, projection);
103 sprite_prog.SetTexture(block_label);
109 Interface::Interface(const Config &config, const Assets &assets, World &world)
111 , ctrl(world.Player())
112 , font(assets.LoadFont("DejaVuSans", 16))
113 , hud(world.BlockTypes(), font)
114 , aim{{ 0, 0, 0 }, { 0, 0, -1 }}
119 , outline_transform(1.0f)
127 hud.Viewport(960, 600);
128 hud.Display(selection);
132 void Interface::HandlePress(const SDL_KeyboardEvent &event) {
133 if (config.keyboard_disabled) return;
135 switch (event.keysym.sym) {
176 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 std::cout << "collision " << (ctrl.Controlled().WorldCollidable() ? "on" : "off") << std::endl;
221 void Interface::PrintBlockInfo() {
222 std::cout << std::endl;
224 std::cout << "not looking at any block" << std::endl;
225 Ray aim = ctrl.Aim();
226 std::cout << "aim ray: " << aim.orig << ", " << aim.dir << std::endl;
229 std::cout << "looking at block " << aim_block
230 << " " << Chunk::ToCoords(aim_block)
231 << " of chunk " << aim_chunk->Position()
233 Print(aim_chunk->BlockAt(aim_block));
236 void Interface::PrintChunkInfo() {
237 std::cout << std::endl;
239 std::cout << "not looking at any block" << std::endl;
242 std::cout << "looking at chunk "
243 << aim_chunk->Position()
246 std::cout << " neighbors:" << std::endl;
247 if (aim_chunk->HasNeighbor(Block::FACE_LEFT)) {
248 std::cout << " left " << aim_chunk->GetNeighbor(Block::FACE_LEFT).Position() << std::endl;
250 if (aim_chunk->HasNeighbor(Block::FACE_RIGHT)) {
251 std::cout << " right " << aim_chunk->GetNeighbor(Block::FACE_RIGHT).Position() << std::endl;
253 if (aim_chunk->HasNeighbor(Block::FACE_UP)) {
254 std::cout << " up " << aim_chunk->GetNeighbor(Block::FACE_UP).Position() << std::endl;
256 if (aim_chunk->HasNeighbor(Block::FACE_DOWN)) {
257 std::cout << " down " << aim_chunk->GetNeighbor(Block::FACE_DOWN).Position() << std::endl;
259 if (aim_chunk->HasNeighbor(Block::FACE_FRONT)) {
260 std::cout << " front " << aim_chunk->GetNeighbor(Block::FACE_FRONT).Position() << std::endl;
262 if (aim_chunk->HasNeighbor(Block::FACE_BACK)) {
263 std::cout << " back " << aim_chunk->GetNeighbor(Block::FACE_BACK).Position() << std::endl;
265 std::cout << std::endl;
268 void Interface::PrintLightInfo() {
270 << "light level " << world.PlayerChunk().GetLight(world.Player().Position())
271 << " at position " << world.Player().Position()
275 void Interface::PrintSelectionInfo() {
276 std::cout << std::endl;
280 void Interface::Print(const Block &block) {
281 std::cout << "type: " << block.type
282 << ", face: " << block.GetFace()
283 << ", turn: " << block.GetTurn()
288 void Interface::Handle(const SDL_MouseMotionEvent &event) {
289 if (config.mouse_disabled) return;
290 ctrl.RotateYaw(event.xrel * config.yaw_sensitivity);
291 ctrl.RotatePitch(event.yrel * config.pitch_sensitivity);
294 void Interface::HandlePress(const SDL_MouseButtonEvent &event) {
295 if (config.mouse_disabled) return;
297 if (event.button == SDL_BUTTON_LEFT) {
299 remove_timer.Start();
300 } else if (event.button == SDL_BUTTON_MIDDLE) {
302 } else if (event.button == SDL_BUTTON_RIGHT) {
308 void Interface::HandleRelease(const SDL_MouseButtonEvent &event) {
309 if (config.mouse_disabled) return;
311 if (event.button == SDL_BUTTON_LEFT) {
313 } else if (event.button == SDL_BUTTON_RIGHT) {
318 void Interface::PickBlock() {
319 if (!aim_chunk) return;
320 selection = aim_chunk->BlockAt(aim_block);
321 hud.Display(selection);
324 void Interface::PlaceBlock() {
325 if (!aim_chunk) return;
326 Chunk *mod_chunk = aim_chunk;
327 glm::vec3 next_pos = Chunk::ToCoords(aim_block) + aim_normal;
328 if (!Chunk::InBounds(next_pos)) {
329 mod_chunk = &world.Next(*aim_chunk, aim_normal);
330 next_pos -= aim_normal * glm::vec3(Chunk::Extent());
332 mod_chunk->SetBlock(next_pos, selection);
333 mod_chunk->Invalidate();
336 void Interface::RemoveBlock() noexcept {
337 if (!aim_chunk) return;
338 aim_chunk->SetBlock(aim_block, remove);
339 aim_chunk->Invalidate();
343 void Interface::Handle(const SDL_MouseWheelEvent &event) {
344 if (config.mouse_disabled) return;
348 } else if (event.y > 0) {
353 void Interface::SelectNext() {
355 if (size_t(selection.type) >= world.BlockTypes().Size()) {
358 hud.Display(selection);
361 void Interface::SelectPrevious() {
363 if (selection.type <= 0) {
364 selection.type = world.BlockTypes().Size() - 1;
366 hud.Display(selection);
369 void Interface::Handle(const SDL_WindowEvent &event) noexcept {
370 if (event.event == SDL_WINDOWEVENT_RESIZED) {
371 hud.Viewport(event.data1, event.data2);
376 void Interface::Update(int dt) {
377 ctrl.Velocity(glm::vec3(fwd - rev) * config.move_velocity);
380 place_timer.Update(dt);
381 remove_timer.Update(dt);
386 if (remove_timer.Hit()) {
391 if (place_timer.Hit()) {
397 void Interface::CheckAim() {
399 if (world.Intersection(aim, glm::mat4(1.0f), aim_chunk, aim_block, dist, aim_normal)) {
401 aim_chunk->Type(aim_chunk->BlockAt(aim_block)).FillOutlineModel(outline);
402 outline_transform = glm::scale(glm::vec3(1.0002f));
403 outline_transform *= aim_chunk->Transform(world.Player().ChunkCoords());
404 outline_transform *= aim_chunk->ToTransform(Chunk::ToPos(aim_block), aim_block);
411 void Interface::Render(DirectionalLighting &world_prog, BlendedSprite &sprite_prog) noexcept {
412 if (config.visual_disabled) return;
415 world_prog.Activate();
416 world_prog.SetM(outline_transform);
420 hud.Render(world_prog, sprite_prog);