Browse Source

Add sample that shows how to load assets in the background. Submitted by CircleOf14

Kenneth Pouncey 13 years ago
parent
commit
1f0bb6e8b3

+ 76 - 0
Samples/MacOS/BackgroundThreadTester/BackgroundThreadTester.csproj

@@ -0,0 +1,76 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <PropertyGroup>
+    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+    <Platform Condition=" '$(Platform)' == '' ">x86</Platform>
+    <ProductVersion>10.0.0</ProductVersion>
+    <SchemaVersion>2.0</SchemaVersion>
+    <ProjectGuid>{8592FA21-37B6-41C8-BC7F-17EB4930B96A}</ProjectGuid>
+    <ProjectTypeGuids>{948B3504-5B70-4649-8FE4-BDE1FB46EC69};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
+    <OutputType>Exe</OutputType>
+    <RootNamespace>BackgroundThreadTester</RootNamespace>
+    <AssemblyName>BackgroundThreadTester</AssemblyName>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
+    <DebugSymbols>true</DebugSymbols>
+    <DebugType>full</DebugType>
+    <Optimize>false</Optimize>
+    <OutputPath>bin\Debug</OutputPath>
+    <DefineConstants>DEBUG;</DefineConstants>
+    <ErrorReport>prompt</ErrorReport>
+    <WarningLevel>4</WarningLevel>
+    <PlatformTarget>x86</PlatformTarget>
+    <ConsolePause>false</ConsolePause>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
+    <DebugType>none</DebugType>
+    <Optimize>false</Optimize>
+    <OutputPath>bin\Release</OutputPath>
+    <ErrorReport>prompt</ErrorReport>
+    <WarningLevel>4</WarningLevel>
+    <PlatformTarget>x86</PlatformTarget>
+    <ConsolePause>false</ConsolePause>
+  </PropertyGroup>
+  <ItemGroup>
+    <Reference Include="System" />
+    <Reference Include="System.Xml" />
+    <Reference Include="System.Core" />
+    <Reference Include="System.Xml.Linq" />
+    <Reference Include="System.Drawing" />
+    <Reference Include="MonoMac" />
+    <Reference Include="System.Windows.Forms" />
+  </ItemGroup>
+  <ItemGroup>
+    <None Include="Info.plist" />
+    <None Include="Content\beehive.png" />
+    <None Include="README.md" />
+  </ItemGroup>
+  <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
+  <Import Project="$(MSBuildExtensionsPath)\Mono\MonoMac\v0.0\Mono.MonoMac.targets" />
+  <ItemGroup>
+    <Folder Include="Content\" />
+  </ItemGroup>
+  <ItemGroup>
+    <Content Include="Content\fntStandard.xnb" />
+    <Content Include="Content\UiCursor.xnb" />
+    <Content Include="Content\beehive.xnb" />
+  </ItemGroup>
+  <ItemGroup>
+    <Compile Include="Enums.cs" />
+    <Compile Include="Game1.cs" />
+    <Compile Include="InputManager.cs" />
+    <Compile Include="Object.cs" />
+    <Compile Include="Program.cs" />
+    <Compile Include="TextManager.cs" />
+    <Compile Include="TestTexture.cs" />
+  </ItemGroup>
+  <ItemGroup>
+    <InterfaceDefinition Include="MainMenu.xib" />
+  </ItemGroup>
+  <ItemGroup>
+    <ProjectReference Include="..\..\..\..\MonoGame\MonoGame.Framework\MonoGame.Framework.MacOS.csproj">
+      <Project>{36C538E6-C32A-4A8D-A39C-566173D7118E}</Project>
+      <Name>MonoGame.Framework.MacOS</Name>
+    </ProjectReference>
+  </ItemGroup>
+</Project>

BIN
Samples/MacOS/BackgroundThreadTester/Content/UiCursor.xnb


BIN
Samples/MacOS/BackgroundThreadTester/Content/beehive.png


BIN
Samples/MacOS/BackgroundThreadTester/Content/beehive.xnb


BIN
Samples/MacOS/BackgroundThreadTester/Content/fntStandard.xnb


