Vb. NET implementation of DIRECTDRAW9 (1) Hosted DDraw

Source: Internet
Author: User
Tags exit datetime integer reference sleep thread time interval tostring
Managed Keywords: vb.net DirectX DirectDraw 9 Author: Dong Gangjun
Reprint please indicate from: http://blog.csdn.net/a11s

================ below is nonsense (keeping a diary is a good habit) ===============
Should have continued to DirectSound mixed sound, but today due to the time relationship, the completion of the task.

Found a foreign site, inside the people compare cow x directly with BinaryReader read wav

To be humbled ~ ~ ~

================end nonsense ===============================

Many people think that DirectDraw ended up in the DirectX7 era, to vb.net and managed DirectX

Microsoft is going to inherit the old DirectDraw and be hosted. So DDraw has changed after all.

Now pick it up. A little uncomfortable. But the basic concepts are not going to change.


Domestic master to this disdain, and some people will be hostile to the 2d after 3D, I will not comment, but let me have the opportunity to fill the gaps in the country

First of all, the general process. My Learning notes are based not on the performance of DDraw, but on the steps (Microsoft's example would be a hassle 4)

0 Project Add DirectDraw Reference, the code inside imports Microsoft.DirectX.DirectDraw

1 Create device set the level of cooperation (as long as it is DirectX)

2 Create a description (in order to create a surface to prepare, with DirectSound create a buffer description of a reason)

3 Create a primary buffer with a description and a level two buffer (that is, backsurface)

4 Specifies a clipper for the primary buffer

============= Loading Complete ==================

The drawing process is simpler, and you can set the time interval using the timer. But most people do not, although the implementation process is simple.

In order to reflect the smooth animation, often the CPU can use, FPS higher the better (in fact, too high is useless, but when the animation is complex when it will be useful)

Here is a reference to my timer from the Gale.



Dim TFP as Integer = 0 ' fps

Dim mytime as Date = DateTime.Now ' ' Temporary use

while (run = True) ' If the game is not over

BLT () "' main drawing process

TFP + 1 "' fps++

If TFP = Then ' ' 200 Time of calculation

TFP = 0

Dim ts as New TimeSpan

TS = (DateTime.Now.Subtract (mytime))

MyTime = DateTime.Now

If ts. TotalSeconds <> 0 Then

Dim QiQi as Double =/(ts. TotalSeconds)

Me.Text = QiQi. ToString ("##.##") + "F C"

End If



End If



Tt. Sleep (20) "" "hard rule rest, of course, can be removed to play Max speed

End While

Need to know, inside the variables are temporarily defined, if you are very concerned about the speed, it is not advisable, but in order to understand the convenience, so do it, optimize the time will be taken out.

With the counter, use it to build the game loop. Loop yourself to manage the main BLT () process, BLT is also very simple



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

is to set up a rectangular area and then draw, here is no longer the DDraw7 of the Rect, the number of some changes inside.

Calling the game loop is simple, direct main2 can be, but not desirable, this will make your program "card dead", causing the program does not respond

Need to open a new thread to handle this thing



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, the specific need to pay attention to so much, the following is the full source code

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



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 "code generated by the Windows forms Designer"



Public Sub New ()

MyBase.New ()



' This call is required by the Windows Forms Designer.

InitializeComponent ()



' Add any initialization after the InitializeComponent () call



End Sub



' Form overrides dispose to clean up the list of components.

Protected Overloads Overrides Sub Dispose (ByVal disposing as Boolean)

If disposing Then

If not (components are nothing) Then

Components. Dispose ()

End If

End If

Mybase.dispose (disposing)

End Sub



' Required by the Windows Forms Designer

Private Components as System.ComponentModel.IContainer



' NOTE: The following procedure is required by the Windows Forms Designer

' You can use the Windows Forms Designer to modify this procedure.

' Do not modify it using the Code Editor.

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 ' ' Temporary use

while (run = True) ' If the game is not over

BLT () "' main drawing process

TFP + 1 "' fps++

If TFP = Then ' ' 200 Time of calculation

TFP = 0

Dim ts as New TimeSpan

TS = (DateTime.Now.Subtract (mytime))

MyTime = DateTime.Now

If ts. TotalSeconds <> 0 Then

Dim QiQi as Double =/(ts. TotalSeconds)

Me.Text = QiQi. ToString ("##.##") + "F C"

End If



End If



Tt. Sleep (20) "" "hard rule rest, of course, can be removed to play Max speed

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



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

Note that when drawing here, the position of the picture is relative to the upper-left corner of the screen, as long as you change the response of the RECT can

Because the coordinates to get involved in other places, it is not appropriate to reduce the difficulty of this article, so did not add.

Next DirectDraw Full-screen animations and the presence of role drawing

DirectSound mix sounds like a lot of trouble ...




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.