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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 //
19 #if defined (HAVE_CONFIG_H)
20 #include <config.h>
21 #endif // HAVE_CONFIG_H
22 #include "File.h"
23 #include "Font.h"
24 #include "System.h"
25 #include "VersusState.h"
26 
27 using namespace Amoebax;
28 
29 /// The time to wait, in ms., until the versus state ends.
30 static const uint32_t k_TimeToWait = 5000;
31 
32 // The separation in pixels from the center to a player in the versus screen.
33 static const uint16_t k_PlayersSeparation = 200;
34 
35 ///
36 /// \brief Constructor.
37 ///
38 /// \param player The name of the player's avatar.
39 /// \param opponent The name of the opponent player.
40 ///
VersusState(const std::string & player,const std::string & opponent)41 VersusState::VersusState(const std::string &player,
42                          const std::string &opponent):
43     m_Background (0),
44     m_BackgroundMusic (0),
45     m_End (false),
46     m_Opponent (opponent),
47     m_Player (player),
48     m_TimeElapsed (0)
49 {
50     loadGraphicResources ();
51 }
52 
53 void
activate(void)54 VersusState::activate (void)
55 {
56     if ( !Music::isPlaying () )
57     {
58         if ( 0 == m_BackgroundMusic.get () )
59         {
60             m_BackgroundMusic.reset (Music::fromFile
61                     (File::getMusicFilePath ("menu.ogg")));
62         }
63         m_BackgroundMusic->play ();
64     }
65 }
66 
67 ///
68 /// \brief Signals that the versus screen must end.
69 ///
70 /// This is used when a key or joystick is pressed or when
71 /// the time elapsed is greater than the time to wait to tell
72 /// to the VersusState class that the next update() it must remove
73 /// itself from the System. See mustEnd().
74 ///
75 inline void
endVersusState(void)76 VersusState::endVersusState (void)
77 {
78     m_End = true;
79 }
80 
81 ///
82 /// \brief Gets the opponent's name.
83 ///
84 /// \return The opponent's name.
85 ///
86 inline const std::string &
getOpponentName(void) const87 VersusState::getOpponentName (void) const
88 {
89     return m_Opponent;
90 }
91 
92 ///
93 /// \brief Gets the player's avatar name.
94 ///
95 /// \return The player's avatar name.
96 ///
97 inline const std::string &
getPlayerName(void) const98 VersusState::getPlayerName (void) const
99 {
100     return m_Player;
101 }
102 
103 void
joyMotion(uint8_t joystick,uint8_t axis,int16_t value)104 VersusState::joyMotion (uint8_t joystick, uint8_t axis, int16_t value)
105 {
106 }
107 
108 void
joyDown(uint8_t joystick,uint8_t button)109 VersusState::joyDown (uint8_t joystick, uint8_t button)
110 {
111     endVersusState ();
112 }
113 
114 void
joyUp(uint8_t joystick,uint8_t button)115 VersusState::joyUp (uint8_t joystick, uint8_t button)
116 {
117 }
118 
119 #if !defined (IS_GP2X_HOST)
120 void
keyDown(uint32_t key)121 VersusState::keyDown (uint32_t key)
122 {
123     endVersusState ();
124 }
125 
126 void
keyUp(uint32_t key)127 VersusState::keyUp (uint32_t key)
128 {
129 }
130 #endif // !IS_GP2X_HOST
131 
132 ///
133 /// \brief Loads all graphical resources.
134 ///
135 void
loadGraphicResources(void)136 VersusState::loadGraphicResources (void)
137 {
138     const float screenScale = System::getInstance ().getScreenScaleFactor ();
139 #if defined (IS_GP2X_HOST)
140     uint16_t playersSeparation = static_cast<uint16_t> (k_PlayersSeparation *
141                                                         screenScale);
142 #else // !IS_GP2X_HOST
143     uint16_t playersSeparation = k_PlayersSeparation;
144 #endif // IS_GP2X_HOST
145 
146     m_Background.reset (
147             Surface::fromFile (File::getGraphicsFilePath ("menuBackground.png")));
148 
149     std::auto_ptr<Font> font (
150             Font::fromFile (File::getFontFilePath ("fontMenu")));
151     {
152         std::auto_ptr<Surface> player (
153                 Surface::fromFile (
154                     File::getGraphicsFilePath (getPlayerName () + ".png")));
155         int16_t x = m_Background->getWidth () / 2 + playersSeparation;
156         int16_t y = m_Background->getHeight () / 2 - player->getHeight () / 2;
157         player->blit (x, y, m_Background->toSDLSurface ());
158 
159         x += player->getWidth () / 2 - font->getTextWidth (getPlayerName ()) / 2;
160         y += player->getHeight () + font->getHeight ();
161         font->write (getPlayerName (), x, y, m_Background->toSDLSurface ());
162     }
163     {
164         std::auto_ptr<Surface> opponent (
165                 Surface::fromFile (
166                     File::getGraphicsFilePath (getOpponentName () + ".png")));
167         int16_t x = m_Background->getWidth () / 2 - playersSeparation -
168                     opponent->getWidth ();
169         int16_t y = m_Background->getHeight () / 2 - opponent->getHeight () / 2;
170         opponent->blit (x, y, m_Background->toSDLSurface ());
171 
172         x += opponent->getWidth () / 2 -
173              font->getTextWidth (getOpponentName ()) / 2;
174         y += opponent->getHeight () + font->getHeight ();
175         font->write (getOpponentName (), x, y, m_Background->toSDLSurface ());
176     }
177     {
178         std::auto_ptr<Surface> versus (
179                 Surface::fromFile (File::getGraphicsFilePath ("versus.png")));
180         versus->blit (m_Background->getWidth () / 2 - versus->getWidth () / 2,
181                       m_Background->getHeight () / 2 - versus->getHeight () / 2,
182                       m_Background->toSDLSurface ());
183     }
184     m_Background->resize (screenScale);
185 }
186 
187 ///
188 /// \brief Tells if the versus screen should end.
189 ///
190 /// \return \a true if the versus screen should end, \a false otherwise.
191 ///
192 inline bool
mustEnd(void) const193 VersusState::mustEnd (void) const
194 {
195     return m_End;
196 }
197 
198 void
redrawBackground(SDL_Rect * region,SDL_Surface * screen)199 VersusState::redrawBackground (SDL_Rect *region, SDL_Surface *screen)
200 {
201     m_Background->blit (region->x, region->y, region->w, region->h,
202                         region->x, region->y, screen);
203 }
204 
205 void
render(SDL_Surface * screen)206 VersusState::render (SDL_Surface *screen)
207 {
208 }
209 
210 void
update(uint32_t elapsedTime)211 VersusState::update (uint32_t elapsedTime)
212 {
213     if ( mustEnd () )
214     {
215         System::getInstance ().removeActiveState ();
216     }
217     else
218     {
219         m_TimeElapsed += elapsedTime;
220         if ( m_TimeElapsed >= k_TimeToWait )
221         {
222             endVersusState ();
223         }
224     }
225 }
226 
227 void
videoModeChanged(void)228 VersusState::videoModeChanged (void)
229 {
230 }
231