]> git.localhorst.tv Git - l2e.git/blob - src/graphics/ColorFade.cpp
72bf40978791cfb1d0bad8143eda8d241fc98506
[l2e.git] / src / graphics / 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
23 }
24
25 void ColorFade::OnEnterState(SDL_Surface *screen) {
26         if (leadIn > 0) {
27                 timer = GraphicsTimers().StartCountdown(leadIn);
28         } else {
29                 timer = GraphicsTimers().StartCountdown(duration);
30
31         }
32 }
33
34 void ColorFade::OnExitState(SDL_Surface *screen) {
35         if (blinds) {
36                 SDL_FreeSurface(blinds);
37                 blinds = 0;
38         }
39 }
40
41 void ColorFade::OnResumeState(SDL_Surface *screen) {
42         UpdateBlinds(screen->w, screen->h);
43 }
44
45 void ColorFade::OnPauseState(SDL_Surface *screen) {
46
47 }
48
49
50 void ColorFade::OnResize(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 (!leadInDone) {
66                 return in ? 255 : 0;
67         } else if (!fadeDone) {
68                 return (in ? timer.Remaining() : timer.Elapsed()) * 255 / duration;
69         } else {
70                 return in ? 0 : 255;
71         }
72 }
73
74
75 void ColorFade::HandleEvents(const Input &input) {
76         if (interactive) {
77                 slave->HandleEvents(input);
78         }
79         if (timer.Finished()) {
80                 if (!leadInDone) {
81                         leadInDone = true;
82                         timer = GraphicsTimers().StartCountdown(duration);
83                 } else if (!fadeDone) {
84                         fadeDone = true;
85                         if (leadOut > 0) {
86                                 timer = GraphicsTimers().StartCountdown(leadOut);
87                         } else {
88                                 Ctrl().PopState();
89                         }
90                 } else {
91                         Ctrl().PopState();
92                 }
93         }
94 }
95
96
97 void ColorFade::UpdateWorld(Uint32 deltaT) {
98         if (interactive) {
99                 slave->UpdateWorld(deltaT);
100         }
101 }
102
103 void ColorFade::Render(SDL_Surface *screen) {
104         slave->Render(screen);
105         SDL_SetAlpha(blinds, SDL_SRCALPHA, GetAlpha());
106         SDL_BlitSurface(blinds, 0, screen, 0);
107 }
108
109 }