X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fapp.cpp;h=21eb85c309830eca5f332d6f5cdd719d1b8da04a;hb=753be639d7d04f9f7415db9fc2721485c531f0a1;hp=8c40c61fbc5ab2c74ea00257ff40fe4823b79264;hpb=982b69ce8c393ae18beed5239191b8bc2ee1d5d1;p=blank.git diff --git a/src/app.cpp b/src/app.cpp index 8c40c61..21eb85c 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -16,21 +16,13 @@ Application::Application() , ctx(window.CreateContext()) , init_glew() , program() -, move_velocity(0.003f) -, pitch_sensitivity(-0.0025f) -, yaw_sensitivity(-0.001f) , cam() +, hud() , world() , outline() , outline_visible(false) , outline_transform(1.0f) , running(false) -, front(false) -, back(false) -, left(false) -, right(false) -, up(false) -, down(false) , place(false) , remove(false) , pick(false) @@ -42,26 +34,10 @@ Application::Application() glGenVertexArrays(1, &VertexArrayID); glBindVertexArray(VertexArrayID); - cam.Position(glm::vec3(0, 4, 4)); - world.Generate(); - outline.vertices = std::vector({ - { 0.0f, 0.0f, 0.0f }, { 1.0f, 0.0f, 0.0f }, - { 1.0f, 0.0f, 0.0f }, { 1.0f, 1.0f, 0.0f }, - { 1.0f, 1.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, - { 0.0f, 1.0f, 0.0f }, { 0.0f, 0.0f, 0.0f }, - { 0.0f, 0.0f, 0.0f }, { 0.0f, 0.0f, 1.0f }, - { 1.0f, 0.0f, 0.0f }, { 1.0f, 0.0f, 1.0f }, - { 1.0f, 1.0f, 0.0f }, { 1.0f, 1.0f, 1.0f }, - { 0.0f, 1.0f, 0.0f }, { 0.0f, 1.0f, 1.0f }, - { 0.0f, 0.0f, 1.0f }, { 1.0f, 0.0f, 1.0f }, - { 1.0f, 0.0f, 1.0f }, { 1.0f, 1.0f, 1.0f }, - { 1.0f, 1.0f, 1.0f }, { 0.0f, 1.0f, 1.0f }, - { 0.0f, 1.0f, 1.0f }, { 0.0f, 0.0f, 1.0f }, - }); - outline.colors.resize(24, { -1, -1, -1 }); - outline.Invalidate(); + hud.Viewport(960, 600); + hud.Display(*world.BlockTypes()[place_id]); glClearColor(0.0, 0.0, 0.0, 1.0); } @@ -92,26 +68,7 @@ void Application::HandleEvents() { switch (event.type) { case SDL_KEYDOWN: case SDL_KEYUP: - switch (event.key.keysym.sym) { - case SDLK_w: - front = event.key.state == SDL_PRESSED; - break; - case SDLK_s: - back = event.key.state == SDL_PRESSED; - break; - case SDLK_a: - left = event.key.state == SDL_PRESSED; - break; - case SDLK_d: - right = event.key.state == SDL_PRESSED; - break; - case SDLK_q: - up = event.key.state == SDL_PRESSED; - break; - case SDLK_e: - down = event.key.state == SDL_PRESSED; - break; - } + world.Controller().HandleKeyboard(event.key); break; case SDL_MOUSEBUTTONDOWN: if (event.button.button == 1) { @@ -126,8 +83,7 @@ void Application::HandleEvents() { } break; case SDL_MOUSEMOTION: - cam.RotateYaw(event.motion.xrel * yaw_sensitivity); - cam.RotatePitch(event.motion.yrel * pitch_sensitivity); + world.Controller().HandleMouse(event.motion); break; case SDL_QUIT: running = false; @@ -136,6 +92,7 @@ void Application::HandleEvents() { switch (event.window.event) { case SDL_WINDOWEVENT_RESIZED: cam.Viewport(event.window.data1, event.window.data2); + hud.Viewport(event.window.data1, event.window.data2); break; default: break; @@ -148,27 +105,9 @@ void Application::HandleEvents() { } void Application::Update(int dt) { - glm::vec3 vel; - if (right && !left) { - vel.x = move_velocity; - } else if (left && !right) { - vel.x = -move_velocity; - } - if (up && !down) { - vel.y = move_velocity; - } else if (down && !up) { - vel.y = -move_velocity; - } - if (back && !front) { - vel.z = move_velocity; - } else if (front && !back) { - vel.z = -move_velocity; - } - cam.OrientationVelocity(vel); - - cam.Update(dt); + world.Update(dt); - Ray aim = cam.Aim(); + Ray aim = world.Controller().Aim(); Chunk *chunk; int blkid; float dist; @@ -176,7 +115,10 @@ void Application::Update(int dt) { if (world.Intersection(aim, glm::mat4(1.0f), &chunk, &blkid, &dist, &normal)) { glm::vec3 pos = Chunk::ToCoords(blkid); outline_visible = true; + outline.Clear(); + chunk->BlockAt(blkid).type->FillOutlineModel(outline); outline_transform = glm::translate(chunk->Transform(), pos); + outline_transform = glm::scale(outline_transform, glm::vec3(1.0001f)); } else { outline_visible = false; } @@ -184,6 +126,7 @@ void Application::Update(int dt) { if (pick) { if (chunk) { place_id = chunk->BlockAt(blkid).type->id; + hud.Display(*world.BlockTypes()[place_id]); } pick = false; } @@ -214,18 +157,16 @@ void Application::Render() { program.Activate(); - program.SetVP(cam.View(), cam.Projection()); - - for (Chunk &chunk : world.LoadedChunks()) { - program.SetM(chunk.Transform()); - chunk.Draw(); - } + program.SetProjection(cam.Projection()); + world.Render(program); if (outline_visible) { program.SetM(outline_transform); outline.Draw(); } + hud.Render(program); + window.Flip(); }