Nick Data Język
hollowone 04/12/2009 10:17:48 C#

Blitter i Framebuffer w SL

  1.   public class GpuSurface
  2.     {
  3.         private WriteableBitmap surfaceBitmap;
  4.         private int width, height;
  5.             
  6.         public int Width
  7.         {
  8.             get
  9.             {
  10.                 return width;
  11.             }
  12.         }
  13.         public int Height
  14.         {
  15.             get
  16.             {
  17.                 return height;
  18.             }
  19.         }
  20.  
  21.         public int[] Pixels
  22.         {
  23.             get
  24.             {
  25.                 return surfaceBitmap.Pixels;
  26.             }
  27.         }
  28.         internal WriteableBitmap Bitmap
  29.         {
  30.             get
  31.             {
  32.                 return surfaceBitmap;
  33.             }
  34.         }
  35.  
  36.         public ImageBrush GetImageBrush()
  37.         {
  38.             ImageBrush brush = new ImageBrush();
  39.             brush.ImageSource = this.surfaceBitmap;
  40.  
  41.             return brush;
  42.         }
  43.  
  44.         public void Clear(uint rawColor)
  45.         {
  46.             for (int i = 0; i < width * height; i++)
  47.             {
  48.                 unchecked
  49.                 {
  50.                     surfaceBitmap.Pixels[i] = (int)rawColor;
  51.                 }
  52.             }
  53.         }
  54.  
  55.         public void Blit(UIElement element, int xpos, int ypos, float scalex, float scaley, float angle)
  56.         {
  57.             TranslateTransform position = new TranslateTransform();
  58.             ScaleTransform scale = new ScaleTransform();
  59.             RotateTransform rotation = new RotateTransform();
  60.  
  61.             position.X = xpos;
  62.             position.Y = ypos;
  63.             scale.ScaleX = scalex;
  64.             scale.ScaleY = scaley;
  65.             rotation.Angle = angle;
  66.  
  67.             TransformGroup transform = new TransformGroup();
  68.             transform.Children.Add(position);
  69.             transform.Children.Add(scale);
  70.             transform.Children.Add(rotation);
  71.  
  72.             surfaceBitmap.Render(element, transform);
  73.         }
  74.  
  75.         public void Update()
  76.         {
  77.             surfaceBitmap.Invalidate();
  78.         }
  79.  
  80.         #region Constructor and Static Creators
  81.         private GpuSurface()
  82.         {
  83.  
  84.         }
  85.  
  86.         public static GpuSurface Create(GpuDevice device)
  87.         {
  88.             GpuSurface surface = new GpuSurface();
  89.            
  90.             surface.surfaceBitmap = new WriteableBitmap(device.BackBuffer.Width,
  91.                                                 device.BackBuffer.Height);
  92.  
  93.             surface.width = surface.surfaceBitmap.PixelWidth;
  94.             surface.height = surface.surfaceBitmap.PixelHeight;
  95.  
  96.             return surface;
  97.         }
  98.         public static GpuSurface Create(int width, int height)
  99.         {
  100.             GpuSurface surface = new GpuSurface();
  101.  
  102.             surface.surfaceBitmap = new WriteableBitmap(width, height);
  103.  
  104.             surface.width = surface.surfaceBitmap.PixelWidth;
  105.             surface.height = surface.surfaceBitmap.PixelHeight;
  106.  
  107.             return surface;
  108.         }
  109.         #endregion
  110.     }