using System; using System.Windows.Forms; using System.Drawing; using Microsoft.DirectX; using Microsoft.DirectX.DirectDraw; namespace DXEngine { public delegate void DrawFunc(Graphics2D g2d); public struct Picture { public Surface BitmapSurface; public bool UseColorKey; public Picture(Surface surf) { BitmapSurface=surf; UseColorKey=false; } public Picture(Surface surf,Color colorkey) { BitmapSurface=surf; ColorKey key=new ColorKey(); int rgb=ColorTranslator.ToWin32(colorkey);//这个函数的作用跟RGB(x,x,x)是一样的 key.ColorSpaceLowvalue=rgb; key.ColorSpaceHighvalue=rgb; BitmapSurface.SetColorKey(ColorKeyFlags.SourceDraw,key); UseColorKey=true; } } /// <summary> /// Graphic2D 的摘要说明: /// Graphic2D类负责所有二维图形的绘制 /// </summary> public class Graphics2D { Control Window=null; Device display = null; SurfaceDescription description; Surface front = null; Surface back = null; Clipper clip = null; int ScreenWidth=800; int ScreenHeight=600; public int bitPerPixel=16; public Graphics2D(Control window,int width,int height) { // // TODO: 在此处添加构造函数逻辑 // this.Window=window; this.ScreenWidth=width; this.ScreenHeight=height; this.InitDirectDraw(); } public event DrawFunc OnDraw; /// <summary> /// 执行初始化 /// </summary> private void InitDirectDraw() { display = new Device(); // 创建显示设备. display.SetCooperativeLevel(this.Window,CooperativeLevelFlags.FullscreenExclusive);//全屏独占模式 display.SetDisplayMode(ScreenWidth, ScreenHeight, bitPerPixel, 0, false);//设置分辨率,色深 description = new SurfaceDescription();// 新建一个页面描述 description.SurfaceCaps.PrimarySurface = true;//为主页面 description.SurfaceCaps.Flip = true;//有换页链 description.SurfaceCaps.Complex = true;//有后台缓存 description.BackBufferCount = 1;//一个后台缓存 front = new Surface(description, display);//得到主页面 clip = new Clipper(display);//裁剪器 clip.Window = Window; front.Clipper = clip; SurfaceCaps caps = new SurfaceCaps(); caps.BackBuffer = true; back = front.GetAttachedSurface(caps);//缓冲页面 } public Picture CreateBitmapFromBMP(string filename) { description.Clear();// 每次新建一个页面时,页面描述要清空一下 description.SurfaceCaps.OffScreenPlain=true; Surface BitmapSurface=new Surface(filename,description,display); return new Picture(BitmapSurface); } public Picture CreateBitmapFromBMP(string filename,Color key) { description.Clear(); description.SurfaceCaps.OffScreenPlain=true; Surface BitmapSurface=new Surface(filename,description,display); return new Picture(BitmapSurface,key); } public void DrawBitmapBMP(int x,int y,Picture bitmap) { if(bitmap.UseColorKey==false) { back.DrawFast(x,y,bitmap.BitmapSurface,DrawFastFlags.Wait); } else { back.DrawFast(x,y,bitmap.BitmapSurface,DrawFastFlags.Wait|DrawFastFlags.SourceColorKey); } } public void DrawText(int x,int y,string text,Color forecolor) { back.ForeColor=forecolor; back.DrawText(x,y,text,true); } public void Draw() { if (front == null) { return; } try { front.Flip(back, FlipFlags.Wait); this.OnDraw(this); } catch(WasStillDrawingException) { return; } catch(SurfaceLostException) { RestoreSurfaces();//恢复页面 } } private void RestoreSurfaces() { display.RestoreAllSurfaces(); this.OnDraw(this); return; } } } |