]> git.localhorst.tv Git - l2e.git/blob - src/graphics/ColorFade.cpp
added color fading state
[l2e.git] / src / graphics / ColorFade.cpp
1 /*
2  * ColorFade.cpp
3  *
4  *  Created on: Oct 7, 2012
5  *      Author: holy
6  */
7
8 #include "ColorFade.h"
9
10 #include "../app/Application.h"
11
12 using app::Application;
13 using app::State;
14 using app::Input;
15
16 namespace graphics {
17
18 ColorFade::ColorFade(State *slave, Uint32 color, int duration, bool in, bool interactive)
19 : ctrl(0)
20 , slave(slave)
21 , blinds(0)
22 , color(color)
23 , duration(duration)
24 , in(in)
25 , interactive(interactive) {
26
27 }
28
29 void ColorFade::EnterState(Application &c, SDL_Surface *screen) {
30         timer = GraphicsTimers().StartCountdown(duration);
31         ctrl = &c;
32 }
33
34 void ColorFade::ExitState(Application &, SDL_Surface *screen) {
35         if (blinds) {
36                 SDL_FreeSurface(blinds);
37                 blinds = 0;
38         }
39 }
40
41 void ColorFade::ResumeState(Application &ctrl, SDL_Surface *screen) {
42         UpdateBlinds(screen->w, screen->h);
43 }
44
45 void ColorFade::PauseState(Application &ctrl, SDL_Surface *screen) {
46
47 }
48
49
50 void ColorFade::Resize(int width, int height) {
51         slave->Resize(width, height);
52         UpdateBlinds(width, height);
53 }
54
55 void ColorFade::UpdateBlinds(int width, int height) {
56         if (blinds && blinds->w == width && blinds->h == height) return;
57         if (blinds) {
58                 SDL_FreeSurface(blinds);
59         }
60         blinds = SDL_CreateRGBSurface(0, width, height, 32, 0xFF000000, 0x00FF0000, 0x0000FF00, 0x00000000);
61         SDL_FillRect(blinds, 0, color);
62 }
63
64 Uint8 ColorFade::GetAlpha() const {
65         if (in) {
66                 return timer.Remaining() * 255 / duration;
67         } else {
68                 return timer.Elapsed() * 255 / duration;
69         }
70 }
71
72
73 void ColorFade::HandleEvents(const Input &input) {
74         if (interactive) {
75                 slave->HandleEvents(input);
76         }
77         if (timer.Finished()) {
78                 ctrl->PopState();
79         }
80 }
81
82
83 void ColorFade::UpdateWorld(float deltaT) {
84         if (interactive) {
85                 slave->UpdateWorld(deltaT);
86         }
87 }
88
89 void ColorFade::Render(SDL_Surface *screen) {
90         slave->Render(screen);
91         SDL_SetAlpha(blinds, SDL_SRCALPHA, GetAlpha());
92         SDL_BlitSurface(blinds, 0, screen, 0);
93 }
94
95 }