3 #include "geometry.hpp"
11 Application::Application()
16 , ctx(window.CreateContext())
22 , controller(world.Player())
24 , outline_visible(false)
25 , outline_transform(1.0f)
32 GLContext::EnableVSync();
34 hud.Viewport(960, 600);
35 hud.Display(*world.BlockTypes()[place_id]);
37 glClearColor(0.0, 0.0, 0.0, 1.0);
41 void Application::RunN(size_t n) {
42 Uint32 last = SDL_GetTicks();
43 for (size_t i = 0; i < n; ++i) {
44 Uint32 now = SDL_GetTicks();
45 int delta = now - last;
51 void Application::RunT(size_t t) {
52 Uint32 last = SDL_GetTicks();
53 Uint32 finish = last + t;
54 while (last < finish) {
55 Uint32 now = SDL_GetTicks();
56 int delta = now - last;
62 void Application::RunS(size_t n, size_t t) {
63 for (size_t i = 0; i < n; ++i) {
69 void Application::Run() {
71 Uint32 last = SDL_GetTicks();
74 Uint32 now = SDL_GetTicks();
75 int delta = now - last;
81 void Application::Loop(int dt) {
88 void Application::HandleEvents() {
90 while (SDL_PollEvent(&event)) {
94 controller.HandleKeyboard(event.key);
96 case SDL_MOUSEBUTTONDOWN:
97 if (event.button.button == 1) {
100 } else if (event.button.button == 2) {
103 } else if (event.button.button == 3) {
108 case SDL_MOUSEMOTION:
109 controller.HandleMouse(event.motion);
112 if (event.wheel.y < 0) {
114 if (size_t(place_id) >= world.BlockTypes().Size()) {
117 hud.Display(*world.BlockTypes()[place_id]);
118 } else if (event.wheel.y > 0) {
121 place_id = world.BlockTypes().Size() - 1;
123 hud.Display(*world.BlockTypes()[place_id]);
129 case SDL_WINDOWEVENT:
130 switch (event.window.event) {
131 case SDL_WINDOWEVENT_FOCUS_GAINED:
134 case SDL_WINDOWEVENT_FOCUS_LOST:
135 window.ReleaseMouse();
137 case SDL_WINDOWEVENT_RESIZED:
138 cam.Viewport(event.window.data1, event.window.data2);
139 hud.Viewport(event.window.data1, event.window.data2);
151 void Application::Update(int dt) {
152 controller.Update(dt);
155 Ray aim = controller.Aim();
160 if (world.Intersection(aim, glm::mat4(1.0f), &chunk, &blkid, &dist, &normal)) {
161 outline_visible = true;
163 chunk->Type(chunk->BlockAt(blkid)).FillOutlineModel(outline);
164 outline_transform = glm::scale(glm::mat4(1.0f), glm::vec3(1.0002f));
165 outline_transform = chunk->Transform(world.Player().ChunkCoords());
166 outline_transform *= chunk->ToTransform(blkid);
168 outline_visible = false;
173 place_id = chunk->BlockAt(blkid).type;
174 hud.Display(*world.BlockTypes()[place_id]);
180 chunk->BlockAt(blkid).type = remove_id;
187 Chunk *mod_chunk = chunk;
188 glm::vec3 next_pos = Chunk::ToCoords(blkid) + normal;
189 if (!Chunk::InBounds(next_pos)) {
190 mod_chunk = &world.Next(*chunk, normal);
191 next_pos -= normal * glm::vec3(Chunk::Extent());
193 mod_chunk->BlockAt(next_pos).type = place_id;
194 mod_chunk->Invalidate();
200 void Application::Render() {
205 program.SetProjection(cam.Projection());
206 world.Render(program);
208 if (outline_visible) {
209 program.SetM(outline_transform);