瑞雪年

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: :: 订阅 订阅 :: 管理 ::
  261 随笔 :: 0 文章 :: 432 评论 :: 0 引用

眼看着MonoGame就要"所有平台"(MonoTouch, MonoDroid, Mac OS X, Windows & Linux)通吃了,这倍增了我对XNA的兴趣,为了打下一个良好的基础,就从最基本的绘图函数开始吧。

这个XNA Graphics API Library基于XNA的类库,实现了最基本最常用的绘图类库,如:画线,画矩形,画圆等。

特点:

1. API十分简单。

2. 性能非常好。

3. 支持多线程。

代码示例:

1. DrawingBatch

            drawingBatch = new DrawingBatch(GraphicsDevice);

drawingBatch.Begin();
drawingBatch.DrawLine(
10, 20, 100, 20, Color.Red);
drawingBatch.DrawRectangle(
120, 10, 100, 20, Color.Blue);
drawingBatch.DrawTriangle(
240, 10, 240, 60, 200, 60, Color.Black);
drawingBatch.DrawEllipse(
310, 10, 50, 50, Color.Green);
drawingBatch.DrawPolyline(
new Vector2[] { new Vector2(410, 10), new Vector2(440, 10), new Vector2(420, 20), new Vector2(440, 40), new Vector2(410, 60) }, Color.Aqua);
drawingBatch.DrawFilledRectangle(
120, 110, 50, 0, Color.Blue);
drawingBatch.DrawFilledTriangle(
240, 110, 240, 160, 200, 160, Color.Brown);
drawingBatch.DrawFilledEllipse(
310, 110, 80, 40, Color.Green);
drawingBatch.End();

2.  DrawingContext (基于 DrawingBatch 并且包装了SpriteBatch的所有方法)

            drawingContext = new DrawingContext(GraphicsDevice);
spriteFont
= Content.Load<SpriteFont>("SegoeUI");
texture
= Content.Load<Texture2D>("Tulips");

drawingContext.Begin();
drawingContext.DrawLine(
10, 20, 100, 20, Color.Red);
drawingContext.DrawRectangle(
120, 10, 100, 20, Color.Blue);
drawingContext.DrawTriangle(
240, 10, 240, 60, 200, 60, Color.Black);
drawingContext.DrawEllipse(
310, 10, 50, 50, Color.Green);
drawingContext.DrawTexture(texture,
new Vector2(10, 300), Color.White);
drawingContext.DrawPolyline(
new Vector2[] { new Vector2(410, 10), new Vector2(440, 10), new Vector2(420, 20), new Vector2(440, 40), new Vector2(410, 60) }, Color.Aqua);
drawingContext.DrawFilledRectangle(
120, 110, 50, 50, Color.Blue);
drawingContext.DrawFilledTriangle(
240, 110, 240, 160, 200, 160, Color.Brown);
drawingContext.DrawFilledEllipse(
310, 110, 80, 40, Color.Green);
drawingContext.DrawText(spriteFont,
"Hello World!", new Vector2(120, 300), Color.Black);
drawingContext.End();

3. DrawingTexture (可以在贴图上先绘制图形,然后再将贴图输出)

            spriteFont = Content.Load<SpriteFont>("SegoeUI");
texture
= Content.Load<Texture2D>("Tulips");

PresentationParameters pp
= GraphicsDevice.PresentationParameters;
int bufferWidth = pp.BackBufferWidth;
int bufferHeight = pp.BackBufferHeight;
drawingTexture
= new DrawingTexture(GraphicsDevice, bufferWidth, bufferHeight);
drawingTexture.Clear(Color.White);

DrawingContext drawingContext
= drawingTexture.DrawingContext;
drawingContext.Begin();
drawingContext.DrawLine(
10, 20, 100, 20, Color.Red);
drawingContext.DrawRectangle(
120, 10, 100, 20, Color.Blue);
drawingContext.DrawTriangle(
240, 10, 240, 60, 200, 60, Color.Black);
drawingContext.DrawEllipse(
310, 10, 50, 50, Color.Green);
drawingContext.DrawTexture(texture,
new Vector2(10, 300), Color.White);
drawingContext.DrawPolyline(
new Vector2[] { new Vector2(410, 10), new Vector2(440, 10), new Vector2(420, 20), new Vector2(440, 40), new Vector2(410, 60) }, Color.Aqua);
drawingContext.DrawFilledRectangle(
120, 110, 50, 50, Color.Blue);
drawingContext.DrawFilledTriangle(
240, 110, 240, 160, 200, 160, Color.Brown);
drawingContext.DrawFilledEllipse(
310, 110, 80, 40, Color.Green);
drawingContext.DrawText(spriteFont,
"Hello World!", new Vector2(120, 300), Color.Black);
drawingContext.End();

spriteBatch.Begin();
spriteBatch.Draw(drawingTexture, Vector2.Zero, Color.White);
spriteBatch.End();

4. DrawingComponent

            drawingComponent = new DrawingComponent(this);
this.Components.Add(this.drawingComponent);

drawingComponent.DrawingContext.DrawLine(
0, 0, 100, 100, Color.Black);

示例程序截屏:

下载地址:

可执行示例程序: bin.zip

全部源码: xnagraphics.zip

谢谢支持!!!

posted on 2011-08-25 21:09 瑞雪年 阅读(...) 评论(...) 编辑 收藏