Skip to content

Commit

Permalink
Merge branch 'develop3d' of http://github.com/mono/MonoGame into deve…
Browse files Browse the repository at this point in the history
…lop3d
  • Loading branch information
danzel committed May 17, 2012
2 parents 6795526 + 2f94187 commit 391a157
Show file tree
Hide file tree
Showing 11 changed files with 147 additions and 177 deletions.
4 changes: 0 additions & 4 deletions MonoGame.Framework/Android/AndroidGameActivity.cs
Expand Up @@ -64,10 +64,6 @@ protected override void OnResume()
return;
(deviceManager as GraphicsDeviceManager).ForceSetFullScreen();
Game.Window.RequestFocus();
if (Game.GraphicsDevice != null)
{
Game.GraphicsDevice.platform = Game.Platform;
}
}

}
Expand Down
4 changes: 2 additions & 2 deletions MonoGame.Framework/Android/AndroidGamePlatform.cs
Expand Up @@ -161,8 +161,8 @@ public override bool BeforeRun()
// Get the Accelerometer going
Accelerometer.SetupAccelerometer();

Window.Run(1.0 / Game.TargetElapsedTime.TotalSeconds);
//Window.Pause();
// Run it as fast as we can to allow for more response on threaded GPU resource creation
Window.Run();

return false;
}
Expand Down
31 changes: 12 additions & 19 deletions MonoGame.Framework/Android/AndroidGameWindow.cs
Expand Up @@ -81,6 +81,8 @@ public class AndroidGameWindow : AndroidGameView , Android.Views.View.IOnTouchLi
double updateFrameLast;
double renderFrameLast;
public GraphicsContext BackgroundContext;
// Work-around for FrameEventArgs.Time being unusable as it is being modified by a background thread constantly
System.Diagnostics.Stopwatch frameTime = new System.Diagnostics.Stopwatch();

