+/*
+ * 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);
+}
+
+}