Game1.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Microsoft.Xna.Framework;
  5. using Microsoft.Xna.Framework.Audio;
  6. using Microsoft.Xna.Framework.Content;
  7. using Microsoft.Xna.Framework.GamerServices;
  8. using Microsoft.Xna.Framework.Graphics;
  9. using Microsoft.Xna.Framework.Input;
  10. using Microsoft.Xna.Framework.Media;
  11. using System.Windows.Forms;
  12. using System.Threading;
  13. using MonoMac.AppKit;
  14. using MonoMac.Foundation;
  15. namespace BackgroundThreadTester
  16. {
  17. public class Game1 : Microsoft.Xna.Framework.Game
  18. {
  19. GraphicsDeviceManager graphics;
  20. SpriteBatch spriteBatch;
  21. public TextManager aTm;
  22. public InputManager cIm;
  23. private SpriteFont sfStandard;
  24. public MouseState mousestatus;
  25. public Object aObjects;
  26. TimeSpan tsElapsed = TimeSpan.Zero;
  27. private String sLoading = "Loading";
  28. public Game1 ()
  29. {
  30. this.IsMouseVisible = true;
  31. graphics = new GraphicsDeviceManager (this);
  32. Content.RootDirectory = "Content";
  33. cIm = new InputManager (this);
  34. CenterWindow ();
  35. }
  36. protected override void Initialize ()
  37. {
  38. // TODO: Add your initialization logic here
  39. base.Initialize ();
  40. }
  41. protected override void LoadContent ()
  42. {
  43. // Create a new SpriteBatch, which can be used to draw textures.
  44. spriteBatch = new SpriteBatch (GraphicsDevice);
  45. Services.AddService (typeof(SpriteBatch), spriteBatch);
  46. sfStandard = Content.Load<SpriteFont> ("fntStandard");
  47. aTm = new TextManager (this, sfStandard);
  48. Components.Add (aTm);
  49. }
  50. protected override void UnloadContent ()
  51. {
  52. // TODO: Unload any non ContentManager content here
  53. }
  54. public void CreateBackgroundThread ()
  55. {
  56. System.Console.WriteLine ("before invoke");
  57. // create a new thread using BackgroundWorkerThread as method to execute
  58. var thread = new Thread (BackgroundWorkerThread as ThreadStart);
  59. // start it
  60. thread.Start ();
  61. System.Console.WriteLine ("after invoke");
  62. }//if
  63. void BackgroundWorkerThread ()
  64. {
  65. // Create an Autorelease Pool or we will leak objects.
  66. using (var pool = new NSAutoreleasePool()) {
  67. // Create a loop that will add 5 new components with
  68. // a 2 second pause between additions
  69. Console.WriteLine ("Before component load");
  70. for (int x = 1; x <= 5; x++) {
  71. Console.WriteLine ("Before add");
  72. // Make sure we invoke this on the Main Thread or OpenGL will throw an error
  73. MonoMac.AppKit.NSApplication.SharedApplication.BeginInvokeOnMainThread (delegate {
  74. Components.Add (new TestTexture (this));
  75. });
  76. Console.WriteLine ("After add");
  77. // Sleep for 2 seconds between each component addition
  78. Thread.Sleep (2000);
  79. }
  80. Console.WriteLine ("After component load");
  81. }
  82. }
  83. public int GetBackBufferWidth ()
  84. {
  85. return graphics.PreferredBackBufferWidth;
  86. }//GetBackBufferWidth
  87. public int GetBackBufferHeight ()
  88. {
  89. return graphics.PreferredBackBufferHeight;
  90. }//GetBackBufferWidth
  91. public String GetStyleMask ()
  92. {
  93. return this.Window.Window.StyleMask.ToString ();
  94. }//GetStyleMask
  95. protected override void Update (GameTime gameTime)
  96. {
  97. mousestatus = Mouse.GetState ();
  98. cIm.InputHandler (mousestatus, gameTime);
  99. base.Update (gameTime);
  100. }
  101. protected override void Draw (GameTime gameTime)
  102. {
  103. GraphicsDevice.Clear (Color.CornflowerBlue);
  104. spriteBatch.Begin (SpriteSortMode.Immediate, BlendState.AlphaBlend);
  105. DrawLoadingAnimation (gameTime);
  106. base.Draw (gameTime);
  107. spriteBatch.End ();
  108. }
  109. void DrawLoadingAnimation (GameTime gameTime)
  110. {
  111. tsElapsed += gameTime.ElapsedGameTime;
  112. // it's time for next char
  113. if (tsElapsed > TimeSpan.FromMilliseconds (500)) {
  114. tsElapsed = TimeSpan.Zero;
  115. sLoading = sLoading.Insert (sLoading.Length, ".");
  116. if (sLoading.Length == 13) {
  117. sLoading = "Loading";
  118. }//if
  119. }//if
  120. spriteBatch.DrawString (sfStandard, sLoading, new Vector2 (50, 50), Color.White);
  121. }//DrawLoadingAnimation
  122. public void CenterWindow ()
  123. {
  124. int index;
  125. int upperBound;
  126. float fScreenWidth, fScreenHeight, fNewX, fNewY, fWindowWidth, fWindowHeight, fTitleBarHeight;
  127. Screen[] screens = Screen.AllScreens;
  128. fScreenWidth = fScreenHeight = 0;
  129. upperBound = screens.GetUpperBound (0);
  130. for (index = 0; index <= upperBound; index++) {
  131. if (screens [index].Primary) {
  132. fScreenWidth = (float)screens [index].Bounds.Width;
  133. fScreenHeight = (float)screens [index].Bounds.Height;
  134. index = upperBound;
  135. }//if
  136. }//for
  137. fWindowWidth = graphics.PreferredBackBufferWidth;
  138. fWindowHeight = graphics.PreferredBackBufferHeight;
  139. fNewX = (fScreenWidth - fWindowWidth) / 2;
  140. fNewY = (fScreenHeight - fWindowHeight) / 2;
  141. fTitleBarHeight = this.Window.Window.Frame.Height - fWindowHeight;
  142. System.Drawing.PointF pfLocation = new System.Drawing.PointF (fNewX, fNewY);
  143. System.Drawing.PointF pfSize = new System.Drawing.PointF (fWindowWidth, fWindowHeight + fTitleBarHeight);
  144. System.Drawing.SizeF sfSize = new System.Drawing.SizeF (pfSize);
  145. System.Drawing.RectangleF rectTemp = new System.Drawing.RectangleF (pfLocation, sfSize);
  146. this.Window.Window.SetFrame (rectTemp, true);
  147. }//CenterWindow
  148. }
  149. }