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);
114 case SDL_WINDOWEVENT:
115 switch (event.window.event) {
116 case SDL_WINDOWEVENT_RESIZED:
117 cam.Viewport(event.window.data1, event.window.data2);
118 hud.Viewport(event.window.data1, event.window.data2);
130 void Application::Update(int dt) {
131 controller.Update(dt);
134 Ray aim = controller.Aim();
139 if (world.Intersection(aim, glm::mat4(1.0f), &chunk, &blkid, &dist, &normal)) {
140 outline_visible = true;
142 chunk->Type(chunk->BlockAt(blkid)).FillOutlineModel(outline);
143 outline_transform = glm::scale(glm::mat4(1.0f), glm::vec3(1.0002f));
144 outline_transform = chunk->Transform(world.Player().ChunkCoords());
145 outline_transform *= chunk->ToTransform(blkid);
147 outline_visible = false;
152 place_id = chunk->BlockAt(blkid).type;
153 hud.Display(*world.BlockTypes()[place_id]);
159 chunk->BlockAt(blkid).type = remove_id;
166 Chunk *mod_chunk = chunk;
167 glm::vec3 next_pos = Chunk::ToCoords(blkid) + normal;
168 if (!Chunk::InBounds(next_pos)) {
169 mod_chunk = &world.Next(*chunk, normal);
170 next_pos -= normal * glm::vec3(Chunk::Extent());
172 mod_chunk->BlockAt(next_pos).type = place_id;
173 mod_chunk->Invalidate();
179 void Application::Render() {
184 program.SetProjection(cam.Projection());
185 world.Render(program);
187 if (outline_visible) {
188 program.SetM(outline_transform);