X-Git-Url: https://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fapp%2Fapp.cpp;h=59070a56311d01c7e129a852b717e9313431aae2;hb=7c2a8b8285278b8a3077b311d82f05ea0463a96e;hp=9e83cd416b229cb18fce4c4f40ec94b38d678c3f;hpb=55dbd6b35a39888f245e247d2e140f141f918178;p=blank.git diff --git a/src/app/app.cpp b/src/app/app.cpp index 9e83cd4..59070a5 100644 --- a/src/app/app.cpp +++ b/src/app/app.cpp @@ -1,6 +1,8 @@ #include "Application.hpp" #include "Assets.hpp" +#include "FrameCounter.hpp" +#include "../audio/Sound.hpp" #include "../graphics/Font.hpp" #include "../world/BlockType.hpp" #include "../world/Entity.hpp" @@ -26,27 +28,20 @@ string get_asset_path() { namespace blank { Application::Application(const Config &config) -: init_sdl() -, init_img() -, init_ttf() -, init_gl(config.doublebuf, config.multisampling) -, window() -, ctx(window.CreateContext()) -, init_glew() +: init(config.doublebuf, config.multisampling) +, viewport() , assets(get_asset_path()) -, chunk_prog() -, entity_prog() -, sprite_prog() -, cam() +, audio() +, counter() , world(config.world) -, interface(config.interface, assets, world) +, interface(config.interface, assets, audio, counter, world) , test_controller(MakeTestEntity(world)) , running(false) { - if (config.vsync) { - GLContext::EnableVSync(); - } + viewport.VSync(config.vsync); +} - glClearColor(0.0, 0.0, 0.0, 1.0); +Application::~Application() { + audio.StopAll(); } Entity &Application::MakeTestEntity(World &world) { @@ -92,7 +87,7 @@ void Application::RunS(size_t n, size_t t) { void Application::Run() { running = true; Uint32 last = SDL_GetTicks(); - window.GrabMouse(); + init.window.GrabMouse(); while (running) { Uint32 now = SDL_GetTicks(); int delta = now - last; @@ -102,13 +97,16 @@ void Application::Run() { } void Application::Loop(int dt) { + counter.EnterFrame(); HandleEvents(); Update(dt); Render(); + counter.ExitFrame(); } void Application::HandleEvents() { + counter.EnterHandle(); SDL_Event event; while (SDL_PollEvent(&event)) { switch (event.type) { @@ -140,48 +138,58 @@ void Application::HandleEvents() { break; } } + counter.ExitHandle(); } void Application::Handle(const SDL_WindowEvent &event) { switch (event.event) { case SDL_WINDOWEVENT_FOCUS_GAINED: - window.GrabMouse(); + init.window.GrabMouse(); break; case SDL_WINDOWEVENT_FOCUS_LOST: - window.ReleaseMouse(); + init.window.ReleaseMouse(); break; case SDL_WINDOWEVENT_RESIZED: - cam.Viewport(event.data1, event.data2); - interface.Handle(event); + viewport.Resize(event.data1, event.data2); break; default: - interface.Handle(event); break; } } void Application::Update(int dt) { + counter.EnterUpdate(); interface.Update(dt); test_controller.Update(dt); world.Update(dt); -} -void Application::Render() { - GLContext::Clear(); + glm::mat4 trans = world.Player().Transform(Chunk::Pos(0, 0, 0)); + glm::vec3 dir(trans * glm::vec4(0.0f, 0.0f, -1.0f, 0.0f)); + glm::vec3 up(trans * glm::vec4(0.0f, 1.0f, 0.0f, 0.0f)); + audio.Position(world.Player().Position()); + audio.Velocity(world.Player().Velocity()); + audio.Orientation(dir, up); - chunk_prog.SetProjection(cam.Projection()); - entity_prog.SetProjection(cam.Projection()); + counter.ExitUpdate(); +} - world.Render(chunk_prog, entity_prog); +void Application::Render() { + // gl implementation may (and will probably) delay vsync blocking until + // the first write after flipping, which is this clear call + viewport.Clear(); + counter.EnterRender(); - interface.Render(entity_prog, sprite_prog); + world.Render(viewport); + interface.Render(viewport); - window.Flip(); + counter.ExitRender(); + init.window.Flip(); } Assets::Assets(const string &base) -: fonts(base + "fonts/") { +: fonts(base + "fonts/") +, sounds(base + "sounds/") { } @@ -190,4 +198,94 @@ Font Assets::LoadFont(const string &name, int size) const { return Font(full.c_str(), size); } +Sound Assets::LoadSound(const string &name) const { + string full = sounds + name + ".wav"; + return Sound(full.c_str()); +} + + +void FrameCounter::EnterFrame() noexcept { + last_enter = SDL_GetTicks(); + last_tick = last_enter; +} + +void FrameCounter::EnterHandle() noexcept { + Tick(); +} + +void FrameCounter::ExitHandle() noexcept { + current.handle = Tick(); +} + +void FrameCounter::EnterUpdate() noexcept { + Tick(); +} + +void FrameCounter::ExitUpdate() noexcept { + current.update = Tick(); +} + +void FrameCounter::EnterRender() noexcept { + Tick(); +} + +void FrameCounter::ExitRender() noexcept { + current.render = Tick(); +} + +void FrameCounter::ExitFrame() noexcept { + Uint32 now = SDL_GetTicks(); + current.total = now - last_enter; + current.running = current.handle + current.update + current.render; + current.waiting = current.total - current.running; + Accumulate(); + + ++cur_frame; + if (cur_frame >= NUM_FRAMES) { + Push(); + cur_frame = 0; + changed = true; + } else { + changed = false; + } +} + +int FrameCounter::Tick() noexcept { + Uint32 now = SDL_GetTicks(); + int delta = now - last_tick; + last_tick = now; + return delta; +} + +void FrameCounter::Accumulate() noexcept { + sum.handle += current.handle; + sum.update += current.update; + sum.render += current.render; + sum.running += current.running; + sum.waiting += current.waiting; + sum.total += current.total; + + max.handle = std::max(current.handle, max.handle); + max.update = std::max(current.update, max.update); + max.render = std::max(current.render, max.render); + max.running = std::max(current.running, max.running); + max.waiting = std::max(current.waiting, max.waiting); + max.total = std::max(current.total, max.total); + + current = Frame(); +} + +void FrameCounter::Push() noexcept { + peak = max; + avg.handle = sum.handle * factor; + avg.update = sum.update * factor; + avg.render = sum.render * factor; + avg.running = sum.running * factor; + avg.waiting = sum.waiting * factor; + avg.total = sum.total * factor; + + sum = Frame(); + max = Frame(); +} + }