DirectX 9 SDK编程的教程

DirectX 9 SDK编程的教程

2023年7月29日发(作者:)

DirectX 9 SDK编程的教程

2008-11-11 14:19

译:kevin-y

程序代码:下载

导言

欢迎来到我的第一个关于DirectX编程的教程,我们使用的是DirectX 9 SDK。很大一部

分的C#开发人员都等待这个版本。在此这前,C#开发人员都是通过DirectX 7

或 8 的

VB组件,使用COM接口的方式,来进行DirectX的开发。新版本的DirectX组件提供的更好

的性能、更容易的编程。该教程适合DirectX编程的新手(如我),还有其他使用COM接

口进行DirectX开发的人员。在这教程里,我们将重建一个Super Metroid的游戏(我们

就叫她做Managed metroid吧)。我们将要应用所有DirectX编程中的方方面面(Direct

Draw,DirectSound,DirectInput,Direct3D,DirectPlay 和AudioVideoPlayback)。在第

一节我们将要学习在全屏下绘制文字和窗口标题,以此灌输一些基本的DirectDraw知识

我们所需要的

-Microsoft Windows NT4 SP6, Windows 2000, Windows XP Pro(只是编译所需)

-Visual C# .NET or Visual Studio .NET

-DirectX 9 SDK Full or only C# Part(没有的话要到这Microsoft)

-Image editing tool(可选但很有用)

-SNES Emulator with Super Metroid ROM(可选)

第一节:标题窗口

1. 加入DirectX 的命名空间到project中。

在引用(reference)中加入 and

D

using X;

using Draw;

2. 加入变量

一开始,我们需要建立DirectDraw Device,接着建立Surfaces。Surfaces是我们将在其

上绘制实体的一块内存,不过用起来又象是一个层、一个页面。我们设置的Device会剪

去Device边界以外的内容。变量back 是BackBuffer,title和text是标题窗口的一个层

。最后,titlescree是指向“”文件的字符串,并可作为程序的标题。

// The DirectDraw Device, used in all the application private Device display;

// The Front Surface

private Surface front = null;

// The Back Surface

private Surface back = null;

// Surface to store the title screen

private Surface title = null;

// Surface to store the text

private Surface text = null;

// The Clipper

private Clipper clip = null;

string titlescreen = pPath + "";

3. 初始化DirectDraw

在初始化任何DirectX组件时,我建议你为每个你使用的组件方法建立一个专门的方法。

在这里我用一个方法来初始化DirectDraw的素材。

private void InitDirectDraw()

{

// Used to describe a Surface

SurfaceDescription description = new SurfaceDescription();

// Init the Device

display = new Device();

#if DEBUG

perativeLevel(this,

);

#else

// Set the Cooperative Level and parent, Setted to Full Screen

Exclu

sive to the form)

perativeLevel(this,

reenEx

clusive);

// Set the resolution and color depth used in full

screen(640x480, 1

6 bit color)

playMode(640, 480, 16, 0, false);

#endif

// Define the attributes for the front Surface

ySurface = true;

#if DEBUG

front = new Surface(description, display);

#else

= true;

x = true; // Set the Back Buffer count

fferCount = 1;

// Create the Surface with specifed description and device)

front = new Surface(description, display);

#endif

();

#if DEBUG

= ;

= ;

eenPlain = true;

back = new Surface(description, display);

#else

// A Caps is a set of attributes used by most of DirectX

compone

nts

SurfaceCaps caps = new SurfaceCaps();

// Yes, we are using a back buffer

ffer = true;

// Associate the front buffer to back buffer with specified

caps

back = achedSurface(caps);

#endif

// Create the Clipper

clip = new Clipper(display);

/// Set the region to this form

= this;

// Set the clipper for the front Surface

r = clip;

// Reset the description

();

// Create the title screen

title = new Surface(titlescreen, description, display);

();

// Set the height and width of the text.

= 600;

= 16;

// OffScreenPlain means that this Surface is not a front, back, alpha

Su

rface.

eenPlain = true;

// Create the text Surface

text = new Surface(description, display);

// Set the backgroup color ill();

// Set the fore color of the text

lor = ;

// Draw the Text to the Surface to coords (0,0)

xt(0, 0, "Managned DirectX Tutorial 1 - Press Enter or

Escape

to exit", true);

}

4. 绘制方法

方法Draw是向窗口的表面绘制文本的唯一方法。

private void Draw()

{

// If the front isnt create, ignore this function

if (front == null)

{

return;

}

// If the form is minimized, ignore this function

if(State == zed)

{

return;

}

try

{

// Draw the title to the back buffer using source copy blit

st(0, 0, title, );

// Draw the text also to the back buffer using source copy blit

st(10, 10, text, );

#if DEBUG

// Draw all this to the front

(back, );

#else

// Doing a flip to transfer back buffer to the front,

faster

(back, );

#endif

}

catch(WasStillDrawingException)

{

return;

}

catch(SurfaceLostException)

{ // If we lost the surfaces, restore the surfaces

RestoreSurfaces();

}

}

5. RestoreSurfaces 方法

当用户离开了我们的程序,不一会又回来的,我们就要用这方法。

private void RestoreSurfaces()

{

// Used to describe a Surface

SurfaceDescription description = new SurfaceDescription();

// Restore al the surface associed with the device

eAllSurfaces();

// Redraw the text

ill();

xt(0, 0, "Managned DirectX Tutorial 1 - Press Enter or

Escape

to exit", true);

// For the title screen, we need to dispose it first and then

re-create

it

e();

title = null;

title = new Surface(titlescreen, description, display);

return;

}

6. 最后要做的

在这里,将InitDirectDraw 方法加入到构造函数中。并在程序的主遁环中加入Draw方法

在程序遁环执行中,需要调用ts()使程序处理信息事件。

public Tutorial1()

{

//

// Required for Windows Form Designer support

//

InitializeComponent();

// Initialize DirectDraw stuffs

InitDirectDraw();

// Remove the cursor

e();

// Show the form if isnt already do

();

// The main loop

while(Created) {

Draw();

// Make sure that the application process the messages

ts();

}

}

在这里,方法并没有工作,所有你必须改动Main方法。你要自己建立你

的表单(form)。

static void Main()

{

Tutorial1 app = new Tutorial1();

();

}

处理KeyUP事件,用她来退出程序。

private void Tutorial1_KeyUp(object sender,

ntArg

s e)

{

// If the user press Escape or Enter, the tutorial exits

if(e == || e == )

();

}

发布者:admin,转转请注明出处:http://www.yc00.com/xiaochengxu/1690642122a384031.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信