X-Git-Url: https://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fapp%2Fapp.cpp;h=d13dfc6aafe7c398a633de83d48bef3857b3993f;hb=0e3f96ecb9ade07a7b831078fee025aff44d44d4;hp=9e83cd416b229cb18fce4c4f40ec94b38d678c3f;hpb=55dbd6b35a39888f245e247d2e140f141f918178;p=blank.git diff --git a/src/app/app.cpp b/src/app/app.cpp index 9e83cd4..d13dfc6 100644 --- a/src/app/app.cpp +++ b/src/app/app.cpp @@ -1,5 +1,6 @@ #include "Application.hpp" #include "Assets.hpp" +#include "FrameCounter.hpp" #include "../graphics/Font.hpp" #include "../world/BlockType.hpp" @@ -26,27 +27,15 @@ 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() +, counter() , world(config.world) -, interface(config.interface, assets, world) +, interface(config.interface, assets, counter, world) , test_controller(MakeTestEntity(world)) , running(false) { - if (config.vsync) { - GLContext::EnableVSync(); - } - - glClearColor(0.0, 0.0, 0.0, 1.0); + viewport.VSync(config.vsync); } Entity &Application::MakeTestEntity(World &world) { @@ -92,7 +81,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 +91,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,43 +132,44 @@ 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); + counter.ExitUpdate(); } void Application::Render() { - GLContext::Clear(); - - chunk_prog.SetProjection(cam.Projection()); - entity_prog.SetProjection(cam.Projection()); - - world.Render(chunk_prog, entity_prog); + // 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(); } @@ -190,4 +183,89 @@ Font Assets::LoadFont(const string &name, int size) const { return Font(full.c_str(), size); } + +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(); +} + }