+ 16 - 0
Samples/MacOS/BackgroundThreadTester/Enums.cs

@@ -0,0 +1,16 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace BackgroundThreadTester
+{
+    public enum MsState
+    {
+        ButtonWasPressed,
+        ButtonWasDoublePressed,
+        ButtonWasReleased,
+        ButtonStillPressed,
+        ButtonStillReleased
+    }//MsState
+}

+ 195 - 0
Samples/MacOS/BackgroundThreadTester/Game1.cs

@@ -0,0 +1,195 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using Microsoft.Xna.Framework;
+using Microsoft.Xna.Framework.Audio;
+using Microsoft.Xna.Framework.Content;
+using Microsoft.Xna.Framework.GamerServices;
+using Microsoft.Xna.Framework.Graphics;
+using Microsoft.Xna.Framework.Input;
+using Microsoft.Xna.Framework.Media;
+using System.Windows.Forms;
+using System.Threading;
+
+using MonoMac.AppKit;
+using MonoMac.Foundation;
+
+namespace BackgroundThreadTester
+{
+	public class Game1 : Microsoft.Xna.Framework.Game
+	{
+		GraphicsDeviceManager graphics;
+		SpriteBatch spriteBatch;
+		public TextManager aTm;
+		public InputManager cIm;
+		private SpriteFont sfStandard;
+		public MouseState mousestatus;
+		public Object aObjects;
+		TimeSpan tsElapsed = TimeSpan.Zero;
+		private String sLoading = "Loading";
+
+		public Game1 ()
+		{
+			this.IsMouseVisible = true;
+			graphics = new GraphicsDeviceManager (this);
+			Content.RootDirectory = "Content";
+
+			cIm = new InputManager (this);   
+            
+			CenterWindow ();    
+		}
+
+		protected override void Initialize ()
+		{
+			// TODO: Add your initialization logic here
+
+			base.Initialize ();
+		}
+
+		protected override void LoadContent ()
+		{
+			// Create a new SpriteBatch, which can be used to draw textures.
+			spriteBatch = new SpriteBatch (GraphicsDevice);
+			Services.AddService (typeof(SpriteBatch), spriteBatch);
+
+			sfStandard = Content.Load<SpriteFont> ("fntStandard");
+
+			aTm = new TextManager (this, sfStandard);
+			Components.Add (aTm);
+		}
+
+		protected override void UnloadContent ()
+		{
+			// TODO: Unload any non ContentManager content here
+		}
+
+		public void CreateBackgroundThread ()
+		{
+			System.Console.WriteLine ("before invoke");
+			// create a new thread using BackgroundWorkerThread as method to execute
+			var thread = new Thread (BackgroundWorkerThread as ThreadStart);
+			// start it
+			thread.Start ();
+
+			System.Console.WriteLine ("after invoke");
+		}//if
+        
+		void BackgroundWorkerThread ()
+		{
+			// Create an Autorelease Pool or we will leak objects.
+			using (var pool = new NSAutoreleasePool()) {
+				// Create a loop that will add 5 new components with
+				// a 2 second pause between additions
+				Console.WriteLine ("Before component load");
+				for (int x = 1; x <= 5; x++) {
+
+					Console.WriteLine ("Before add");
+
+					// Make sure we invoke this on the Main Thread or OpenGL will throw an error
+					MonoMac.AppKit.NSApplication.SharedApplication.BeginInvokeOnMainThread (delegate {
+						Components.Add (new TestTexture (this));
+					});
+					Console.WriteLine ("After add");
+					// Sleep for 2 seconds between each component addition
+					Thread.Sleep (2000);
+
+				}
+				Console.WriteLine ("After component load");
+
+			}
+
+		}
+
+		public int GetBackBufferWidth ()
+		{
+			return graphics.PreferredBackBufferWidth;
+		}//GetBackBufferWidth
+
+		public int GetBackBufferHeight ()
+		{
+			return graphics.PreferredBackBufferHeight;
+		}//GetBackBufferWidth
+        
+		public String GetStyleMask ()
+		{
+			return this.Window.Window.StyleMask.ToString ();
+		}//GetStyleMask
+
+
+		protected override void Update (GameTime gameTime)
+		{
+			mousestatus = Mouse.GetState ();
+
+			cIm.InputHandler (mousestatus, gameTime);
+
+			base.Update (gameTime);
+		}
+
+		protected override void Draw (GameTime gameTime)
+		{
+            
+			GraphicsDevice.Clear (Color.CornflowerBlue);
+
+			spriteBatch.Begin (SpriteSortMode.Immediate, BlendState.AlphaBlend);
+			DrawLoadingAnimation (gameTime);
+			base.Draw (gameTime); 
+			spriteBatch.End ();
+
+       
+		}
+        
+		void DrawLoadingAnimation (GameTime gameTime)
+		{
+			tsElapsed += gameTime.ElapsedGameTime;
+
+			// it's time for next char
+			if (tsElapsed > TimeSpan.FromMilliseconds (500)) {
+				tsElapsed = TimeSpan.Zero;
+
+				sLoading = sLoading.Insert (sLoading.Length, ".");
+
+				if (sLoading.Length == 13) {
+					sLoading = "Loading";
+				}//if
+			}//if
+
+
+			spriteBatch.DrawString (sfStandard, sLoading, new Vector2 (50, 50), Color.White);
+		}//DrawLoadingAnimation
+        
+		public void CenterWindow ()
+		{
+        	
+        		
+			int index;
+			int upperBound;
+			float fScreenWidth, fScreenHeight, fNewX, fNewY, fWindowWidth, fWindowHeight, fTitleBarHeight;
+			Screen[] screens = Screen.AllScreens;
+	        
+			fScreenWidth = fScreenHeight = 0;
+	        
+			upperBound = screens.GetUpperBound (0);
+			for (index = 0; index <= upperBound; index++) {
+				if (screens [index].Primary) {
+					fScreenWidth = (float)screens [index].Bounds.Width;
+					fScreenHeight = (float)screens [index].Bounds.Height;  
+					index = upperBound;
+				}//if
+			}//for
+            
+			fWindowWidth = graphics.PreferredBackBufferWidth;
+			fWindowHeight = graphics.PreferredBackBufferHeight;
+            	
+			fNewX = (fScreenWidth - fWindowWidth) / 2;
+			fNewY = (fScreenHeight - fWindowHeight) / 2;
+            
+			fTitleBarHeight = this.Window.Window.Frame.Height - fWindowHeight;
+            
+			System.Drawing.PointF pfLocation = new System.Drawing.PointF (fNewX, fNewY);
+			System.Drawing.PointF pfSize = new System.Drawing.PointF (fWindowWidth, fWindowHeight + fTitleBarHeight);
+			System.Drawing.SizeF sfSize = new System.Drawing.SizeF (pfSize);
+			System.Drawing.RectangleF rectTemp = new System.Drawing.RectangleF (pfLocation, sfSize);
+			this.Window.Window.SetFrame (rectTemp, true);
+		}//CenterWindow
+	}
+}

