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/") {
226 Font Assets::LoadFont(const string &name, int size) const {
227 string full = fonts + name + ".ttf";
228 return Font(full.c_str(), size);
231 Sound Assets::LoadSound(const string &name) const {
232 string full = sounds + name + ".wav";
233 return Sound(full.c_str());
236 Texture Assets::LoadTexture(const string &name) const {
237 string full = textures + name + ".png";
239 SDL_Surface *srf = IMG_Load(full.c_str());
241 throw SDLError("IMG_Load");
245 SDL_FreeSurface(srf);
249 void Assets::LoadTexture(const string &name, ArrayTexture &tex, int layer) const {
250 string full = textures + name + ".png";
251 SDL_Surface *srf = IMG_Load(full.c_str());
253 throw SDLError("IMG_Load");
257 tex.Data(layer, *srf);
259 SDL_FreeSurface(srf);
262 SDL_FreeSurface(srf);
266 void FrameCounter::EnterFrame() noexcept {
267 last_enter = SDL_GetTicks();
268 last_tick = last_enter;
271 void FrameCounter::EnterHandle() noexcept {
275 void FrameCounter::ExitHandle() noexcept {
276 current.handle = Tick();
279 void FrameCounter::EnterUpdate() noexcept {
283 void FrameCounter::ExitUpdate() noexcept {
284 current.update = Tick();
287 void FrameCounter::EnterRender() noexcept {
291 void FrameCounter::ExitRender() noexcept {
292 current.render = Tick();
295 void FrameCounter::ExitFrame() noexcept {
296 Uint32 now = SDL_GetTicks();
297 current.total = now - last_enter;
298 current.running = current.handle + current.update + current.render;
299 current.waiting = current.total - current.running;
303 if (cur_frame >= NUM_FRAMES) {
312 int FrameCounter::Tick() noexcept {
313 Uint32 now = SDL_GetTicks();
314 int delta = now - last_tick;
319 void FrameCounter::Accumulate() noexcept {
320 sum.handle += current.handle;
321 sum.update += current.update;
322 sum.render += current.render;
323 sum.running += current.running;
324 sum.waiting += current.waiting;
325 sum.total += current.total;
327 max.handle = std::max(current.handle, max.handle);
328 max.update = std::max(current.update, max.update);
329 max.render = std::max(current.render, max.render);
330 max.running = std::max(current.running, max.running);
331 max.waiting = std::max(current.waiting, max.waiting);
332 max.total = std::max(current.total, max.total);
334 current = Frame<int>();
337 void FrameCounter::Push() noexcept {
339 avg.handle = sum.handle * factor;
340 avg.update = sum.update * factor;
341 avg.render = sum.render * factor;
342 avg.running = sum.running * factor;
343 avg.waiting = sum.waiting * factor;
344 avg.total = sum.total * factor;