So, here I am looking at an empty form project in VB.NET. I have finally figured out how to get references to Microsoft.DirectX and Microsoft.DirectX.DirectDraw. Now, what next?
Let's test out DirectDraw. (It's very similar to DirectX8, except with new names and no more Hungarian Notation, making the variable names shorter :D.)
Imports
Microsoft.DirectXWindows Form Designer generated code
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As _Ah, a wonderful blank project. :P (I wrote Begin to make it look less intimidating.)
Well, in DirectX8, we needed to create a DirectX object and then a DirectDraw object... not anymore... these things do not need to be created. Really, the only things we need to create here in DirectX9 are a device and two surfaces (and other things that can go elsewhere). The device is the thing that does the drawing Insert Picture of Device here and the surface is the thing that holds our picture Insert Picture of Surface here. But, before we actually get our fullscreen, we need to set them up. You can't get something for nothing :).
'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.
Now, the variables have been created... now it's time for them to do something. Yes, it's time to add something to that Form1_Load event procedure. :o
'Things that we'll
need to use here. This goes at the top of the Form1_Load.
Dim surfDesc
As Microsoft.DirectX.DirectDraw.SurfaceDescription
= _
New
SurfaceDescription
'This will hold the description of the surface that we
want to make. We give
'it a nice description of what it can do: be primary, be flipped to, have
'backbuffers, etc. ;) kind of like an application form.
Dim sC As
Microsoft.DirectX.DirectDraw.SurfaceCaps
'I think of these things like pen caps for some reason.
It lets you clip
'another surface to your primary surface... like a pen cap clips onto a suit
'pocket. :)
Now, those are just declarations. Here comes the real heart of the matter... what to do with them... and how to avoid getting errors.
'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
If Not (MainSf Is Nothing)
Then
'We do have to
check somewhere that our 'Application Form' for our
'main surface was approved, right?
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.
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.
Now, that might seem like a lot of work just
to get "Hello World!" to show up. And then, there's an ugly splash of yellow at the top.
But, this is just the beginning.
This is like learning how to drive to store downtown just to get a candy bar.
Once we add things, we'll be able to do them faster... we'll be able to buy more
stuff from the store at one time with minimal effort. We will learn how to
shop healthy... er, we will learn how to draw efficiently.
We need to somehow close the application... right now, I have "Hello World"
stamped on the screen... and I need to close this out so I can return back to
Windows. We can use the click event to close the form.
'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
Now, our form can be closed when clicked. :)
Now, to take care of that ugly splash of yellow at the top where the backbuffer rubbed against the main surface:
'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.
And now, we get a pretty black screen with Hello World on it. That's all for this lesson. Next, we'll display pictures!
Summary of what we have done today.
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.
Windows Form Designer generated code
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As _'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
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.
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, Drawing a Picture to the Screen