Friday, April 6, 2007

Tutorial #3 - Basics of DarkBASIC

Naturally, after creating your first Matrix, you'll be eager to check it out. So instead of adding objects into over projects, we'll see some basics of DarkBASIC.

Open DarkBASIC (Start Menu -> Programs -> DarkBASIC -> DarkBASIC v1.13)

Open your project file from File->Open Menu. (The one you created in last chapter. DarkBASIC/Projects/Proj1/Proj1.dba)

You'll see some code, and after that a line stating
`Your Code Starts From Here

If you have noticed, those code has a line #include "LoadMatrix.dba"
(Remember the one we exported using MatrixRebuild?)

Now, let's write some code of our own. Let's say load the matrix we have made. (Proj1.MDF & _Proj1.BMP)

Loading Matrix in your Game

For that, insert a command after the line `Your Code Starts From Here

LoadMatrix("Proj1",1)

Is that it? Yes! Simple?

Now let's Compile the code using F4 and run the code using F5

We can see our matrix, but the program is terminated because we haven't added anything else.

Main Loop

Now you must keep one thing in mind. In general programming, we don't use infinite loop much often, but in Game Programming, all the main tasks are in infinite loop because the program has to check constantly which key is being pressed and act accordingly. To terminate this loop we use F12 (or the Escape Key if the game is complete and we are running it from an EXE).

Another thing. We must refresh the screen after each time this loop has been executed. Why? Suppose we are pressing FWD key, and your player is moving Forward. Now if we don't refresh the screen, your player will leave its trail, means he will be present at both positions, the old one the new one. hence, we will use the following loop as our main (like we use main() in c/c++)

sync on
do
.
.
.
.
sync
loop

sync on -> we are telling computer that we will refresh the screen ourself, and when we need.
do -> starting of main loop
loop -> end of main loop
... -> commands to be executed - the main logic (like what should be done when player presses a key)
sync -> refreshes the screen

Now our code is like following:

LoadMatrix("Proj1",1)
sync on
do
sync
loop

It will load the matrix, and then do nothing. You must use F12 to return to the editor.

Adding Logic

Logic can be added using If .. Then ..
(If Something happens Do something)

Syntax:-

If Then Else

OR

If
.
.
.
Else
.
.
.
EndIf

Conditions

To understand conditions, we must know variables and operators.

Variable Type Example
______________________
Integer i
Real r#
String st$

We need not define variables. We can use them directly.
Ex-
i = 10
r# = 2.12
st$ = "Constantin"

Of course, we must declare them if they are array or global variables.

Dim i(10)
Dim r#(2,4)
Dim st$(5)
etc. for arrays

Dim i(0)
Dim r#(0)
etc. if we want to use them globally in our program.

Operators are
+ Plus
- Minus
= Equal To
<> not Equal To
> Greater Than
>= Greater Than or Equal To
< Less Than
<= Less Than or Equal To
and AND
or OR

So, if we want to check whether the value of i is 10 or not, we simply use this code.

i = 0
if i = 10 then status$ = "True" else status$ = "False"

Later on we can use the value of status$ to determine further logic.

Advanced Logic

In 3D Game, our main logic will be to sort out what to do when a user presses certain key.
There are some in built functions which returns 1 if associated key is being pressed.

upkey()
downkey()
leftkey()
rightkey()
spacekey()
returnkey()
shiftkey()
controlkey()

Thus, we can work out some kind of logic in our main loop
if upkey() = 1 then CODE FOR MOVING CAMERA FORWARD
if downkey() = 1 then CODE FOR MOVING CAMERA REVERSE
etc.

For mouse there is a single command

MouseClick()

Which returns 1 for leftclick & 2 for rightclick

So, now we know how to make matrix, load it in our program. We also know how to implement logic. But to move camera, we will need to know some Camera Basics. which will be covered in the next chapter.

No comments: