Now that we've figured out how to write on our surface, it is now time to figure out how to get a picture to display onto our form. To get a picture, we need to make another surface that will hold the picture that we want to display. Then, we will draw the picture onto the back buffer so that it will be flipped onto the display.
First we need to add another surface.
Dim Dev
As Microsoft.DirectX.DirectDraw.Device
Dim MainSf
As Microsoft.DirectX.DirectDraw.Surface
Dim BackupSf
As Microsoft.DirectX.DirectDraw.Surface
'New surface PictureSf added below... this will contain
the picture that we have
'in the file.
Dim PictureSf
As Microsoft.DirectX.DirectDraw.Surface
Windows Form Designer
generated code
Now, we need to go into our Form1_Load and figure out how to draw our picture. (I am basing this off of the code that we have used in the previous page.)
'After we have set the BackupSf.ForeColor to yellow:
surfDesc.Clear()
'This is where
we get another empty application from the application form
'bin. We can make this empty
since the information we need to store the
'surface is in the file.
PictureSf = New Surface("n7.bmp",
surfDesc, Dev)
'I give the
filename and my empty application form to the Dev. The Dev
'approves all surface creation.
The backbuffer is an exception because it
'comes with the main surface.
'Now, PictureSf is a surface that has
the picture on it. You'll need a
'picture called n7 in the bin folder.
:)
OK, now we have the picture in the surface, and we need to draw it.
'After we have drawn Hello World to the Backup surface,
(before we flip)
BackupSf.DrawFast(200, 200, PictureSf,
DrawFastFlags.Wait)
'Draws the
surface containing our picture (PictureSf) to (200,200) on the
'screen. Wait means that we are
going to wait until the form is ready
'before we draw the picture to the
surface.
Now, wasn't that simple? :)
Summary of what we have done:
Imports
Microsoft.DirectX'After the Inherits
System.Windows.Forms.Form
Dim Dev
As Microsoft.DirectX.DirectDraw.Device
'Handles the drawing of the things that you want to be
drawn.
Dim MainSf
As Microsoft.DirectX.DirectDraw.Surface
'What you will see on the screen.
Dim BackupSf
As Microsoft.DirectX.DirectDraw.Surface
'This will function as the 'Vice President, where the
MainSf is the President.
'This surface will have the data that the President will display to the screen.
'It does this by flipping. More on that later.
'New surface PictureSf added below... this will contain
the picture that we have
'in the file.
Dim PictureSf
As Microsoft.DirectX.DirectDraw.Surface
Windows Form Designer
generated code
'This goes right
after the declarations above. Read along to see what's happening.
Try
Dev =
New Device
'We'll
need to do this as soon as possible. :)
Me.Visible
= True
'It would be
counterproductive to exert all of our efforts
'onto an invisible viewing area.
Dev.SetCooperativeLevel(Me,
CooperativeLevelFlags.FullscreenExclusive)
'The device is
now prepared to start a fullscreen display for you.
'Exclusive means we're going to
violently scratch at any window that
'pops up while the device is doing
the fullscreen :).
Dev.SetDisplayMode(640, 480,
32, 0, False)
'The device
will now kick the screen into 640x480 mode with 32-bit color
'mode (lots and lots of color mode).
0 and False are there to keep
'Dev happy. :)
'Now, we'll fill out our surfDesc 'application form' for our Main surface.
surfDesc.SurfaceCaps.PrimarySurface = True
'Do we want it
to be the main surface which appears on the screen... Yes.
surfDesc.SurfaceCaps.Flip =
True
'Do we want
stuff to be flipped onto this surface? Yes.
surfDesc.SurfaceCaps.Complex =
True
'The
description for Complex was scratched out, but I checked it anyway.
surfDesc.BackBufferCount = 1
'Fast Food:
Would you like backbuffers with that? Just one, please.
MainSf = New Surface(surfDesc, Dev)
'Our device Dev
now looks over our surfDesc 'application form' to make
'sure we filled it out correctly...
and then it gives us a surface if it
'worked. Yes, the Backbuffer
came with it and I don't need to contact the
'manager about my surface. (Thank you
for shopping at DirectDraw9)
'OK, now we've got our surface... we can't just reach in and grab our
'backbuffer
'we need to find a pen cap so that we
'can clip the backbuffer onto to the
main surface. Look... we can use sC!
sC = New
SurfaceCaps
'Now, we can
set it to clip to a backbuffer.
sC.BackBuffer =
True
'With this cap,
we can clip it directly to the main surface.
BackupSf =
MainSf.GetAttachedSurface(sC)
'And, the
backbuffer that we ordered with the main surface is now clipped
'onto the main surface. Clip!
'Let's make it yellow.
BackupSf.ForeColor =
System.Drawing.Color.Yellow
'This will make
all of my little scratches and scrapes on it turn yellow...
'as if the outer coat was removed
(the outer coat is black... once we
'scratch it, part of the outer coat
will flake off and reveal the yellow
'underneath. :P
'After we have set the BackupSf.ForeColor to yellow:
surfDesc.Clear()
'This is where
we get another empty application from the application form
'bin. We can make this empty
since the information we need to store the
'surface is in the file.
PictureSf = New Surface("n7.bmp",
surfDesc, Dev)
'I give the
filename and my empty application form to the Dev. The Dev
'approves all surface creation.
The backbuffer is an exception because it
'comes with the main surface.
'Now, PictureSf is a surface that has
the picture on it. You'll need a
'picture called n7 in the bin folder.
:)
If Not (MainSf Is Nothing)
Then
'We do have to
check somewhere that our 'Application Form' for our
'main surface was approved, right?
'This goes right before the BackupSf.DrawText call.
BackupSf.ColorFill(Color.Black)
'This will paint the whole form black and get rid of that
'ugly splash of yellow along the top of the display.
BackupSf.DrawText(10, 10, "Hello World!", False)
'I will scratch out Hello World on my backup surface. At this
'point, I
can't see it because the backbuffer is not what the form
'is looking
at... the form is looking at the primary buffer.
'In order to
see the backbuffer form, we must perform a Flip.
'After we have drawn Hello World to the
Backup surface, (before we flip)
BackupSf.DrawFast(200, 200, PictureSf, DrawFastFlags.Wait)
'Draws the surface containing our picture (PictureSf) to (200,200) on the
'screen.
Wait means that we are going to wait until the form is ready
'before we
draw the picture to the surface.
MainSf.Flip(BackupSf, FlipFlags.NoVSync)
'Here's where I flip the Back buffer over and bash it against the Main
'surface.
The stuff on the backbuffer is now transferred to the MainSf
'and the text
is now visible.
'The
FlipFlags.NoVSync means that I'm going to wait until the form is
'looking.
Without this, I would just bash it against the surface
'and the
monitor may not notice that I've bashed it... it'll notice
'sooner or
later. If I do this with a game loop, the surface will be
'bashed a bit
too much and you'll actually see me bashing it. (On
'certain
conditions, you won't, like if the whole loop is too slow :p)
End If
Catch ex As Exception
'Exception
catching which we all know and love... or not (especially when
'one occurs :P)
End Try
'End Sub goes below.
End Sub
'After the Form1_Load
event procedure.
Private Sub
Form1_Click(ByVal
sender As
Object,
ByVal e
As System.EventArgs) _
Handles
MyBase.Click
Me.Close()
End
Sub
End Class
Next, Redrawing, Refreshing, and Restoring the display.