+ 18 - 0
Samples/MacOS/BackgroundThreadTester/Info.plist

@@ -0,0 +1,18 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
+<plist version="1.0">
+<dict>
+	<key>CFBundleIdentifier</key>
+	<string>com.yourcompany.BackgroundThreadTester</string>
+	<key>CFBundleName</key>
+	<string>BackgroundThreadTester</string>
+	<key>CFBundleVersion</key>
+	<string>1</string>
+	<key>LSMinimumSystemVersion</key>
+	<string>10.6</string>
+	<key>NSMainNibFile</key>
+	<string>MainMenu</string>
+	<key>NSPrincipalClass</key>
+	<string>NSApplication</string>
+</dict>
+</plist>

+ 119 - 0
Samples/MacOS/BackgroundThreadTester/InputManager.cs

@@ -0,0 +1,119 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using Microsoft.Xna.Framework;
+using Microsoft.Xna.Framework.Input;
+using Microsoft.Xna.Framework.Net;
+using Microsoft.Xna.Framework.Storage;
+using Microsoft.Xna.Framework.Media;
+using System.IO;
+using Microsoft.Xna.Framework.GamerServices;
+
+namespace BackgroundThreadTester
+{
+    public class InputManager
+    {
+        private Game1 cG;
+        private MouseState mousestatus;
+        private MsState mssButtonLeft;
+        public TimeSpan tsTimeSinceLastClick;
+        private int nClicksForDoubleClick;
+
+
+        public InputManager(Game game)
+        {
+            cG = (Game1)game;
+            mssButtonLeft = MsState.ButtonWasReleased;
+            tsTimeSinceLastClick = TimeSpan.Zero;
+            nClicksForDoubleClick = 0;
+
+        }//InputManger
+
+        public void InputHandler(MouseState mst, GameTime gameTime)
+        {
+            if (!cG.IsActive) return;
+            
+            mousestatus = mst;
+
+            HandleMouseLeftButton(gameTime);
+
+            if (mssButtonLeft == MsState.ButtonWasPressed)
+            {
+                if (mousestatus.X >= 38 && mousestatus.X <= 702)
+                {
+                    if (mousestatus.Y >= 200 && mousestatus.Y <= 296)
+                    {
+                        cG.CreateBackgroundThread();
+                    }//if
+                }//if
+            }//if
+            
+
+        }//InputHandler
+
+        private void HandleMouseLeftButton(GameTime gTime)
+        {
+            tsTimeSinceLastClick += gTime.ElapsedGameTime;
+
+            if (tsTimeSinceLastClick >= TimeSpan.FromMilliseconds(250))
+            {
+                nClicksForDoubleClick = 0;
+            }//if
+
+            if (mousestatus.LeftButton == ButtonState.Pressed)
+            {
+                if (mssButtonLeft == MsState.ButtonWasReleased)
+                {
+                    if (GetMouseX() >= 0 && GetMouseX() <= cG.GetBackBufferWidth())
+                    {
+                        if (GetMouseY() >= 0 && GetMouseY() <= cG.GetBackBufferHeight())
+                        {
+                            mssButtonLeft = MsState.ButtonWasPressed;
+                            nClicksForDoubleClick++;
+
+                            if (nClicksForDoubleClick == 1)
+                            {
+                                tsTimeSinceLastClick = TimeSpan.Zero;
+                            }//if
+
+                            if (nClicksForDoubleClick == 2)
+                            {
+                                if (tsTimeSinceLastClick < TimeSpan.FromMilliseconds(250))
+                                {
+                                    nClicksForDoubleClick = 0;
+                                    mssButtonLeft = MsState.ButtonWasDoublePressed;
+                                }//if
+                            }//if
+
+                            if (nClicksForDoubleClick == 3) nClicksForDoubleClick = 0;
+
+                        }//if
+                    }//if
+                }//if
+                else
+                {
+                    if (mssButtonLeft == MsState.ButtonWasPressed || mssButtonLeft == MsState.ButtonWasDoublePressed)
+                    {
+                        mssButtonLeft = MsState.ButtonStillPressed;
+                    }//if
+                }//else
+            }//if
+
+            if (mousestatus.LeftButton == ButtonState.Released)
+            {
+                mssButtonLeft = MsState.ButtonWasReleased;
+            }//if
+        }//HandleMouseLeftButton
+
+        public float GetMouseX()
+        {
+            return mousestatus.X;
+        }//GetMouseX
+
+        public float GetMouseY()
+        {
+            return mousestatus.Y;
+        }//GetMouseY
+    }
+}

