]> git.localhorst.tv Git - l2e.git/commitdiff
added color fading state
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Sun, 7 Oct 2012 13:49:42 +0000 (15:49 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Sun, 7 Oct 2012 13:49:42 +0000 (15:49 +0200)
this can fade a state to and from a solid color

Debug/src/graphics/subdir.mk
Release/src/graphics/subdir.mk
src/graphics/ColorFade.cpp [new file with mode: 0644]
src/graphics/ColorFade.h [new file with mode: 0644]

index 1f5268e567c9c2ca42d7873c1b16e7a31eb436db..23f43a82fecf4bc4a8e1a778685e2f12532953a6 100644 (file)
@@ -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 \
index ed834d987c9abe69be39cd275414a9b2f77b6e68..c020692572cd22254b2cc66ce9aead1a1774d47c 100644 (file)
@@ -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 (file)
index 0000000..51fe973
--- /dev/null
@@ -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 (file)
index 0000000..66d8192
--- /dev/null
@@ -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 <SDL.h>
+
+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<Uint32> timer;
+       app::Application *ctrl;
+       app::State *slave;
+       SDL_Surface *blinds;
+       Uint32 color;
+       int duration;
+       bool in;
+       bool interactive;
+
+};
+
+}
+
+#endif /* GRAPHICS_COLORFADE_H_ */