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;
93 up = event.key.state == SDL_PRESSED;
97 down = event.key.state == SDL_PRESSED;
101 case SDL_MOUSEBUTTONDOWN:
102 if (event.button.button == 1) {
105 } else if (event.button.button == 2) {
108 } else if (event.button.button == 3) {
113 case SDL_MOUSEMOTION:
114 cam.RotateYaw(event.motion.xrel * yaw_sensitivity);
115 cam.RotatePitch(event.motion.yrel * pitch_sensitivity);
120 case SDL_WINDOWEVENT:
121 switch (event.window.event) {
122 case SDL_WINDOWEVENT_RESIZED:
123 cam.Viewport(event.window.data1, event.window.data2);
135 void Application::Update(int dt) {
137 if (right && !left) {
138 vel.x = move_velocity;
139 } else if (left && !right) {
140 vel.x = -move_velocity;
143 vel.y = move_velocity;
144 } else if (down && !up) {
145 vel.y = -move_velocity;
147 if (back && !front) {
148 vel.z = move_velocity;
149 } else if (front && !back) {
150 vel.z = -move_velocity;
152 cam.OrientationVelocity(vel);
161 if (world.Intersection(aim, glm::mat4(1.0f), &chunk, &blkid, &dist, &normal)) {
162 glm::vec3 pos = Chunk::ToCoords(blkid);
163 outline_visible = true;
165 chunk->BlockAt(blkid).type->FillOutlineModel(outline);
166 outline_transform = glm::translate(chunk->Transform(), pos);
168 outline_visible = false;
173 place_id = chunk->BlockAt(blkid).type->id;
179 chunk->BlockAt(blkid).type = world.BlockTypes()[remove_id];
186 Chunk *mod_chunk = chunk;
187 glm::vec3 next_pos = Chunk::ToCoords(blkid) + normal;
188 if (!Chunk::InBounds(next_pos)) {
189 mod_chunk = &world.Next(*chunk, normal);
190 next_pos -= normal * Chunk::Extent();
192 mod_chunk->BlockAt(next_pos).type = world.BlockTypes()[place_id];
193 mod_chunk->Invalidate();
199 void Application::Render() {
204 program.SetVP(cam.View(), cam.Projection());
206 for (Chunk &chunk : world.LoadedChunks()) {
207 program.SetM(chunk.Transform());
211 if (outline_visible) {
212 program.SetM(outline_transform);