]> git.localhorst.tv Git - l2e.git/blobdiff - src/app/State.cpp
store an application handle in each state
[l2e.git] / src / app / State.cpp
index 50b5bf601bc52c7027ce7408cc9410f3e767815d..f1c7712b01a63101c004a47e32b6e6be23bf78d3 100644 (file)
@@ -7,19 +7,30 @@
 
 #include "State.h"
 
+#include <stdexcept>
+
+using std::domain_error;
+
 namespace app {
 
+State::State()
+: ctrl(0) {
+
+}
+
 State::~State() {
 
 }
 
 
-void State::EnterState(Application &ctrl, SDL_Surface *screen) {
-       OnEnterState(ctrl, screen);
+void State::EnterState(Application &c, SDL_Surface *screen) {
+       ctrl = &c;
+       OnEnterState(c, screen);
 }
 
-void State::ExitState(Application &ctrl, SDL_Surface *screen) {
-       OnExitState(ctrl, screen);
+void State::ExitState(Application &c, SDL_Surface *screen) {
+       OnExitState(c, screen);
+       ctrl = 0;
 }
 
 void State::ResumeState(Application &ctrl, SDL_Surface *screen) {
@@ -34,4 +45,21 @@ void State::Resize(int width, int height) {
        OnResize(width, height);
 }
 
+
+Application &State::Ctrl() {
+       if (ctrl) {
+               return *ctrl;
+       } else {
+               throw domain_error("call to app::State::Ctrl() without application context");
+       }
+}
+
+const Application &State::Ctrl() const {
+       if (ctrl) {
+               return *ctrl;
+       } else {
+               throw domain_error("call to app::State::Ctrl() without application context");
+       }
+}
+
 }