首页 > 编程 > .NET > 正文

VB.NET实现DirectDraw9 (1) 托管的DDraw

2024-07-10 13:00:51
字体:
来源:转载
供稿:网友
关键字: vb.net directx directdraw 9 作者:董含君
转载请注明来自: http://blog.csdn.net/a11s

================以下为废话(记日记是好习惯)===============
本来应该继续directsound 混音的,但是今天由于时间关系,完不成任务了.

发现国外的一个站点,里面的人比较牛x 直接自己用binaryreader读取wav

自愧不如啊~~~

================end 废话===============================

很多人都认为directdraw在directx7时代就终结了,到了vb.net以及托管的directx

微软要继承旧的directdraw,又要托管.所以ddraw毕竟是有变化的.

现在拾起来.有点不适应.但是基本概念还是不会变化的.


国内高手对此不屑,而且还有人会了3d后敌视2d,我就不发表评论了,倒是让我有了填补国内空白的机会了

首先说大体过程.我的学习笔记比较基础,不是为了体现ddraw的性能,而是理清步骤(微软的例子就麻烦4了)

0 工程添加directdraw的引用,代码里面imports microsoft.directx.directdraw

1 创建device 设置合作级别 (只要是directx 都要这样)

2 创建描述 (为了创建surface做准备,跟directsound创建buffer的描述一个道理)

3 利用描述创建主缓冲以及二级缓冲(就是backsurface)

4 为主缓冲指定一个clipper

=============加载完成==================

绘制过程比较简单,你可以使用timer设置时间间隔.但是一般人不会这么作,虽然实现过程简单.

为了体现平滑的动画,往往cpu能用就用,fps越高越好(其实太高了也没用,但是当动画复杂的时候就有用了)

以下是参考大风给我的计时器.



dim tfp as integer = 0 '''fps

dim mytime as date = datetime.now '''临时用一下

while (run = true) '''如果游戏没有结束

blt() '''主要绘制过程

tfp += 1 '''fps++

if tfp = 200 then '''200次的时候计算时间

tfp = 0

dim ts as new timespan

ts = (datetime.now.subtract(mytime))

mytime = datetime.now

if ts.totalseconds <> 0 then

dim qiqi as double = 200 / (ts.totalseconds)

me.text = qiqi.tostring("##.##") + "f c"

end if



end if