+ 118 - 0
Samples/MacOS/BackgroundThreadTester/MainMenu.xib

@@ -0,0 +1,118 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<archive type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="7.10">
+	<data>
+		<int key="IBDocument.SystemTarget">1060</int>
+		<string key="IBDocument.SystemVersion">10D573</string>
+		<string key="IBDocument.InterfaceBuilderVersion">762</string>
+		<string key="IBDocument.AppKitVersion">1038.29</string>
+		<string key="IBDocument.HIToolboxVersion">460.00</string>
+		<object class="NSMutableDictionary" key="IBDocument.PluginVersions">
+			<string key="NS.key.0">com.apple.InterfaceBuilder.CocoaPlugin</string>
+			<string key="NS.object.0">762</string>
+		</object>
+		<object class="NSMutableArray" key="IBDocument.EditedObjectIDs">
+			<bool key="EncodedWithXMLCoder">YES</bool>
+		</object>
+		<object class="NSArray" key="IBDocument.PluginDependencies">
+			<bool key="EncodedWithXMLCoder">YES</bool>
+			<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
+		</object>
+		<object class="NSMutableDictionary" key="IBDocument.Metadata">
+			<bool key="EncodedWithXMLCoder">YES</bool>
+			<object class="NSArray" key="dict.sortedKeys" id="0">
+				<bool key="EncodedWithXMLCoder">YES</bool>
+			</object>
+			<object class="NSMutableArray" key="dict.values">
+				<bool key="EncodedWithXMLCoder">YES</bool>
+			</object>
+		</object>
+		<object class="NSMutableArray" key="IBDocument.RootObjects" id="1000">
+			<bool key="EncodedWithXMLCoder">YES</bool>
+			<object class="NSCustomObject" id="1001">
+				<string key="NSClassName">NSObject</string>
+			</object>
+			<object class="NSCustomObject" id="1003">
+				<string key="NSClassName">FirstResponder</string>
+			</object>
+			<object class="NSCustomObject" id="1004">
+				<string key="NSClassName">NSApplication</string>
+			</object>
+		</object>
+		<object class="IBObjectContainer" key="IBDocument.Objects">
+			<object class="NSMutableArray" key="connectionRecords">
+				<bool key="EncodedWithXMLCoder">YES</bool>
+			</object>
+			<object class="IBMutableOrderedSet" key="objectRecords">
+				<object class="NSArray" key="orderedObjects">
+					<bool key="EncodedWithXMLCoder">YES</bool>
+					<object class="IBObjectRecord">
+						<int key="objectID">0</int>
+						<reference key="object" ref="0" />
+						<reference key="children" ref="1000" />
+						<nil key="parent" />
+					</object>
+					<object class="IBObjectRecord">
+						<int key="objectID">-2</int>
+						<reference key="object" ref="1001" />
+						<reference key="parent" ref="0" />
+						<string key="objectName">File's Owner</string>
+					</object>
+					<object class="IBObjectRecord">
+						<int key="objectID">-1</int>
+						<reference key="object" ref="1003" />
+						<reference key="parent" ref="0" />
+						<string key="objectName">First Responder</string>
+					</object>
+					<object class="IBObjectRecord">
+						<int key="objectID">-3</int>
+						<reference key="object" ref="1004" />
+						<reference key="parent" ref="0" />
+						<string key="objectName">Application</string>
+					</object>
+				</object>
+			</object>
+			<object class="NSMutableDictionary" key="flattenedProperties">
+				<bool key="EncodedWithXMLCoder">YES</bool>
+				<object class="NSArray" key="dict.sortedKeys">
+					<bool key="EncodedWithXMLCoder">YES</bool>
+					<string>-1.IBPluginDependency</string>
+					<string>-2.IBPluginDependency</string>
+					<string>-3.IBPluginDependency</string>
+				</object>
+				<object class="NSMutableArray" key="dict.values">
+					<bool key="EncodedWithXMLCoder">YES</bool>
+					<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
+					<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
+					<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
+				</object>
+			</object>
+			<object class="NSMutableDictionary" key="unlocalizedProperties">
+				<bool key="EncodedWithXMLCoder">YES</bool>
+				<reference key="dict.sortedKeys" ref="0" />
+				<object class="NSMutableArray" key="dict.values">
+					<bool key="EncodedWithXMLCoder">YES</bool>
+				</object>
+			</object>
+			<nil key="activeLocalization" />
+			<object class="NSMutableDictionary" key="localizations">
+				<bool key="EncodedWithXMLCoder">YES</bool>
+				<reference key="dict.sortedKeys" ref="0" />
+				<object class="NSMutableArray" key="dict.values">
+					<bool key="EncodedWithXMLCoder">YES</bool>
+				</object>
+			</object>
+			<nil key="sourceID" />
+			<int key="maxID">0</int>
+		</object>
+		<object class="IBClassDescriber" key="IBDocument.Classes" />
+		<int key="IBDocument.localizationMode">0</int>
+		<string key="IBDocument.TargetRuntimeIdentifier">IBCocoaFramework</string>
+		<object class="NSMutableDictionary" key="IBDocument.PluginDeclaredDevelopmentDependencies">
+			<string key="NS.key.0">com.apple.InterfaceBuilder.CocoaPlugin.InterfaceBuilder3</string>
+			<integer value="3000" key="NS.object.0" />
+		</object>
+		<bool key="IBDocument.PluginDeclaredDependenciesTrackSystemTargetVersion">YES</bool>
+		<nil key="IBDocument.LastKnownRelativeProjectPath" />
+		<int key="IBDocument.defaultPropertyAccessControl">3</int>
+	</data>
+</archive>

