]> git.localhorst.tv Git - blank.git/blob - src/interface.cpp
fixed light propagation
[blank.git] / src / interface.cpp
1 #include "interface.hpp"
2
3 #include "geometry.hpp"
4 #include "world.hpp"
5
6 #include <iostream>
7 #include <glm/glm.hpp>
8 #include <glm/gtc/matrix_transform.hpp>
9 #include <glm/gtx/io.hpp>
10
11
12 namespace blank {
13
14 Interface::Interface(const Config &config, World &world)
15 : world(world)
16 , ctrl(world.Player())
17 , hud(world.BlockTypes())
18 , aim_chunk(nullptr)
19 , aim_block(0)
20 , aim_normal()
21 , outline()
22 , outline_transform(1.0f)
23 , config(config)
24 , remove(0)
25 , selection(1)
26 , front(false)
27 , back(false)
28 , left(false)
29 , right(false)
30 , up(false)
31 , down(false) {
32         hud.Viewport(960, 600);
33         hud.Display(selection);
34 }
35
36
37 void Interface::Handle(const SDL_KeyboardEvent &event) {
38         if (config.keyboard_disabled) return;
39
40         switch (event.keysym.sym) {
41                 case SDLK_w:
42                         front = event.state == SDL_PRESSED;
43                         break;
44                 case SDLK_s:
45                         back = event.state == SDL_PRESSED;
46                         break;
47                 case SDLK_a:
48                         left = event.state == SDL_PRESSED;
49                         break;
50                 case SDLK_d:
51                         right = event.state == SDL_PRESSED;
52                         break;
53                 case SDLK_SPACE:
54                         up = event.state == SDL_PRESSED;
55                         break;
56                 case SDLK_LSHIFT:
57                         down = event.state == SDL_PRESSED;
58                         break;
59
60                 case SDLK_q:
61                         if (event.state == SDL_PRESSED) {
62                                 FaceBlock();
63                         }
64                         break;
65                 case SDLK_e:
66                         if (event.state == SDL_PRESSED) {
67                                 TurnBlock();
68                         }
69                         break;
70
71                 case SDLK_b:
72                         if (event.state == SDL_PRESSED) {
73                                 PrintBlockInfo();
74                         }
75                         break;
76                 case SDLK_l:
77                         if (event.state == SDL_PRESSED) {
78                                 PrintLightInfo();
79                         }
80                         break;
81                 case SDLK_p:
82                         if (event.state == SDL_PRESSED) {
83                                 PrintSelectionInfo();
84                         }
85                         break;
86         }
87 }
88
89 void Interface::FaceBlock() {
90         selection.SetFace(Block::Face((selection.GetFace() + 1) % Block::FACE_COUNT));
91         hud.Display(selection);
92 }
93
94 void Interface::TurnBlock() {
95         selection.SetTurn(Block::Turn((selection.GetTurn() + 1) % Block::TURN_COUNT));
96         hud.Display(selection);
97 }
98
99 void Interface::PrintBlockInfo() {
100         std::cout << std::endl;
101         if (!aim_chunk) {
102                 std::cout << "not looking at any block" << std::endl;
103                 return;
104         }
105         std::cout << "looking at block " << aim_block
106                 << " " << Chunk::ToCoords(aim_block)
107                 << " of chunk " << aim_chunk->Position()
108                 << std::endl;
109         Print(aim_chunk->BlockAt(aim_block));
110 }
111
112 void Interface::PrintLightInfo() {
113         std::cout
114                 << "light level " << world.PlayerChunk().GetLight(world.Player().Position())
115                 << " at position " << world.Player().Position()
116                 << std::endl;
117 }
118
119 void Interface::PrintSelectionInfo() {
120         std::cout << std::endl;
121         Print(selection);
122 }
123
124 void Interface::Print(const Block &block) {
125         std::cout << "type: " << block.type
126                 << ", face: " << block.GetFace()
127                 << ", turn: " << block.GetTurn()
128                 << std::endl;
129 }
130
131
132 void Interface::Handle(const SDL_MouseMotionEvent &event) {
133         if (config.mouse_disabled) return;
134         ctrl.RotateYaw(event.xrel * config.yaw_sensitivity);
135         ctrl.RotatePitch(event.yrel * config.pitch_sensitivity);
136 }
137
138 void Interface::Handle(const SDL_MouseButtonEvent &event) {
139         if (config.mouse_disabled) return;
140
141         if (event.state != SDL_PRESSED) return;
142
143         if (event.button == 1) {
144                 RemoveBlock();
145         } else if (event.button == 2) {
146                 PickBlock();
147         } else if (event.button == 3) {
148                 PlaceBlock();
149         }
150 }
151
152 void Interface::PickBlock() {
153         if (!aim_chunk) return;
154         selection = aim_chunk->BlockAt(aim_block);
155         hud.Display(selection);
156 }
157
158 void Interface::PlaceBlock() {
159         if (!aim_chunk) return;
160         Chunk *mod_chunk = aim_chunk;
161         glm::vec3 next_pos = Chunk::ToCoords(aim_block) + aim_normal;
162         if (!Chunk::InBounds(next_pos)) {
163                 mod_chunk = &world.Next(*aim_chunk, aim_normal);
164                 next_pos -= aim_normal * glm::vec3(Chunk::Extent());
165         }
166         mod_chunk->SetBlock(next_pos, selection);
167         mod_chunk->Invalidate();
168 }
169
170 void Interface::RemoveBlock() {
171         if (!aim_chunk) return;
172         aim_chunk->SetBlock(aim_block, remove);
173         aim_chunk->Invalidate();
174 }
175
176
177 void Interface::Handle(const SDL_MouseWheelEvent &event) {
178         if (config.mouse_disabled) return;
179
180         if (event.y < 0) {
181                 SelectNext();
182         } else if (event.y > 0) {
183                 SelectPrevious();
184         }
185 }
186
187 void Interface::SelectNext() {
188         ++selection.type;
189         if (size_t(selection.type) >= world.BlockTypes().Size()) {
190                 selection.type = 1;
191         }
192         hud.Display(selection);
193 }
194
195 void Interface::SelectPrevious() {
196         --selection.type;
197         if (selection.type <= 0) {
198                 selection.type = world.BlockTypes().Size() - 1;
199         }
200         hud.Display(selection);
201 }
202
203 void Interface::Handle(const SDL_WindowEvent &event) {
204         if (event.event == SDL_WINDOWEVENT_RESIZED) {
205                 hud.Viewport(event.data1, event.data2);
206         }
207 }
208
209
210 void Interface::Update(int dt) {
211         glm::vec3 vel;
212         if (right && !left) {
213                 vel.x = config.move_velocity;
214         } else if (left && !right) {
215                 vel.x = -config.move_velocity;
216         }
217         if (up && !down) {
218                 vel.y = config.move_velocity;
219         } else if (down && !up) {
220                 vel.y = -config.move_velocity;
221         }
222         if (back && !front) {
223                 vel.z = config.move_velocity;
224         } else if (front && !back) {
225                 vel.z = -config.move_velocity;
226         }
227         ctrl.Velocity(vel);
228         ctrl.Update(dt);
229
230         Ray aim = ctrl.Aim();
231         float dist;
232         if (world.Intersection(aim, glm::mat4(1.0f), &aim_chunk, &aim_block, &dist, &aim_normal)) {
233                 outline.Clear();
234                 aim_chunk->Type(aim_chunk->BlockAt(aim_block)).FillOutlineModel(outline);
235                 outline_transform = glm::scale(glm::mat4(1.0f), glm::vec3(1.0002f));
236                 outline_transform = aim_chunk->Transform(world.Player().ChunkCoords());
237                 outline_transform *= aim_chunk->ToTransform(aim_block);
238         } else {
239                 aim_chunk = nullptr;
240         }
241
242 }
243
244
245 void Interface::Render(DirectionalLighting &program) {
246         if (config.visual_disabled) return;
247
248         if (aim_chunk) {
249                 program.SetM(outline_transform);
250                 outline.Draw();
251         }
252
253         hud.Render(program);
254 }
255
256 }