tt.sleep(20) '''硬性规定休息一下,当然可以去掉发挥max速度

end while

需要知道,里面的变量都是临时定义的,如果你对速度很在意,这样做是不可取的,但是为了理解方便,所以才这么作,进行优化的时候就要拿出来了.

有了计数器,用它建立了游戏循环.循环自己管理主要的blt()过程,blt也很简单



sub blt()



if ps is nothing then exit sub

dim r1 as rectangle

dim r2 as rectangle

r2.height = desc2.height

r2.width = desc2.width

r1.height = p.size.height

r1.x = 0

r1.y = 0

r1.width = p.size.width

ps.draw(r1, bs, r2, drawflags.wait)



end sub

就是设置好矩形区域然后绘制,这里不再是ddraw7的rect了,里面多少有些变化.

调用游戏循环很简单,直接main2就可以,但是不可取,这样做会让你的程序”卡死”,导致程序没有响应

需要新开一个线程处理这件事情



private sub label2_click(byval sender as system.object, byval e as system.eventargs) handles label2.click

run = not run

if tt is nothing then

tt = new threading.thread(addressof main2)

tt.start()

end if

end sub

ok,具体需要注意的就这么多,以下为全部源代码

======================================



imports microsoft.directx.directdraw

imports microsoft.directx

imports system.drawing





public class form1

inherits system.windows.forms.form

dim dev as device

dim ps as surface

dim bs as surface

dim desc as surfacedescription

dim desc2 as surfacedescription

dim clip as clipper

dim t as new date

dim run as boolean

dim tt as threading.thread

const fn = "d:/nerv.bmp"



#region " windows 窗体设计器生成的代码 "



public sub new()

mybase.new()



'该调用是 windows 窗体设计器所必需的。

initializecomponent()



'在 initializecomponent() 调用之后添加任何初始化



end sub



'窗体重写 dispose 以清理组件列表。

protected overloads overrides sub dispose(byval disposing as boolean)

if disposing then

if not (components is nothing) then

components.dispose()

end if

end if

mybase.dispose(disposing)

end sub



'windows 窗体设计器所必需的

private components as system.componentmodel.icontainer



'注意: 以下过程是 windows 窗体设计器所必需的

'可以使用 windows 窗体设计器修改此过程。

'不要使用代码编辑器修改它。

friend withevents p as system.windows.forms.picturebox

friend withevents label1 as system.windows.forms.label

friend withevents label2 as system.windows.forms.label

<system.diagnostics.debuggerstepthrough()> private sub initializecomponent()

me.p = new system.windows.forms.picturebox

me.label1 = new system.windows.forms.label

me.label2 = new system.windows.forms.label

me.suspendlayout()

'

'p

'

me.p.location = new system.drawing.point(40, 48)

me.p.name = "p"

me.p.size = new system.drawing.size(640, 480)

me.p.tabindex = 0

me.p.tabstop = false

'

'label1

'

me.label1.location = new system.drawing.point(16, 16)

me.label1.name = "label1"

me.label1.size = new system.drawing.size(72, 24)

me.label1.tabindex = 1

me.label1.text = "init"

'

'label2

'

me.label2.location = new system.drawing.point(112, 16)

me.label2.name = "label2"

me.label2.size = new system.drawing.size(80, 16)

me.label2.tabindex = 2

me.label2.text = "draw"

'

'form1

'

me.autoscalebasesize = new system.drawing.size(6, 14)

me.clientsize = new system.drawing.size(728, 541)

me.controls.add(me.label2)

me.controls.add(me.label1)

me.controls.add(me.p)

me.name = "form1"

me.text = "form1"

me.resumelayout(false)



end sub



#end region



private sub label1_click(byval sender as system.object, byval e as system.eventargs) handles label1.click

initdd()

end sub



sub initdd()

dev = new device(createflags.default)

dev.setcooperativelevel(me, cooperativelevelflags.normal)



dim caps as new surfacecaps

dim caps2 as new surfacecaps

caps.primarysurface = true

caps.videomemory = true

caps2.offscreenplain = true



desc = new surfacedescription(caps)

desc2 = new surfacedescription(caps2)

desc2.emptyfacecolor = rgb(0, 0, 255)



clip = new clipper(dev)

clip.window = p

bs = new surface(fn, desc2, dev)

ps = new surface(desc, dev)

ps.clipper = clip

me.text = "loaded"



end sub



private sub label2_click(byval sender as system.object, byval e as system.eventargs) handles label2.click

run = not run

if tt is nothing then

tt = new threading.thread(addressof main2)

tt.start()

end if

end sub

sub blt()



if ps is nothing then exit sub

dim r1 as rectangle

dim r2 as rectangle

r2.height = desc2.height

r2.width = desc2.width

r1.height = p.size.height

r1.x = 0

r1.y = 0

r1.width = p.size.width

ps.draw(r1, bs, r2, drawflags.wait)



end sub



sub main2()

dim tfp as integer = 0 '''fps

dim mytime as date = datetime.now '''临时用一下

while (run = true) '''如果游戏没有结束

blt() '''主要绘制过程

tfp += 1 '''fps++

if tfp = 200 then '''200次的时候计算时间

tfp = 0

dim ts as new timespan

ts = (datetime.now.subtract(mytime))

mytime = datetime.now

if ts.totalseconds <> 0 then

dim qiqi as double = 200 / (ts.totalseconds)

me.text = qiqi.tostring("##.##") + "f c"

end if



end if



tt.sleep(20) '''硬性规定休息一下,当然可以去掉发挥max速度

end while

end sub



private sub form1_closing(byval sender as object, byval e as system.componentmodel.canceleventargs) handles mybase.closing

end

end sub

end class



====================================================

需要注意的是,这里进行绘制的时候,图片的位置是相对屏幕左上角的,只要你改变响应的rect就可

因为获得坐标牵扯到其他地方,不宜于降低本文难度,所以没加.

下一步的directdraw 全屏幕的动画 以及出现角色绘制

directsound混音好像很麻烦.....


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表