Guest User

Untitled

a guest
May 15th, 2012
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.67 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Text;
  6. using Microsoft.Xna.Framework;
  7. using Microsoft.Xna.Framework.Graphics;
  8.  
  9. namespace Mist
  10. {
  11.     /// <summary>
  12.     /// The graphics module controls all graphical rendering in the engine
  13.     /// </summary>
  14.     public class Graphics : IGraphicsDeviceService
  15.     {
  16.         /// <summary>
  17.         /// The graphics device that we'll use for rendering
  18.         /// </summary>
  19.         private GraphicsDevice Device;
  20.  
  21.         /// <summary>
  22.         /// The presentation paramters is basically device initialization data
  23.         /// </summary>
  24.         private PresentationParameters DeviceData;
  25.  
  26.  
  27.         /// <summary>
  28.         /// Occurs when we finally get added to the engine.
  29.         /// </summary>
  30.         protected override void Initialize()
  31.         {
  32.             base.Initialize();
  33.  
  34.             // Tell the engine to add us as a service
  35.             // Note that 'Services' is my implementation of IServiceContainer
  36.             this.Engine.Services.AddService<IGraphicsDeviceService>(this);
  37.         }
  38.  
  39.         /// <summary>
  40.         /// An event that occurs when the device is initially created
  41.         /// </summary>
  42.         public event EventHandler<EventArgs> DeviceCreated;
  43.  
  44.         /// <summary>
  45.         /// An event that occurs when the device is disposing
  46.         /// </summary>
  47.         public event EventHandler<EventArgs> DeviceDisposing;
  48.  
  49.         /// <summary>
  50.         /// An event that occurs when the device is reset
  51.         /// </summary>
  52.         public event EventHandler<EventArgs> DeviceReset;
  53.  
  54.         /// <summary>
  55.         /// An event that occurs when the device is resetting
  56.         /// </summary>
  57.         public event EventHandler<EventArgs> DeviceResetting;
  58.  
  59.         /// <summary>
  60.         /// Gets the graphics device from the graphics manager
  61.         /// </summary>
  62.         public GraphicsDevice GraphicsDevice
  63.         {
  64.             get { return this.Device; }
  65.         }
  66.  
  67.         /// <summary>
  68.         /// Initialize the graphics device
  69.         /// </summary>
  70.         /// <param name="windowHandle">The handle to the window we'd like to draw to</param>
  71.         /// <param name="width">The width of the back buffer</param>
  72.         /// <param name="height">The height of the back buffer</param>
  73.         private void SetupGraphics(IntPtr windowHandle, Int32 width, Int32 height)
  74.         {
  75.             // Fill out the presentation parameters
  76.             this.DeviceData = new PresentationParameters();
  77.  
  78.             // Keep the contents around for the editor
  79.             this.DeviceData.RenderTargetUsage = RenderTargetUsage.PreserveContents;
  80.  
  81.             // Set the window handle
  82.             this.DeviceData.DeviceWindowHandle = windowHandle;
  83.  
  84.             // Set the back buffer width and height
  85.             this.DeviceData.BackBufferWidth = width;
  86.             this.DeviceData.BackBufferHeight = height;
  87.  
  88.             // Don't launch in full screen yet
  89.             this.DeviceData.IsFullScreen = false;
  90.  
  91.             // Make sure we have a stencil buffer
  92.             this.DeviceData.DepthStencilFormat = DepthFormat.Depth24Stencil8;
  93.  
  94.             // Create the new graphics device
  95.             this.Device = new GraphicsDevice(
  96.                 GraphicsAdapter.DefaultAdapter,
  97.                 GraphicsProfile.HiDef,
  98.                 this.DeviceData);
  99.  
  100.             // If we have any listeners to the device created event...
  101.             if (this.DeviceCreated != null)
  102.             {
  103.                 this.DeviceCreated(this, new EventArgs());
  104.             }
  105.  
  106.             // You have to create a draw function, and manually call it!
  107.         }
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment