基于C#弹幕类射击游戏的实现——(二)渲染

本文介绍了使用C#和GDI+开发弹幕类射击游戏时的渲染方法,重点在于通过批处理提高渲染效率。首先定义了一个渲染接口,便于未来可能更换更高效的渲染器。接着实现了一个GDI渲染器,能够进行屏幕绘图。为了实现批处理,设计了一个图形设备类,其Begin和End方法分别用于清除内容和批量绘制RenderObject实例,这些实例是基本的渲染图元,如线条和图像。最终,通过GraphiceDevice的Begin、RenderObject和End方法,实现了游戏画面的绘制流程。
摘要由CSDN通过智能技术生成

这个游戏打算是用C#+GDI做~所以渲染效率上还是要进行一些考虑的

这里对传统的GDI+封装了下,通过批处理来提高一些效率

 

首先给出的是渲染接口的定义,方面以后更换高性能的渲染器(当然很遥远)

/// <summary>
    /// 渲染器接口
    /// </summary>
    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实现的一个渲染器

/// <summary>
    /// GDI渲染器
    /// </summary>
    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,
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值