]> git.localhorst.tv Git - l2e.git/commitdiff
added basic application class
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Sun, 5 Aug 2012 13:19:11 +0000 (15:19 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Sun, 5 Aug 2012 13:19:11 +0000 (15:19 +0200)
Debug/makefile
Debug/sources.mk
Debug/src/app/subdir.mk [new file with mode: 0644]
Release/makefile
Release/sources.mk
Release/src/app/subdir.mk [new file with mode: 0644]
src/app/Application.cpp [new file with mode: 0644]
src/app/Application.h [new file with mode: 0644]
src/app/State.h [new file with mode: 0644]

index 253ef222bb3c6a8816699f62b04761f288f7bb07..3f180390f2dbc5771d3608ef73c631ce787d0751 100644 (file)
@@ -10,6 +10,7 @@ RM := rm -rf
 -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
index 8a951acbb83854324a8f740d93719764d37743c5..92d6efe084fb4ab72e20274484a24d8569fde56d 100644 (file)
@@ -26,4 +26,5 @@ SUBDIRS := \
 src/sdl \
 src \
 src/battle \
+src/app \
 
diff --git a/Debug/src/app/subdir.mk b/Debug/src/app/subdir.mk
new file mode 100644 (file)
index 0000000..76160a8
--- /dev/null
@@ -0,0 +1,24 @@
+################################################################################
+# 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 ' '
+
+
index 253ef222bb3c6a8816699f62b04761f288f7bb07..3f180390f2dbc5771d3608ef73c631ce787d0751 100644 (file)
@@ -10,6 +10,7 @@ RM := rm -rf
 -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
index 8a951acbb83854324a8f740d93719764d37743c5..92d6efe084fb4ab72e20274484a24d8569fde56d 100644 (file)
@@ -26,4 +26,5 @@ SUBDIRS := \
 src/sdl \
 src \
 src/battle \
+src/app \
 
diff --git a/Release/src/app/subdir.mk b/Release/src/app/subdir.mk
new file mode 100644 (file)
index 0000000..555eb00
--- /dev/null
@@ -0,0 +1,24 @@
+################################################################################
+# 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 ' '
+
+
diff --git a/src/app/Application.cpp b/src/app/Application.cpp
new file mode 100644 (file)
index 0000000..37f3135
--- /dev/null
@@ -0,0 +1,117 @@
+/*
+ * 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);
+}
+
+}
diff --git a/src/app/Application.h b/src/app/Application.h
new file mode 100644 (file)
index 0000000..e43faf4
--- /dev/null
@@ -0,0 +1,60 @@
+/*
+ * 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_ */
diff --git a/src/app/State.h b/src/app/State.h
new file mode 100644 (file)
index 0000000..471b44b
--- /dev/null
@@ -0,0 +1,36 @@
+/*
+ * 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_ */