X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fapp%2Fapp.cpp;h=8577e45b48383ded5ef5d793b27a152cf424af42;hb=1bc6f085c53cdeaa08e2c00e821d4e2e25cae1c8;hp=9e83cd416b229cb18fce4c4f40ec94b38d678c3f;hpb=55dbd6b35a39888f245e247d2e140f141f918178;p=blank.git diff --git a/src/app/app.cpp b/src/app/app.cpp index 9e83cd4..8577e45 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,45 @@ 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); + interface.Resize(viewport); 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 +184,68 @@ 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 { + running.handle += Tick(); +} + +void FrameCounter::EnterUpdate() noexcept { + Tick(); +} + +void FrameCounter::ExitUpdate() noexcept { + running.update += Tick(); +} + +void FrameCounter::EnterRender() noexcept { + Tick(); +} + +void FrameCounter::ExitRender() noexcept { + running.render += Tick(); +} + +void FrameCounter::ExitFrame() noexcept { + Uint32 now = SDL_GetTicks(); + running.total += now - last_enter; + ++cur_frame; + if (cur_frame >= NUM_FRAMES) { + avg.handle = running.handle * factor; + avg.update = running.update * factor; + avg.render = running.render * factor; + avg.total = running.total * factor; + running = Frame{}; + 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::Print(std::ostream &out) const { + out << "frame: " << AvgFrame() << std::endl; + out << " handle: " << AvgHandle() << std::endl; + out << " update: " << AvgUpdate() << std::endl; + out << " render: " << AvgRender() << std::endl; + out << " running: " << AvgRunning() << std::endl; + out << " waiting: " << AvgWaiting() << std::endl; + out << std::endl; +} + }