• XNA Graphics API Library 介绍


    眼看着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

    谢谢支持!!!

  • 相关阅读:
    JQuery Ajax 的简单使用
    使用PHP QR Code生成二维码
    SQL中Group By的使用
    虚拟主机的配置
    文件上传
    session机制详解以及session的相关应用
    thinkphp权限管理Rbac实例
    jQuery处理点击父级checkbox所有子级checkbox都选中,取消选中所有子级checkbox都取消
    传递一个父id返回所有子id的用法,可用于删除父级以下的所有子级
    thinkphp3.2独立分组的建立
  • 原文地址:https://www.cnblogs.com/zhongzf/p/2153763.html
Copyright © 2020-2023  润新知