X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fapp.cpp;h=7ddbbf189180d43dccd577a91e68a057ef1c8f0f;hb=e70967c971f77a4ac0f5c074e6eb94bdd0e2b7ab;hp=4f7cbdca49e0097620c1de82a7a8f09cb612592a;hpb=b79bc060068daf80c707f7ca08cb40a716367784;p=blank.git diff --git a/src/app.cpp b/src/app.cpp index 4f7cbdc..7ddbbf1 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -16,21 +16,14 @@ Application::Application() , ctx(window.CreateContext()) , init_glew() , program() -, move_velocity(0.003f) -, pitch_sensitivity(-0.0025f) -, yaw_sensitivity(-0.001f) , cam() +, hud() , world() +, controller(world.Player()) , 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) @@ -38,18 +31,43 @@ Application::Application() , place_id(1) { GLContext::EnableVSync(); - GLuint VertexArrayID; - glGenVertexArrays(1, &VertexArrayID); - glBindVertexArray(VertexArrayID); + world.Generate({ -4, -4, -4 }, { 5, 5, 5}); - cam.Position(glm::vec3(0, 4, 4)); - - world.Generate(); + hud.Viewport(960, 600); + hud.Display(*world.BlockTypes()[place_id]); glClearColor(0.0, 0.0, 0.0, 1.0); } +void Application::RunN(size_t n) { + Uint32 last = SDL_GetTicks(); + for (size_t i = 0; i < n; ++i) { + Uint32 now = SDL_GetTicks(); + int delta = now - last; + Loop(delta); + last = now; + } +} + +void Application::RunT(size_t t) { + Uint32 last = SDL_GetTicks(); + Uint32 finish = last + t; + while (last < finish) { + Uint32 now = SDL_GetTicks(); + int delta = now - last; + Loop(delta); + last = now; + } +} + +void Application::RunS(size_t n, size_t t) { + for (size_t i = 0; i < n; ++i) { + Loop(t); + } +} + + void Application::Run() { running = true; Uint32 last = SDL_GetTicks(); @@ -75,26 +93,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; - } + controller.HandleKeyboard(event.key); break; case SDL_MOUSEBUTTONDOWN: if (event.button.button == 1) { @@ -109,8 +108,7 @@ void Application::HandleEvents() { } break; case SDL_MOUSEMOTION: - cam.RotateYaw(event.motion.xrel * yaw_sensitivity); - cam.RotatePitch(event.motion.yrel * pitch_sensitivity); + controller.HandleMouse(event.motion); break; case SDL_QUIT: running = false; @@ -119,6 +117,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; @@ -131,27 +130,10 @@ 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); + controller.Update(dt); + world.Update(dt); - cam.Update(dt); - - Ray aim = cam.Aim(); + Ray aim = controller.Aim(); Chunk *chunk; int blkid; float dist; @@ -160,21 +142,23 @@ void Application::Update(int dt) { glm::vec3 pos = Chunk::ToCoords(blkid); outline_visible = true; outline.Clear(); - chunk->BlockAt(blkid).type->FillOutlineModel(outline); - outline_transform = glm::translate(chunk->Transform(), pos); + chunk->Type(chunk->BlockAt(blkid)).FillOutlineModel(outline); + outline_transform = glm::translate(chunk->Transform(world.Player().ChunkCoords()), pos); + outline_transform = glm::scale(outline_transform, glm::vec3(1.0001f)); } else { outline_visible = false; } if (pick) { if (chunk) { - place_id = chunk->BlockAt(blkid).type->id; + place_id = chunk->BlockAt(blkid).type; + hud.Display(*world.BlockTypes()[place_id]); } pick = false; } if (remove) { if (chunk) { - chunk->BlockAt(blkid).type = world.BlockTypes()[remove_id]; + chunk->BlockAt(blkid).type = remove_id; chunk->Invalidate(); } remove = false; @@ -185,9 +169,9 @@ void Application::Update(int dt) { glm::vec3 next_pos = Chunk::ToCoords(blkid) + normal; if (!Chunk::InBounds(next_pos)) { mod_chunk = &world.Next(*chunk, normal); - next_pos -= normal * Chunk::Extent(); + next_pos -= normal * glm::vec3(Chunk::Extent()); } - mod_chunk->BlockAt(next_pos).type = world.BlockTypes()[place_id]; + mod_chunk->BlockAt(next_pos).type = place_id; mod_chunk->Invalidate(); } place = false; @@ -199,18 +183,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(); }