From f7555575e8f2f8b9dbf740628f66e34ff93d8390 Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Sun, 7 Oct 2012 15:49:42 +0200 Subject: [PATCH] added color fading state this can fade a state to and from a solid color --- Debug/src/graphics/subdir.mk | 3 ++ Release/src/graphics/subdir.mk | 3 ++ src/graphics/ColorFade.cpp | 95 ++++++++++++++++++++++++++++++++++ src/graphics/ColorFade.h | 55 ++++++++++++++++++++ 4 files changed, 156 insertions(+) create mode 100644 src/graphics/ColorFade.cpp create mode 100644 src/graphics/ColorFade.h diff --git a/Debug/src/graphics/subdir.mk b/Debug/src/graphics/subdir.mk index 1f5268e..23f43a8 100644 --- a/Debug/src/graphics/subdir.mk +++ b/Debug/src/graphics/subdir.mk @@ -6,6 +6,7 @@ CPP_SRCS += \ ../src/graphics/Animation.cpp \ ../src/graphics/Camera.cpp \ +../src/graphics/ColorFade.cpp \ ../src/graphics/ComplexAnimation.cpp \ ../src/graphics/Font.cpp \ ../src/graphics/Frame.cpp \ @@ -17,6 +18,7 @@ CPP_SRCS += \ OBJS += \ ./src/graphics/Animation.o \ ./src/graphics/Camera.o \ +./src/graphics/ColorFade.o \ ./src/graphics/ComplexAnimation.o \ ./src/graphics/Font.o \ ./src/graphics/Frame.o \ @@ -28,6 +30,7 @@ OBJS += \ CPP_DEPS += \ ./src/graphics/Animation.d \ ./src/graphics/Camera.d \ +./src/graphics/ColorFade.d \ ./src/graphics/ComplexAnimation.d \ ./src/graphics/Font.d \ ./src/graphics/Frame.d \ diff --git a/Release/src/graphics/subdir.mk b/Release/src/graphics/subdir.mk index ed834d9..c020692 100644 --- a/Release/src/graphics/subdir.mk +++ b/Release/src/graphics/subdir.mk @@ -6,6 +6,7 @@ CPP_SRCS += \ ../src/graphics/Animation.cpp \ ../src/graphics/Camera.cpp \ +../src/graphics/ColorFade.cpp \ ../src/graphics/ComplexAnimation.cpp \ ../src/graphics/Font.cpp \ ../src/graphics/Frame.cpp \ @@ -17,6 +18,7 @@ CPP_SRCS += \ OBJS += \ ./src/graphics/Animation.o \ ./src/graphics/Camera.o \ +./src/graphics/ColorFade.o \ ./src/graphics/ComplexAnimation.o \ ./src/graphics/Font.o \ ./src/graphics/Frame.o \ @@ -28,6 +30,7 @@ OBJS += \ CPP_DEPS += \ ./src/graphics/Animation.d \ ./src/graphics/Camera.d \ +./src/graphics/ColorFade.d \ ./src/graphics/ComplexAnimation.d \ ./src/graphics/Font.d \ ./src/graphics/Frame.d \ diff --git a/src/graphics/ColorFade.cpp b/src/graphics/ColorFade.cpp new file mode 100644 index 0000000..51fe973 --- /dev/null +++ b/src/graphics/ColorFade.cpp @@ -0,0 +1,95 @@ +/* + * ColorFade.cpp + * + * Created on: Oct 7, 2012 + * Author: holy + */ + +#include "ColorFade.h" + +#include "../app/Application.h" + +using app::Application; +using app::State; +using app::Input; + +namespace graphics { + +ColorFade::ColorFade(State *slave, Uint32 color, int duration, bool in, bool interactive) +: ctrl(0) +, slave(slave) +, blinds(0) +, color(color) +, duration(duration) +, in(in) +, interactive(interactive) { + +} + +void ColorFade::EnterState(Application &c, SDL_Surface *screen) { + timer = GraphicsTimers().StartCountdown(duration); + ctrl = &c; +} + +void ColorFade::ExitState(Application &, SDL_Surface *screen) { + if (blinds) { + SDL_FreeSurface(blinds); + blinds = 0; + } +} + +void ColorFade::ResumeState(Application &ctrl, SDL_Surface *screen) { + UpdateBlinds(screen->w, screen->h); +} + +void ColorFade::PauseState(Application &ctrl, SDL_Surface *screen) { + +} + + +void ColorFade::Resize(int width, int height) { + slave->Resize(width, height); + UpdateBlinds(width, height); +} + +void ColorFade::UpdateBlinds(int width, int height) { + if (blinds && blinds->w == width && blinds->h == height) return; + if (blinds) { + SDL_FreeSurface(blinds); + } + blinds = SDL_CreateRGBSurface(0, width, height, 32, 0xFF000000, 0x00FF0000, 0x0000FF00, 0x00000000); + SDL_FillRect(blinds, 0, color); +} + +Uint8 ColorFade::GetAlpha() const { + if (in) { + return timer.Remaining() * 255 / duration; + } else { + return timer.Elapsed() * 255 / duration; + } +} + + +void ColorFade::HandleEvents(const Input &input) { + if (interactive) { + slave->HandleEvents(input); + } + if (timer.Finished()) { + ctrl->PopState(); + } +} + + +void ColorFade::UpdateWorld(float deltaT) { + if (interactive) { + slave->UpdateWorld(deltaT); + } +} + +void ColorFade::Render(SDL_Surface *screen) { + slave->Render(screen); + SDL_SetAlpha(blinds, SDL_SRCALPHA, GetAlpha()); + SDL_BlitSurface(blinds, 0, screen, 0); +} + +} diff --git a/src/graphics/ColorFade.h b/src/graphics/ColorFade.h new file mode 100644 index 0000000..66d8192 --- /dev/null +++ b/src/graphics/ColorFade.h @@ -0,0 +1,55 @@ +/* + * ColorFade.h + * + * Created on: Oct 7, 2012 + * Author: holy + */ + +#ifndef GRAPHICS_COLORFADE_H_ +#define GRAPHICS_COLORFADE_H_ + +#include "../app/State.h" +#include "../app/Timer.h" + +#include + +namespace graphics { + +class ColorFade +: public app::State { + +public: + ColorFade(app::State *slave, Uint32 color, int duration, bool in = false, bool interactive = false); + virtual ~ColorFade() { } + +public: + virtual void EnterState(app::Application &ctrl, SDL_Surface *screen); + virtual void ExitState(app::Application &ctrl, SDL_Surface *screen); + virtual void ResumeState(app::Application &ctrl, SDL_Surface *screen); + virtual void PauseState(app::Application &ctrl, SDL_Surface *screen); + + virtual void Resize(int width, int height); + + virtual void HandleEvents(const app::Input &); + virtual void UpdateWorld(float deltaT); + virtual void Render(SDL_Surface *); + +private: + void UpdateBlinds(int width, int height); + Uint8 GetAlpha() const; + +private: + app::Timer timer; + app::Application *ctrl; + app::State *slave; + SDL_Surface *blinds; + Uint32 color; + int duration; + bool in; + bool interactive; + +}; + +} + +#endif /* GRAPHICS_COLORFADE_H_ */ -- 2.39.2