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