使用Manged DirectX 9.0 --- Part I

本文介绍如何使用Managed DirectX 9.0进行游戏引擎开发,重点讲解了封装DirectX编程的方法,并通过C#实现了一个名为Graphics2D的类,用于处理二维绘图功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

href="style/snow/style.css" type="text/css" rel="stylesheet" />
 
注册GameRes用户 登录 找回密码 |  精华区索引 | 新帖快速索引 | 本版精华区欢迎访问本论坛!

GameRes游戏开发论坛 » 游戏程式技术(Game Program) » 使用Manged DirectX 9.0 --- Part I

本主题共有1张帖子, 被点击383发表新主题发表回复发布新投票
rufi
Exp:87

侦察兵
 发表于: 2003-8-14 1:14:00

博客 | 档案 | Email | 短信 | 平板 | 收藏 | 编辑 | 删除 | 引用   


使用Manged DirectX 9.0 --- Part I

我们的目的是做一个引擎, 所以一开始就把所有DirectX编程的部分封装起来.
各功能模块用类来实现, 这些类放在同一个名字空间中, 我起的名字是DXEngine.
其中二维绘图的部分封装在Graphics2D类中.
给出完整的代码, 目的是想说明如何来实现封装.
Graphics2D类最初具备的功能是绘制图片和文字,以及使用颜色键绘图.
以后会逐步添加和完善起来.

 



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;
}
}
}

 

 


怎么样, C#的代码是不是比C++清爽多了!

rufi 2003-8-14 1:19:59

 注册: 2003-8    状态: Offline   Top
1:  1 

 主题管理:  删除关闭/取消置顶/取消精华/取消移动复制编辑主题提问交流原创作品业内关闭清除

树型显示: 使用Manged DirectX 9.0 --- Part I
使用Manged DirectX 9.0 --- Part I    (rufi 2003-8-14 1:14:00)

您尚未登录论坛,可以同通过这里进行登录,如果您不是GameRes用户,可以点击这里立即注册


关于本站 | 投稿指南 | 广告服务 | 联系本站

id="contentFRM" name="contentFRM" marginwidth="0" marginheight="0" src="http://images.gameres.com/images/ad/ad_insert468x60.htm" frameborder="0" width="468" scrolling="no" height="60">

未经书面许可,请勿转载、链接本站内容,否则非法引用所造成的后果自负;
本站不对所提供的所有资讯正确性负责,若因资讯导致的任何损失本站概不负责,请自行斟酌。
闽ICP备05005107号
Copyright © 2001-2006 GameRes游戏开发资源网 All Rights Reserved.
<script language="javascript" src="http://data.gameres.com/asp/stat/stat.asp?style=&amp;siteid=1&amp;tzone=8&amp;tcolor=32&amp;ssize=1280,1024&amp;referrer=http%3a//www.baidu.com/s%3fwd%3d%25ca%25b9%25d3%25c3manged+directx+9.0+---+part+i%26cl%3d3" type="text/javascript"></script>

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值