@ -50,11 +50,21 @@ public class AppStateManager {
private final ArrayList < AppState > states = new ArrayList < AppState > ( ) ;
private final Application app ;
private AppState [ ] stateArray ;
public AppStateManager ( Application app ) {
this . app = app ;
}
protected AppState [ ] getArray ( ) {
synchronized ( states ) {
if ( stateArray = = null ) {
stateArray = states . toArray ( new AppState [ states . size ( ) ] ) ;
}
return stateArray ;
}
}
/ * *
* Attach a state to the AppStateManager , the same state cannot be attached
* twice .
@ -68,6 +78,7 @@ public class AppStateManager {
if ( ! states . contains ( state ) ) {
state . stateAttached ( this ) ;
states . add ( state ) ;
stateArray = null ;
return true ;
} else {
return false ;
@ -87,6 +98,7 @@ public class AppStateManager {
if ( states . contains ( state ) ) {
state . stateDetached ( this ) ;
states . remove ( state ) ;
stateArray = null ;
return true ;
} else {
return false ;
@ -132,10 +144,8 @@ public class AppStateManager {
* @param tpf Time per frame .
* /
public void update ( float tpf ) {
synchronized ( states ) {
int num = states . size ( ) ;
for ( int i = 0 ; i < num ; i + + ) {
AppState state = states . get ( i ) ;
AppState [ ] array = getArray ( ) ;
for ( AppState state : array ) {
if ( ! state . isInitialized ( ) )
state . initialize ( this , app ) ;
@ -144,17 +154,14 @@ public class AppStateManager {
}
}
}
}
/ * *
* Calls render for all attached states , do not call directly .
* @param rm The RenderManager
* /
public void render ( RenderManager rm ) {
synchronized ( states ) {
int num = states . size ( ) ;
for ( int i = 0 ; i < num ; i + + ) {
AppState state = states . get ( i ) ;
AppState [ ] array = getArray ( ) ;
for ( AppState state : array ) {
if ( ! state . isInitialized ( ) )
state . initialize ( this , app ) ;
@ -163,17 +170,14 @@ public class AppStateManager {
}
}
}
}
/ * *
* Calls render for all attached states , do not call directly .
* @param rm The RenderManager
* /
public void postRender ( ) {
synchronized ( states ) {
int num = states . size ( ) ;
for ( int i = 0 ; i < num ; i + + ) {
AppState state = states . get ( i ) ;
AppState [ ] array = getArray ( ) ;
for ( AppState state : array ) {
if ( ! state . isInitialized ( ) )
state . initialize ( this , app ) ;
@ -182,17 +186,14 @@ public class AppStateManager {
}
}
}
}
/ * *
* Calls cleanup on attached states , do not call directly .
* /
public void cleanup ( ) {
synchronized ( states ) {
for ( int i = 0 ; i < states . size ( ) ; i + + ) {
AppState state = states . get ( i ) ;
AppState [ ] array = getArray ( ) ;
for ( AppState state : array ) {
state . cleanup ( ) ;
}
}
}
}