3 #include "geometry.hpp"
11 Application::Application()
16 , ctx(window.CreateContext())
19 , move_velocity(0.003f)
20 , pitch_sensitivity(-0.0025f)
21 , yaw_sensitivity(-0.001f)
25 , outline_visible(false)
26 , outline_transform(1.0f)
39 GLContext::EnableVSync();
42 glGenVertexArrays(1, &VertexArrayID);
43 glBindVertexArray(VertexArrayID);
45 cam.Position(glm::vec3(0, 4, 4));
49 outline.vertices = std::vector<glm::vec3>({
50 { 0.0f, 0.0f, 0.0f }, { 1.0f, 0.0f, 0.0f },
51 { 1.0f, 0.0f, 0.0f }, { 1.0f, 1.0f, 0.0f },
52 { 1.0f, 1.0f, 0.0f }, { 0.0f, 1.0f, 0.0f },
53 { 0.0f, 1.0f, 0.0f }, { 0.0f, 0.0f, 0.0f },
54 { 0.0f, 0.0f, 0.0f }, { 0.0f, 0.0f, 1.0f },
55 { 1.0f, 0.0f, 0.0f }, { 1.0f, 0.0f, 1.0f },
56 { 1.0f, 1.0f, 0.0f }, { 1.0f, 1.0f, 1.0f },
57 { 0.0f, 1.0f, 0.0f }, { 0.0f, 1.0f, 1.0f },
58 { 0.0f, 0.0f, 1.0f }, { 1.0f, 0.0f, 1.0f },
59 { 1.0f, 0.0f, 1.0f }, { 1.0f, 1.0f, 1.0f },
60 { 1.0f, 1.0f, 1.0f }, { 0.0f, 1.0f, 1.0f },
61 { 0.0f, 1.0f, 1.0f }, { 0.0f, 0.0f, 1.0f },
63 outline.colors.resize(24, { -1, -1, -1 });
66 glClearColor(0.0, 0.0, 0.0, 1.0);
70 void Application::Run() {
72 Uint32 last = SDL_GetTicks();
75 Uint32 now = SDL_GetTicks();
76 int delta = now - last;
82 void Application::Loop(int dt) {
89 void Application::HandleEvents() {
91 while (SDL_PollEvent(&event)) {
95 switch (event.key.keysym.sym) {
97 front = event.key.state == SDL_PRESSED;
100 back = event.key.state == SDL_PRESSED;
103 left = event.key.state == SDL_PRESSED;
106 right = event.key.state == SDL_PRESSED;
109 up = event.key.state == SDL_PRESSED;
112 down = event.key.state == SDL_PRESSED;
116 case SDL_MOUSEBUTTONDOWN:
117 if (event.button.button == 1) {
120 } else if (event.button.button == 2) {
123 } else if (event.button.button == 3) {
128 case SDL_MOUSEMOTION:
129 cam.RotateYaw(event.motion.xrel * yaw_sensitivity);
130 cam.RotatePitch(event.motion.yrel * pitch_sensitivity);
135 case SDL_WINDOWEVENT:
136 switch (event.window.event) {
137 case SDL_WINDOWEVENT_RESIZED:
138 cam.Viewport(event.window.data1, event.window.data2);
150 void Application::Update(int dt) {
152 if (right && !left) {
153 vel.x = move_velocity;
154 } else if (left && !right) {
155 vel.x = -move_velocity;
158 vel.y = move_velocity;
159 } else if (down && !up) {
160 vel.y = -move_velocity;
162 if (back && !front) {
163 vel.z = move_velocity;
164 } else if (front && !back) {
165 vel.z = -move_velocity;
167 cam.OrientationVelocity(vel);
176 if (world.Intersection(aim, glm::mat4(1.0f), &chunk, &blkid, &dist, &normal)) {
177 glm::vec3 pos = Chunk::ToCoords(blkid);
178 outline_visible = true;
179 outline_transform = glm::translate(chunk->Transform(), pos);
181 outline_visible = false;
186 place_id = chunk->BlockAt(blkid).type->id;
192 chunk->BlockAt(blkid).type = world.BlockTypes()[remove_id];
199 Chunk *mod_chunk = chunk;
200 glm::vec3 next_pos = Chunk::ToCoords(blkid) + normal;
201 if (!Chunk::InBounds(next_pos)) {
202 mod_chunk = &world.Next(*chunk, normal);
203 next_pos -= normal * Chunk::Extent();
205 mod_chunk->BlockAt(next_pos).type = world.BlockTypes()[place_id];
206 mod_chunk->Invalidate();
212 void Application::Render() {
217 program.SetVP(cam.View(), cam.Projection());
219 for (Chunk &chunk : world.LoadedChunks()) {
220 program.SetM(chunk.Transform());
224 if (outline_visible) {
225 program.SetM(outline_transform);