Search Tools Links Login

DirectX BASIC Tutorial


I am just trying to learn DirectX 8.0 right now. I am finding it tough even with ome really good tutorials. I thought there might be some other people in the same situation so here is a simple tutorial on the very basics of programming with DirectX 8.0. Hope you find this useful. Please leave feedback so that I can hopefully improve it for the future.

Original Author: Coding Genius

Code

xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:w="urn:schemas-microsoft-com:office:word"
xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882"
xmlns="http://www.w3.org/TR/REC-html40">







Learn DirectX 8




lang=EN-GB link="#3366ff" vlink="#9900ff" style='tab-interval:36.0pt'>


Learn
DirectX 8.0


style='font-size:22.0pt;mso-bidi-font-size:12.0pt;font-family:"Times New Roman";
color:red'> 


Contents:


 


1. Introduction


2. Getting started


3. Declarations


4. Initialisation


5. Rendering


6. Main loop


 


style='font-size:22.0pt;mso-bidi-font-size:24.0pt;font-family:"Times New Roman";
color:purple'>Introduction


 


style='mso-tab-count:1'>??????????? DirectX is a very powerful Tool or
creating amazing graphics and effects. If you like making games or animations
then DirectX is what you are looking for. The newest version, DirectX 8.0, can
support everything from drawing ????? 3D
shapes, right up to creating the lighting and shading effects on them. To give
you some idea of the power behind DirectX consider the fact that many of the
most popular PC games harvest the power and peed of DirectX. Just think, you
too could achieve graphics such as that in Unreal Tournament one day.


name="_Getting_started"> 


Getting started


 


style='mso-tab-count:1'>??????????? Before you even begin bare in mind
that you should have a reasonable knowledge of visual basic. If not then learn
something a bit easier. I will assume that you do have a reasonable knowledge
of Visual Basic. In order to make a DirectX 8.0 project, you must have the
necessary components installed including the Dll files and type libraries.
First open up a new project in visual basic. A standard exe will do perfectly
well. On the “Projects” menu on the menu bar, select “References” near the
bottom. A large list should appear. Look for the “DirectX 8.0 for visual basic
type library”. Once you have found it, check the box net to it and click “OK”.
If you don’t see it then you don’t have DirectX 8.0 Installed. You can download
it here.
(Also worth mentioning that the visual basic DirectX SDK is also downloadable href="http://www.msdn.microsoft.com/downloads/default.asp?URL=/code/topic.asp?URL=/msdn-files/028/000/114/topic.xml">here.
I recommend it.) If you succeed in loading the type library then Visual basic
will now be able to recognise every single Type Structure, Constant and
Variable that exists in DirectX 8.0. You are now ready to create a DirectX 8.0
application. If your lost right now then give it up now and go back to bed!


style='font-size:22.0pt;mso-bidi-font-size:12.0pt;font-family:"Times New Roman";
color:red'> 


Declarations


 


style='mso-tab-count:1'>??????????? Well done you made it this far. Be
warned, it gets hard from here now. To get a direct reference to DirectX you
must create it as an object. This will be your master object so to speak. O.K.
You should have a standard exe in Visual Basic with a reference to DirectX by
now. Name your form “FrmMain”. Copy and paste this code into the declarations
section of the form code window and then I will try to explain some of it.


 


Option
Explicit
‘DirectX
likes all its variables to be declared first. This makes sure we declare them
all first


 


Dimstyle='font-family:"Times New Roman"'> DX As style='font-family:"Times New Roman";color:blue'>Newstyle='font-family:"Times New Roman"'> DirectX8? ‘This
is the main DirectX object. You will notice that when you press the spacebar
after you type “As” that the list of variables and types appears. You can have
a look at the hundreds of DirectX Type structures and types here.
style='font-family:"Times New Roman"'>


Dimstyle='font-family:"Times New Roman"'> D3D As
Direct3D8?
style='font-family:"Times New Roman";color:#339966'>‘This is your Direct 3D
object. A small part of DirectX. This also explains why you don’t need to set
it as a “New” object. It is part of DirectX!


