]> git.localhorst.tv Git - blank.git/blobdiff - src/app/app.cpp
also show peak in frame counter
[blank.git] / src / app / app.cpp
index 5c068a45bc15419fa9be74a5c9af36c8d51607f1..d13dfc6aafe7c398a633de83d48bef3857b3993f 100644 (file)
@@ -27,28 +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())
 , counter()
-, chunk_prog()
-, entity_prog()
-, sprite_prog()
-, cam()
 , 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) {
@@ -94,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;
@@ -151,17 +138,15 @@ void Application::HandleEvents() {
 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;
        }
 }
@@ -177,18 +162,14 @@ void Application::Update(int dt) {
 void Application::Render() {
        // gl implementation may (and will probably) delay vsync blocking until
        // the first write after flipping, which is this clear call
-       GLContext::Clear();
+       viewport.Clear();
        counter.EnterRender();
 
-       chunk_prog.SetProjection(cam.Projection());
-       entity_prog.SetProjection(cam.Projection());
-
-       world.Render(chunk_prog, entity_prog);
-
-       interface.Render(entity_prog, sprite_prog);
+       world.Render(viewport);
+       interface.Render(viewport);
 
        counter.ExitRender();
-       window.Flip();
+       init.window.Flip();
 }
 
 
@@ -213,7 +194,7 @@ void FrameCounter::EnterHandle() noexcept {
 }
 
 void FrameCounter::ExitHandle() noexcept {
-       running.handle += Tick();
+       current.handle = Tick();
 }
 
 void FrameCounter::EnterUpdate() noexcept {
@@ -221,7 +202,7 @@ void FrameCounter::EnterUpdate() noexcept {
 }
 
 void FrameCounter::ExitUpdate() noexcept {
-       running.update += Tick();
+       current.update = Tick();
 }
 
 void FrameCounter::EnterRender() noexcept {
@@ -229,19 +210,19 @@ void FrameCounter::EnterRender() noexcept {
 }
 
 void FrameCounter::ExitRender() noexcept {
-       running.render += Tick();
+       current.render = Tick();
 }
 
 void FrameCounter::ExitFrame() noexcept {
        Uint32 now = SDL_GetTicks();
-       running.total += now - last_enter;
+       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) {
-               avg.handle = running.handle * factor;
-               avg.update = running.update * factor;
-               avg.render = running.render * factor;
-               avg.total = running.total * factor;
-               running = Frame<int>{};
+               Push();
                cur_frame = 0;
                changed = true;
        } else {
@@ -256,14 +237,35 @@ int FrameCounter::Tick() noexcept {
        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;
+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<int>();
+}
+
+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<int>();
+       max = Frame<int>();
 }
 
 }