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();
35 glGenVertexArrays(1, &VertexArrayID);
36 glBindVertexArray(VertexArrayID);
38 world.Generate({ -4, -4, -4 }, { 5, 5, 5});
40 hud.Viewport(960, 600);
41 hud.Display(*world.BlockTypes()[place_id]);
43 glClearColor(0.0, 0.0, 0.0, 1.0);
47 void Application::Run() {
49 Uint32 last = SDL_GetTicks();
52 Uint32 now = SDL_GetTicks();
53 int delta = now - last;
59 void Application::Loop(int dt) {
66 void Application::HandleEvents() {
68 while (SDL_PollEvent(&event)) {
72 controller.HandleKeyboard(event.key);
74 case SDL_MOUSEBUTTONDOWN:
75 if (event.button.button == 1) {
78 } else if (event.button.button == 2) {
81 } else if (event.button.button == 3) {
87 controller.HandleMouse(event.motion);
93 switch (event.window.event) {
94 case SDL_WINDOWEVENT_RESIZED:
95 cam.Viewport(event.window.data1, event.window.data2);
96 hud.Viewport(event.window.data1, event.window.data2);
108 void Application::Update(int dt) {
109 controller.Update(dt);
112 Ray aim = controller.Aim();
117 if (world.Intersection(aim, glm::mat4(1.0f), &chunk, &blkid, &dist, &normal)) {
118 glm::vec3 pos = Chunk::ToCoords(blkid);
119 outline_visible = true;
121 chunk->BlockAt(blkid).type->FillOutlineModel(outline);
122 outline_transform = glm::translate(chunk->Transform(world.Player().ChunkCoords()), pos);
123 outline_transform = glm::scale(outline_transform, glm::vec3(1.0001f));
125 outline_visible = false;
130 place_id = chunk->BlockAt(blkid).type->id;
131 hud.Display(*world.BlockTypes()[place_id]);
137 chunk->BlockAt(blkid).type = world.BlockTypes()[remove_id];
144 Chunk *mod_chunk = chunk;
145 glm::vec3 next_pos = Chunk::ToCoords(blkid) + normal;
146 if (!Chunk::InBounds(next_pos)) {
147 mod_chunk = &world.Next(*chunk, normal);
148 next_pos -= normal * Chunk::Extent();
150 mod_chunk->BlockAt(next_pos).type = world.BlockTypes()[place_id];
151 mod_chunk->Invalidate();
157 void Application::Render() {
162 program.SetProjection(cam.Projection());
163 world.Render(program);
165 if (outline_visible) {
166 program.SetM(outline_transform);