Dimstyle='font-family:"Times New Roman"'> D3DDevice As
Direct3DDevice8
‘This
is the device we will use. It can be hardware or software.
style='font-family:"Times New Roman"'>


 


Dimstyle='font-family:"Times New Roman"'> Running As
Boolean style='font-family:"Times New Roman";color:#339966'>‘This stores whether
DirectX is running or not. You will see why later


 


Privatestyle='font-family:"Times New Roman"'> Declare style='font-family:"Times New Roman";color:blue'>Functionstyle='font-family:"Times New Roman"'> GetTickCount style='font-family:"Times New Roman";color:blue'>Libstyle='font-family:"Times New Roman"'> "kernel32" () style='font-family:"Times New Roman";color:blue'>Asstyle='font-family:"Times New Roman"'> Long
‘If you know windows API then you will know what this line means. It gets our
frame.
Thisstyle='font-family:"Times New Roman";color:#339966'> isn’t required; it just
adds a nice little touch.


 


‘Just
some variables to hold the frame rates. If you don’t want to include a frame
rate then leave these out too


Dimstyle='font-family:"Times New Roman"'> LastTimeCheckFPS style='font-family:"Times New Roman";color:blue'>Asstyle='font-family:"Times New Roman"'> Long


Dimstyle='font-family:"Times New Roman"'> FramesDrawn style='font-family:"Times New Roman";color:blue'>Asstyle='font-family:"Times New Roman"'> Long


Dimstyle='font-family:"Times New Roman"'> FrameRate As
Longstyle='font-family:"Times New Roman"'>


 


Well that is DirectX
all declared now let’s go on…


 


Initialisation


 


The next
step is to Initiate DirectX. All this means is that you will set what
Hardware/Software you want to use and set all the objects. Again copy and paste
the following.


 


style='font-family:"Times New Roman";color:blue'>Publicstyle='font-family:"Times New Roman"'> Function
InitDX()
Asstyle='font-family:"Times New Roman"'> Boolean ‘The initialisation function will return true if successful or
false if an error occurred


style='font-family:"Times New Roman";color:blue'>Onstyle='font-family:"Times New Roman"'> Error style='font-family:"Times New Roman";color:blue'>GoTostyle='font-family:"Times New Roman"'> ErrHandler style='font-family:"Times New Roman";color:green'>‘If an error did occur go to
the error handler


style='font-family:"Times New Roman"'> 


style='font-family:"Times New Roman";color:blue'>Dimstyle='font-family:"Times New Roman"'> DispMode As
D3DDISPLAYMODE?
style='font-family:"Times New Roman";color:green'>‘The display mode we will usestyle='font-family:"Times New Roman"'>


style='font-family:"Times New Roman";color:blue'>Dimstyle='font-family:"Times New Roman"'> D3DWindow As
D3DPRESENT_PARAMETERS
‘The window we will use


style='font-family:"Times New Roman"'> 


style='font-family:"Times New Roman";color:blue'>Setstyle='font-family:"Times New Roman"'> DX = New
DirectX8
‘Set
the DirectX object


style='font-family:"Times New Roman";color:blue'>Setstyle='font-family:"Times New Roman"'> D3D = DX.Direct3Dcreate style='font-family:"Times New Roman";color:green'>‘Set the direct3D objectstyle='font-family:"Times New Roman"'>


style='font-family:"Times New Roman"'> 


D3D.GetAdapterDisplayMode
D3DADAPTER_DEFAULT, DispMode
‘Set the display mode to the default style='font-family:"Times New Roman"'>


style='font-family:"Times New Roman"'> 


style='font-family:"Times New Roman"'>D3DWindow.Windowed = 1 style='font-family:"Times New Roman";color:green'>‘We want it in a window, not
full screen


