Advertisement
Guest User

GraphicsManager for XNA/WPF interop

a guest
Aug 6th, 2010
880
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.29 KB | None | 0 0
  1. using System;
  2. using System.Windows.Forms;
  3. using Microsoft.Xna.Framework;
  4. using Microsoft.Xna.Framework.Graphics;
  5.  
  6. namespace XnaGuest
  7. {
  8.     class GraphicsManager
  9.     {
  10.         private GraphicsDevice device;
  11.         private Control parentControl;
  12.         private PresentationParameters presentParams;
  13.  
  14.         public GraphicsDevice GraphicsDevice { get { return device; } }
  15.         public Control ParentControl { get { return parentControl; } }
  16.  
  17.         public event EventHandler<EventArgs> Draw;
  18.         public System.Drawing.Size Size { get { return parentControl.Size; } }
  19.         public float AspectRation { get { return (float)Size.Width / (float)Size.Height; } }
  20.         public Color BackgroundColor { get; set; }
  21.  
  22.         Timer timer = new Timer();
  23.  
  24.         public void Create(Control parentControl)
  25.         {
  26.             this.parentControl = parentControl;
  27.             presentParams = new PresentationParameters();
  28.             presentParams.IsFullScreen = false;
  29.             presentParams.BackBufferWidth = parentControl.ClientSize.Width;
  30.             presentParams.BackBufferHeight = parentControl.ClientSize.Height;
  31.             presentParams.DeviceWindowHandle = parentControl.Handle;
  32.             presentParams.MultiSampleCount = 4;
  33.             device = new GraphicsDevice(GraphicsAdapter.DefaultAdapter, GraphicsProfile.HiDef, presentParams);
  34.             device.DeviceLost += new EventHandler<EventArgs>(OnDeviceLost);
  35.  
  36.             parentControl.Paint += new PaintEventHandler(OnPaint);
  37.             parentControl.Disposed += new EventHandler(OnDisposed);
  38.             parentControl.SizeChanged += new EventHandler(OnSizeChanged);
  39.             BackgroundColor = new Color(0.8f, 0.8f, 0.8f, 0);
  40.             timer.Interval = 1;
  41.             timer.Tick += new EventHandler(TimerTick);
  42.             timer.Enabled = true;
  43.         }
  44.  
  45.         public event EventHandler<EventArgs> Update;
  46.         public event EventHandler<EventArgs> SizeChanged;
  47.  
  48.         private void TimerTick(object sender, EventArgs e)
  49.         {
  50.             if (Update != null)
  51.                 Update(sender, e);
  52.             Redraw();
  53.         }
  54.  
  55.         public void Redraw()
  56.         {
  57.             parentControl.Invalidate();
  58.         }
  59.  
  60.         private void OnDeviceLost(object sender, EventArgs e)
  61.         {
  62.             device.Reset(presentParams);
  63.         }
  64.  
  65.         private void OnSizeChanged(object sender, EventArgs e)
  66.         {
  67.             if (parentControl.ClientSize.Width == 0 || parentControl.ClientSize.Height == 0)
  68.                 return;
  69.             presentParams.BackBufferWidth = parentControl.ClientSize.Width;
  70.             presentParams.BackBufferHeight = parentControl.ClientSize.Height;
  71.             device.Reset(presentParams);
  72.             parentControl.Invalidate();
  73.             if (SizeChanged != null)
  74.                 SizeChanged(sender, e);
  75.         }
  76.  
  77.         private void OnDisposed(object sender, EventArgs e)
  78.         {
  79.             if (device != null)
  80.                 device.Dispose();
  81.             device = null;
  82.         }
  83.  
  84.         private void OnPaint(object sender, PaintEventArgs e)
  85.         {
  86.             device.Clear(ClearOptions.Target, BackgroundColor, 0.0f, 0);
  87.             if (Draw != null)
  88.                 Draw(this, new EventArgs());
  89.             device.Present();
  90.         }
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement