日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > C# >内容正文

C#

c#控件弹幕效果_基于C#弹幕类射击游戏的实现——(二)渲染

發(fā)布時間:2024/3/26 C# 67 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c#控件弹幕效果_基于C#弹幕类射击游戏的实现——(二)渲染 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

這個游戲打算是用C#+GDI做~所以渲染效率上還是要進行一些考慮的

public interface IRenderHandler

{

void Clear(Color backgroundColor);

void DrawLine(int x1, int y1, int x2, int y2, Color color);

void DrawBox(int x, int y, int width, int height, Color color, bool fill);

void DrawImage(int destX, int destY, int destWidth, int destHeight, Bitmap source, int sourceX, int sourceY, int sourceWidth, int sourceHeight);

object GetSurface();

}

這是實現(xiàn)一個渲染器需要實現(xiàn)的接口,大體上就這么多

然后是用GDI實現(xiàn)的一個渲染器

///

/// GDI渲染器

///

public class GDIRender : IRenderHandler

{

private Bitmap mSurface;

private Graphics mG;

public GDIRender(int width, int height)

{

mSurface = new Bitmap(width, height);

mG = Graphics.FromImage(mSurface);

}

public void Clear(Color backgroundColor)

{

mG.Clear(backgroundColor);

}

public void DrawLine(int x1, int y1, int x2, int y2, Color color)

{

mG.DrawLine(new Pen(color), x1, y1, x2, y2);

}

public void DrawBox(int x, int y, int width, int height, Color color, bool fill)

{

if (fill == true)

{

mG.FillRectangle(new SolidBrush(color), new Rectangle(x - width / 2, y - height / 2, width, height));

}

else

{

mG.DrawRectangle(new Pen(color), new Rectangle(x - width / 2, y - height / 2, width, height));

}

}

public void DrawImage(int destX, int destY, int destWidth, int destHeight, Bitmap source, int sourceX, int sourceY, int sourceWidth, int sourceHeight)

{

mG.DrawImage(source,

new Rectangle(destX - destWidth / 2, destY - destHeight / 2, destWidth, destHeight),

new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),

GraphicsUnit.Pixel);

}

public object GetSurface()

{

return mSurface;

}

}

有了這個東西,我們就可以在屏幕上畫東西了。。。

下面實現(xiàn)的是一個圖形設(shè)備,封裝了一下渲染器

///

/// 圖形設(shè)備

///

public class GraphicDevice

{

public IRenderHandler RenderHandler;

private int mRenderStatus; // 渲染器的狀態(tài)(0:Normal 1:Begin)

private int mWidth;

private int mHeight;

private Color mBackgroundColor;

public object Surface

{

get

{

return RenderHandler.GetSurface();

}

}

private List mRenderObjects;

public event RenderOverHandler RenderOver;

public GraphicDevice(int width, int height, Color backColor)

{

this.RenderHandler = new GDIRender(width, height);

this.mWidth = width;

this.mHeight = height;

this.mBackgroundColor = backColor;

this.mRenderObjects = new List();

this.RenderOver = null;

}

public void Begin()

{

if (mRenderStatus != 0)

{

throw new Exception("上一次調(diào)用Begin()后未調(diào)用End()");

}

mRenderStatus = 1;

mRenderObjects.Clear();

RenderHandler.Clear(mBackgroundColor);

}

public void End()

{

if (mRenderStatus != 1)

{

throw new Exception("調(diào)用End()之前必須調(diào)用Begin()");

}

mRenderStatus = 0;

mRenderObjects.Sort(DepthComparer);

int len = mRenderObjects.Count;

for (int i = 0; i < len; i++)

{

mRenderObjects[i].Render(RenderHandler);

}

if (RenderOver != null)

{

RenderOver(this);

}

}

public void RenderObject(RenderObject obj)

{

mRenderObjects.Add(obj);

}

private int DepthComparer(RenderObject obj1, RenderObject obj2)

{

if (obj1.Depth < obj2.Depth)

{

return -1;

}

else if (obj1.Depth > obj2.Depth)

{

return 1;

}

return 0;

}

}

在Begin的時候清空需要渲染的東西,在End的時候進行批處理繪制。

繪制的對象是一個叫做RenderObject的類,這個類是基礎(chǔ)渲染圖元,也就是Line,Image之類的基類了

///

/// 渲染目標基類

///

public class RenderObject

{

///

/// 渲染深度(0-100越大越靠前)

///

public int Depth;

public RenderObject()

{

Depth = 0;

}

public virtual void Render(IRenderHandler renderHandler)

{

}

}

然后接下來給出幾種具體實現(xiàn)

public class RenderLine : RenderObject

{

public int X1;

public int Y1;

public int X2;

public int Y2;

public Color Color;

public RenderLine(int x1, int y1, int x2, int y2, Color color, int depth = 100)

: base()

{

this.Depth = depth;

this.X1 = x1;

this.Y1 = y1;

this.X2 = x2;

this.Y2 = y2;

this.Color = color;

}

public override void Render(IRenderHandler renderHandler)

{

renderHandler.DrawLine(X1, Y1, X2, Y2, Color);

}

}

public class RenderBox : RenderObject

{

public int X;

public int Y;

public int Width;

public int Height;

public Color Color;

public bool Fill;

public RenderBox(int x, int y, int width, int height, Color color, bool fill, int depth = 100)

{

this.Depth = depth;

this.X = x;

this.Y = y;

this.Width = width;

this.Height = height;

this.Color = color;

this.Fill = fill;

}

public override void Render(IRenderHandler renderHandler)

{

renderHandler.DrawBox(X, Y, Width, Height, Color, Fill);

}

}

其余的類似~~

好了,有了這些東西,我們才真正的開始在屏幕上繪制東西了,大概流程是這樣

GraphiceDevice.Begin()

...

GraphiceDevice.RenderObject(obj);

...

GraphiceDevice.End()

總結(jié)

以上是生活随笔為你收集整理的c#控件弹幕效果_基于C#弹幕类射击游戏的实现——(二)渲染的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。