D3DWindow.SwapEffect
= D3DSWAPEFFECT_COPY_VSYNC
‘We draw all our graphics into a piece of memory, then when
finished, swap the memory location pointers of the memory of what will go to
the screen and where we drew our graphics. This makes sure that our graphics
are all drawn before they go to the screen. Fast and flicker free. This just
sets the swap mode.


D3DWindow.BackBufferFormat
= DispMode.Format
‘The
backbuffer is this area of memory where we draw our graphics. Just set it to
the same format as the display mode


style='font-family:"Times New Roman"'> 


style='font-family:"Times New Roman";color:blue'>Setstyle='font-family:"Times New Roman"'> D3Ddevice =
D3D.CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,FrmMain.hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING, D3DWindow) ‘This line is a bit complicated and will be
explained below


style='font-family:"Times New Roman"'> 


style='font-family:"Times New Roman"'>InitDX = True ‘If we get here we have succeeded so set the function to true


style='font-family:"Times New Roman"'> 


style='font-family:"Times New Roman";color:blue'>Exitstyle='font-family:"Times New Roman"'> Function


style='font-family:"Times New Roman"'> 


style='font-family:"Times New Roman"'>ErrHandler:


style='font-family:"Times New Roman"'>InitDX = False ‘If we end up here, something has gone wrong, set the function to
false


style='font-family:"Times New Roman"'> 


End
Function


 


style='font-family:"Times New Roman"'>The line “Set
D3Ddevice = D3D.CreateDevice (D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, FrmMain.hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING, D3DWindow)” is a bit complex, so I will
explain it in more detail. The backbone of it is:
style='font-size:10.0pt;font-family:"Times New Roman"'>


style='font-size:10.0pt;font-family:"Times New Roman"'> 


style='mso-bidi-font-size:10.0pt;font-family:"Times New Roman"'>“object.CreateDevice(Adapter
As Long, DeviceType As CONST_D3DDEVTYPE, hFocusWindow As Long, BehaviorFlags As
Long, PresentationParameters As D3DPRESENT_PARAMETERS) As Direct3DDevice8”


style='font-size:10.0pt;font-family:"Times New Roman"'> 


style='mso-bidi-font-size:10.0pt;font-family:"Times New Roman"'>Adapterstyle='mso-bidi-font-size:10.0pt;font-family:"Times New Roman"'> : This is
either primary or secondary. D3DADAPTER_DEFAULT always represents the primary
adapter; we'll cover the usage of secondary adapters in a later lesson.
Basically all they do is switch between a primary video card (a standard 2D/3D
card) or a secondary card (such as the 3D-only voodoo 1's and 2's)

DeviceType : This basically allows you to choose between the HAL
(Hardware Accelerator) and the reference rasterizer (A debugging tool). Specify
either D3DDEVTYPE_HAL or D3DDEVTYPE_REF. Bare in mind that if there is no
support for the HAL available this call will fail and you won’t create a
device.

hFocusWindow : This is just a value that DirectX can use to track your
applications status. This value comes from the Form.hWnd value; the value
specified must be a valid, created form with nothing fancy about it.

BehaviorFlags: This just sets how the Direct3D engine processes
textures, vertices, lighting and so on. The best option to use here would be
D3DCREATE_PUREDEVICE - but very few graphics cards will support this (even the
relatively ones), using this option will mean that the 3D card does almost
everything - transformation, shading, lighting, texturing and rasterization. If
you can't use this on you're hardware the next best thing will be
D3DCREATE_HARDWARE_VERTEXPROCESSING - this uses hardware acceleration as much
as possible. Most recent 3D cards will support this. Failing this you could
try: D3DCREATE_MIXED_VERTEXPROCESSING which will use hardware when possible,
but if the hardware can't handle it then the software components will kick in.
Last of all is the plain software rasterizer; should there be no 3D hardware
available this is likely to be the only option. It's almost always very slow:
D3DCREATE_SOFTWARE_VERTEXPROCESSING.

