Anyone like me, that has come from a VB6/VB.NET/C# background has probably found themselves sorely missing events in DBPro. Those that haven't had experience with them, just don't know what they are missing. Well, no longer... events are now possible in DBPro.
Here is how you can get them to work...
First of all, you will need two plugins: Matrix1Util_20.dll and DBPEventManager.dll.
The first one can be found at the bottom of the first post on this thread (you may also need the VC++ redistributables located at the same place):
http://forum.thegamecreators.com/?m=forum_view&t=85209&b=18
The second plugin can be found attached to the first post at this thread:
http://forum.thegamecreators.com/?m=forum_view&t=123804&b=18
To install the plugins, just put the archive in your DBPro root folder and extract them. Download the VC++ redistributables and run the installer if you need those as well.
Now for the fun part... here is a simple demo program that uses events to handle a click of the left mouse button to change the color of a cube. You should already know how to do basic coding in DBPro including setting up a camera, making an object, and setting up a main loop. This is about as bare bones as it gets:
` Simple demo... just click the cube and it changes color.
autocam off
Position Camera 0,0,-200
Make Object Cube 1, 50
Color Object 1, rgb(255,0,0)
`enable polling for mouse events
Mouse Events On
`declare handlers for two events:
`pressing of the left mouse button and releasing of the left mouse button.
Declare DBP Event Handler "OnMouseLeftDown", fp_onmouseleftdown()
Declare DBP Event Handler "OnMouseLeftUp", fp_onmouseleftup()
global ObjClicked as Boolean
ObjClicked = 0
Do
`Handle any core events that have been raised.
DBP Do Core Events
Text 0,0, "FPS = " + str$(Screen FPS())
loop
`*****************************************************
`Our first handler function. The first function just gets
`a pointer to the second function.
function fp_onmouseleftdown()
value = Get PTR to Next Function()
endfunction value
`if the left mouse button pressed while we are over our cube, change the color.
function OnMouseLeftDown()
obj as integer
obj = Pick Object (MouseX(), MouseY(), 1, 1)
if obj = 1
Color Object 1, rgb(0,255,0)
ObjClicked = 1
endif
endfunction
`******************************************************
`******************************************************
`This is our second handler function.
function fp_onmouseleftup()
value = Get PTR to Next Function()
endfunction value
`If the mouse button is release, change the color back to red.
function OnMouseLeftUp()
if ObjClicked = 1
Color Object 1, rgb(255,0,0)
endif
endfunction
`******************************************************
Now lets examine this code and see what it was doing. I will skip over the common DBPro commands as there are other tutorials to teach these.
The first thing, after setting up the camera and cube is this line:
What that does is pretty self explanitory. It tells DBPro that we are going to be using the mouse and want the event manager to tell us when something is done with the mouse.
Now that we have done that we need to create a function that will handle the mouse clicks. First we will make a function that will be triggered when the left mouse button has been pressed down. We will make this function change the color of the cube from red, to green. I won't bother repeating the whole function here, I will just show you the relevant parts:
function fp_onmouseleftdown()
value = Get PTR to Next Function()
endfunction value
function OnMouseLeftDown()
endfunction
You will notice that there are two functions that are similarly named and are grouped together. You should treat these as one big function. The first one is used to get a pointer to the second function using the "Get PTR to Next Function()" command. A function pointer is just a memory address where our function is located. This is used in a second when we add our handler declaration.
Next we go back to the top of our code and add this line:
Declare DBP Event Handler "OnMouseLeftDown", fp_onmouseleftdown()
What this does is give the name of the event that we want to handle (ie. "OnMouseLeftDown"), and gives the address of our function we just made. Now the event manager knows what to do if it we press the left mouse button.
Now that just handles when the mouse button is pressed down, but we want the cube to turn back to red when we release the button. For that we need another event handler. At the bottom of the code we will add another set of functions:
function fp_onmouseleftup()
value = Get PTR to Next Function()
endfunction value
function OnMouseLeftUp()
endfunction
This should look very familiar. This is the same way we set up our OnMouseLeftDown function. So the next set should be just as familiar. At the top portion of our code add this:
Declare DBP Event Handler "OnMouseLeftUp", fp_onmouseleftup()
We have given it a new event name (ie. "OnMouseLeftUp") this time and are passing it the pointer to our new function.
There is only one more necessary step to get our events to work... we need to tell DBPro when to do then. Consequently the next command does just that. In the main loop of our program add this line:
When this line is executed, DBPro will look for mouse activity and look to see if a handler function has been declared for it. If one has, it will run that function.
At the second link listed above is a more in-depth demo of what the Event Manager plugin can do, as well as a full list of supported core events. It can handle mouse, keyboard, and joystick events as well as allow you to make your own events.
Enjoy.
Design documents?!? What design documents??? I thought we were just going to wing it!!!