#include "Application.hpp"
#include "Assets.hpp"
+#include "FrameCounter.hpp"
#include "../graphics/Font.hpp"
#include "../world/BlockType.hpp"
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) {
void Application::Run() {
running = true;
Uint32 last = SDL_GetTicks();
- window.GrabMouse();
+ init.window.GrabMouse();
while (running) {
Uint32 now = SDL_GetTicks();
int delta = now - last;
}
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) {
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();
}
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<int>{};
+ 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;
+}
+
}