Advertisement
Guest User

Untitled

a guest
Oct 29th, 2010
1,459
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.73 KB | None | 0 0
  1. using D2D = Microsoft.WindowsAPICodePack.DirectX.Direct2D1;
  2. using System;
  3. using Microsoft.WindowsAPICodePack.DirectX.Graphics;
  4.  
  5. namespace Example
  6. {
  7.     internal sealed class MyScene : Direct2D.Scene
  8.     {
  9.         private D2D.SolidColorBrush redBrush;
  10.         D2D.D2DBitmap m_atlasBitmap;
  11.         int m_tileSize = 32;
  12.  
  13.         protected override void OnCreateResources()
  14.         {
  15.             this.redBrush = this.RenderTarget.CreateSolidColorBrush(new D2D.ColorF(1, 0, 0));
  16.  
  17.             m_atlasBitmap = this.RenderTarget.CreateBitmap(new D2D.SizeU((uint)m_tileSize, (uint)m_tileSize),
  18.                 new D2D.BitmapProperties(new D2D.PixelFormat(Format.B8G8R8A8UNorm, D2D.AlphaMode.Ignore), 96, 96));
  19.         }
  20.  
  21.         protected override void OnFreeResources()
  22.         {
  23.             if (this.redBrush != null)
  24.             {
  25.                 this.redBrush.Dispose();
  26.                 this.redBrush = null;
  27.             }
  28.  
  29.             if (m_atlasBitmap != null)
  30.             {
  31.                 m_atlasBitmap.Dispose();
  32.                 m_atlasBitmap = null;
  33.             }
  34.         }
  35.  
  36.         protected override void OnRender()
  37.         {
  38.             this.RenderTarget.BeginDraw();
  39.  
  40.             // clear to green
  41.             this.RenderTarget.Clear(new D2D.ColorF(0, 1, 0, 1));
  42.  
  43.             int columns = (int)Math.Ceiling(this.RenderTarget.Size.Width / m_tileSize);
  44.             int rows = (int)Math.Ceiling(this.RenderTarget.Size.Height / m_tileSize);
  45.  
  46.             for (int y = 0; y < rows; y++)
  47.             {
  48.                 for (int x = 0; x < columns; x++)
  49.                 {
  50.                     var rect = new D2D.RectF(x * m_tileSize, y * m_tileSize, x * m_tileSize + m_tileSize, y * m_tileSize + m_tileSize);
  51.                     // draw red tile
  52.                     this.RenderTarget.FillRectangle(rect, redBrush);
  53.                     // draw black bitmap tile
  54.                     this.RenderTarget.DrawBitmap(m_atlasBitmap, 1.0f, D2D.BitmapInterpolationMode.Linear, rect,
  55.                         new D2D.RectF(0, 0, m_tileSize, m_tileSize));
  56.                 }
  57.             }
  58.  
  59.             this.RenderTarget.EndDraw();
  60.         }
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement