]> git.localhorst.tv Git - l2e.git/blob - ColorFade.cpp
55e14fc811d87e2427a4ef0443259a3da996d1da
[l2e.git] / ColorFade.cpp
1 #include "ColorFade.h"
2
3 #include "../app/Application.h"
4
5 using app::Application;
6 using app::State;
7 using app::Input;
8
9 namespace graphics {
10
11 ColorFade::ColorFade(State *slave, Uint32 color, int duration, bool in, bool interactive)
12 : slave(slave)
13 , blinds(0)
14 , color(color)
15 , duration(duration)
16 , leadIn(0)
17 , leadOut(0)
18 , leadInDone(true)
19 , fadeDone(false)
20 , in(in)
21 , interactive(interactive)
22 , doublePop(false) {
23
24 }
25
26 void ColorFade::OnEnterState(SDL_Surface *screen) {
27         if (leadIn > 0) {
28                 timer = GraphicsTimers().StartCountdown(leadIn);
29         } else {
30                 timer = GraphicsTimers().StartCountdown(duration);
31
32         }
33 }
34
35 void ColorFade::OnExitState(SDL_Surface *screen) {
36         if (blinds) {
37                 SDL_FreeSurface(blinds);
38                 blinds = 0;
39         }
40 }
41
42 void ColorFade::OnResumeState(SDL_Surface *screen) {
43         UpdateBlinds(screen->w, screen->h);
44 }
45
46 void ColorFade::OnPauseState(SDL_Surface *screen) {
47
48 }
49
50
51 void ColorFade::OnResize(int width, int height) {
52         slave->Resize(width, height);
53         UpdateBlinds(width, height);
54 }
55
56 void ColorFade::UpdateBlinds(int width, int height) {
57         if (blinds && blinds->w == width && blinds->h == height) return;
58         if (blinds) {
59                 SDL_FreeSurface(blinds);
60         }
61         blinds = SDL_CreateRGBSurface(0, width, height, 32, 0xFF000000, 0x00FF0000, 0x0000FF00, 0x00000000);
62         SDL_FillRect(blinds, 0, color);
63 }
64
65 Uint8 ColorFade::GetAlpha() const {
66         if (!leadInDone) {
67                 return in ? 255 : 0;
68         } else if (!fadeDone) {
69                 return (in ? timer.Remaining() : timer.Elapsed()) * 255 / duration;
70         } else {
71                 return in ? 0 : 255;
72         }
73 }
74
75
76 void ColorFade::HandleEvents(const Input &input) {
77         if (interactive) {
78                 slave->HandleEvents(input);
79         }
80         if (timer.Finished()) {
81                 if (!leadInDone) {
82                         leadInDone = true;
83                         timer = GraphicsTimers().StartCountdown(duration);
84                 } else if (!fadeDone) {
85                         fadeDone = true;
86                         if (leadOut > 0) {
87                                 timer = GraphicsTimers().StartCountdown(leadOut);
88                         } else {
89                                 Ctrl().PopState();
90                                 if (doublePop) {
91                                         Ctrl().PopState();
92                                 }
93                         }
94                 } else {
95                         Ctrl().PopState();
96                         if (doublePop) {
97                                 Ctrl().PopState();
98                         }
99                 }
100         }
101 }
102
103
104 void ColorFade::UpdateWorld(Uint32 deltaT) {
105         if (interactive) {
106                 slave->UpdateWorld(deltaT);
107         }
108 }
109
110 void ColorFade::Render(SDL_Surface *screen) {
111         slave->Render(screen);
112         SDL_SetAlpha(blinds, SDL_SRCALPHA, GetAlpha());
113         SDL_BlitSurface(blinds, 0, screen, 0);
114 }
115
116 }