1 #include "Application.hpp"
3 #include "Environment.hpp"
4 #include "FrameCounter.hpp"
6 #include "StateControl.hpp"
9 #include "../audio/Sound.hpp"
10 #include "../graphics/ArrayTexture.hpp"
11 #include "../graphics/Font.hpp"
12 #include "../graphics/Texture.hpp"
13 #include "../world/BlockType.hpp"
14 #include "../world/Entity.hpp"
18 #include <SDL_image.h>
25 Application::Application(Environment &e)
31 Application::~Application() {
36 void Application::RunN(size_t n) {
37 Uint32 last = SDL_GetTicks();
38 for (size_t i = 0; HasState() && i < n; ++i) {
39 Uint32 now = SDL_GetTicks();
40 int delta = now - last;
46 void Application::RunT(size_t t) {
47 Uint32 last = SDL_GetTicks();
48 Uint32 finish = last + t;
49 while (HasState() && last < finish) {
50 Uint32 now = SDL_GetTicks();
51 int delta = now - last;
57 void Application::RunS(size_t n, size_t t) {
58 for (size_t i = 0; HasState() && i < n; ++i) {
64 void Application::Run() {
65 Uint32 last = SDL_GetTicks();
66 env.window.GrabMouse();
68 Uint32 now = SDL_GetTicks();
69 int delta = now - last;
75 void Application::Loop(int dt) {
76 env.counter.EnterFrame();
78 if (!HasState()) return;
80 env.state.Commit(*this);
81 if (!HasState()) return;
83 env.counter.ExitFrame();
87 void Application::HandleEvents() {
88 env.counter.EnterHandle();
90 while (HasState() && SDL_PollEvent(&event)) {
92 env.state.Commit(*this);
94 env.counter.ExitHandle();
97 void Application::Handle(const SDL_Event &event) {
100 Handle(event.window);
103 GetState().Handle(event);
108 void Application::Handle(const SDL_WindowEvent &event) {
109 switch (event.event) {
110 case SDL_WINDOWEVENT_FOCUS_GAINED:
111 env.window.GrabMouse();
113 case SDL_WINDOWEVENT_FOCUS_LOST:
114 env.window.ReleaseMouse();
116 case SDL_WINDOWEVENT_RESIZED:
117 env.viewport.Resize(event.data1, event.data2);
124 void Application::Update(int dt) {
125 env.counter.EnterUpdate();
127 GetState().Update(dt);
129 env.counter.ExitUpdate();
132 void Application::Render() {
133 // gl implementation may (and will probably) delay vsync blocking until
134 // the first write after flipping, which is this clear call
135 env.viewport.Clear();
136 env.counter.EnterRender();
139 GetState().Render(env.viewport);
142 env.counter.ExitRender();
147 void Application::PushState(State *s) {
148 if (!states.empty()) {
149 states.top()->OnPause();
153 if (s->ref_count == 1) {
159 State *Application::PopState() {
160 State *s = states.top();
164 if (!states.empty()) {
165 states.top()->OnResume();
170 State *Application::SwitchState(State *s_new) {
171 State *s_old = states.top();
172 states.top() = s_new;
176 if (s_old->ref_count == 0) {
179 if (s_new->ref_count == 1) {
186 State &Application::GetState() {
187 return *states.top();
190 bool Application::HasState() const noexcept {
191 return !states.empty();
195 void StateControl::Commit(Application &app) {
196 while (!cue.empty()) {
201 app.PushState(m.state);
204 app.SwitchState(m.state);
210 while (app.HasState()) {
219 Assets::Assets(const string &base)
220 : fonts(base + "fonts/")
221 , sounds(base + "sounds/")
222 , textures(base + "textures/")
223 , large_ui_font(LoadFont("DejaVuSans", 24))
224 , small_ui_font(LoadFont("DejaVuSans", 16)) {
228 Font Assets::LoadFont(const string &name, int size) const {
229 string full = fonts + name + ".ttf";
230 return Font(full.c_str(), size);
233 Sound Assets::LoadSound(const string &name) const {
234 string full = sounds + name + ".wav";
235 return Sound(full.c_str());
238 Texture Assets::LoadTexture(const string &name) const {
239 string full = textures + name + ".png";
241 SDL_Surface *srf = IMG_Load(full.c_str());
243 throw SDLError("IMG_Load");
247 SDL_FreeSurface(srf);
251 void Assets::LoadTexture(const string &name, ArrayTexture &tex, int layer) const {
252 string full = textures + name + ".png";
253 SDL_Surface *srf = IMG_Load(full.c_str());
255 throw SDLError("IMG_Load");
259 tex.Data(layer, *srf);
261 SDL_FreeSurface(srf);
264 SDL_FreeSurface(srf);
268 void FrameCounter::EnterFrame() noexcept {
269 last_enter = SDL_GetTicks();
270 last_tick = last_enter;
273 void FrameCounter::EnterHandle() noexcept {
277 void FrameCounter::ExitHandle() noexcept {
278 current.handle = Tick();
281 void FrameCounter::EnterUpdate() noexcept {
285 void FrameCounter::ExitUpdate() noexcept {
286 current.update = Tick();
289 void FrameCounter::EnterRender() noexcept {
293 void FrameCounter::ExitRender() noexcept {
294 current.render = Tick();
297 void FrameCounter::ExitFrame() noexcept {
298 Uint32 now = SDL_GetTicks();
299 current.total = now - last_enter;
300 current.running = current.handle + current.update + current.render;
301 current.waiting = current.total - current.running;
305 if (cur_frame >= NUM_FRAMES) {
314 int FrameCounter::Tick() noexcept {
315 Uint32 now = SDL_GetTicks();
316 int delta = now - last_tick;
321 void FrameCounter::Accumulate() noexcept {
322 sum.handle += current.handle;
323 sum.update += current.update;
324 sum.render += current.render;
325 sum.running += current.running;
326 sum.waiting += current.waiting;
327 sum.total += current.total;
329 max.handle = std::max(current.handle, max.handle);
330 max.update = std::max(current.update, max.update);
331 max.render = std::max(current.render, max.render);
332 max.running = std::max(current.running, max.running);
333 max.waiting = std::max(current.waiting, max.waiting);
334 max.total = std::max(current.total, max.total);
336 current = Frame<int>();
339 void FrameCounter::Push() noexcept {
341 avg.handle = sum.handle * factor;
342 avg.update = sum.update * factor;
343 avg.render = sum.render * factor;
344 avg.running = sum.running * factor;
345 avg.waiting = sum.waiting * factor;
346 avg.total = sum.total * factor;