Vb. NET implementation DirectDraw9 (2) animation

Source: Internet
Author: User
Tags exit datetime drawtext integer transparent color modify prepare thread
Keyword: vb.net DirectX DirectDraw 9 Author: Dong Gangjun
Reprint please indicate from http://blog.csdn.net/a11s

=========== Diary ================
Found that the recent lazy, code a little bit longer than that, do not want to read. It's better to read a book.
Consider playing games, but the knowledge you have learned is not enough. The revolution has not yet succeeded, comrades still need to work hard AH
===========end Diary =============

Today is complete Full-screen animation, 1024x768, plus multi-layer rendering (the mouse position is not worth mentioning)

Think first about how we usually draw pictures. (should be a picture)
1 Prepare a piece of paper, this is for others to see (Primarysurface)
2 One pen (Draw method)
Then free to play on it (find coordinates, painting ...)

This is OK, your audience (the person sitting in front of the monitor) not only can see your drawing, but you can also see what you are drawing (maybe you're drawing with a sketch ...). Obviously, we do not want the audience to see the "graceful gesture" of our drawing. We want speed and effect, so slow is not called DirectDraw.

Programme 2
1 Prepare a piece of paper, this is for the audience to appreciate (Primarysurface)
2 Prepare a molded piece, such as the egg Leonardo had painted (Surface)
3 brushes, knives, glue
Maybe you know what I'm going to do, the way to paste is naturally faster than the stroke. However, the audience can still know that you are pasted up! When you show a piece of animation, it will always be pasted everywhere ...

Programme 3
1 Find a workbench, please sit down and take a look at the current image (Primarysurface)
2 Find another or more artboards on your own (Backsurface)
3 various tools, as long as it is convenient to use (Drawfast drawrect DrawText drawcircle ...)
4 Ask an assistant to help change the work of the workbench. (Primarysurface.flip)
So you can liberate yourself and never worry that your audience will see the process you are drawing. Although they can "guess" how you draw. We do this, and we do it in backsurface with any quick method, and of course we can copy it from other surface, Then paste it into the backsurface, submit it to primarysurface, let him flip, and the user sees the result (Primarysurface), and when the audience is raving about your work, you are modifying it on the artboard after the flip, Then flip the revised palette again ...

In this way, the two artboards solve the problem. At the same time only 1 viewers are watching, the other 1 is on hand, your assistant (Primarysurface) is very smart, not wrong, so you just feel at ease to draw the current artboard (Backbuffer) is enough

To emphasize, you always draw the Backbuffer,flip method will render your backbuffer to Primarybuffer, then draw, Render, draw, render ...
Or you understand that copying to Primarysurface can also (actually a chunk of memory move)

The general steps are consistent in the previous article, just a few surface for replication, the source code is only 200 lines, the main code is less than 100 lines, there are more detailed comments. So there's no need to explain this anymore.

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

Imports Microsoft.DirectX.DirectDraw



Public Class Form1

Inherits System.Windows.Forms.Form

Private Structure Pointapi

Public x as Integer

Public y as Integer

End Structure

"The API used to .... Just keep using it ...

Private Declare Function getcursorpos Lib "user32" (ByRef Lppoint as Pointapi) as Integer



Dim Dev as New Device (Createflags.default)



Dim PS as Surface ' Primarysurface

Dim BS as Surface ' Backsurface

Dim S1 as Surface ' Surface1 for storing images, imagine a BMP on the line

Dim S2 as Surface ' Surface2 ditto

"" corresponds to the top four surface respectively

Dim Desc1 as Surfacedescription

Dim DESC2 as Surfacedescription

Dim DESC3 as Surfacedescription

Dim DESC4 as Surfacedescription

"Corresponding to the surface above

Dim RP as Rectangle

Dim RB as Rectangle

Dim R1 as Rectangle

Dim R2 as Rectangle



"'" Timer related

Dim Tlast as TimeSpan

Dim fps as String

Dim TFP as Integer = 0

Dim mytime as Date = DateTime.Now

Dim ts as New TimeSpan

Dim QiQi as Double

"Game control

Dim running as Boolean = False

Dim TT as Threading.thread

"" Mouse position

Dim M as Pointapi

"' The image to read

Const FN1 = "D:\nerv.bmp"

Const FN2 = "D:\logo.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 Label1 as System.Windows.Forms.Label

<system.diagnostics.debuggerstepthrough () > Private Sub InitializeComponent ()

Me.label1 = New System.Windows.Forms.Label

Me.suspendlayout ()

'

' Label1

'

Me.Label1.Location = New System.Drawing.Point (64, 64)

Me.Label1.Name = "Label1"

Me.Label1.Size = New System.Drawing.Size (80, 24)

Me.Label1.TabIndex = 0

Me.Label1.Text = "Init&&play"

'

' Form1

'

Me.autoscalebasesize = New System.Drawing.Size (6, 14)

Me.clientsize = New System.Drawing.Size (292, 273)

ME.CONTROLS.ADD (ME.LABEL1)

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

Initddraw () ' Initialization

Loadsurfaces () ' Read the image

If TT is nothing Then ' threads for drawing

TT = New threading.thread (AddressOf mainloop)

running = True

Tt. Start ()

End If

End Sub



Sub Initddraw ()

Dev. SetCooperativeLevel (Me, Cooperativelevelflags.fullscreenexclusiveallowmodex)

Dev. Setdisplaymode (1024, 768, 0, False)



' Primarybuffer settings

DESC1 = New surfacedescription

Desc1. Surfacecaps.videomemory = True

Desc1. Surfacecaps.primarysurface = True

Desc1. Surfacecaps.flip = True

Desc1.SurfaceCaps.Complex = True

Desc1. BackBufferCount = 1



PS = New Surface (desc1, Dev)



DESC2 = New surfacedescription

DESC2. Surfacecaps.backbuffer = True

BS = PS. Getattachedsurface (DESC2. Surfacecaps)



Bs. ForeColor = System.Drawing.Color.Blue

Bs. Fonttransparency = True

"OK, just put the Primarybuffer and Backbuffer set up, even if the initialization is complete, the other images are posted





End Sub



Sub loadsurfaces ()

"Read other layers

"Don't think surface so mysterious, is a BMP attached to more attributes just, so understand a lot of simple

DESC3 = New surfacedescription

Desc3. Surfacecaps.offscreenplain = True ' behind the scenes

Desc3. Height = BS. Surfacedescription.height ' Size

Desc3. Width = BS. Surfacedescription.width

S1 = New Surface (FN1, desc3, dev) ' Read



DESC4 = New surfacedescription

Desc4. Surfacecaps.offscreenplain = True ' Direct read

S2 = New Surface (FN2, desc4, Dev)



Dim Key as ColorKey ' is used to set the transparent

Key. Colorspacehighvalue = 0

Key. Colorspacelowvalue = 0

S2. SetColorKey (Colorkeyflags.sourcedraw, key) ' Set Transparent color

"' Set Rectangle position information

'''

Rb. Width = BS. Surfacedescription.width

Rb. Height = BS. Surfacedescription.height



R1. Width = S1. Surfacedescription.width

R1. Height = S1. Surfacedescription.height



R2. Width = S2. Surfacedescription.width

R2. Height = S2. Surfacedescription.height

R2. X = 100

R2. Y = 100



End Sub



Sub Mainloop ()

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

BLT () "' main drawing process

TFP + 1 "' fps++

If TFP = Then ' ' 200 Time of calculation

TFP = 0

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

MyTime = DateTime.Now

If ts. TotalSeconds <> 0 Then

QiQi =/(ts. TotalSeconds)

fps = QiQi. ToString ("##.##") + "F C"

End If



End If



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

End While

End Sub



Sub BLT ()

If BS is nothing Then Exit Sub

Getmousepos () ' Get mouse position

"The following is the use of Backsurface method to draw, casually painting, hehe

Bs. Drawfast (0, 0, S1, R1, drawfastflags.wait)

Bs. DrawText (A, "1024x768 Frames per Second" + fps, False)

Bs. DrawText (10, 30, "Current position: x=" + m.x.tostring + ", y=" + m.y.tostring, False)

Bs. DrawText ("ESC exit", False)

"Paint the map."

Bs. Drawfast (m.x, M.y, S2, Drawfastflags.sourcecolorkey Or drawfastflags.wait)

"By the way, hit its coordinates in the mouse position.

Bs. DrawText (m.x +, M.y, "(" + m.x.tostring + "," + m.y.tostring + ")", False)

' Key step, flip, and do not use wait, this may drop frames, but does not affect the speed

Ps. Flip (Nothing, Flipflags.novsync)



End Sub



Private Sub Form1_keyup (ByVal sender as Object, ByVal e as System.Windows.Forms.KeyEventArgs) Handles Mybase.keyup

If E.keycode = keys.escape Then End ' ESC exit

End Sub

Sub Getmousepos ()

GetCursorPos (M) ' Call API, get mouse position

End Sub

End Class



=====================================
A lot of the code is the same as the previous one, and the general idea is the same as the example I gave (Scenario 3).

Through the code. Can understand the specific steps

DirectDraw and then down is the image of the transparency of the calculation (this has the formula), the size of the transformation, rotation. Please refer to the ready-made code and formula, I did not learn computer graphics, it is very difficult to estimate. and DDraw has a lot of built-in ways to provide a simple way to transform, This estimate needs to check the English original DirectDraw7 to find the corresponding method ...

OK DirectDraw to this end, if possible, but also write an application DirectDraw demo, but the estimate will not be so specific description, hehe.

Finally, we can study that mix.


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.