]> git.localhorst.tv Git - blank.git/blob - src/app.cpp
4f7cbdca49e0097620c1de82a7a8f09cb612592a
[blank.git] / src / app.cpp
1 #include "app.hpp"
2
3 #include "geometry.hpp"
4
5 #include <iostream>
6 #include <stdexcept>
7
8
9 namespace blank {
10
11 Application::Application()
12 : init_sdl()
13 , init_img()
14 , init_gl()
15 , window()
16 , ctx(window.CreateContext())
17 , init_glew()
18 , program()
19 , move_velocity(0.003f)
20 , pitch_sensitivity(-0.0025f)
21 , yaw_sensitivity(-0.001f)
22 , cam()
23 , world()
24 , outline()
25 , outline_visible(false)
26 , outline_transform(1.0f)
27 , running(false)
28 , front(false)
29 , back(false)
30 , left(false)
31 , right(false)
32 , up(false)
33 , down(false)
34 , place(false)
35 , remove(false)
36 , pick(false)
37 , remove_id(0)
38 , place_id(1) {
39         GLContext::EnableVSync();
40
41         GLuint VertexArrayID;
42         glGenVertexArrays(1, &VertexArrayID);
43         glBindVertexArray(VertexArrayID);
44
45         cam.Position(glm::vec3(0, 4, 4));
46
47         world.Generate();
48
49         glClearColor(0.0, 0.0, 0.0, 1.0);
50 }
51
52
53 void Application::Run() {
54         running = true;
55         Uint32 last = SDL_GetTicks();
56         window.GrabMouse();
57         while (running) {
58                 Uint32 now = SDL_GetTicks();
59                 int delta = now - last;
60                 Loop(delta);
61                 last = now;
62         }
63 }
64
65 void Application::Loop(int dt) {
66         HandleEvents();
67         Update(dt);
68         Render();
69 }
70
71
72 void Application::HandleEvents() {
73         SDL_Event event;
74         while (SDL_PollEvent(&event)) {
75                 switch (event.type) {
76                         case SDL_KEYDOWN:
77                         case SDL_KEYUP:
78                                 switch (event.key.keysym.sym) {
79                                         case SDLK_w:
80                                                 front = event.key.state == SDL_PRESSED;
81                                                 break;
82                                         case SDLK_s:
83                                                 back = event.key.state == SDL_PRESSED;
84                                                 break;
85                                         case SDLK_a:
86                                                 left = event.key.state == SDL_PRESSED;
87                                                 break;
88                                         case SDLK_d:
89                                                 right = event.key.state == SDL_PRESSED;
90                                                 break;
91                                         case SDLK_q:
92                                                 up = event.key.state == SDL_PRESSED;
93                                                 break;
94                                         case SDLK_e:
95                                                 down = event.key.state == SDL_PRESSED;
96                                                 break;
97                                 }
98                                 break;
99                         case SDL_MOUSEBUTTONDOWN:
100                                 if (event.button.button == 1) {
101                                         // left
102                                         remove = true;
103                                 } else if (event.button.button == 2) {
104                                         // middle
105                                         pick = true;
106                                 } else if (event.button.button == 3) {
107                                         // right
108                                         place = true;
109                                 }
110                                 break;
111                         case SDL_MOUSEMOTION:
112                                 cam.RotateYaw(event.motion.xrel * yaw_sensitivity);
113                                 cam.RotatePitch(event.motion.yrel * pitch_sensitivity);
114                                 break;
115                         case SDL_QUIT:
116                                 running = false;
117                                 break;
118                         case SDL_WINDOWEVENT:
119                                 switch (event.window.event) {
120                                         case SDL_WINDOWEVENT_RESIZED:
121                                                 cam.Viewport(event.window.data1, event.window.data2);
122                                                 break;
123                                         default:
124                                                 break;
125                                 }
126                                 break;
127                         default:
128                                 break;
129                 }
130         }
131 }
132
133 void Application::Update(int dt) {
134         glm::vec3 vel;
135         if (right && !left) {
136                 vel.x = move_velocity;
137         } else if (left && !right) {
138                 vel.x = -move_velocity;
139         }
140         if (up && !down) {
141                 vel.y = move_velocity;
142         } else if (down && !up) {
143                 vel.y = -move_velocity;
144         }
145         if (back && !front) {
146                 vel.z = move_velocity;
147         } else if (front && !back) {
148                 vel.z = -move_velocity;
149         }
150         cam.OrientationVelocity(vel);
151
152         cam.Update(dt);
153
154         Ray aim = cam.Aim();
155         Chunk *chunk;
156         int blkid;
157         float dist;
158         glm::vec3 normal;
159         if (world.Intersection(aim, glm::mat4(1.0f), &chunk, &blkid, &dist, &normal)) {
160                 glm::vec3 pos = Chunk::ToCoords(blkid);
161                 outline_visible = true;
162                 outline.Clear();
163                 chunk->BlockAt(blkid).type->FillOutlineModel(outline);
164                 outline_transform = glm::translate(chunk->Transform(), pos);
165         } else {
166                 outline_visible = false;
167         }
168
169         if (pick) {
170                 if (chunk) {
171                         place_id = chunk->BlockAt(blkid).type->id;
172                 }
173                 pick = false;
174         }
175         if (remove) {
176                 if (chunk) {
177                         chunk->BlockAt(blkid).type = world.BlockTypes()[remove_id];
178                         chunk->Invalidate();
179                 }
180                 remove = false;
181         }
182         if (place) {
183                 if (chunk) {
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();
189                         }
190                         mod_chunk->BlockAt(next_pos).type = world.BlockTypes()[place_id];
191                         mod_chunk->Invalidate();
192                 }
193                 place = false;
194         }
195 }
196
197 void Application::Render() {
198         GLContext::Clear();
199
200         program.Activate();
201
202         program.SetVP(cam.View(), cam.Projection());
203
204         for (Chunk &chunk : world.LoadedChunks()) {
205                 program.SetM(chunk.Transform());
206                 chunk.Draw();
207         }
208
209         if (outline_visible) {
210                 program.SetM(outline_transform);
211                 outline.Draw();
212         }
213
214         window.Flip();
215 }
216
217 }