+ 80 - 0
Samples/MacOS/BackgroundThreadTester/Object.cs

@@ -0,0 +1,80 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using Microsoft.Xna.Framework;
+using Microsoft.Xna.Framework.Audio;
+using Microsoft.Xna.Framework.Content;
+using Microsoft.Xna.Framework.GamerServices;
+using Microsoft.Xna.Framework.Graphics;
+using Microsoft.Xna.Framework.Input;
+using Microsoft.Xna.Framework.Media;
+using Microsoft.Xna.Framework.Net;
+using Microsoft.Xna.Framework.Storage;
+
+
+namespace BackgroundThreadTester
+{
+    public class Object : Microsoft.Xna.Framework.DrawableGameComponent
+    {
+        public Vector2 pos;
+        protected Texture2D texture;
+        private Vector2 v2Temp;
+        protected Rectangle recCurrentFrame;
+        public int nAlpha;
+        public float fRotation;
+        public Vector2 vecRotationCenter;
+        private List<Rectangle> frames;
+
+        public Object(Game game, ref Texture2D theTexture)
+            : base(game)
+        {
+            texture = theTexture;
+            v2Temp = new Vector2();
+            fRotation = 0;
+            
+            pos = new Vector2();
+            pos.X = 0;
+            pos.Y = 0;
+
+            vecRotationCenter.X = 0;
+            vecRotationCenter.Y = 0;
+
+            Frames = new List<Rectangle>();
+            Rectangle frame = new Rectangle();
+
+            //Extract the frames from the texture
+            frame.X = 0;
+            frame.Y = 0;
+            frame.Width = texture.Width;
+            frame.Height = texture.Height;
+
+            Frames.Add(frame);
+        }
+
+        public override void Update(GameTime gameTime)
+        {
+            recCurrentFrame = frames[0];
+            
+            base.Update(gameTime);
+        }
+
+        public override void Draw(GameTime gameTime)
+        {
+            v2Temp.X = pos.X - (texture.Width / 2);
+            v2Temp.Y = pos.Y - texture.Height;
+
+            // Get the current spritebatch
+            SpriteBatch sBatch = (SpriteBatch)Game.Services.GetService(typeof(SpriteBatch));
+
+            sBatch.Draw(texture, v2Temp, recCurrentFrame, new Color(255, 255, 255, (byte)nAlpha), fRotation, vecRotationCenter, 1f, SpriteEffects.None, 0);
+
+            base.Draw(gameTime);
+        }
+
+        public List<Rectangle> Frames
+        {
+            get { return frames; }
+            set { frames = value; }
+        }//Frames
+    }
+}

