~DIRECTX in VB.NET~
Tutorial 1
"The Basics - The BlueScreen"
Interested in exploring the world of directX? Then read on!
Alright lets see, where should we start? Ahhh yes, lets start with the basics, the very very basics, rendering a blank screen.
lets get started.
- What you need -
{
-Visual Basic 2008 or 2005
-the most recent DirectX SDK - you can get it here Download details: DirectX SDK - (February 2010)
-a decent video card.
}
- Creating the Project -
{
1. Create a new Windows project in Visual Basics 2008( will work with earlier versions of VB.net)
2. Add a new reference to Microsoft.Microsoft.DirectX , Microsoft.DirectX.Direct3D , and Microsoft.DirectX.Direct3D.D3DX .
If you don't know how to do this already, follow the Steps Listed in " -Adding the References- "
}
-Adding The References-
{
1. Goto to the "project" drop down menu and you should see "Add Reference". Wait for it to load up (sometimes it takes a minute or to as it is searching the computer for references)
2. Scroll down and add all these References:
Code:
- Microsoft.Microsoft.DirectX ,
Microsoft.DirectX.Direct3D ,
Microsoft.DirectX.Direct3D.3DX
}
-Using the References-
{
1. Add these lines to the top of your code.
Code:
Imports Microsoft.DirectX
Imports Microsoft.DirectX.Direct3D
Imports Microsoft.DirectX.Direct3D.D3DX
}
We are not going to be using form1 Just yet. Instead we are going to be adding a class.
-Creating the gameClass-
{
In this part of the tutorial we'll be adding a class to our project. Classes are very important. Without classes you would end up
writing the same code over and over again, and that's not very fun or efficient. Using Classes also make your project much more organized. You'll understand why later on.
Lets add a class to our project!
1. GoTo the "Project" drop down menu and you should see "Add New Item", Click on it.
2. Click on the Class template. Name our class "gameClass".
3. Lets add the Imports Statements to the top of our class. (you can get rid of any other Statements)
Code:
Imports Microsoft.DirectX
Imports Microsoft.DirectX.Direct3D
Imports Microsoft.DirectX.Direct3D.D3DX
}
- Declaring our variables -
{
We need to add some variables to our class.
1. Open up the code page for our gameClass
2. add these variables :
Code:
Private displaySettings As DisplayMode
Private deviceParameters As PresentParameters
Public device As Device
Public gameExit As Boolean
}
Now we need to add a new Sub to our class.
- Adding the Initialize Sub -
{
1. Add a new Sub and call it Initialize.
Code:
Public Sub Initialize (ByVal fullScreen As Boolean)
End Sub
2. Add this code inside of the Sub:
Code:
If fullScreen Then
displaySettings.Width = 800
displaySettings.Height = 600
displaySettings.Format = Format.X8R8G8B8
Else
displaySettings = Manager.Adapters.Default.CurrentDisplayMode
End If
What we just did:
- first we checked to see if fullScreen is true
if true, we set the resolution to 800 by 600 and set the format to 32bits, else we set it to the CurrentDisplayMode of the Computer.
3. Now add this code right under it:
Code:
deviceParameters = New PresentParameters()
deviceParameters.BackBufferWidth = displaySettings.Width
deviceParameters.BackBufferHeight = displaySettings.Height
deviceParameters.BackBufferFormat = displaySettings.Format
deviceParameters.SwapEffect = SwapEffect.Discard
deviceParameters.PresentationInterval = PresentInterval.Immediate
If fullScreen Then
deviceParameters.Windowed = False
Else
deviceParameters.Windowed = True
End If
What we just did:
- We set the deviceParameters to the displaySettings. Then we set the SwapEffect to discard and then PresentInterval to Immediate.
but what does SwapEffect and PresentationInterval even mean? Well the SwapEffect tells the device how to go from one frame to the next. We have set it to overwrite the previous frames. The PresentationInterval tells the device how often to show the frames. We've set it to show the frames immediately, not to wait for anything.
- After we set up the deviceParameters, we checked if fullScreen was true again. If true we set the display window to Fullscreen, if false, We set the display window to windowed.
}
Lets add another sub to our gameClass
- Adding the LoadContent sub -
{
The loadContent sub is where we are going to load all our textures and models later on.
1.Add a new sub and call it loadContent()
2.Add this line of code inside of it.
Code:
device = New Device(Manager.Adapters.Default.Adapter, DeviceType.Hardware, Form1.Handle, CreateFlags.SoftwareVertexProcessing, deviceParameters)
3. add this code to the end of our initialize sub
What we just did:
- we set the up the device. Which gets the graphics adapter, sets the devicetype, gets the window to draw to, sets the vertex processing to Software processing, and gets the deviceParameters.
i could explain this more if you would like..but i figured you just want to move along
- then we told it to execute the loadContent sub inside of our initialize sub.
info : You may be wondering what vertex's are? Well there the points that make up the triangles, and the triangles make up everything. Well be using triangles and verticals in the next tutorial.
}
We will be adding yet another sub to our gameClass.
- Adding the Draw sub -
{
The Draw Sub is where we will be doing all our rendering.
1. Add a new sub to your gameClass and call it Draw
Code:
Public Sub Draw()
End sub
2. now add this code inside the Draw sub:
Code:
Do While Not gameExit
Loop
thisExit()
What we just did :
- We added a loop inside of our Draw sub. We are going to render inside this loop. Then we told
the game to excectute a sub called, thisExit(), if the loop is no longer true. We are going to create
the thisExit() sub now. We are not finished with the draw sub yet, but will be back to it in a minute.
}
Lets now add the thisExit sub.
- Adding the thisExit sub -
{
This sub will be the Exit sub for our app.
1. Add a new sub to your gameClass and call it thisExit()
2. add this code inside:
Code:
displaySettings = Nothing
deviceParameters = Nothing
device.Dispose()
device = Nothing
Application.Exit()
What We just did.
First we cleared the displaySettings, deviceParameters, and the device. Then We Exited the
application.
}
Now we go back to our Draw sub
- Setting up our Draw Sub -
1. We need to add this code to our Draw Sub inside the loop we added earlier.
Code:
device.Clear(ClearFlags.Target, Color.CornFlowerBlue, 1, 0)
device.BeginScene()
device.EndScene()
device.Present()
Application.DoEvents()
What We just did:
-We set the device to clear the screen everytime the loop is executed. We set the back color to blue.
then we began the drawScene(). This is were we draw everything. Then we Ended the Scene
and we added Application.DoEvents to so that our application wont freeze and ignore keypresses.
}
We are now going to load our gameClass and detect keypresses.
- Detecting Keypresses, loading our class, and Exiting the game -
{
1. Go to our Form1 one and go to the code page.
2. Add this varable in our globals.
Code:
Dim game As New gameClass
4. Add this code.
Code:
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.Escape Then game.gameExit = True
End sub
5. Now add this code inside the Form1_Load sub:
Code:
Me.Show()
game.Initialize(True)
game.Draw()
What we just did:
- First We detected if the "Escape" key was pressed, then we told we set the gameExit to true which will exit the game.
- then we loaded up our Initialize and Draw subs.
}
Now run this code and you should get a blue screen! Aren't you exited! A blue screen isn't very exciting is it. Don't worry, well be getting into some
more exciting stuff in the next tutorial! Don't feel like you wasted your time. Its very important to get a grasp of the basics. Ive tried to help you do that. This code is the backbone for everything else.
Suggestion: While your waiting for the next tutorial, i recommend learning it so you can write it out from memory. That should not be to difficult, as its not much code.
Now that we have the base of our code set up, We are ready to start rendering. We will learn how to render a sprite, a triangle in 3d space, and
draw text to our screen in the next tutorial "Basic rendering".
Your gameClass Code should look something like this:
Code:
Imports Microsoft.DirectX
Imports Microsoft.DirectX.Direct3D
Imports Microsoft.DirectX.Direct3D.D3DX
Public Class gameClass
Private displaySettings As DisplayMode
Private deviceParameters As PresentParameters
Public device As Device
Public gameExit As Boolean
Public Sub Initialize(ByVal fullScreen As Boolean)
If fullScreen Then
displaySettings.Width = 800
displaySettings.Height = 600
displaySettings.Format = Format.X8R8G8B8
Else
displaySettings = Manager.Adapters.Default.CurrentDisplayMode
End If
deviceParameters = New PresentParameters()
deviceParameters.BackBufferWidth = displaySettings.Width
deviceParameters.BackBufferHeight = displaySettings.Height
deviceParameters.BackBufferFormat = displaySettings.Format
deviceParameters.SwapEffect = SwapEffect.Discard
deviceParameters.PresentationInterval = PresentInterval.Immediate
If fullScreen Then
deviceParameters.Windowed = False
Else
deviceParameters.Windowed = True
End If
LoadContent()
End Sub
Public Sub LoadContent()
device = New Device(Manager.Adapters.Default.Adapter, DeviceType.Hardware, Form1.Handle, CreateFlags.SoftwareVertexProcessing, deviceParameters)
End Sub
Public Sub Draw()
Do While Not gameExit
device.Clear(ClearFlags.Target, Color.CornFlowerBlue, 1, 0)
device.BeginScene()
device.EndScene()
device.Present()
Application.DoEvents()
Loop
thisExit()
End Sub
Public Sub thisExit()
displaySettings = Nothing
deviceParameters = Nothing
device.Dispose()
device = Nothing
Application.Exit()
End Sub
End Class
Next Tutorial - " The Basics - Basic Rendering "
Upcoming Tutorials:
- "Basic Rendering"
- "Loading a Model"
- "Texturing"
- "Cameras and Movement"