1 //  Fade.cpp -- Generic fade in/out effect.
2 //  Copyright (C) 2008  Nick Gasson
3 //
4 //  This program is free software: you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation, either version 3 of the License, or
7 //  (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 //
17 
18 #include "Fade.hpp"
19 #include "OpenGL.hpp"
20 
21 #include <cassert>
22 
23 const float Fade::DEFAULT_FADE_SPEED(0.05f);
24 
Fade(float s)25 Fade::Fade(float s)
26    : state(fNone), alpha(0.0f), speed(s)
27 {
28 
29 }
30 
BeginFadeIn()31 void Fade::BeginFadeIn()
32 {
33    assert(state == fNone);
34 
35    state = fIn;
36    alpha = 1.0f;
37 }
38 
BeginFadeOut()39 void Fade::BeginFadeOut()
40 {
41    assert(state == fNone);
42 
43    state = fOut;
44    alpha = 0.0f;
45 }
46 
Process()47 bool Fade::Process()
48 {
49    switch (state) {
50    case fOut:
51       alpha += speed;
52       if (alpha >= 1.0f) {
53          state = fNone;
54          return true;
55       }
56       else
57          return false;
58    case fIn:
59       alpha -= speed;
60       if (alpha <= 0.0f) {
61          state = fNone;
62          return true;
63       }
64       else
65          return false;
66    default:
67       assert(false);
68    }
69 }
70 
Display()71 void Fade::Display()
72 {
73    const int w = OpenGL::GetInstance().GetWidth();
74    const int h = OpenGL::GetInstance().GetHeight();
75 
76    glEnable(GL_BLEND);
77    glDisable(GL_TEXTURE_2D);
78    glColor4f(0.0f, 0.0f, 0.0f, alpha);
79    glLoadIdentity();
80    glBegin(GL_QUADS);
81    glVertex3i(0, 0, 0);
82    glVertex3i(0, h, 0);
83    glVertex3i(w, h, 0);
84    glVertex3i(w, 0, 0);
85    glEnd();
86 }
87