+ 68 - 0
Samples/MacOS/BackgroundThreadTester/Program.cs

@@ -0,0 +1,68 @@
+using MonoMac.AppKit;
+using MonoMac.Foundation;
+
+
+namespace BackgroundThreadTester
+{
+	class Program
+	{
+		static void Main (string [] args)
+		{
+			NSApplication.Init ();
+			
+			using (var p = new NSAutoreleasePool ()) {
+				NSApplication.SharedApplication.Delegate = new AppDelegate();
+				NSApplication.Main(args);
+			}
+		}
+	}
+	
+	
+	class AppDelegate : NSApplicationDelegate
+	{
+		Game1 game; 
+		
+		public override void FinishedLaunching (MonoMac.Foundation.NSObject notification)
+		{			
+			using (game = new Game1()) {
+				game.Run ();
+			}
+		}
+		
+		public override bool ApplicationShouldTerminateAfterLastWindowClosed (NSApplication sender)
+		{
+			return true;
+		}
+		
+		public override NSApplicationTerminateReply ApplicationShouldTerminate (NSApplication sender)
+		{
+
+
+				
+				NSAlert alert = NSAlert.WithMessage("Warning", "Yes", "No", null, "Do you really want to close?");
+				
+				var button = alert.RunModal();
+				
+				if ( button == 0 )
+				{
+					return NSApplicationTerminateReply.Cancel;		
+				}//if
+				else
+				{
+					return NSApplicationTerminateReply.Now;
+				}//else
+
+		}
+		
+		
+
+
+
+
+	}
+	
+	
+	
+
+}
+

