From 59d528aaa84a2210b0a357887853f534cfbea156 Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Sun, 5 Aug 2012 15:19:11 +0200 Subject: [PATCH 1/1] added basic application class --- Debug/makefile | 1 + Debug/sources.mk | 1 + Debug/src/app/subdir.mk | 24 ++++++++ Release/makefile | 1 + Release/sources.mk | 1 + Release/src/app/subdir.mk | 24 ++++++++ src/app/Application.cpp | 117 ++++++++++++++++++++++++++++++++++++++ src/app/Application.h | 60 +++++++++++++++++++ src/app/State.h | 36 ++++++++++++ 9 files changed, 265 insertions(+) create mode 100644 Debug/src/app/subdir.mk create mode 100644 Release/src/app/subdir.mk create mode 100644 src/app/Application.cpp create mode 100644 src/app/Application.h create mode 100644 src/app/State.h diff --git a/Debug/makefile b/Debug/makefile index 253ef22..3f18039 100644 --- a/Debug/makefile +++ b/Debug/makefile @@ -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 diff --git a/Debug/sources.mk b/Debug/sources.mk index 8a951ac..92d6efe 100644 --- a/Debug/sources.mk +++ b/Debug/sources.mk @@ -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 index 0000000..76160a8 --- /dev/null +++ b/Debug/src/app/subdir.mk @@ -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 ' ' + + diff --git a/Release/makefile b/Release/makefile index 253ef22..3f18039 100644 --- a/Release/makefile +++ b/Release/makefile @@ -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 diff --git a/Release/sources.mk b/Release/sources.mk index 8a951ac..92d6efe 100644 --- a/Release/sources.mk +++ b/Release/sources.mk @@ -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 index 0000000..555eb00 --- /dev/null +++ b/Release/src/app/subdir.mk @@ -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 index 0000000..37f3135 --- /dev/null +++ b/src/app/Application.cpp @@ -0,0 +1,117 @@ +/* + * Application.cpp + * + * Created on: Apr 8, 2012 + * Author: holy + */ + +#include "Application.h" + +#include "State.h" + +#include + +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 index 0000000..e43faf4 --- /dev/null +++ b/src/app/Application.h @@ -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 +#include + + +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 states; + Uint32 last; + +}; + +} + +#endif /* APP_APPLICATION_H_ */ diff --git a/src/app/State.h b/src/app/State.h new file mode 100644 index 0000000..471b44b --- /dev/null +++ b/src/app/State.h @@ -0,0 +1,36 @@ +/* + * State.h + * + * Created on: Apr 8, 2012 + * Author: holy + */ + +#ifndef APP_APPLICATIONSTATE_H_ +#define APP_APPLICATIONSTATE_H_ + +#include + +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_ */ -- 2.39.2