c#控件弹幕效果_基于C#弹幕类射击游戏的实现——(二)渲染
这个游戏打算是用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();
}
这是实现一个渲染器需要实现的接口,大体上就这么多
然后是用GDI实现的一个渲染器
///
/// 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;
}
}
有了这个东西,我们就可以在屏幕上画东西了。。。
下面实现的是一个图形设备,封装了一下渲染器
///
/// 图形设备
///
public class GraphicDevice
{
public IRenderHandler RenderHandler;
private int mRenderStatus; // 渲染器的状态(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("上一次调用Begin()后未调用End()");
}
mRenderStatus = 1;
mRenderObjects.Clear();
RenderHandler.Clear(mBackgroundColor);
}
public void End()
{
if (mRenderStatus != 1)
{
throw new Exception("调用End()之前必须调用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的类,这个类是基础渲染图元,也就是Line,Image之类的基类了
///
/// 渲染目标基类
///
public class RenderObject
{
///
/// 渲染深度(0-100越大越靠前)
///
public int Depth;
public RenderObject()
{
Depth = 0;
}
public virtual void Render(IRenderHandler renderHandler)
{
}
}
然后接下来给出几种具体实现
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()
c#控件弹幕效果_基于C#弹幕类射击游戏的实现——(二)渲染相关推荐
- c#控件弹幕效果_基于C#弹幕类射击游戏的实现——(十)整合
先看实现的效果 剩下部分代码,首先是入口,MainForm public partial class MainForm : Form { public MainForm() { // // The I ...
- c#控件弹幕效果_基于C#弹幕类射击游戏的实现——(六)爆炸效果
接下来就像填充积木一样,把GameScene里用到的东西一个个写完~~ 先来个最简单的.GameBomb 一目了然 public class GameBomb : GameObject { publi ...
- global 仪表控件 无人机地面站_基于GL Studio的无人机地面站天线控件设计与实现...
基于 GL Studio 的无人机地面站天线控件设计与实现 李兴岷 ; 陈怀民 ; 喻戈 ; 任伟 [期刊名称] <测控技术> [年 ( 卷 ), 期] 2011(030)009 [摘要] ...
- dev里timeedit控件如何赋值_如何制作高逼格的数据地图(二)
上一期介绍的是如何通过Power Map制作数据地图.但如果Excel在没有安装该组件,应该怎么办?如果希望实现更加个性化的设置,又有什么方法? 那么,通过VBA代码也可以实现数据地图的制作. 相比之 ...
- Silverlight使用DataGrid的模板列(DataGridTemplateColumn)实现类似TreeListView控件的效果
Silverlight使用DataGrid的模板列(DataGridTemplateColumn)实现类似TreeListView控件的效果 转载于:https://www.cnblogs.com/K ...
- [MFC] 几种子控件半透明效果的实现办法
什么是"控件半透明效果" ? 如下图所示,美图秀秀中间的四个按钮,边缘四个角都是透明的,呈现出椭圆效果: 熟悉MFC开发的都知道 虽然四个按钮的边缘是椭圆的 实际上椭圆之外的角落 ...
- C#WPF实现TextBox控件水印效果的两种实现方法
C#WPF实现TextBox控件水印效果的两种实现方法 在WPF实际项目中往往需要在TextBox中加入水印,来告诉使用者输入TextBox中的内容,如下图片所示: 下面介绍两种方式,来添加上图中的水 ...
- Android控件晃动效果实现
Android控件晃动效果实现 原创 2016-08-12 亚运 Android学习交流 动画是android中的重要组成部分,可以让APP的交互更有趣味性,今天介绍的这个小实例可以用于提醒功能.首先 ...
- mschart走势图 vc_VC++操作MSChart表格控件,效果不错。
[实例简介] 在VC++中使用MSChart表格控件,效果不错.MSChart,微软的一个很不错的画图控件,功能比较全面 [实例截图] [核心代码] TestMsChart └── TestMsCha ...
最新文章
- linux eth0 device not found,mini2440的nfs文件系统挂不上问题“IP-Config: Device `eth0' not found”...
- 52是什么水平_送礼送什么白酒,适合送礼的白酒推荐,看这篇最新白酒送礼指南就行了...
- CentOS 7下Python环境设置 - 1
- mysql中的运算符的执行顺序_【MySQL】执行顺序
- 总结Java常见面试题和答案
- mysql中limit关键字_【JAVA】关于mysql的limit关键字使用。
- Hadoop生态圈-zookeeper本地搭建以及常用命令介绍
- Java基础知识学习06-封装、继承
- Android API 中文 (53) —— BaseAdapter
- jquery操作CSS样式全记录
- JAVA练手--数组
- SQLServer数据库分页查询
- 【计算机科学】【2017】无组织三维点云的边缘检测
- 1217: 青蛙(二)
- fmri优缺点_fmri原理优缺点
- java properties文件 变量_properties文件和环境变量
- html 简繁文件转换器,在线简繁
- 研究:随机因素使硬清扫区产生软件效应(Soft Shoulders)
- 客户资料搜索软件_CRM企业管理软件
- 算法:基于比较的排序算法