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 glClearColor(0.0, 0.0, 0.0, 1.0);
53 void Application::Run() {
55 Uint32 last = SDL_GetTicks();
58 Uint32 now = SDL_GetTicks();
59 int delta = now - last;
65 void Application::Loop(int dt) {
72 void Application::HandleEvents() {
74 while (SDL_PollEvent(&event)) {
78 switch (event.key.keysym.sym) {
80 front = event.key.state == SDL_PRESSED;
83 back = event.key.state == SDL_PRESSED;
86 left = event.key.state == SDL_PRESSED;
89 right = event.key.state == SDL_PRESSED;
92 up = event.key.state == SDL_PRESSED;
95 down = event.key.state == SDL_PRESSED;
99 case SDL_MOUSEBUTTONDOWN:
100 if (event.button.button == 1) {
103 } else if (event.button.button == 2) {
106 } else if (event.button.button == 3) {
111 case SDL_MOUSEMOTION:
112 cam.RotateYaw(event.motion.xrel * yaw_sensitivity);
113 cam.RotatePitch(event.motion.yrel * pitch_sensitivity);
118 case SDL_WINDOWEVENT:
119 switch (event.window.event) {
120 case SDL_WINDOWEVENT_RESIZED:
121 cam.Viewport(event.window.data1, event.window.data2);
133 void Application::Update(int dt) {
135 if (right && !left) {
136 vel.x = move_velocity;
137 } else if (left && !right) {
138 vel.x = -move_velocity;
141 vel.y = move_velocity;
142 } else if (down && !up) {
143 vel.y = -move_velocity;
145 if (back && !front) {
146 vel.z = move_velocity;
147 } else if (front && !back) {
148 vel.z = -move_velocity;
150 cam.OrientationVelocity(vel);
159 if (world.Intersection(aim, glm::mat4(1.0f), &chunk, &blkid, &dist, &normal)) {
160 glm::vec3 pos = Chunk::ToCoords(blkid);
161 outline_visible = true;
163 chunk->BlockAt(blkid).type->FillOutlineModel(outline);
164 outline_transform = glm::translate(chunk->Transform(), pos);
166 outline_visible = false;
171 place_id = chunk->BlockAt(blkid).type->id;
177 chunk->BlockAt(blkid).type = world.BlockTypes()[remove_id];
184 Chunk *mod_chunk = chunk;
185 glm::vec3 next_pos = Chunk::ToCoords(blkid) + normal;
186 if (!Chunk::InBounds(next_pos)) {
187 mod_chunk = &world.Next(*chunk, normal);
188 next_pos -= normal * Chunk::Extent();
190 mod_chunk->BlockAt(next_pos).type = world.BlockTypes()[place_id];
191 mod_chunk->Invalidate();
197 void Application::Render() {
202 program.SetVP(cam.View(), cam.Projection());
204 for (Chunk &chunk : world.LoadedChunks()) {
205 program.SetM(chunk.Transform());
209 if (outline_visible) {
210 program.SetM(outline_transform);