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) {
151 State *Application::PopState() {
152 State *s = states.top();
157 State *Application::SwitchState(State *s_new) {
158 State *s_old = states.top();
159 states.top() = s_new;
163 State &Application::GetState() {
164 return *states.top();
167 bool Application::HasState() const noexcept {
168 return !states.empty();
172 void StateControl::Commit(Application &app) {
173 while (!cue.empty()) {
178 app.PushState(m.state);
181 app.SwitchState(m.state);
187 while (app.HasState()) {
196 Assets::Assets(const string &base)
197 : fonts(base + "fonts/")
198 , sounds(base + "sounds/")
199 , textures(base + "textures/") {
203 Font Assets::LoadFont(const string &name, int size) const {
204 string full = fonts + name + ".ttf";
205 return Font(full.c_str(), size);
208 Sound Assets::LoadSound(const string &name) const {
209 string full = sounds + name + ".wav";
210 return Sound(full.c_str());
213 Texture Assets::LoadTexture(const string &name) const {
214 string full = textures + name + ".png";
216 SDL_Surface *srf = IMG_Load(full.c_str());
218 throw SDLError("IMG_Load");
222 SDL_FreeSurface(srf);
226 void Assets::LoadTexture(const string &name, ArrayTexture &tex, int layer) const {
227 string full = textures + name + ".png";
228 SDL_Surface *srf = IMG_Load(full.c_str());
230 throw SDLError("IMG_Load");
234 tex.Data(layer, *srf);
236 SDL_FreeSurface(srf);
239 SDL_FreeSurface(srf);
243 void FrameCounter::EnterFrame() noexcept {
244 last_enter = SDL_GetTicks();
245 last_tick = last_enter;
248 void FrameCounter::EnterHandle() noexcept {
252 void FrameCounter::ExitHandle() noexcept {
253 current.handle = Tick();
256 void FrameCounter::EnterUpdate() noexcept {
260 void FrameCounter::ExitUpdate() noexcept {
261 current.update = Tick();
264 void FrameCounter::EnterRender() noexcept {
268 void FrameCounter::ExitRender() noexcept {
269 current.render = Tick();
272 void FrameCounter::ExitFrame() noexcept {
273 Uint32 now = SDL_GetTicks();
274 current.total = now - last_enter;
275 current.running = current.handle + current.update + current.render;
276 current.waiting = current.total - current.running;
280 if (cur_frame >= NUM_FRAMES) {
289 int FrameCounter::Tick() noexcept {
290 Uint32 now = SDL_GetTicks();
291 int delta = now - last_tick;
296 void FrameCounter::Accumulate() noexcept {
297 sum.handle += current.handle;
298 sum.update += current.update;
299 sum.render += current.render;
300 sum.running += current.running;
301 sum.waiting += current.waiting;
302 sum.total += current.total;
304 max.handle = std::max(current.handle, max.handle);
305 max.update = std::max(current.update, max.update);
306 max.render = std::max(current.render, max.render);
307 max.running = std::max(current.running, max.running);
308 max.waiting = std::max(current.waiting, max.waiting);
309 max.total = std::max(current.total, max.total);
311 current = Frame<int>();
314 void FrameCounter::Push() noexcept {
316 avg.handle = sum.handle * factor;
317 avg.update = sum.update * factor;
318 avg.render = sum.render * factor;
319 avg.running = sum.running * factor;
320 avg.waiting = sum.waiting * factor;
321 avg.total = sum.total * factor;