]> git.localhorst.tv Git - l2e.git/blob - src/graphics/ColorFade.cpp
8afd87be72b8345c76b1ca753b34ac8c5e6d6674
[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 : slave(slave)
20 , blinds(0)
21 , color(color)
22 , duration(duration)
23 , leadIn(0)
24 , leadOut(0)
25 , leadInDone(true)
26 , fadeDone(false)
27 , in(in)
28 , interactive(interactive) {
29
30 }
31
32 void ColorFade::OnEnterState(SDL_Surface *screen) {
33         if (leadIn > 0) {
34                 timer = GraphicsTimers().StartCountdown(leadIn);
35         } else {
36                 timer = GraphicsTimers().StartCountdown(duration);
37
38         }
39 }
40
41 void ColorFade::OnExitState(SDL_Surface *screen) {
42         if (blinds) {
43                 SDL_FreeSurface(blinds);
44                 blinds = 0;
45         }
46 }
47
48 void ColorFade::OnResumeState(SDL_Surface *screen) {
49         UpdateBlinds(screen->w, screen->h);
50 }
51
52 void ColorFade::OnPauseState(SDL_Surface *screen) {
53
54 }
55
56
57 void ColorFade::OnResize(int width, int height) {
58         slave->Resize(width, height);
59         UpdateBlinds(width, height);
60 }
61
62 void ColorFade::UpdateBlinds(int width, int height) {
63         if (blinds && blinds->w == width && blinds->h == height) return;
64         if (blinds) {
65                 SDL_FreeSurface(blinds);
66         }
67         blinds = SDL_CreateRGBSurface(0, width, height, 32, 0xFF000000, 0x00FF0000, 0x0000FF00, 0x00000000);
68         SDL_FillRect(blinds, 0, color);
69 }
70
71 Uint8 ColorFade::GetAlpha() const {
72         if (!leadInDone) {
73                 return in ? 255 : 0;
74         } else if (!fadeDone) {
75                 return (in ? timer.Remaining() : timer.Elapsed()) * 255 / duration;
76         } else {
77                 return in ? 0 : 255;
78         }
79 }
80
81
82 void ColorFade::HandleEvents(const Input &input) {
83         if (interactive) {
84                 slave->HandleEvents(input);
85         }
86         if (timer.Finished()) {
87                 if (!leadInDone) {
88                         leadInDone = true;
89                         timer = GraphicsTimers().StartCountdown(duration);
90                 } else if (!fadeDone) {
91                         fadeDone = true;
92                         if (leadOut > 0) {
93                                 timer = GraphicsTimers().StartCountdown(leadOut);
94                         } else {
95                                 Ctrl().PopState();
96                         }
97                 } else {
98                         Ctrl().PopState();
99                 }
100         }
101 }
102
103
104 void ColorFade::UpdateWorld(float 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 }