1 //
2 // Cross-platform free Puyo-Puyo clone.
3 // Copyright (C) 2006, 2007 Emma's software
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 //
19 #if defined (HAVE_CONFIG_H)
20 #include <config.h>
21 #endif // !HAVE_CONFIG_H
22 #include "FadeInState.h"
23 #include "System.h"
24 
25 using namespace Amoebax;
26 
27 ///
28 /// \brief Constructor.
29 ///
30 /// \param nextState The state that will active once the fade in is done.
31 /// \param fadeTime The time the fade should take in milliseconds.
32 ///
FadeInState(IState * nextState,uint32_t fadeTime)33 FadeInState::FadeInState (IState *nextState, uint32_t fadeTime):
34     IState (),
35     m_Alpha (0),
36     m_Background (0),
37     m_FadeTime (fadeTime),
38     m_NextState (nextState)
39 {
40 }
41 
42 void
activate(void)43 FadeInState::activate (void)
44 {
45     m_Background.reset (Surface::fromScreen ());
46     SDL_Rect region;
47     region.x = 0;
48     region.y = 0;
49     region.w = m_Background->getWidth ();
50     region.h = m_Background->getHeight ();
51     m_NextState->redrawBackground (&region, m_Background->toSDLSurface ());
52     m_NextState->render (m_Background->toSDLSurface ());
53     m_Background->setAlpha (getAlpha ());
54 }
55 
56 ///
57 /// \brief The current alpha to apply to the screen.
58 ///
59 /// \return The current alpha value to apply to the screen.
60 ///
61 inline int16_t
getAlpha(void) const62 FadeInState::getAlpha (void) const
63 {
64     return m_Alpha;
65 }
66 
67 ///
68 /// \brief Gets the time the fade should take, in milliseconds.
69 ///
70 /// \return The time the fade should take, in milliseconds.
71 ///
72 inline uint32_t
getFadeTime(void) const73 FadeInState::getFadeTime (void) const
74 {
75     return m_FadeTime;
76 }
77 
78 void
redrawBackground(SDL_Rect * region,SDL_Surface * screen)79 FadeInState::redrawBackground (SDL_Rect *region, SDL_Surface *screen)
80 {
81     SDL_FillRect (screen, region, SDL_MapRGB (screen->format, 0, 0, 0));
82 }
83 
84 void
render(SDL_Surface * screen)85 FadeInState::render (SDL_Surface *screen)
86 {
87     m_Background->blit (screen);
88 }
89 
90 ///
91 /// \brief Sets the alpha value to apply to the screen.
92 ///
93 /// \param alpha The alpha value to set to the screen.
94 ///
95 inline void
setAlpha(int16_t alpha)96 FadeInState::setAlpha (int16_t alpha)
97 {
98     m_Alpha = alpha;
99 }
100 
101 void
update(uint32_t elapsedTime)102 FadeInState::update (uint32_t elapsedTime)
103 {
104     setAlpha (getAlpha () +
105               static_cast<int16_t>(Surface::k_MaxAlpha *
106                     static_cast<float> (elapsedTime) / getFadeTime ()));
107     if ( getAlpha () <= Surface::k_MaxAlpha )
108     {
109         m_Background->setAlpha (getAlpha ());
110     }
111     else
112     {
113         System::getInstance ().removeActiveState (false);
114     }
115 }
116 
117 void
videoModeChanged(void)118 FadeInState::videoModeChanged (void)
119 {
120 }
121