-include sources.mk
-include src/sdl/subdir.mk
-include src/battle/subdir.mk
+-include src/app/subdir.mk
-include src/subdir.mk
-include subdir.mk
-include objects.mk
src/sdl \
src \
src/battle \
+src/app \
--- /dev/null
+################################################################################
+# Automatically-generated file. Do not edit!
+################################################################################
+
+# Add inputs and outputs from these tool invocations to the build variables
+CPP_SRCS += \
+../src/app/Application.cpp
+
+OBJS += \
+./src/app/Application.o
+
+CPP_DEPS += \
+./src/app/Application.d
+
+
+# Each subdirectory must supply rules for building sources it contributes
+src/app/%.o: ../src/app/%.cpp
+ @echo 'Building file: $<'
+ @echo 'Invoking: GCC C++ Compiler'
+ g++ -I/usr/include/SDL -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" -o "$@" "$<"
+ @echo 'Finished building: $<'
+ @echo ' '
+
+
-include sources.mk
-include src/sdl/subdir.mk
-include src/battle/subdir.mk
+-include src/app/subdir.mk
-include src/subdir.mk
-include subdir.mk
-include objects.mk
src/sdl \
src \
src/battle \
+src/app \
--- /dev/null
+################################################################################
+# Automatically-generated file. Do not edit!
+################################################################################
+
+# Add inputs and outputs from these tool invocations to the build variables
+CPP_SRCS += \
+../src/app/Application.cpp
+
+OBJS += \
+./src/app/Application.o
+
+CPP_DEPS += \
+./src/app/Application.d
+
+
+# Each subdirectory must supply rules for building sources it contributes
+src/app/%.o: ../src/app/%.cpp
+ @echo 'Building file: $<'
+ @echo 'Invoking: GCC C++ Compiler'
+ g++ -I/usr/include/SDL -O3 -Wall -c -fmessage-length=0 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" -o "$@" "$<"
+ @echo 'Finished building: $<'
+ @echo ' '
+
+
--- /dev/null
+/*
+ * Application.cpp
+ *
+ * Created on: Apr 8, 2012
+ * Author: holy
+ */
+
+#include "Application.h"
+
+#include "State.h"
+
+#include <cassert>
+
+namespace app {
+
+Application::Application(SDL_Surface *screen, State *initialState)
+: screen(screen)
+, states()
+, last(SDL_GetTicks()) {
+ assert(screen && "cannot create application without screen");
+ assert(initialState && "cannot create application without initial state");
+ RealPushState(initialState);
+}
+
+Application::~Application(void) {
+ PopAllStates();
+}
+
+
+State *Application::CurrentState(void) {
+ return states.top();
+}
+
+void Application::ChangeState(State *s) {
+ RealPopState();
+ RealPushState(s);
+}
+
+void Application::PushState(State *s) {
+ RealPushState(s);
+}
+
+void Application::RealPushState(State *s) {
+ states.push(s);
+ s->EnterState(*this, screen);
+}
+
+void Application::PopState(void) {
+ RealPopState();
+}
+
+void Application::RealPopState(void) {
+ if (states.empty()) return;
+ states.top()->ExitState();
+ delete states.top();
+ states.pop();
+}
+
+void Application::Quit(void) {
+ PopAllStates();
+}
+
+void Application::PopAllStates(void) {
+ while (!states.empty()) {
+ RealPopState();
+ }
+}
+
+
+void Application::Run(void) {
+ while (CurrentState()) {
+ Loop();
+ }
+}
+
+void Application::Loop(void) {
+ Uint32 now(SDL_GetTicks());
+ Uint32 deltaT(now - last);
+ if (deltaT > 34) deltaT = 34;
+
+ HandleEvents();
+ UpdateWorld(deltaT);
+ Render();
+
+ last = now;
+}
+
+
+void Application::HandleEvents(void) {
+ if (!CurrentState()) return;
+ SDL_Event event;
+ while (SDL_PollEvent(&event)) {
+ switch (event.type) {
+ case SDL_QUIT:
+ PopAllStates();
+ break;
+ default:
+ CurrentState()->HandleEvent(event);
+ break;
+ }
+ }
+}
+
+void Application::UpdateWorld(Uint32 deltaT) {
+ if (!CurrentState()) return;
+ for (Uint32 i(0); i < deltaT; ++i) {
+ CurrentState()->UpdateWorld(0.001f);
+ }
+}
+
+void Application::Render(void) {
+ if (!CurrentState()) return;
+ CurrentState()->Render(screen);
+ SDL_Flip(screen);
+}
+
+}
--- /dev/null
+/*
+ * Application.h
+ *
+ * Created on: Apr 8, 2012
+ * Author: holy
+ */
+
+#ifndef APP_APPLICATION_H_
+#define APP_APPLICATION_H_
+
+#include "../sdl/InitScreen.h"
+
+#include <stack>
+#include <SDL/SDL.h>
+
+
+namespace app {
+
+class State;
+
+class Application {
+
+public:
+ explicit Application(SDL_Surface *screen, State *initialState);
+ ~Application(void);
+private:
+ Application(const Application &);
+ Application &operator =(const Application &);
+
+public:
+ void Run(void);
+ void Loop(void);
+
+public:
+ void ChangeState(State *);
+ void PushState(State *);
+ void PopState(void);
+ void Quit(void);
+
+private:
+ State *CurrentState(void);
+ void RealPushState(State *);
+ void RealPopState(void);
+ void PopAllStates(void);
+
+private:
+ void HandleEvents(void);
+ void UpdateWorld(Uint32 deltaT);
+ void Render(void);
+
+private:
+ SDL_Surface *screen;
+ std::stack<State *> states;
+ Uint32 last;
+
+};
+
+}
+
+#endif /* APP_APPLICATION_H_ */
--- /dev/null
+/*
+ * State.h
+ *
+ * Created on: Apr 8, 2012
+ * Author: holy
+ */
+
+#ifndef APP_APPLICATIONSTATE_H_
+#define APP_APPLICATIONSTATE_H_
+
+#include <SDL/SDL.h>
+
+namespace app {
+
+class Application;
+
+class State {
+
+public:
+ virtual ~State() { };
+
+public:
+ /// do some setup
+ /// @param ctrl the Application running the state
+ virtual void EnterState(Application &ctrl, SDL_Surface *screen) = 0;
+ virtual void ExitState() = 0;
+
+ virtual void HandleEvent(const SDL_Event &) = 0;
+ virtual void UpdateWorld(float deltaT) = 0;
+ virtual void Render(SDL_Surface *) = 0;
+
+};
+
+}
+
+#endif /* APP_STATE_H_ */