+ 15 - 0
Samples/MacOS/BackgroundThreadTester/README.md

@@ -0,0 +1,15 @@
+BackgroundThreadSample
+======================
+
+This sample demonstrates the loading of assets in a background thread.
+
+Sample originally created by CircleOf14 and modified by Kenneth Pouncey to create new textures to be added
+to the game components dynamically in the background.
+
+Of special interest look at the following methods:
+
+CreateBackgroundThread () - Creates a thread to be executed in the background and starts it.
+BackgroundWorkerThread () - Worker thread that actually does the work of creating the new asset and adding
+
+Make sure to read the comments in these two methods.
+

+ 50 - 0
Samples/MacOS/BackgroundThreadTester/TestTexture.cs

@@ -0,0 +1,50 @@
+using System;
+using Microsoft.Xna.Framework;
+using Microsoft.Xna.Framework.Content;
+using Microsoft.Xna.Framework.Graphics;
+
+namespace BackgroundThreadTester
+{
+	public class TestTexture : DrawableGameComponent
+	{
+		Texture2D texture;
+		int x, y;
+		static Random random = new Random();
+		Game1 game;
+		SpriteBatch spriteBatch;
+		Vector2 position;
+
+		public TestTexture (Game1 game) : base (game)
+		{
+			this.game = game;
+		}
+
+		public override void Initialize ()
+		{
+
+			base.Initialize ();
+		}
+
+		protected override void LoadContent ()
+		{
+
+			texture = game.Content.Load<Texture2D>("beehive");
+
+			// Create a random position
+			x = random.Next(0, this.game.GetBackBufferWidth () - texture.Width);
+			y = random.Next(0, this.game.GetBackBufferHeight () - texture.Height);
+			position = new Vector2(x,y);
+
+			// Create a new spritebatch
+			spriteBatch = new SpriteBatch(this.Game.GraphicsDevice);
+		}
+
+		public override void Draw (GameTime gameTime)
+		{
+			spriteBatch.Begin();
+			spriteBatch.Draw(texture, position, Color.White);
+			spriteBatch.End();
+		}
+	}
+}
+

+ 33 - 0
Samples/MacOS/BackgroundThreadTester/TextManager.cs

@@ -0,0 +1,33 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using Microsoft.Xna.Framework;
+using Microsoft.Xna.Framework.Audio;
+using Microsoft.Xna.Framework.Content;
+using Microsoft.Xna.Framework.GamerServices;
+using Microsoft.Xna.Framework.Graphics;
+using Microsoft.Xna.Framework.Input;
+using Microsoft.Xna.Framework.Media;
+using Microsoft.Xna.Framework.Net;
+using Microsoft.Xna.Framework.Storage;
+namespace BackgroundThreadTester
+{
+    public class TextManager : Microsoft.Xna.Framework.DrawableGameComponent
+    {
+        public SpriteFont sfStandard;
+        protected SpriteBatch spriteBatch = null;
+        
+        public TextManager(Game game, SpriteFont sfStandardFont)
+            : base(game)
+        {
+            spriteBatch = (SpriteBatch)Game.Services.GetService(typeof(SpriteBatch));
+            sfStandard = sfStandardFont;
+
+        }//TextManager
+
+        public override void Draw(GameTime gameTime)
+        {  
+            spriteBatch.DrawString(sfStandard, "Click here to create background thread and make it\nadd 5 new components", new Vector2(50, 200), Color.White);
+        }//Draw
+    }
+}

