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 world.Generate({ -4, -4, -4 }, { 5, 5, 5});
36 hud.Viewport(960, 600);
37 hud.Display(*world.BlockTypes()[place_id]);
39 glClearColor(0.0, 0.0, 0.0, 1.0);
43 void Application::RunN(size_t n) {
44 Uint32 last = SDL_GetTicks();
45 for (size_t i = 0; i < n; ++i) {
46 Uint32 now = SDL_GetTicks();
47 int delta = now - last;
53 void Application::RunT(size_t t) {
54 Uint32 last = SDL_GetTicks();
55 Uint32 finish = last + t;
56 while (last < finish) {
57 Uint32 now = SDL_GetTicks();
58 int delta = now - last;
64 void Application::RunS(size_t n, size_t t) {
65 for (size_t i = 0; i < n; ++i) {
71 void Application::Run() {
73 Uint32 last = SDL_GetTicks();
76 Uint32 now = SDL_GetTicks();
77 int delta = now - last;
83 void Application::Loop(int dt) {
90 void Application::HandleEvents() {
92 while (SDL_PollEvent(&event)) {
96 controller.HandleKeyboard(event.key);
98 case SDL_MOUSEBUTTONDOWN:
99 if (event.button.button == 1) {
102 } else if (event.button.button == 2) {
105 } else if (event.button.button == 3) {
110 case SDL_MOUSEMOTION:
111 controller.HandleMouse(event.motion);
116 case SDL_WINDOWEVENT:
117 switch (event.window.event) {
118 case SDL_WINDOWEVENT_RESIZED:
119 cam.Viewport(event.window.data1, event.window.data2);
120 hud.Viewport(event.window.data1, event.window.data2);
132 void Application::Update(int dt) {
133 controller.Update(dt);
136 Ray aim = controller.Aim();
141 if (world.Intersection(aim, glm::mat4(1.0f), &chunk, &blkid, &dist, &normal)) {
142 glm::vec3 pos = Chunk::ToCoords(blkid);
143 outline_visible = true;
145 chunk->Type(chunk->BlockAt(blkid)).FillOutlineModel(outline);
146 outline_transform = glm::translate(chunk->Transform(world.Player().ChunkCoords()), pos);
147 outline_transform = glm::scale(outline_transform, glm::vec3(1.0001f));
149 outline_visible = false;
154 place_id = chunk->BlockAt(blkid).type;
155 hud.Display(*world.BlockTypes()[place_id]);
161 chunk->BlockAt(blkid).type = remove_id;
168 Chunk *mod_chunk = chunk;
169 glm::vec3 next_pos = Chunk::ToCoords(blkid) + normal;
170 if (!Chunk::InBounds(next_pos)) {
171 mod_chunk = &world.Next(*chunk, normal);
172 next_pos -= normal * glm::vec3(Chunk::Extent());
174 mod_chunk->BlockAt(next_pos).type = place_id;
175 mod_chunk->Invalidate();
181 void Application::Render() {
186 program.SetProjection(cam.Projection());
187 world.Render(program);
189 if (outline_visible) {
190 program.SetM(outline_transform);