DirectX for Visual Basic 5
To get started you'll need the following :
DirectX support for Visual Basic
Thanks to COM, the DIRECTX.TLB file is also usable with DirectX 5 (but don't use new DirectX 5 capabilities). Once downloaded, copy this file in your Windows system directory. Under the "Project" menu, choose the "References" option and select this TLB file (or check the "DirectX" library if it's already in the list). DirectX objects are now available to Visual Basic. Check the "COM" section for more info about the underlaying technology.
- DIRECTX.TLB - v 1.0 - 51 Kb:
- Direct3DRMPickedArray.GetPick : a wrong type parameter is corrected.
- I finally found more convenient to pack the whole DirectX api in this single file. The other TLB files are no more needed (you may want to download older files to get the source code samples. For convenience, I'll provide now samples separately).
- D3D.TLB - v 0.2.1 - 42 Kb :
- Direct3DRM.CreateDeviceFromSurface : the GUID argument is now declared as "Any" (to allow the use of a NULL pointer by using ByVal 0&).
- Direct3DRMFrame.GetChildren : the Direct3DRMFrameArray argument is now declared (was previously omitted).
v 0.2 - 180 Kb This release includes the following improvements :
- DirectDraw is now fully implemented
- Direct3D Retained Mode is now fully implemented : all methods such as Load & Save for Direct3DRMMeshBuilder and other objects previously ommited (such as Direct3DRMAnimation, Direct3DRMAnimationSet, Direct3DRMShadow, array objects...) are now available.
- DirectSound support is added.
- Source code for the StarFld and DSCheck samples.
- Contextual online help - to be improved !
- Please note that, even if Direct3D Immediate objects are available, Direct3D Immediate Mode is not intended to be usable in this version.
DSCheck is a DirectSound sample that allow to play two wave files simultaneously. StarFld is the well known starfield sample using DirectDraw. The Visual Basic code for these samples will be part of the next release. Please note that both samples are using the default DirectX driver (you may have to change the display mode if 640x480 with 256 colors is not available).
v 0.1 - 67.3 Kb The main goal was to find out if using COM objects (such as DirectX objects) was doable. There are some lacks in the implementation but it can already be used to create and experiment with DirectX.
You can also go to the samples and contributions page for screen shots of DirectX stuff written with Visual Basic...
According to the last information about DirectX 5 support for VB users, DirectX 5 should finally provide support for DirectX Media but not for DirectX Foundation (last point according to directx@microsoft.com).
Sorry but I just installed the DirectX 5 docs and SDK on my home computer... I expect to provide support for DirectX 5 in the middle of November
Thanks to COM, the DIRECTX.TLB file is also usable with DirectX 5 (but don't use new DirectX 5 capabilities). Once downloaded, copy this file in your Windows system directory. Under the "Project" menu, choose the "References" option and select this TLB file (or check the "DirectX" library if it's already in the list). DirectX objects are now available to Visual Basic. Check the "COM" section for more info about the underlaying technology.
A very important point is to make sure that you set to nothing every DirectX objects before your app ends. Or your apps will likely crash...
Currently, and as pointed by some of you, most of DirectX functions are seen from VB as subs. This is because these functions returns a HRESULT value, that is a sort of "OLE automation return code". This allow VB to apply standard trapping error code to these "functions" (there are *only* functions in C).
If you take no special actions, errors returned by DirectX "functions" are seen by VB as OLE automation errors and VB displays then automatically a message box that mention the error number.
Let's take the following code - an attempt to use an invalid video mode - as as basis :
Sub Main()
Dim dd As DirectDraw
DirectDrawCreate byval 0&, dd, Nothing
dd.SetCooperativeLevel hWnd, DDSCL_EXCLUSIVE Or DDSCL_FULLSCREEN
dd.SetDisplayMode 350, 210, 8, 0, 0
dd.RestoreDiplayMode
Set dd = Nothing
End Sub
When no special action is taken, a message box that mention a 88860078 automation error will appear. This number is the DDERR_INVALIDMODE.
You can still trap errors yourself by using :
On Error Resume Next
and
If Err<>0 Then
' Error code
End If
If DirectX functions were declared as VB functions, the VB error trapping mechanism wouldn't apply and you would have to test every return value. For this reason, I do not plan to change this and stay with subroutines...
Objects are sometimes created through a special method called QueryInterface. Objects are existing only though a mean to communicate with external world, their interface (the set of properties or methods you can use to handle this object). Sometimes it can be usefull to be able to deal with an object by using several interfaces. Let's take a quick sample. A sound is basically a piece of memory filled with sound data. No matter if you want 3D effects or something else. In this case the data are probably modified before earing but this is always just a piece of memory filled with sound data... So if you want just a simple sound you needs only to load, play and stop the sound. If you want to position this sound in a 3D space, you needs also a method that do so. So this objects supports two interfaces, one for simple sounds and the other for more elaborate effects. That's exactly the existing relationship between the DirectSoundBuffer and the DirectSound3DBuffer. The same underlaying object can be accessed using any (or both) of these interfaces just depending on what you needs to perform. QueryInterface is a COM instruction that ask to an existing object for another interface.
And the good news is that VB just do so for you.
Dim obj1 As New ObjectType1
Dim obj2 As ObjectType2
Set obj2 = obj1
I'm nearly sure that this code finally leads to the following pseudo-code :
obj1.QueryInterface IID_ObjectType2, obj2
That is to "ask" the obj1 object if it supports the interface known as ObjectType2 (uses in fact a numerical value associated with each interface). If so, the obj2 object is valid and allow to access the same object using this other interface...
The following sample allow to obtain a Direct3D object from a DirectDraw object.
Dim DDraw As DirectDraw
Dim D3D As Direct3D
Sub Main()
DirectDrawCreate ByVal 0&, DDraw, Nothing
Set D3D = DDraw
End Main
The 3D sound contribution is also a good example of this. A DirectSound3DBuffer or a DirectSoundListener is created by "querying" the interface from a DirectSoundBuffer object.