3 #include "geometry.hpp"
11 Application::Application()
16 , ctx(window.CreateContext())
23 , outline_visible(false)
24 , outline_transform(1.0f)
31 GLContext::EnableVSync();
34 glGenVertexArrays(1, &VertexArrayID);
35 glBindVertexArray(VertexArrayID);
39 hud.Viewport(960, 600);
40 hud.Display(*world.BlockTypes()[place_id]);
42 glClearColor(0.0, 0.0, 0.0, 1.0);
46 void Application::Run() {
48 Uint32 last = SDL_GetTicks();
51 Uint32 now = SDL_GetTicks();
52 int delta = now - last;
58 void Application::Loop(int dt) {
65 void Application::HandleEvents() {
67 while (SDL_PollEvent(&event)) {
71 world.Controller().HandleKeyboard(event.key);
73 case SDL_MOUSEBUTTONDOWN:
74 if (event.button.button == 1) {
77 } else if (event.button.button == 2) {
80 } else if (event.button.button == 3) {
86 world.Controller().HandleMouse(event.motion);
92 switch (event.window.event) {
93 case SDL_WINDOWEVENT_RESIZED:
94 cam.Viewport(event.window.data1, event.window.data2);
95 hud.Viewport(event.window.data1, event.window.data2);
107 void Application::Update(int dt) {
110 Ray aim = world.Controller().Aim();
115 if (world.Intersection(aim, glm::mat4(1.0f), &chunk, &blkid, &dist, &normal)) {
116 glm::vec3 pos = Chunk::ToCoords(blkid);
117 outline_visible = true;
119 chunk->BlockAt(blkid).type->FillOutlineModel(outline);
120 outline_transform = glm::translate(chunk->Transform(), pos);
121 outline_transform = glm::scale(outline_transform, glm::vec3(1.0001f));
123 outline_visible = false;
128 place_id = chunk->BlockAt(blkid).type->id;
129 hud.Display(*world.BlockTypes()[place_id]);
135 chunk->BlockAt(blkid).type = world.BlockTypes()[remove_id];
142 Chunk *mod_chunk = chunk;
143 glm::vec3 next_pos = Chunk::ToCoords(blkid) + normal;
144 if (!Chunk::InBounds(next_pos)) {
145 mod_chunk = &world.Next(*chunk, normal);
146 next_pos -= normal * Chunk::Extent();
148 mod_chunk->BlockAt(next_pos).type = world.BlockTypes()[place_id];
149 mod_chunk->Invalidate();
155 void Application::Render() {
160 program.SetProjection(cam.Projection());
161 world.Render(program);
163 if (outline_visible) {
164 program.SetM(outline_transform);