1 #include "Application.hpp"
3 #include "Environment.hpp"
4 #include "FrameCounter.hpp"
6 #include "StateControl.hpp"
7 #include "TextureIndex.hpp"
10 #include "../audio/Sound.hpp"
11 #include "../graphics/ArrayTexture.hpp"
12 #include "../graphics/Font.hpp"
13 #include "../graphics/Texture.hpp"
14 #include "../io/TokenStreamReader.hpp"
15 #include "../model/shapes.hpp"
16 #include "../world/BlockType.hpp"
17 #include "../world/BlockTypeRegistry.hpp"
18 #include "../world/Entity.hpp"
23 #include <SDL_image.h>
25 using std::runtime_error;
31 Application::Application(Environment &e)
37 Application::~Application() {
42 void Application::RunN(size_t n) {
43 Uint32 last = SDL_GetTicks();
44 for (size_t i = 0; HasState() && i < n; ++i) {
45 Uint32 now = SDL_GetTicks();
46 int delta = now - last;
52 void Application::RunT(size_t t) {
53 Uint32 last = SDL_GetTicks();
54 Uint32 finish = last + t;
55 while (HasState() && last < finish) {
56 Uint32 now = SDL_GetTicks();
57 int delta = now - last;
63 void Application::RunS(size_t n, size_t t) {
64 for (size_t i = 0; HasState() && i < n; ++i) {
70 void Application::Run() {
71 Uint32 last = SDL_GetTicks();
72 env.window.GrabMouse();
74 Uint32 now = SDL_GetTicks();
75 int delta = now - last;
81 void Application::Loop(int dt) {
82 env.counter.EnterFrame();
84 if (!HasState()) return;
86 env.state.Commit(*this);
87 if (!HasState()) return;
89 env.counter.ExitFrame();
93 void Application::HandleEvents() {
94 env.counter.EnterHandle();
96 while (HasState() && SDL_PollEvent(&event)) {
98 env.state.Commit(*this);
100 env.counter.ExitHandle();
103 void Application::Handle(const SDL_Event &event) {
104 switch (event.type) {
105 case SDL_WINDOWEVENT:
106 Handle(event.window);
109 GetState().Handle(event);
114 void Application::Handle(const SDL_WindowEvent &event) {
115 switch (event.event) {
116 case SDL_WINDOWEVENT_FOCUS_GAINED:
117 env.window.GrabMouse();
119 case SDL_WINDOWEVENT_FOCUS_LOST:
120 env.window.ReleaseMouse();
122 case SDL_WINDOWEVENT_RESIZED:
123 env.viewport.Resize(event.data1, event.data2);
130 void Application::Update(int dt) {
131 env.counter.EnterUpdate();
133 GetState().Update(dt);
135 env.counter.ExitUpdate();
138 void Application::Render() {
139 // gl implementation may (and will probably) delay vsync blocking until
140 // the first write after flipping, which is this clear call
141 env.viewport.Clear();
142 env.counter.EnterRender();
145 GetState().Render(env.viewport);
148 env.counter.ExitRender();
153 void Application::PushState(State *s) {
154 if (!states.empty()) {
155 states.top()->OnPause();
159 if (s->ref_count == 1) {
165 State *Application::PopState() {
166 State *s = states.top();
170 if (!states.empty()) {
171 states.top()->OnResume();
176 State *Application::SwitchState(State *s_new) {
177 State *s_old = states.top();
178 states.top() = s_new;
182 if (s_old->ref_count == 0) {
185 if (s_new->ref_count == 1) {
192 State &Application::GetState() {
193 return *states.top();
196 bool Application::HasState() const noexcept {
197 return !states.empty();
201 void StateControl::Commit(Application &app) {
202 while (!cue.empty()) {
207 app.PushState(m.state);
210 app.SwitchState(m.state);
216 while (app.HasState()) {
225 Assets::Assets(const string &base)
226 : fonts(base + "fonts/")
227 , sounds(base + "sounds/")
228 , textures(base + "textures/")
229 , data(base + "data/")
230 , large_ui_font(LoadFont("DejaVuSans", 24))
231 , small_ui_font(LoadFont("DejaVuSans", 16)) {
237 CuboidShape block_shape({{ -0.5f, -0.5f, -0.5f }, { 0.5f, 0.5f, 0.5f }});
238 StairShape stair_shape({{ -0.5f, -0.5f, -0.5f }, { 0.5f, 0.5f, 0.5f }}, { 0.0f, 0.0f });
239 CuboidShape slab_shape({{ -0.5f, -0.5f, -0.5f }, { 0.5f, 0.0f, 0.5f }});
243 void Assets::LoadBlockTypes(const std::string &set_name, BlockTypeRegistry ®, TextureIndex &tex_index) const {
244 string full = data + set_name + ".types";
245 std::ifstream file(full);
247 throw std::runtime_error("failed to open block type file " + full);
249 TokenStreamReader in(file);
254 while (in.HasMore()) {
255 in.ReadIdentifier(type_name);
256 in.Skip(Token::EQUALS);
260 in.Skip(Token::ANGLE_BRACKET_OPEN);
261 while (in.Peek().type != Token::ANGLE_BRACKET_CLOSE) {
262 in.ReadIdentifier(name);
263 in.Skip(Token::EQUALS);
264 if (name == "visible") {
265 type.visible = in.GetBool();
266 } else if (name == "texture") {
267 in.ReadString(tex_name);
268 type.texture = tex_index.GetID(tex_name);
269 } else if (name == "color") {
270 in.ReadVec(type.color);
271 } else if (name == "label") {
272 in.ReadString(type.label);
273 } else if (name == "luminosity") {
274 type.luminosity = in.GetInt();
275 } else if (name == "block_light") {
276 type.block_light = in.GetBool();
277 } else if (name == "collision") {
278 type.collision = in.GetBool();
279 } else if (name == "collide_block") {
280 type.collide_block = in.GetBool();
281 } else if (name == "shape") {
282 in.ReadIdentifier(shape_name);
283 if (shape_name == "block") {
284 type.shape = &block_shape;
285 type.fill = { true, true, true, true, true, true };
286 } else if (shape_name == "slab") {
287 type.shape = &slab_shape;
288 type.fill = { false, true, false, false, false, false };
289 } else if (shape_name == "stair") {
290 type.shape = &stair_shape;
291 type.fill = { false, true, false, false, false, true };
293 throw runtime_error("unknown block shape: " + shape_name);
296 throw runtime_error("unknown block property: " + name);
298 in.Skip(Token::SEMICOLON);
300 in.Skip(Token::ANGLE_BRACKET_CLOSE);
301 in.Skip(Token::SEMICOLON);
307 Font Assets::LoadFont(const string &name, int size) const {
308 string full = fonts + name + ".ttf";
309 return Font(full.c_str(), size);
312 Sound Assets::LoadSound(const string &name) const {
313 string full = sounds + name + ".wav";
314 return Sound(full.c_str());
317 Texture Assets::LoadTexture(const string &name) const {
318 string full = textures + name + ".png";
320 SDL_Surface *srf = IMG_Load(full.c_str());
322 throw SDLError("IMG_Load");
326 SDL_FreeSurface(srf);
330 void Assets::LoadTexture(const string &name, ArrayTexture &tex, int layer) const {
331 string full = textures + name + ".png";
332 SDL_Surface *srf = IMG_Load(full.c_str());
334 throw SDLError("IMG_Load");
338 tex.Data(layer, *srf);
340 SDL_FreeSurface(srf);
343 SDL_FreeSurface(srf);
346 void Assets::LoadTextures(const TextureIndex &index, ArrayTexture &tex) const {
347 // TODO: where the hell should that size come from?
348 tex.Reserve(16, 16, index.Size(), Format());
349 for (const auto &entry : index.Entries()) {
350 LoadTexture(entry.first, tex, entry.second);
355 TextureIndex::TextureIndex()
360 int TextureIndex::GetID(const string &name) {
361 auto entry = id_map.find(name);
362 if (entry == id_map.end()) {
363 auto result = id_map.emplace(name, Size());
364 return result.first->second;
366 return entry->second;
371 void FrameCounter::EnterFrame() noexcept {
372 last_enter = SDL_GetTicks();
373 last_tick = last_enter;
376 void FrameCounter::EnterHandle() noexcept {
380 void FrameCounter::ExitHandle() noexcept {
381 current.handle = Tick();
384 void FrameCounter::EnterUpdate() noexcept {
388 void FrameCounter::ExitUpdate() noexcept {
389 current.update = Tick();
392 void FrameCounter::EnterRender() noexcept {
396 void FrameCounter::ExitRender() noexcept {
397 current.render = Tick();
400 void FrameCounter::ExitFrame() noexcept {
401 Uint32 now = SDL_GetTicks();
402 current.total = now - last_enter;
403 current.running = current.handle + current.update + current.render;
404 current.waiting = current.total - current.running;
408 if (cur_frame >= NUM_FRAMES) {
417 int FrameCounter::Tick() noexcept {
418 Uint32 now = SDL_GetTicks();
419 int delta = now - last_tick;
424 void FrameCounter::Accumulate() noexcept {
425 sum.handle += current.handle;
426 sum.update += current.update;
427 sum.render += current.render;
428 sum.running += current.running;
429 sum.waiting += current.waiting;
430 sum.total += current.total;
432 max.handle = std::max(current.handle, max.handle);
433 max.update = std::max(current.update, max.update);
434 max.render = std::max(current.render, max.render);
435 max.running = std::max(current.running, max.running);
436 max.waiting = std::max(current.waiting, max.waiting);
437 max.total = std::max(current.total, max.total);
439 current = Frame<int>();
442 void FrameCounter::Push() noexcept {
444 avg.handle = sum.handle * factor;
445 avg.update = sum.update * factor;
446 avg.render = sum.render * factor;
447 avg.running = sum.running * factor;
448 avg.waiting = sum.waiting * factor;
449 avg.total = sum.total * factor;