+ 20 - 0
Samples/MonoGame.Samples.MacOS.sln

@@ -69,6 +69,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StarWarrior", "MacOS\StarWa
 EndProject
 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Primitives", "MacOS\Primitives\Primitives.csproj", "{2DDBFED4-9955-4F02-9937-B9AE82836329}"
 EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BackgroundThreadTester", "MacOS\BackgroundThreadTester\BackgroundThreadTester.csproj", "{8592FA21-37B6-41C8-BC7F-17EB4930B96A}"
+EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
 		Debug|Any CPU = Debug|Any CPU
@@ -334,6 +336,24 @@ Global
 		{81313197-A8DF-4465-B503-2DD8248B80A9}.Release|Any CPU.Build.0 = Release|Any CPU
 		{81313197-A8DF-4465-B503-2DD8248B80A9}.Release|x86.ActiveCfg = Release|Any CPU
 		{81313197-A8DF-4465-B503-2DD8248B80A9}.Release|x86.Build.0 = Release|Any CPU
+		{8592FA21-37B6-41C8-BC7F-17EB4930B96A}.Debug|Any CPU.ActiveCfg = Debug|x86
+		{8592FA21-37B6-41C8-BC7F-17EB4930B96A}.Debug|Any CPU.Build.0 = Debug|x86
+		{8592FA21-37B6-41C8-BC7F-17EB4930B96A}.Debug|iPhone.ActiveCfg = Debug|x86
+		{8592FA21-37B6-41C8-BC7F-17EB4930B96A}.Debug|iPhone.Build.0 = Debug|x86
+		{8592FA21-37B6-41C8-BC7F-17EB4930B96A}.Debug|iPhoneSimulator.ActiveCfg = Debug|x86
+		{8592FA21-37B6-41C8-BC7F-17EB4930B96A}.Debug|iPhoneSimulator.Build.0 = Debug|x86
+		{8592FA21-37B6-41C8-BC7F-17EB4930B96A}.Debug|x86.ActiveCfg = Debug|x86
+		{8592FA21-37B6-41C8-BC7F-17EB4930B96A}.Debug|x86.Build.0 = Debug|x86
+		{8592FA21-37B6-41C8-BC7F-17EB4930B96A}.Distribution|Any CPU.ActiveCfg = Debug|x86
+		{8592FA21-37B6-41C8-BC7F-17EB4930B96A}.Distribution|Any CPU.Build.0 = Debug|x86
+		{8592FA21-37B6-41C8-BC7F-17EB4930B96A}.Release|Any CPU.ActiveCfg = Release|x86
+		{8592FA21-37B6-41C8-BC7F-17EB4930B96A}.Release|Any CPU.Build.0 = Release|x86
+		{8592FA21-37B6-41C8-BC7F-17EB4930B96A}.Release|iPhone.ActiveCfg = Release|x86
+		{8592FA21-37B6-41C8-BC7F-17EB4930B96A}.Release|iPhone.Build.0 = Release|x86
+		{8592FA21-37B6-41C8-BC7F-17EB4930B96A}.Release|iPhoneSimulator.ActiveCfg = Release|x86
+		{8592FA21-37B6-41C8-BC7F-17EB4930B96A}.Release|iPhoneSimulator.Build.0 = Release|x86
+		{8592FA21-37B6-41C8-BC7F-17EB4930B96A}.Release|x86.ActiveCfg = Release|x86
+		{8592FA21-37B6-41C8-BC7F-17EB4930B96A}.Release|x86.Build.0 = Release|x86
 		{9522776F-02BA-4BED-B8BB-6BAE580F4068}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
 		{9522776F-02BA-4BED-B8BB-6BAE580F4068}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{9522776F-02BA-4BED-B8BB-6BAE580F4068}.Debug|x86.ActiveCfg = Debug|Any CPU