Keywords: VB.NET DirectX DirectDraw 9 Author: Dong Jun containing
Reprint, please indicate from http://blog.9cbs.net/a11s
=========== Diary =================
I found that it is relatively lazy, the code is slightly longer, I don't want to see it. I still read the book better.
Consider making a game, but the knowledge you have learned is not enough. The revolution has not been successful, and comrades still need to work.
=========== End diary =============
Today is the full screen movie, 1024x768, plus multiple layers (not worth mentioning in the mouse position)
Let me recall how we usually draw. (It should be a picture)
1 Prepare a piece of paper, this is going to see someone (PrimarySurface)
2 pen (DRAW method)
Then you can play freely (find coordinates, painting ..)
This is ok, your viewer (the person sitting in front of the monitor) can not only see your drawing work, but even how you draw it (maybe you are using the despot gauge ...). It is obvious, We don't want the audience to see "elegant gestures" when we draw. We want the speed and effect, so slowly not DirectDraw.
Scenario 2
1 Prepare a piece of paper, this is appreciated by the audience (PrimarySurface)
2 Prepare a molded work, such as Dathenge's egg (SURFACE)
3 brush, sharp knife, glue
Probably, you know what I want to do, naturally it is natural to be more than the point. However, the audience can still know that you are paste! When you demonstrate a movie, it is often pasted everywhere. Yes.....
Solution 3
1 Looking for a workstation, please sit down and slowly see the current image (PrimarySurface)
2 you can find another picture board yourself (Backsurface)
3 various tools, as long as it can be used easy to use (Drawfast DrawRect DrawText Drawcircle .....)
4 Please help with the work of the workbench. (PrimarySurface.flip)
This way you can liberate, no longer have to worry about you see the process you draw. Although they can "guess" how you draw it. We do this, in the backsurface, you can make pictures, of course you can Copy it from other Surface, then paste into the backsurface, submit it to PrimarySurface, let him flip, the user see is the result after you draw, when the audience is full of praise your work, you are just flip After modified on the drawing board, then put this picture board after the modification flip ...
In this way, the two picture boards have solved the problem. At the same time, only one is what the audience is watching, and one other is painting on your hand. Your Assistant (PrimarySurface) is very smart, not mistaken, so you only have to follow the current painting The BACKBUFFER is enough.
Emphasize that you will always draw backbuffer, flip method presented your Backbuffer to PrimaryBuffer, then draw, present, draw, rendered ...
Or you understand that you can copy to PrimarySurface (actually a block movement)
The general step is consistent with the foreman, just a few life a few Surface is used for copying, the source code is only 200 lines, the main code will not be 100 lines, there is a more detailed note. So there is no need to explain here. In
============================================================================================================================================================================================================= Imports Microsoft.directx.directDraw
Public Class Form1
Inherits System.Windows.Forms.form
Private structure pointapi
Public x as in
Public y as integer
End structure
'' API is used to it .... Just continue to use 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 is used to store images, imagine that it is a BMP.
DIM S2 As Surface 'Surface2
'' 'The four Surface corresponding to the top
DIM DESC1 AS SURFAedScription
DIM DESC2 AS SURFAedScription
DIM DESC3 AS SURFAedScription
DIM DESC4 AS SURFAedScription
'' Corresponds to the Surface above
DIM RP As Rectangle
DIM RB As Rectangle
DIM R1 AS Rectangle
DIM R2 AS Rectangle
'' 'Timer
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
'' 'Images needed to read
Const fn1 = "d: /nerv.bmp"
Const fn2 = "d: /logo.bmp"
#Region "Windows Form Designer Generated Code"
Public Sub New ()
Mybase.new ()
'This call is required for the Windows Form Designer.
InitializeComponent ()
'Add any initialization after INITIALIZECOMPONENT ()
End Sub
'Form rewriting Dispose to clean up the list of components.
Protected Overloads Overrides Sub Dispose (byval disposing as boolean) if disposing the
IF not (Components Is Nothing) THEN
Components.dispose ()
END IF
END IF
Mybase.dispose (Disposing)
End Sub
'Windows Form Designer
Private Components as System.comPonentModel.icontainer
'Note: The following process is necessary for the Windows Form Designer.
'You can modify this process using the Windows Form Designer.
'Don't modify it using the code editor.
Friend Withevents Label1 As System.Windows.Forms.Label
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 e as system.Object, byval e as system.eventargs) handles label1.click
initddraw () 'Initialization
LoadSurfaces () 'Read images
IF tt is nothing then 'for drawing thread
Tt = new threading.thread (Addressof Mainloop)
Running = TRUE
Tt.start ()
END IF
End Sub
Sub initddraw ()
Dev.SetcoOPERATIVELEVELEVELEVELEVELEVELEVEL (ME, COOPERATIVELEVELAGS.FULLSCREENEXCLUSIAALLOWMODEX)
Dev.SetDisplayMode (1024, 768, 16, 0, false)
'PRIMARYBUFFER settings
Desc1 = New SurfaceDescription
Desc1.surfacecaps.videMemory = TRUE
Desc1.SurfaceCaps.primarySurface = true
Desc1.surfacecaps.flip = trueDesc1.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 set the PrimaryBuffer with the Backbuffer to complete, the other images are put on
End Sub
Sub loadsurfaces ()
'' 'Read other layers
'' 'Don't think of Surface so mystery, is a BMP add more attribute, so understanding is 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 'directly read
S2 = New Surface (FN2, DESC4, DEV)
DIM key as colorKey 'is used to set transparency
Key.colorspacehighValue = 0
Key.colorspaceLowValue = 0
S2.SetColorKey (ColorKeyflags.Sourcedraw, key) 'Setting transparent color
'' '' Setting Rectangular Location 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 = 200 Then' '' 200 times calculation time
TFP = 0
TS = (datetime.now.subtract (myTime))
Mytime = datetime.now
IF ts.totalseconds <> 0 THEN
qiqi = 200 / (ts.totalseconds)
FPS = qiqi.tostring ("##. ##") "f C"
END IF
END IF
Tt.sleep (10) '' 'rigid rules take a break, of course you can go to the MAX speed
End while
End Sub
SUB BLT ()
IF bs is nothing kilion
GetMousePos () 'Get the mouse position
'' 'Below is to use the backsurface method to draw, just painted, huh, huh
Bs.drawfast (0, 0, S1, R1, Drawfastflags.wait)
BS. DrawText (10, 10, "1024x768 frames per second" fps, false)
BS. DrawText (10, 30, "Current location: x =" m.x.tostring ", y =" m.y.tostring, false)
Bs.drawtext (10, 50, "ESC Exit", False)
'' Drawing map
Bs.drawfast (M.X, M.Y, S2, Drawfastflags.SourceColorKey or Drawfastflags.wait)
'' 'By the way, in the position of the mouse, it is playing its coordinates.
BS. DrawText (M.X 50, M.Y, "(" M.x.toTString "," M.y.tostring ")", FALSE)
'' 'Key one step, flip, and don't use Wait, this may be lost, but does not affect speed
Ps.flip (Nothing, Flipflags.novsync)
End Sub
Private Sub Form1_Keyup (Byval e as system.windows.forms.keyeventargs) Handles mybase.keyup
If E.Keycode = keys.escape life end 'ESC exits
End Sub
SUB getMousePos ()
GetCursorpos (m) 'calls the API to get the mouse position
End Sub
END CLASS
=====================================
Many of the code is the same as before, the general idea is also the same as the example (Solution 3), can be learned. You can understand the specific steps.
DirectDraw again is the transparency calculation of the image (this formula), size transformation, rotation. Please refer to the ready-made code and formula. My computer graphics did not learn, it is very difficult. And DDRAW still has a lot of built-in Methods can provide a simple conversion method, this estimate needs to see the English original DirectDraw7 to find the appropriate method ...
OK DirectDraw is over, if possible, you will also write a Demo to apply DirectDraw, but it is estimated that it will not be specified, huh, huh.
I can finally study the mix.