Skip to content

Commit

Permalink
added async loading of scenes for transitions
Browse files Browse the repository at this point in the history
  • Loading branch information
prime31 committed Feb 14, 2016
1 parent bb88959 commit fed4608
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 17 deletions.
2 changes: 1 addition & 1 deletion MacTester/Scenes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public static Scene sceneTwo()
{
var scene = new Scene();
scene.clearColor = Color.Coral;
var moonTexture = scene.contentManager.Load<Texture2D>( "Images/moon" );
var moonTexture = scene.contentManager.Load<Texture2D>( "bin/MacOSX/Images/moon" );
var bmFont = scene.contentManager.Load<BitmapFont>( "bin/MacOSX/Fonts/pixelfont" );
bmFont.spacing = 2f;

Expand Down
2 changes: 1 addition & 1 deletion Nez-PCL/Graphics/Transitions/CrossFadeTransition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public override IEnumerator onBeginTransition()
yield return null;

// load up the new Scene
Core.scene = sceneLoadAction();
yield return Core.startCoroutine( loadNextScene() );

var elapsed = 0f;
while( elapsed < fadeDuration )
Expand Down
8 changes: 4 additions & 4 deletions Nez-PCL/Graphics/Transitions/FadeTransition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ public class FadeTransition : SceneTransition
/// <summary>
/// duration to fade to fadeToColor
/// </summary>
public float fadeOutDuration = 1f;
public float fadeOutDuration = 0.8f;

/// <summary>
/// delay to start fading out
/// </summary>
public float delayBeforeFadeInDuration = 0.4f;
public float delayBeforeFadeInDuration = 0.2f;

/// <summary>
/// duration to fade from fadeToColor to the new Scene
Expand Down Expand Up @@ -66,7 +66,7 @@ public override IEnumerator onBeginTransition()
}

// load up the new Scene
Core.scene = sceneLoadAction();
yield return Core.startCoroutine( loadNextScene() );

// dispose of our previousSceneRender. We dont need it anymore.
previousSceneRender.Dispose();
Expand Down Expand Up @@ -95,7 +95,7 @@ public override void render( Graphics graphics )
graphics.spriteBatch.Begin( SpriteSortMode.Deferred, BlendState.NonPremultiplied, Core.defaultSamplerState );

// we only render the previousSceneRender while fading to _color. It will be null after that.
if( previousSceneRender != null )
if( !_isNewSceneLoaded )
graphics.spriteBatch.Draw( previousSceneRender, _destinationRect, Color.White );

graphics.spriteBatch.Draw( _overlayTexture, _destinationRect, _color );
Expand Down
14 changes: 4 additions & 10 deletions Nez-PCL/Graphics/Transitions/ImageMaskTransition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,6 @@ public class ImageMaskTransition : SceneTransition
float _renderScale;
float _renderRotation;

/// <summary>
/// true if we are on the 2nd part of the transition
/// </summary>
bool _isScalingOut;

/// <summary>
/// the Texture used as a mask. It should be white where the mask shows the underlying Scene and transparent elsewhere
/// </summary>
Expand Down Expand Up @@ -119,14 +114,13 @@ public override IEnumerator onBeginTransition()
yield return null;
}

// load up the new Scene
yield return Core.startCoroutine( loadNextScene() );

// dispose of our previousSceneRender. We dont need it anymore.
previousSceneRender.Dispose();
previousSceneRender = null;

// load up the new Scene
Core.scene = sceneLoadAction();
_isScalingOut = true;

yield return delayBeforeMaskOut;

elapsed = 0f;
Expand Down Expand Up @@ -158,7 +152,7 @@ public override void render( Graphics graphics )
Core.graphicsDevice.SetRenderTarget( null );

// if we are scaling out we dont need to render the previous scene anymore since we want the new scene to be visible
if( !_isScalingOut )
if( !_isNewSceneLoaded )
{
graphics.spriteBatch.Begin( SpriteSortMode.Deferred, BlendState.Opaque, Core.defaultSamplerState );
graphics.spriteBatch.Draw( previousSceneRender, Vector2.Zero, Color.White );
Expand Down
37 changes: 37 additions & 0 deletions Nez-PCL/Graphics/Transitions/SceneTransition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ public abstract class SceneTransition
/// </summary>
public bool wantsPreviousSceneRender;

/// <summary>
/// if true, the next Scene will be loaded on a background thread. Note that if raw PNG files are used they cannot be loaded
/// on a background thread.
/// </summary>
public bool loadSceneOnBackgroundThread = false;

/// <summary>
/// function that should return the newly loaded scene
/// </summary>
Expand All @@ -48,6 +54,7 @@ internal bool hasPreviousSceneRender
}
bool _hasPreviousSceneRender;

protected bool _isNewSceneLoaded;


public SceneTransition( Func<Scene> sceneLoadAction, bool wantsPreviousSceneRender = true )
Expand All @@ -61,6 +68,36 @@ public SceneTransition( Func<Scene> sceneLoadAction, bool wantsPreviousSceneRend
}


protected IEnumerator loadNextScene()
{
if( loadSceneOnBackgroundThread )
{
// load the Scene on a background thread
ThreadPool.QueueUserWorkItem( context =>
{
var scene = sceneLoadAction();

// get back to the main thread before setting the new Scene active
var syncContext = context as SynchronizationContext;
syncContext.Post( d =>
{
Core.scene = scene;
_isNewSceneLoaded = true;
}, null );
}, SynchronizationContext.Current );
}
else
{
Core.scene = sceneLoadAction();
_isNewSceneLoaded = true;
}

// wait for the scene to load if it was loaded on a background thread
while( !_isNewSceneLoaded )
yield return null;
}


/// <summary>
/// called after the previousSceneRender occurs for the first (and only) time. At this point you can load your new Scene after
/// yielding one frame (so the first render call happens before scene loading).
Expand Down
2 changes: 1 addition & 1 deletion Nez-PCL/Graphics/Transitions/TransformTransition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public override IEnumerator onBeginTransition()
yield return null;

// load up the new Scene
Core.scene = sceneLoadAction();
yield return Core.startCoroutine( loadNextScene() );

var elapsed = 0f;
while( elapsed < duration )
Expand Down

0 comments on commit fed4608

Please sign in to comment.