Fix the wrap functions again, properly this time
authorMikko Rasa <tdb@tdb.fi>
Mon, 25 Jun 2012 19:58:24 +0000 (22:58 +0300)
committerMikko Rasa <tdb@tdb.fi>
Mon, 25 Jun 2012 19:58:24 +0000 (22:58 +0300)
src/Actor.cpp
src/RageUtil.h
src/ScreenDebugOverlay.cpp
src/ScreenTextEntry.cpp

index e29c7ab..a2e87c4 100755 (executable)
@@ -605,9 +605,9 @@ void Actor::UpdateInternal( float fDeltaTime )
        {
        case spin:
                m_current.rotation += m_fEffectDelta*m_vEffectMagnitude;
-               wrap( m_current.rotation.x, 360 );
-               wrap( m_current.rotation.y, 360 );
-               wrap( m_current.rotation.z, 360 );
+               wrap( m_current.rotation.x, 360.0f );
+               wrap( m_current.rotation.y, 360.0f );
+               wrap( m_current.rotation.z, 360.0f );
                break;
        }
 
index 58e60e8..eda1a3f 100755 (executable)
@@ -56,23 +56,35 @@ inline bool CLAMP(float &x, float l, float h)
        return false;
 }
 
-inline void wrap( int &x, int n )
+template<typename T>
+inline void wrap( T &x, T n )
 {
-       if (x<0)
-               x += ((-x/n)+1)*n;
-       x %= n;
+       int xi = x;
+       if (xi<0)
+               xi += ((-xi/n)+1)*n;
+       xi %= n;
+       x = static_cast<T>(xi);
 }
-inline void wrap( unsigned &x, unsigned n )
+
+template<>
+inline void wrap<unsigned>( unsigned &x, unsigned n )
 {
        x %= n;
 }
-inline void wrap( float &x, float n )
+
+template<>
+inline void wrap<float>( float &x, float n )
 {
        if (x<0)
                x += truncf(((-x/n)+1))*n;
        x = fmodf(x,n);
 }
 
+inline void wrap( int &x, size_t n )
+{
+       wrap<int>(x, n);
+}
+
 inline float fracf( float f ) { return f - truncf(f); }
 
 template<class T>
index 8c68488..c43f211 100755 (executable)
@@ -327,16 +327,12 @@ bool ScreenDebugOverlay::OverlayInput( const DeviceInput& DeviceI, const InputEv
 
                        BitmapText &txt2 = m_textFunction[i];
 
-                       int iWrap;
-
                        switch( i )
                        {
                        case DebugLine_Autoplay:
                                {
                                        PlayerController pc = (PlayerController)(PREFSMAN->m_AutoPlay+1);
-                                       iWrap = (int)pc;
-                                       wrap( iWrap, NUM_PLAYER_CONTROLLERS );
-                                       pc = (PlayerController)iWrap;
+                                       wrap( pc, NUM_PLAYER_CONTROLLERS );
                                        PREFSMAN->m_AutoPlay.Set( pc );
                                        FOREACH_HumanPlayer(pn)
                                                GAMESTATE->m_pPlayerState[pn]->m_PlayerController = PREFSMAN->m_AutoPlay;
@@ -357,9 +353,7 @@ bool ScreenDebugOverlay::OverlayInput( const DeviceInput& DeviceI, const InputEv
                                        if( type != IET_FIRST_PRESS )
                                                return true; /* eat the input but do nothing */
                                        SongOptions::AutosyncType as = (SongOptions::AutosyncType)(GAMESTATE->m_SongOptions.m_AutosyncType+1);
-                                       iWrap = (int)as;
-                                       wrap( iWrap, SongOptions::NUM_AUTOSYNC_TYPES );
-                                       as = (SongOptions::AutosyncType)iWrap;
+                                       wrap( as, SongOptions::NUM_AUTOSYNC_TYPES );
                                        GAMESTATE->m_SongOptions.m_AutosyncType = as;
                                        MESSAGEMAN->Broadcast( MESSAGE_AUTOSYNC_CHANGED );
                                }
@@ -367,9 +361,7 @@ bool ScreenDebugOverlay::OverlayInput( const DeviceInput& DeviceI, const InputEv
                        case DebugLine_CoinMode:
                                {
                                        CoinMode cm = (CoinMode)(PREFSMAN->m_CoinMode+1);
-                                       iWrap = (int)cm;
-                                       wrap( iWrap, NUM_COIN_MODES );
-                                       cm = (CoinMode)iWrap;
+                                       wrap( cm, NUM_COIN_MODES );
                                        PREFSMAN->m_CoinMode.Set( cm );
                                        SCREENMAN->RefreshCreditsMessages();
                                }
index ce12642..83352d0 100755 (executable)
@@ -289,7 +289,7 @@ void ScreenTextEntry::MoveY( int iDir )
        do
        {
                m_iFocusY = (KeyboardRow)(m_iFocusY + iDir);
-               wrap( (int&)m_iFocusY, NUM_KEYBOARD_ROWS );
+               wrap( m_iFocusY, NUM_KEYBOARD_ROWS );
 
                // HACK: Round to nearest option so that we always stop 
                // on KEYBOARD_ROW_SPECIAL.