Please support our sponsor:
Welcome To DirectX 4 VB! Multimedia Visual Basic at its best...




DirectX7: DirectXEvent
By: Jack Hoxley
Written: June 2000


DirectXEvent is used only in DirectMusic, DirectSound, DirectInput and DirectPlay. It allows you (the programmer) to recieve information from DirectX. A good example of this is in DirectSound. When you can use the DirectXEvent to get DirectX to tell you when the sound buffer has got to a certain point; and then you can act on it.

You could think of it like this; almost your entire program is you telling directX to do this, to do that - you're talking to it; but apart from any return values it's not talking to you. Using the directXEvent you can get it to talk back to you without you directly asking it to; although you're setting it up to talk to you, it automatically talks to you after this point - regardless of whether you just asked it to.

DirectXEvent can only be used in a form object; not in modules or classes. It uses this code:

'In the declarations section:
Implements DirectXEvent

'This part will automatically appear:
Private Sub DirectXEvent_DXCallBack(ByVal eventid as Long)

End Sub

'You can then modify it like so:
Private Sub DirectXEvent_DXCallBack(ByVal eventid as Long)
select case eventid
case 0 'Event 0 has happened
'This code is executed when event 0 happens

case 1 'Event 1 has happened
'Event 1 code is here

case 2 'Event 2 code is here
'Do stuff for Event 2

end select
End Sub

'Events Must be created before they can be used;
Dim EventHandle as long
EventHandle = dx.CreateEvent(Me) 'Creates a general event
EventHandle = dx.CreateEvent(0) 'Creates a specific event - you can then use
'the select case method above.


'Events MUST be explicitely destroyed before you're application terminates; otherwise
'you'll get 'unexpected' results - ie. It'll crash :)

If EventHandle <> 0 Then 'If the EventHandle value is 0 there is no event there.
dx.DestroyEvent EventHandle 'Destroy the event represented by the value in the variable.
end if
'NB: destroying events can take several seconds; bare this in mind when you're application
'takes ten minutes to shut down :)

DirectSound
-Getting the current playback position in a normal buffer; or detecting when the end of the sound has been reached.

'The variables that we need,
Public EventID(1) As Long
Public EVNT(1) As DSBPOSITIONNOTIFY

'Create two Events
EventID(0) = DX.CreateEvent(Me)
EventID(1) = DX.CreateEvent(Me)

'Set up the first event.
EVNT(0).hEventNotify = EventID(0) 'This event has this ID (0)
EVNT(0).lOffset = 0 'Time from when the start of the buffer
'to when it is triggered. 0 would therefore mean it is triggered at the start.


'Set up the second event.
EVNT(1).hEventNotify = EventID(1) 'This event is represented by event 1
EVNT(1).lOffset = DSBPN_OFFSETSTOP 'A simple constant. This sets the event
'To be raised when the buffer has finished playback (a natural finish) or it was halted (by you - buffer.Stop)


'The events have been created; but we now need them to be linked to the sound buffer.
Buffer.SetNotificationPositions 2, EVNT() 'the 2 specifies that there are 2 entries in the EVNT() member

'Now we can put code behind these events.
Private Sub DirectXEvent_DXCallback(ByVal EventID As Long)

'Check which event was triggered...
Select Case EventID
case EVNT(0).hEventNotify
msgbox "Event 0 occured: Sound should be playing"
Case EVNT(1).hEventNotify
msgbox "Event 1 occured: Sound should have just finished"
End Select

End Sub

'REMEMBER TO DESTROY THE EVENTS - see above.

DirectMusic and DirectInput
-When the music being played gets to a certain point.
-Change in state of the device: Joystick moved, mouse moved, buttons pressed etc...

Both DirectMusic and DirectInput can be handled in a similiar way:

dim EventHandle as long
Implements DirectXEvent

EventHandle = Dx.CreateEvent(me)

DiDev.SetEventNotification EventHandle 'For directinput
'-Or-

Perf.SetEventNotificationHandle EventHandle
'For DirectMusic
'Because there is no special way of checking what the status of these two are; you can just use
'the fact that when the Event is triggered something has changed. In the case of DirectInput; when
'the event is triggered check the status of whichever device you are using - as it has just changed
'With DirectMusic; check the position of the performance.

DirectPlay
- When a lobby recieves a message.

dim EventHandle as long
Implements DirectXEvent

EventHandle=Dx.CreateEvent(me)

Lobby.SetLobbyMessageEvent(0, EventHandle)
'The first argument is the Application number. The number is obtained from the "runApplication" method.
'If you are only comunicating with one lobby client; this number must be 0.

'The event is raised when the lobby hosting the event recieves a message. The EventHandle is used to create a
'standard event to host this.
DirectX 4 VB 2000 Jack Hoxley. All rights reserved.
Reproduction of this site and it's contents, in whole or in part, is prohibited,
except where explicitly stated otherwise.
Design by Mateo
Contact Webmaster
This site is hosted by Exhedra Solutions, Inc., the parent company of RentACoder.com and PlanetSourceCode.com