Commits

smack0007  committed 6e18d49

Removed Graphics from Game class.
The GraphicsDevice class now returns true or false from BeginDraw.

  • Participants
  • Parent commits 2bbec47

Comments (0)

Files changed (6)

File Samples/CreateTextureFontSample/CreateTextureFontSample.cs

 {
 	public class CreateTextureFontSample : Game
 	{
+		GraphicsDevice graphics;
 		Renderer renderer;
 		TextureFont textureFont;
 
 			: base()
 		{
 			this.Window.Title = "Snowball CreateTextureFont Sample";
+
+			this.graphics = new GraphicsDevice(this.Window);
+			this.Services.AddService(typeof(IGraphicsDevice), this.graphics);
 		}
 
 		protected override void Initialize()
 		{
-			this.Graphics.CreateDevice();
-			this.renderer = new Renderer(this.Graphics);
-			this.textureFont = new TextureFont(this.Graphics, "Segoe UI", 24, true);
+			this.graphics.CreateDevice();
+			this.renderer = new Renderer(this.graphics);
+			this.textureFont = new TextureFont(this.graphics, "Segoe UI", 24, true);
 		}
 		
 		protected override void Draw(GameTime gameTime)
 		{
-			this.Graphics.Clear(Color.Black);
-			this.Graphics.BeginDraw();
+			if(this.graphics.BeginDraw())
+			{
+				this.graphics.Clear(Color.Black);
 
-			this.renderer.Begin();
-			this.renderer.DrawTexture(this.textureFont.Texture, new Vector2(10, 10), Color.White);
-			this.renderer.DrawString(this.textureFont, "Hello World!", new Vector2(10, 410), Color.White);
-			this.renderer.End();
+				this.renderer.Begin();
+				this.renderer.DrawTexture(this.textureFont.Texture, new Vector2(10, 10), Color.White);
+				this.renderer.DrawString(this.textureFont, "Hello World!", new Vector2(10, 410), Color.White);
+				this.renderer.End();
 
-			this.Graphics.EndDraw();
-			this.Graphics.Present();
+				this.graphics.EndDraw();
+				this.graphics.Present();
+			}
 		}
 
 		public static void Main()

File Samples/GamePadReader/GamePadReader.cs

 {
 	public class GamePadReader : Game
 	{
+		GraphicsDevice graphics;
 		GamePadDevice gamePad;
 
 		Renderer renderer;
 			: base()
 		{
 			this.Window.Title = "Snowball GamePad Reader";
+
+			this.graphics = new GraphicsDevice(this.Window);
+			this.Services.AddService(typeof(IGraphicsDevice), this.graphics);
+
 			this.gamePad = new GamePadDevice(PlayerIndex.One);
 		}
 
 		protected override void Initialize()
 		{
-			this.Graphics.CreateDevice();
-			this.renderer = new Renderer(this.Graphics);
-			this.font = new TextureFont(this.Graphics, "Arial", 24, true);
+			this.graphics.CreateDevice();
+			this.renderer = new Renderer(this.graphics);
+			this.font = new TextureFont(this.graphics, "Arial", 24, true);
 		}
 
 		protected override void Update(GameTime gameTime)
 		{
 			string gamePadStatus = this.GetGamePadStatus();
 
-			this.Graphics.Clear(Color.CornflowerBlue);
-			this.Graphics.BeginDraw();
+			if(this.graphics.BeginDraw())
+			{
+				this.graphics.Clear(Color.CornflowerBlue);
 
-			this.renderer.Begin();
+				this.renderer.Begin();
 
-			this.renderer.DrawString(this.font, gamePadStatus, new Vector2(5, 5), Color.White);
+				this.renderer.DrawString(this.font, gamePadStatus, new Vector2(5, 5), Color.White);
 
-			this.renderer.End();
+				this.renderer.End();
 
-			this.Graphics.EndDraw();
-			this.Graphics.Present();
+				this.graphics.EndDraw();
+				this.graphics.Present();
+			}
 		}
 
 		public static void Main()

File Samples/WalkingWizard/WalkingWizardSample.cs

 {
 	public class WalkingWizardSample : Game
 	{
+		GraphicsDevice graphics;
 		Renderer renderer;
 		KeyboardDevice keyboard;
 		ContentLoader content;
 		{
 			this.Window.Title = "Snowball Walking Wizard Sample";
 
+			this.graphics = new GraphicsDevice(this.Window);
+			this.Services.AddService(typeof(IGraphicsDevice), this.graphics);
+
 			this.keyboard = new KeyboardDevice();
 
 			this.content = new ContentLoader(this.Services);
 		protected override void Initialize()
 		{
 			// Renderer must be created after the Graphics Device has been created.
-			this.Graphics.CreateDevice();
-			this.renderer = new Renderer(this.Graphics);
+			this.graphics.CreateDevice();
+			this.renderer = new Renderer(this.graphics);
 
-			// Load a texture and wrap it in a SpriteSheet. The shee contains frame which are 32x32.
+			// Load a texture and wrap it in a SpriteSheet. The sheet contains frames which are 32x32.
 			SpriteSheet spriteSheet = this.content.Load<SpriteSheet>(new LoadSpriteSheetArgs()
 			{
 				FileName = "wizard.png",
 		{
 			base.Draw(gameTime);
 
-			// Clear the backbuffer and begin drawing.
-			this.Graphics.Clear(new Color(192, 192, 192, 255));
-			this.Graphics.BeginDraw();
+			if(this.graphics.BeginDraw())
+			{
+				// Clear the backbuffer and begin drawing.
+				this.graphics.Clear(new Color(192, 192, 192, 255));
 
-			// Draw the single sprite.
-			this.renderer.Begin();
-			this.renderer.DrawSprite(this.sprite);
-			this.renderer.End();
 
-			// End drawing and present the backbuffer.
-			this.Graphics.EndDraw();
-			this.Graphics.Present();
+				// Draw the single sprite.
+				this.renderer.Begin();
+				this.renderer.DrawSprite(this.sprite);
+				this.renderer.End();
+
+				// End drawing and present the backbuffer.
+				this.graphics.EndDraw();
+				this.graphics.Present();
+			}
 		}
 
 		public static void Main()

File Snowball.Demo/DemoGame.cs

 {
 	public class DemoGame : Game
 	{
+		GraphicsDevice graphics;
 		Renderer renderer;
 		DemoContentLoader content;
 		KeyboardDevice keyboard;
 		{
 			this.Window.Title = "Snowball Demo Game";
 
+			this.graphics = new GraphicsDevice(this.Window);
+			this.Services.AddService(typeof(IGraphicsDevice), this.graphics);
+						
 			this.content = new DemoContentLoader(this.Services);
 			this.Services.AddService(typeof(IContentLoader), this.content);
 
 		{
 			base.Initialize();
 
-			this.Graphics.CreateDevice(800, 600);
+			this.graphics.CreateDevice(800, 600);
+			this.graphics.FullscreenToggled += (s, e) => { this.DrawRenderTarget(); };
+			
 			this.sound.CreateDevice();
 
 			this.console.Initialize();
-			this.console.Font = new TextureFont(this.Graphics, "Arial", 12, true);
+			this.console.Font = new TextureFont(this.graphics, "Arial", 12, true);
 			this.console.BackgroundTexture = this.content.Load<Texture>("ConsoleBackground");
 			this.console.InputColor = Color.Blue;
 			this.console.CommandEntered += (s, e) =>
 			    this.console.WriteLine(e.Command);
 			};
 
-			this.starfield = new Starfield(this.Graphics.DisplayWidth, this.Graphics.DisplayHeight);
+			this.starfield = new Starfield(this.graphics.DisplayWidth, this.graphics.DisplayHeight);
 
 			this.ship.Initialize();
 
-			this.renderer = new Renderer(this.Graphics);
+			this.renderer = new Renderer(this.graphics);
 
-			this.renderTarget = this.Graphics.CreateRenderTarget(200, 200);
+			this.renderTarget = this.graphics.CreateRenderTarget(200, 200);
 			this.DrawRenderTarget();
 		}
-
-		protected override void OnToggleFullscreen()
-		{
-			this.DrawRenderTarget();
-		}
-
+		
 		private void DrawRenderTarget()
 		{
-			this.Graphics.SetRenderTarget(this.renderTarget);
-			this.Graphics.BeginDraw();
-			this.Graphics.Clear(Color.Blue);
-			this.renderer.Begin();
-			this.renderer.DrawLine(new Vector2(0, 0), new Vector2(200, 200), Color.Red);
-			this.renderer.End();
-			this.Graphics.EndDraw();
-			this.Graphics.SetRenderTarget(null);
+			this.graphics.SetRenderTarget(this.renderTarget);
+			
+			if(this.graphics.BeginDraw())
+			{
+				this.graphics.Clear(Color.Blue);
+				this.renderer.Begin();
+				this.renderer.DrawLine(new Vector2(0, 0), new Vector2(200, 200), Color.Red);
+				this.renderer.End();
+				this.graphics.EndDraw();
+				this.graphics.SetRenderTarget(null);
+			}
 		}
 
 		protected override void Update(GameTime gameTime)
 				this.Exit();
 
 			if(this.keyboard.IsKeyPressed(Keys.F12))
-				this.Graphics.ToggleFullscreen();
+				this.graphics.ToggleFullscreen();
 
 			if(!this.console.IsVisible)
 			{
 
 		protected override void Draw(GameTime gameTime)
 		{			
-			this.Graphics.Clear(Color.Black);
-			this.Graphics.BeginDraw();
-			this.renderer.Begin();
-			
-			this.starfield.Draw(this.renderer);
-			this.ship.Draw(this.renderer);
-			//this.renderer.DrawRenderTarget(this.renderTarget, Vector2.Zero, Color.White);
+			if(this.graphics.BeginDraw())
+			{
+				this.graphics.Clear(Color.Black);
+				this.renderer.Begin();
 
-			this.console.Draw(this.renderer);
+				this.starfield.Draw(this.renderer);
+				this.ship.Draw(this.renderer);
+				//this.renderer.DrawRenderTarget(this.renderTarget, Vector2.Zero, Color.White);
 
-			this.renderer.End();
-			this.Graphics.EndDraw();
-			this.Graphics.Present();
+				this.console.Draw(this.renderer);
+
+				this.renderer.End();
+				this.graphics.EndDraw();
+				this.graphics.Present();
+			}
 			
 			this.fps++;
 			this.fpsTime += gameTime.ElapsedTotalSeconds;

File Snowball/Game.cs

 			get;
 			private set;
 		}
-
-		/// <summary>
-		/// The GraphicsDevice for the game.
-		/// </summary>
-		public GraphicsDevice Graphics
-		{
-			get;
-			private set;
-		}
-				
+						
 		/// <summary>
 		/// Initializes a new Game instance with the default GameWindow.
 		/// </summary>
 			this.Window = window;
 			this.SubscribeWindowEvents();
 			this.Services.AddService(typeof(IGameWindow), this.Window);			
-
-			this.Graphics = new GraphicsDevice(this.Window);
-			this.Services.AddService(typeof(IGraphicsDevice), this.Graphics);
-			this.SubscribeGraphicsEvents();
 			
 			this.gameClock = new GameClock();
 			this.gameTime = new GameTime();
 		{
 			if(disposing)
 			{
-				if(this.Graphics != null)
-				{
-					this.Graphics.Dispose();
-					this.UnsubscribeGraphicsEvents();
-					this.Graphics = null;
-				}
-
 				if(this.Window != null)
 				{
 					this.UnsubscribeWindowEvents();
 		}
 
 		/// <summary>
-		/// Subscribes to events on the graphics manager.
-		/// </summary>
-		public void SubscribeGraphicsEvents()
-		{
-			this.Graphics.FullscreenToggled += this.Graphics_FullscreenToggled;
-		}
-
-		/// <summary>
-		/// Unsubscribes to events on the graphics manager.
-		/// </summary>
-		public void UnsubscribeGraphicsEvents()
-		{
-			this.Graphics.FullscreenToggled -= this.Graphics_FullscreenToggled;
-		}
-
-		/// <summary>
 		/// Triggers the main loop for the game.
 		/// </summary>
 		public void Run()
 		{
 			this.Initialize();
-
-			if(!this.Graphics.IsDeviceCreated)
-				throw new InvalidOperationException("The Graphics Device must be created after the Initialize method has finished.");
-
 			this.Window.Run();
 		}
 
 
 			if(this.gameClock.ShouldDraw)
 			{
-				if(this.Graphics.EnsureDeviceNotLost())
-					this.Draw(this.gameTime);
-
+				this.Draw(this.gameTime);
 				this.gameClock.ResetShouldDraw();
 			}
 		}
 		}
 
 		/// <summary>
-		/// Called when the graphics device has switched to or from fullscreen.
-		/// </summary>
-		/// <param name="sender"></param>
-		/// <param name="e"></param>
-		private void Graphics_FullscreenToggled(object sender, EventArgs e)
-		{
-			this.OnToggleFullscreen();
-		}
-
-		/// <summary>
 		/// Called when the game should initialize.
 		/// </summary>
 		protected virtual void Initialize()
 		}
 
 		/// <summary>
-		/// Called when the game switches to or from fullscreen.
-		/// </summary>
-		protected virtual void OnToggleFullscreen()
-		{
-		}
-
-		/// <summary>
 		/// Called when the game is exiting.
 		/// </summary>
 		protected virtual void OnExiting()

File Snowball/Graphics/GraphicsDevice.cs

 			if(this.window.ClientHeight != this.presentParams.BackBufferHeight)
 				this.window.ClientHeight = this.presentParams.BackBufferHeight;
 		}
-
-		/// <summary>
-		/// Ensures the graphics device has been created and is not lost.
-		/// </summary>
-		private void EnsureDeviceReady()
-		{
-			this.EnsureDeviceCreated();
-
-			if(this.isDeviceLost)
-				throw new InvalidOperationException("The graphics device is currently lost.");
-		}
-
+				
 		/// <summary>
 		/// Toggles a transition to fullscreen.
 		/// </summary>
 		public void ToggleFullscreen()
 		{
-			this.EnsureDeviceReady();
-
+			this.EnsureDeviceCreated();
+			
 			this.presentParams.Windowed = !this.presentParams.Windowed;
 
 			this.window.BeforeToggleFullscreen(!this.presentParams.Windowed);
 		}
 
 		/// <summary>
-		/// Informs the manager drawing is beginning.
+		/// Requests to begin drawing.
 		/// </summary>
-		public void BeginDraw()
+		public bool BeginDraw()
 		{
-			this.EnsureDeviceReady();
+			this.EnsureDeviceCreated();
+
+			if(this.isDeviceLost)
+				return false;
 
 			if(this.HasDrawBegun)
 				throw new InvalidOperationException("Already within BeginDraw / EndDraw pair.");
 			}
 
 			this.HasDrawBegun = true;
+
+			return true;
 		}
 
 		/// <summary>
 		/// </summary>
 		public void EndDraw()
 		{
-			this.EnsureDeviceReady();
+			this.EnsureDeviceCreated();
 			this.EnsureHasDrawBegun();
 
 			if(this.RenderTarget == null)
 		/// <param name="color"></param>
 		public void Clear(Color color)
 		{
-			this.EnsureDeviceReady();
+			this.EnsureDeviceCreated();
 			
 			if(this.RenderTarget == null)
 				this.InternalDevice.Clear(SlimDX.Direct3D9.ClearFlags.Target | SlimDX.Direct3D9.ClearFlags.ZBuffer, color.ToArgb(), 1.0f, 0);
 		/// </summary>
 		public void Present()
 		{
-			this.EnsureDeviceReady();
+			this.EnsureDeviceCreated();
 
 			try
 			{
 		/// <returns></returns>
 		public Texture LoadTexture(Stream stream, Color? colorKey)
 		{
-			this.EnsureDeviceReady();
+			this.EnsureDeviceCreated();
 			return Texture.FromStream(this, stream, colorKey);			
 		}
 				
 		/// <returns></returns>
 		public TextureFont LoadTextureFont(Stream stream, Color? colorKey)
 		{
-			this.EnsureDeviceReady();
+			this.EnsureDeviceCreated();
 			return TextureFont.FromStream(this, stream, colorKey);
 		}
 
 		/// <returns></returns>
 		public RenderTarget CreateRenderTarget(int width, int height)
 		{
-			this.EnsureDeviceReady();
+			this.EnsureDeviceCreated();
 			return new RenderTarget(this, width, height);
 		}
 
 		/// <param name="renderTarget"></param>
 		public void SetRenderTarget(RenderTarget renderTarget)
 		{
-			this.EnsureDeviceReady();
+			this.EnsureDeviceCreated();
 
 			if(this.HasDrawBegun)
 				throw new InvalidOperationException("Render target cannot be set within BeginDraw / EndDraw pair.");