public AndroidGameWindow(Context context, Game game) : base(context)
{
Expand Down Expand Up @@ -153,9 +155,6 @@ public override bool OnKeyUp(Keycode keyCode, KeyEvent e)

protected override void CreateFrameBuffer()
{
// Allow threaded resource loading
OpenTK.Graphics.GraphicsContext.ShareContexts = true;

#if true
try
{
Expand All @@ -174,25 +173,13 @@ protected override void CreateFrameBuffer()

if (!GraphicsContext.IsCurrent)
MakeCurrent();

// Create a context for the background loading
GraphicsContextFlags flags = GraphicsContextFlags.Default;
#if DEBUG
//flags |= GraphicsContextFlags.Debug;
#endif
GraphicsMode mode = new GraphicsMode();
BackgroundContext = new GraphicsContext(mode, WindowInfo, 2, 0, flags);
Threading.BackgroundContext = BackgroundContext;
Threading.WindowInfo = WindowInfo;
}

protected override void OnLoad(EventArgs e)
{
// FIXME: Uncomment this line when moving to Mono for Android 4.0.5
//MakeCurrent();

// Make sure an Update is called before a Draw
updateFrameElapsed = _game.TargetElapsedTime.TotalSeconds;
frameTime.Start();

base.OnLoad(e);
}
Expand All @@ -210,10 +197,13 @@ protected override void OnRenderFrame(FrameEventArgs e)
if (!GraphicsContext.IsCurrent)
MakeCurrent();

Threading.Run();

if (_game != null)
{
double targetElapsed = _game.TargetElapsedTime.TotalSeconds;
renderFrameElapsed += e.Time;
//renderFrameElapsed += e.Time;
renderFrameElapsed = frameTime.Elapsed.TotalSeconds;
if (renderFrameElapsed < (renderFrameLast + targetElapsed))
return;

Expand All @@ -238,10 +228,13 @@ protected override void OnUpdateFrame(FrameEventArgs e)
{
base.OnUpdateFrame(e);

if (_game != null )
Threading.Run();

if (_game != null)
{
double targetElapsed = _game.TargetElapsedTime.TotalSeconds;
updateFrameElapsed += e.Time;
//updateFrameElapsed += e.Time;
updateFrameElapsed = frameTime.Elapsed.TotalSeconds;
if (updateFrameElapsed < (updateFrameLast + targetElapsed))
return;

Expand Down
53 changes: 32 additions & 21 deletions MonoGame.Framework/Content/ContentManager.cs
Expand Up @@ -162,37 +162,45 @@ public virtual T Load<T>(string assetName)
}

T result = default(T);
// Serialize access to loadedAssets with a lock
lock (ContentManagerLock)

// Check for a previously loaded asset first
object asset = null;
if (loadedAssets.TryGetValue(assetName, out asset))
{
// Check for a previously loaded asset first
object asset = null;
if (loadedAssets.TryGetValue(assetName, out asset))
if (asset is T)
{
if (asset is T)
{
return (T)asset;
}
return (T)asset;
}
}

// Load the asset.
result = ReadAsset<T>(assetName, null);
// Load the asset.
result = ReadAsset<T>(assetName, null);

// Cache the result.
if (!loadedAssets.ContainsKey(assetName))
{
loadedAssets.Add(assetName, result);
}
// Cache the result.
if (!loadedAssets.ContainsKey(assetName))
{
loadedAssets.Add(assetName, result);
}
return result;

return result;
}

protected virtual Stream OpenStream(string assetName)
{
Stream stream;
try {
string assetPath = Path.Combine (_rootDirectory, assetName)+".xnb";
try
{
string assetPath = Path.Combine(_rootDirectory, assetName) + ".xnb";
stream = TitleContainer.OpenStream(assetPath);
#if ANDROID
// Read the asset into memory in one go. This results in a ~50% reduction
// in load times on Android due to slow Android asset streams.
MemoryStream memStream = new MemoryStream();
stream.CopyTo(memStream);
memStream.Seek(0, SeekOrigin.Begin);
stream.Close();
stream = memStream;
#endif
}
catch (FileNotFoundException fileNotFound)
{
Expand Down Expand Up @@ -236,11 +244,14 @@ protected T ReadAsset<T>(string assetName, Action<IDisposable> recordDisposableO

Stream stream = null;
bool loadXnb = false;
try {
try
{
//try load it traditionally
stream = OpenStream(assetName);
loadXnb = true;
} catch (ContentLoadException) {
}
catch (ContentLoadException)
{
//MonoGame try to load as a non-content file

assetName = TitleContainer.GetFilename(Path.Combine (_rootDirectory, assetName));
Expand Down
19 changes: 7 additions & 12 deletions MonoGame.Framework/Graphics/Effect/DXShader.cs
Expand Up @@ -34,11 +34,11 @@ internal class DXShader

public readonly ShaderType ShaderType;

public readonly int ShaderHandle;
public int ShaderHandle;

#if DEBUG
// We only keep around the GLSL code for debugging.
private readonly string _glslCode;
private string _glslCode;
#endif

private struct Attribute
Expand Down Expand Up @@ -151,8 +151,7 @@ internal DXShader(GraphicsDevice device, BinaryReader reader)
}


Threading.Begin();
try
Threading.BlockOnUIThread(() =>
{
ShaderHandle = GL.CreateShader(ShaderType);
#if GLES
Expand Down Expand Up @@ -193,16 +192,12 @@ internal DXShader(GraphicsDevice device, BinaryReader reader)
GL.DeleteShader(ShaderHandle);
throw new InvalidOperationException("Shader Compilation Failed");
}
}
finally
{
Threading.End();
}
});

#endif // OPENGL
}

#if OPENGL
#if OPENGL

public void OnLink(int program)
{
Expand Down Expand Up @@ -237,7 +232,7 @@ public void OnLink(int program)
}
}
}



public void Apply( GraphicsDevice graphicsDevice,
int program,
Expand Down Expand Up @@ -284,7 +279,7 @@ public void OnLink(int program)
tex.Activate();
samplerStates[sampler.index].Activate(tex.glTarget, tex.LevelCount > 1);
}
}
}

// Update and set the constants.
for (var c = 0; c < _cbuffers.Length; c++)
Expand Down
9 changes: 2 additions & 7 deletions MonoGame.Framework/Graphics/Effect/EffectPass.cs
Expand Up @@ -104,8 +104,7 @@ internal EffectPass(Effect effect, EffectPass cloneSource)
private void Initialize()
{
#if OPENGL
Threading.Begin();
try
Threading.BlockOnUIThread(() =>
{
// TODO: Shouldn't we be calling GL.DeleteProgram() somewhere?
Expand Down Expand Up @@ -136,11 +135,7 @@ private void Initialize()
#endif
throw new InvalidOperationException("Unable to link effect program");
}
}
finally
{
Threading.End();
}
});

