]> git.localhorst.tv Git - blank.git/blobdiff - src/app/app.cpp
enhanced application state control
[blank.git] / src / app / app.cpp
index 420dee70aad191c042ac90d719cc01caa70c5fa0..880dc7118eaf2fcaa43e93b2a2e92c6ec91de5f1 100644 (file)
@@ -7,12 +7,15 @@
 
 #include "init.hpp"
 #include "../audio/Sound.hpp"
+#include "../graphics/ArrayTexture.hpp"
 #include "../graphics/Font.hpp"
+#include "../graphics/Texture.hpp"
 #include "../world/BlockType.hpp"
 #include "../world/Entity.hpp"
 
 #include <iostream>
 #include <stdexcept>
+#include <SDL_image.h>
 
 using std::string;
 
@@ -93,9 +96,6 @@ void Application::HandleEvents() {
 
 void Application::Handle(const SDL_Event &event) {
        switch (event.type) {
-               case SDL_QUIT:
-                       env.state.PopAll();
-                       break;
                case SDL_WINDOWEVENT:
                        Handle(event.window);
                        break;
@@ -145,18 +145,41 @@ void Application::Render() {
 
 
 void Application::PushState(State *s) {
+       if (!states.empty()) {
+               states.top()->OnPause();
+       }
        states.emplace(s);
+       ++s->ref_count;
+       if (s->ref_count == 1) {
+               s->OnEnter();
+       }
+       s->OnResume();
 }
 
 State *Application::PopState() {
        State *s = states.top();
        states.pop();
+       s->OnPause();
+       s->OnExit();
+       if (!states.empty()) {
+               states.top()->OnResume();
+       }
        return s;
 }
 
 State *Application::SwitchState(State *s_new) {
        State *s_old = states.top();
        states.top() = s_new;
+       --s_old->ref_count;
+       ++s_new->ref_count;
+       s_old->OnPause();
+       if (s_old->ref_count == 0) {
+               s_old->OnExit();
+       }
+       if (s_new->ref_count == 1) {
+               s_new->OnEnter();
+       }
+       s_new->OnResume();
        return s_old;
 }
 
@@ -195,7 +218,8 @@ void StateControl::Commit(Application &app) {
 
 Assets::Assets(const string &base)
 : fonts(base + "fonts/")
-, sounds(base + "sounds/") {
+, sounds(base + "sounds/")
+, textures(base + "textures/") {
 
 }
 
@@ -209,6 +233,35 @@ Sound Assets::LoadSound(const string &name) const {
        return Sound(full.c_str());
 }
 
+Texture Assets::LoadTexture(const string &name) const {
+       string full = textures + name + ".png";
+       Texture tex;
+       SDL_Surface *srf = IMG_Load(full.c_str());
+       if (!srf) {
+               throw SDLError("IMG_Load");
+       }
+       tex.Bind();
+       tex.Data(*srf);
+       SDL_FreeSurface(srf);
+       return tex;
+}
+
+void Assets::LoadTexture(const string &name, ArrayTexture &tex, int layer) const {
+       string full = textures + name + ".png";
+       SDL_Surface *srf = IMG_Load(full.c_str());
+       if (!srf) {
+               throw SDLError("IMG_Load");
+       }
+       tex.Bind();
+       try {
+               tex.Data(layer, *srf);
+       } catch (...) {
+               SDL_FreeSurface(srf);
+               throw;
+       }
+       SDL_FreeSurface(srf);
+}
+
 
 void FrameCounter::EnterFrame() noexcept {
        last_enter = SDL_GetTicks();