PresentationParameters : This depicts what display mode you want to use.style='font-size:22.0pt;mso-bidi-font-size:10.0pt;font-family:"Times New Roman"'>


 


Rendering


style='font-family:"Times New Roman"'> 


style='font-family:"Times New Roman"'>??????????? You
will call this sub to draw your graphics. This can be 1000s of lines long in
applications with plentiful and complex graphics.


style='font-family:"Times New Roman"'> 


Public
Substyle='font-family:"Times New Roman"'> Render()


On
Errorstyle='font-family:"Times New Roman"'> GoTo ErrHandler style='font-family:"Times New Roman";color:green'>‘In the event of an errorstyle='font-family:"Times New Roman"'>


 


D3DDevice.Clear
0,
ByValstyle='font-family:"Times New Roman"'> 0, D3DCLEAR_TARGET, VbBlue, 1#, style='font-family:"Times New Roman";color:blue'>ByValstyle='font-family:"Times New Roman"'> 0 ‘We must clear away the current scene before we can draw the next.
You can try other colours instead of VbBlue


 


D3DDevice.BeginScene


??????????????????????? style='font-family:"Times New Roman";color:green'>‘All Graphics drawing will go
in here


D3DDevice.EndScene


 


D3DDevice.Present
ByVal 0, ByVal 0, 0, ByVal 0
‘And draw the scene and flip it.


 


Exit
Substyle='font-family:"Times New Roman"'>


 


ErrHandler:


Running
=
False style='font-family:"Times New Roman";color:green'>‘ Uh oh error, break the loop
and get out


 


style='font-family:"Times New Roman";color:blue'>Endstyle='font-family:"Times New Roman"'> Sub


 


Main Loop


style='font-family:"Times New Roman";color:windowtext'> 


style='font-family:"Times New Roman";color:windowtext'>??????????? The main loop goes in the form load section


style='font-family:"Times New Roman";color:windowtext'> 


style='font-family:"Times New Roman";color:blue'>Privatestyle='font-family:"Times New Roman";color:windowtext'> style='font-family:"Times New Roman";color:blue'>Substyle='font-family:"Times New Roman";color:windowtext'> Form_Load()


style='font-family:"Times New Roman";color:windowtext'> 


style='font-family:"Times New Roman";color:windowtext'>Me.Show style='font-family:"Times New Roman";color:green'>‘You must show the form firststyle='font-family:"Times New Roman";color:windowtext'>


style='font-family:"Times New Roman";color:windowtext'>Running = InitDX style='font-family:"Times New Roman";color:green'>‘Initialise DirectX


style='font-family:"Times New Roman";color:windowtext'> 


style='font-family:"Times New Roman";color:blue'>Whilestyle='font-family:"Times New Roman";color:windowtext'> Running style='font-family:"Times New Roman";color:green'>‘Set up a tight loopstyle='font-family:"Times New Roman";color:windowtext'>


style='font-family:"Times New Roman";color:windowtext'>style="mso-spacerun: yes">??? Render ‘Render scene


style='font-family:"Times New Roman";color:windowtext'>style="mso-spacerun: yes">???


style='font-family:"Times New Roman";color:windowtext'>style="mso-spacerun: yes">??? DoEvents ‘Let windows do its stuffstyle='font-family:"Times New Roman";color:windowtext'>


style='font-family:"Times New Roman";color:windowtext'>style="mso-spacerun: yes">???


style='font-family:"Times New Roman";color:green'>‘All for frame rate


style='font-family:"Times New Roman";color:windowtext'>style="mso-spacerun: yes">??? If
GetTickCount - LastTimeCheckFPS >= 1000
Then


style='font-family:"Times New Roman";color:windowtext'>style="mso-spacerun: yes">??????? LastTimeCheckFPS = GetTickCount


style='font-family:"Times New Roman";color:windowtext'>style="mso-spacerun: yes">??????? FrameRate = FramesDrawn


style='font-family:"Times New Roman";color:windowtext'>style="mso-spacerun: yes">??????? FramesDrawn = 0