#elif DIRECTX

Expand Down
6 changes: 1 addition & 5 deletions MonoGame.Framework/Graphics/GraphicsDevice.cs
Expand Up @@ -143,10 +143,6 @@ public class GraphicsDevice : IDisposable

#endif // DIRECTX

#if ANDROID
internal GamePlatform platform;
#endif

#if OPENGL

// OpenGL ES2.0 attribute locations
Expand Down Expand Up @@ -782,7 +778,7 @@ public void Present()
#elif PSS
_graphics.SwapBuffers();
#elif ANDROID
platform.Present();
Game.Instance.Platform.Present();
#elif OPENGL
GL.Flush ();
#endif
Expand Down
51 changes: 13 additions & 38 deletions MonoGame.Framework/Graphics/Texture2D.cs
Expand Up @@ -142,8 +142,7 @@ public Texture2D(GraphicsDevice graphicsDevice, int width, int height, bool mipm

this.glTarget = TextureTarget.Texture2D;

Threading.Begin();
try
Threading.BlockOnUIThread(() =>
{
#if IPHONE || ANDROID
GL.GenTextures(1, ref this.glTexture);
Expand Down Expand Up @@ -223,11 +222,7 @@ public Texture2D(GraphicsDevice graphicsDevice, int width, int height, bool mipm
this.levelCount++;
}
}
}
finally
{
Threading.End();
}
});
#endif
}

Expand Down Expand Up @@ -270,10 +265,8 @@ public int Height
if (data == null)
throw new ArgumentNullException("data");

#if !DIRECTX
// WTF is this? Document your code people!
Threading.Begin();
try
#if OPENGL
Threading.BlockOnUIThread(() =>
{
#endif
#if !PSS
Expand Down Expand Up @@ -340,12 +333,8 @@ public int Height
dataHandle.Free();
#endif
#if !DIRECTX
}
finally
{
Threading.End();
}
#if OPENGL
});
#endif
}

Expand Down Expand Up @@ -426,16 +415,11 @@ public static Texture2D FromStream(GraphicsDevice graphicsDevice, Stream stream)
colorSpace.Dispose();

Texture2D texture = null;
Threading.Begin();
try
Threading.BlockOnUIThread(() =>
{
texture = new Texture2D(graphicsDevice, width, height, false, SurfaceFormat.Color);
texture.SetData(data);
}
finally
{
Threading.End();
}
});

return texture;
}
Expand Down Expand Up @@ -474,16 +458,12 @@ public static Texture2D FromStream(GraphicsDevice graphicsDevice, Stream stream)
}

Texture2D texture = null;
Threading.Begin();
try
Threading.BlockOnUIThread(() =>
{
texture = new Texture2D(graphicsDevice, width, height, false, SurfaceFormat.Color);
texture.SetData<int>(pixels);
}
finally
{
Threading.End();
}
});

return texture;

#elif DIRECTX
Expand All @@ -505,16 +485,11 @@ public static Texture2D FromStream(GraphicsDevice graphicsDevice, Stream stream)
image.UnlockBits(bitmapData);

Texture2D texture = null;
Threading.Begin();
try
Threading.BlockOnUIThread(() =>
{
texture = new Texture2D(graphicsDevice, image.Width, image.Height);
texture.SetData(data);
}
finally
{
Threading.End();
}
});

return texture;
}
Expand Down

0 comments on commit 391a157

Please sign in to comment.