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::Run() {
45 Uint32 last = SDL_GetTicks();
48 Uint32 now = SDL_GetTicks();
49 int delta = now - last;
55 void Application::Loop(int dt) {
62 void Application::HandleEvents() {
64 while (SDL_PollEvent(&event)) {
68 controller.HandleKeyboard(event.key);
70 case SDL_MOUSEBUTTONDOWN:
71 if (event.button.button == 1) {
74 } else if (event.button.button == 2) {
77 } else if (event.button.button == 3) {
83 controller.HandleMouse(event.motion);
89 switch (event.window.event) {
90 case SDL_WINDOWEVENT_RESIZED:
91 cam.Viewport(event.window.data1, event.window.data2);
92 hud.Viewport(event.window.data1, event.window.data2);
104 void Application::Update(int dt) {
105 controller.Update(dt);
108 Ray aim = controller.Aim();
113 if (world.Intersection(aim, glm::mat4(1.0f), &chunk, &blkid, &dist, &normal)) {
114 glm::vec3 pos = Chunk::ToCoords(blkid);
115 outline_visible = true;
117 chunk->Type(chunk->BlockAt(blkid)).FillOutlineModel(outline);
118 outline_transform = glm::translate(chunk->Transform(world.Player().ChunkCoords()), pos);
119 outline_transform = glm::scale(outline_transform, glm::vec3(1.0001f));
121 outline_visible = false;
126 place_id = chunk->BlockAt(blkid).type;
127 hud.Display(*world.BlockTypes()[place_id]);
133 chunk->BlockAt(blkid).type = remove_id;
140 Chunk *mod_chunk = chunk;
141 glm::vec3 next_pos = Chunk::ToCoords(blkid) + normal;
142 if (!Chunk::InBounds(next_pos)) {
143 mod_chunk = &world.Next(*chunk, normal);
144 next_pos -= normal * glm::vec3(Chunk::Extent());
146 mod_chunk->BlockAt(next_pos).type = place_id;
147 mod_chunk->Invalidate();
153 void Application::Render() {
158 program.SetProjection(cam.Projection());
159 world.Render(program);
161 if (outline_visible) {
162 program.SetM(outline_transform);