style='font-family:"Times New Roman";color:windowtext'>style="mso-spacerun: yes">??????? Me.Caption = "DirectX-Graphics:
Lesson 01?? {" & FrameRate
& "fps}"


style='font-family:"Times New Roman";color:windowtext'>style="mso-spacerun: yes">??? End
Ifstyle='font-family:"Times New Roman";color:windowtext'>


style='font-family:"Times New Roman";color:windowtext'>style="mso-spacerun: yes">???


style='font-family:"Times New Roman";color:windowtext'>style="mso-spacerun: yes">??? FramesDrawn = FramesDrawn + 1


style='font-family:"Times New Roman";color:windowtext'> 


style='font-family:"Times New Roman";color:blue'>Wendstyle='font-family:"Times New Roman";color:green'> ‘End loopstyle='font-family:"Times New Roman";color:windowtext'>


style='font-family:"Times New Roman";color:windowtext'> 


style='font-family:"Times New Roman";color:green'>‘ Release variables


style='font-family:"Times New Roman";color:blue'>Setstyle='font-family:"Times New Roman";color:windowtext'> DX = style='font-family:"Times New Roman";color:blue'>Nothingstyle='font-family:"Times New Roman";color:windowtext'>


style='font-family:"Times New Roman";color:blue'>Setstyle='font-family:"Times New Roman";color:windowtext'> D3D = style='font-family:"Times New Roman";color:blue'>Nothingstyle='font-family:"Times New Roman";color:green'>


style='font-family:"Times New Roman";color:blue'>Setstyle='font-family:"Times New Roman";color:windowtext'> D3DDevice = style='font-family:"Times New Roman";color:blue'>Nothingstyle='font-family:"Times New Roman";color:windowtext'>


style='font-family:"Times New Roman";color:windowtext'>Unload Me


style='font-family:"Times New Roman";color:blue'>Endstyle='font-family:"Times New Roman";color:windowtext'>


style='font-family:"Times New Roman";color:windowtext'> 


style='font-family:"Times New Roman";color:blue'>Endstyle='font-family:"Times New Roman";color:windowtext'> style='font-family:"Times New Roman";color:blue'>substyle='font-size:22.0pt;mso-bidi-font-size:12.0pt;font-family:"Times New Roman";
color:windowtext'>


style='font-family:"Times New Roman";color:green'>‘To exit


style='font-family:"Times New Roman";color:blue'>Privatestyle='font-family:"Times New Roman";color:windowtext'> style='font-family:"Times New Roman";color:blue'>Substyle='font-family:"Times New Roman";color:windowtext'> Form_KeyPress (KeyAscii
Asstyle='font-family:"Times New Roman";color:windowtext'> style='font-family:"Times New Roman";color:blue'>Integerstyle='font-family:"Times New Roman";color:windowtext'>)


style='font-family:"Times New Roman";color:windowtext'>style="mso-spacerun: yes">??? Running = False


style='font-family:"Times New Roman";color:blue'>Endstyle='font-family:"Times New Roman";color:windowtext'> style='font-family:"Times New Roman";color:blue'>Substyle='font-family:"Times New Roman";color:windowtext'>


style='font-family:"Times New Roman";color:windowtext'>???????????


style='font-family:"Times New Roman";color:windowtext'>And that’s it. I know
the graphics are really not anything at all to be honest. I will hopefully be
including a future tutorial on how to set up simple graphics. This is just
designed to show you how to set up DirectX. You will always use this type of set-up
no matter how complex an application you make. Leave feedback so that I know
what to change and what to keep.


style='font-size:22.0pt;mso-bidi-font-size:12.0pt;font-family:"Times New Roman";
color:navy'> 


 





About this post

Posted: 2002-06-01
By: ArchiveBot
Viewed: 96 times

Categories

Visual Basic 6

Attachments

DirectX BA211576142001.zip
Posted: 9/3/2020 3:45:00 PM
Size: 2,624 bytes


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.