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 world_prog.SetMVP(crosshair_transform, view, projection);
96 world_prog.SetM(block_transform);
99 sprite_prog.Activate();
100 sprite_prog.SetMVP(label_transform, view, projection);
101 sprite_prog.SetTexture(block_label);
107 Interface::Interface(const Config &config, const Assets &assets, World &world)
109 , ctrl(world.Player())
110 , font(assets.LoadFont("DejaVuSans", 16))
111 , hud(world.BlockTypes(), font)
112 , aim{{ 0, 0, 0 }, { 0, 0, -1 }}
117 , outline_transform(1.0f)
125 hud.Viewport(960, 600);
126 hud.Display(selection);
130 void Interface::HandlePress(const SDL_KeyboardEvent &event) {
131 if (config.keyboard_disabled) return;
133 switch (event.keysym.sym) {
174 PrintSelectionInfo();
179 void Interface::HandleRelease(const SDL_KeyboardEvent &event) {
180 if (config.keyboard_disabled) return;
182 switch (event.keysym.sym) {
204 void Interface::FaceBlock() {
205 selection.SetFace(Block::Face((selection.GetFace() + 1) % Block::FACE_COUNT));
206 hud.Display(selection);
209 void Interface::TurnBlock() {
210 selection.SetTurn(Block::Turn((selection.GetTurn() + 1) % Block::TURN_COUNT));
211 hud.Display(selection);
214 void Interface::ToggleCollision() {
215 ctrl.Controlled().WorldCollidable(!ctrl.Controlled().WorldCollidable());
216 std::cout << "collision " << (ctrl.Controlled().WorldCollidable() ? "on" : "off") << std::endl;
219 void Interface::PrintBlockInfo() {
220 std::cout << std::endl;
222 std::cout << "not looking at any block" << std::endl;
223 Ray aim = ctrl.Aim();
224 std::cout << "aim ray: " << aim.orig << ", " << aim.dir << std::endl;
227 std::cout << "looking at block " << aim_block
228 << " " << Chunk::ToCoords(aim_block)
229 << " of chunk " << aim_chunk->Position()
231 Print(aim_chunk->BlockAt(aim_block));
234 void Interface::PrintChunkInfo() {
235 std::cout << std::endl;
237 std::cout << "not looking at any block" << std::endl;
240 std::cout << "looking at chunk "
241 << aim_chunk->Position()
244 std::cout << " neighbors:" << std::endl;
245 if (aim_chunk->HasNeighbor(Block::FACE_LEFT)) {
246 std::cout << " left " << aim_chunk->GetNeighbor(Block::FACE_LEFT).Position() << std::endl;
248 if (aim_chunk->HasNeighbor(Block::FACE_RIGHT)) {
249 std::cout << " right " << aim_chunk->GetNeighbor(Block::FACE_RIGHT).Position() << std::endl;
251 if (aim_chunk->HasNeighbor(Block::FACE_UP)) {
252 std::cout << " up " << aim_chunk->GetNeighbor(Block::FACE_UP).Position() << std::endl;
254 if (aim_chunk->HasNeighbor(Block::FACE_DOWN)) {
255 std::cout << " down " << aim_chunk->GetNeighbor(Block::FACE_DOWN).Position() << std::endl;
257 if (aim_chunk->HasNeighbor(Block::FACE_FRONT)) {
258 std::cout << " front " << aim_chunk->GetNeighbor(Block::FACE_FRONT).Position() << std::endl;
260 if (aim_chunk->HasNeighbor(Block::FACE_BACK)) {
261 std::cout << " back " << aim_chunk->GetNeighbor(Block::FACE_BACK).Position() << std::endl;
263 std::cout << std::endl;
266 void Interface::PrintLightInfo() {
268 << "light level " << world.PlayerChunk().GetLight(world.Player().Position())
269 << " at position " << world.Player().Position()
273 void Interface::PrintSelectionInfo() {
274 std::cout << std::endl;
278 void Interface::Print(const Block &block) {
279 std::cout << "type: " << block.type
280 << ", face: " << block.GetFace()
281 << ", turn: " << block.GetTurn()
286 void Interface::Handle(const SDL_MouseMotionEvent &event) {
287 if (config.mouse_disabled) return;
288 ctrl.RotateYaw(event.xrel * config.yaw_sensitivity);
289 ctrl.RotatePitch(event.yrel * config.pitch_sensitivity);
292 void Interface::HandlePress(const SDL_MouseButtonEvent &event) {
293 if (config.mouse_disabled) return;
295 if (event.button == SDL_BUTTON_LEFT) {
297 remove_timer.Start();
298 } else if (event.button == SDL_BUTTON_MIDDLE) {
300 } else if (event.button == SDL_BUTTON_RIGHT) {
306 void Interface::HandleRelease(const SDL_MouseButtonEvent &event) {
307 if (config.mouse_disabled) return;
309 if (event.button == SDL_BUTTON_LEFT) {
311 } else if (event.button == SDL_BUTTON_RIGHT) {
316 void Interface::PickBlock() {
317 if (!aim_chunk) return;
318 selection = aim_chunk->BlockAt(aim_block);
319 hud.Display(selection);
322 void Interface::PlaceBlock() {
323 if (!aim_chunk) return;
324 Chunk *mod_chunk = aim_chunk;
325 glm::vec3 next_pos = Chunk::ToCoords(aim_block) + aim_normal;
326 if (!Chunk::InBounds(next_pos)) {
327 mod_chunk = &world.Next(*aim_chunk, aim_normal);
328 next_pos -= aim_normal * glm::vec3(Chunk::Extent());
330 mod_chunk->SetBlock(next_pos, selection);
331 mod_chunk->Invalidate();
334 void Interface::RemoveBlock() noexcept {
335 if (!aim_chunk) return;
336 aim_chunk->SetBlock(aim_block, remove);
337 aim_chunk->Invalidate();
341 void Interface::Handle(const SDL_MouseWheelEvent &event) {
342 if (config.mouse_disabled) return;
346 } else if (event.y > 0) {
351 void Interface::SelectNext() {
353 if (size_t(selection.type) >= world.BlockTypes().Size()) {
356 hud.Display(selection);
359 void Interface::SelectPrevious() {
361 if (selection.type <= 0) {
362 selection.type = world.BlockTypes().Size() - 1;
364 hud.Display(selection);
367 void Interface::Handle(const SDL_WindowEvent &event) noexcept {
368 if (event.event == SDL_WINDOWEVENT_RESIZED) {
369 hud.Viewport(event.data1, event.data2);
374 void Interface::Update(int dt) {
375 ctrl.Velocity(glm::vec3(fwd - rev) * config.move_velocity);
378 place_timer.Update(dt);
379 remove_timer.Update(dt);
384 if (remove_timer.Hit()) {
389 if (place_timer.Hit()) {
395 void Interface::CheckAim() {
397 if (world.Intersection(aim, glm::mat4(1.0f), aim_chunk, aim_block, dist, aim_normal)) {
399 aim_chunk->Type(aim_chunk->BlockAt(aim_block)).FillOutlineModel(outline);
400 outline_transform = glm::scale(glm::vec3(1.0002f));
401 outline_transform *= aim_chunk->Transform(world.Player().ChunkCoords());
402 outline_transform *= aim_chunk->ToTransform(Chunk::ToPos(aim_block), aim_block);
409 void Interface::Render(DirectionalLighting &world_prog, BlendedSprite &sprite_prog) noexcept {
410 if (config.visual_disabled) return;
413 world_prog.Activate();
414 world_prog.SetM(outline_transform);
418 hud.Render(world_prog, sprite_prog);