Free Web space and hosting from gamerpages.com
Search the Web





The GamerPlusPlus.com Introduction to FreeBASIC



Written By:

Jason Bales

A.K.A

Delta-G













































Table of Contents

Level One 3

PowerUp! 3

Why FreeBASIC? 3

Chapter goals/Level Objectives 4

What is Needed? 4

The Compiler 4

The Text Editor 4

Configuration Part One 5

Configuration Part Two 7

Hello World 8

Level Two 9

What Are Variables? 9

Data Types 9

Integer 10

Long-Integer 10

Single-Precision 10

Double-Precision 10

Strings 10

Variable Declaration 10

DIM 10

Type-Characters 11

Option Explicit 12

Naming Rules and Conventions 12

More on PRINT 13

INPUT 13

CLS 13

Line Numbers 13

Comments 13

Shortcuts 13

Control Structures 14

Data Structures 14

arrays 14

DIM 14

databases 14

Type Conversion 14



Level One

PowerUp!

I am a gamer who doesn't know squat about writing a book and who just lost several hours worth of work (on the first draft of this book) because he forgot to save. I am not a professional writer. I'm not even a professional programmer. Truth be told, I'm just starting to work with FreeBASIC. So why am I writing a whole book about FreeBASIC programming? Because there will always be excuses why we can't or shouldn't do what we want to do. So this book will represent where I am right now. Where I am in my writing career. Where I am as a programmer. If this book proves to be not good enough, I'll just have to do better when I know more.

My website, gamerPlusPlus.com, is dedicated to the idea of gamers making the transition from game players to game makers. I didn't know anything about building a data-driven website when I invested in the domain name and now look... it lives! The words “I don't know how!” should never become a reason for not trying. Now I find that I don't know anything about writing a book or about FreeBASIC programming. I'm at Level One as it were. Chances are pretty good someone else is starting out on Level One as well. Where to go from here? It's time to PowerUp!

Why FreeBASIC?

The B in BASIC stands for “Beginners”. It is meant to be an introductory language. In the beginning BASIC was a great way for non-programmers to start programming. The concepts behind BASIC were the concepts behind all programming. But now we find that the older forms of BASIC are hard to deal with on modern operating systems and it is harder or just plain impossible to use popular programming libraries and APIs with the older BASICs. They no longer take advantage of the resources that computers and programmers have to offer.

Now, along comes FreeBASIC. It has a great deal of compatibility with older forms of BASIC and it's lightning fast. FreeBASIC programmers can learn the basics of programming with all of the old QuickBASIC tutorials that have been amassed on the internet and then ease into more advanced programming from the relative comfort of a BASIC programming environment. FreeBASIC allows programmers to use OpenGL, Allegro, and Windows API. In fact, just about any library or API available to a C programmer is available to FreeBASIC programmers. Many tools like the GDB debugger that are normally used by C and C++ programmers may also be used by FreeBASIC programmers. As if that were not enough, FreeBASIC programs may be cross compiled to multiple platforms including Windows, Linux, DOS, and Xbox! I see no better introductory language available free of charge to everyone.

Chapter goals/Level Objectives

In order to stay on target I will set specific goals at the beginning of each chapter and accomplish those goals in the listed order before moving on to the next chapter. I've decided to call each chapter a Level because I am more familiar with game terms than I am with book terms and it makes me feel more at home.

So, on with the Chapter Goals/Level Objectives:

What is Needed?

Before we can start writing FreeBASIC programs we need two things. The first is the FreeBASIC compiler and the second is a good text editor.

The Compiler

FreeBASIC is new, but it has gathered around it many talented individuals. You can be certain that FreeBASIC will be improved over the next couple of years. Right now there are three main websites where you can find information on FreeBASIC. These are:

http://www.freebasic.net/

http://fbc.sourceforge.net/

http://www.sourceforge.net/projects/fbc

Go to these websites and read about FreeBASIC and then locate and download the compiler package. The compiler package comes with its own installation instructions. The current instructions for Windows installation read:

- Unpack the zip archive to any folder of your preference -- C:\ is a good

idea if you are not going to use an IDE later.



- Run the INSTALL.BAT file to generate the import libraries -- they are

needed to call any function on DLL's and to create the executables.

I recommend downloading the package containing the FreeBASIC IDE, FBide, along with FreeBASIC. Some of the tools in the package can come in handy and you may like working with FBide more than Crimson Editor.

The Text Editor

Text editors are easy to come by, but all are not created equal. You could use Notepad or even Edit, but then you would have to leave the editor to pass the source file to the compiler or call the finished program. There are text editors which can be configured to do these things for you. My personal favorite is Crimson Editor. Crimson Editor is a text editor that has many features that are useful to programmers. The Crimson Editor website is located at:

http://www.crimsoneditor.com/

Go there to learn about Crimson and then download the installer.

Run the installer and then “click through” the installation process without changing any options.

Configuration Part One

Now that FreeBASIC and Crimson are both installed, we have to make them work together. Start Crimson and click on Tools>Preferences. You should see the Preferences window open before you. In the Categories box select User Tools. Click on the first slot of the user tools and begin entering information. When you are finished your first tool should look like Figure 1.1

Figure 1.1

This tool will display our compiler's options. Don't forget to check the capture output box so that we can actually see the output inside Crimson.

NOTE: If you installed to a different directory than I did, you will have to change the path to fbc.exe to whatever it is on your machine. You can navigate by pressing the [...] button.

Figure 1.2

Click on the fourth slot in the user tools and make it look like Figure 1.2

This tool compiles whatever code we are working on. Make sure you check Save before execute and Capture output. Again, make sure the path to fbc.exe entered on the Command line is the correct path for your machine.

Click on the next slot and make it look like Figure 1.3



Figure 1.3

This tool executes whatever program we are working on (don't forget to compile it first). Do NOT select the Capture output or Save before execute options (Don't say you weren't warned).

Now is as good a time as any to mention that COMMAND.COM is the program you use for DOS or command prompt work on older forms of Windows and cmd.exe is the program you need to call on newer forms of Windows (like XP). Search your computer for either of these and place the full path into the command section for this tool.

You are now technically ready to start programming and could skip ahead to the Hello World section. If you decide to do that, make sure you come back at some time and cover the second half of configuration.

Configuration Part Two

I originally planned to give instructions on how to make and install all the FreeBASIC syntax files you will need in order take full advantage of Crimson Editor while you work on your FreeBASIC programming. Then I discovered there are excellent files and installation instructions located at the Crimson Editor website. These will probably be updated at least as frequently as FreeBASIC. Depending on what copy of this book you are reading, you may find a zip file labeled “CE-syntax-FB-v0.15b-aug-27-testing-win32.zip” (or something similar) in the book's chapter one files folder. Unzip the file then read and follow the instructions in the install.txt file. If you just have the pdf version of the book, you should go to the Crimson Editor website and download the FreeBASIC syntax files.

Hello World

The first thing a programmer learns to do in any programming language is to print something to the screen. Tradition demands that we print “Hello World” as our first program. So, if Crimson is not already open before you, go ahead and get it there.

Open a new file and type in this one line:

PRINT “Hello World!”

Save this file as “HelloWorld.bas” wherever you want. I suggest creating a separate folder for your work on this book and save this and all other files from this book in that one place.

Once you save this file with the .bas extension you should see the colors change in the editor. If you do not, you may want to go back and do the section entitled Configuration Part Two. Our one line program is the PRINT command followed by what we want it to print. We will return to the PRINT command in the next chapter, but for now you should get the general idea of what it does.

Now press Ctrl+4 to compile the program. Crimson's output window should respond with something like:

---------- Capture Output ----------

> "C:\Freebasic\fbc.exe" "C:\Freebasic\examples\myStuff\HelloWorld.bas"

> Terminated with exit code 0.



Exit code 0 means that our compiler executed and exited normally. That's a good thing. If the last line in the output window gives you any other exit code it means something went wrong. Check your code and then check your compile tool.

If your output window gives you exit code 0, go ahead and press Ctrl+5. Did you catch that? Our program just executed. What? You didn't get a chance to read it? That's just because the program executed and exited too fast. You could change this by changing your execute tool, but I have a better way. Add a second line to our program. The line should read:

SLEEP

This line tells our processor to sleep until we press a button. You can tell the computer to sleep for a specific length of time. Sleep measures time in milliseconds, so five thousand milliseconds is equal to five seconds. If you add the number 3000 after the SLEEP command your program will wait three seconds. Now press Ctrl+4 to recompile our program and then press Ctrl+5 to run it. You should see a prompt window like in Figure 1.4



Figure 1.4

If you specified a time after your SLEEP command, your program will sleep that long and then exit. If you didn't specify a time after your SLEEP command, your program will stay on screen until you press a button.

You have just used the PRINT and SLEEP commands in your very first compiled FreeBASIC program! Level One objectives complete! Move on to the next level.



Level Two

What Are Variables?

Variables are spaces in your computer's memory. Think of variables as boxes within a warehouse. When you create a variable within a program it is like placing a box in the warehouse. Each box has a label (variable name). The boxes can be of different dimensions (data types). The box that contains a double is about twice as large as a box containing an integer. But now we need to back up and learn what is meant by double and single.

Data Types

A shoe box holds shoes. Hat boxes hold hats. A variable of type double holds doubles. A string variable holds strings.

Variables hold data. Data may be of different types. Each variable only holds one type of data. The five built-in data types that FreeBASIC inherits from Microsoft QuickBASIC are:

Integer

Integers are numbers without decimals from -32768 to 32767. If you can use integers instead of Long-Integers or Doubles in your programs, your program will run faster and your program will take up less of your computer's memory. Integers are BASIC's default data type.

Long-Integer

Long-Integers (Longs) are like the regular integers you just read about except for one thing. They come in a bigger box. Longs can hold numbers between -2147483648 and 2147483647 without any decimals. If you can use a Long instead a double in your program, you probably should.

Single-Precision

Single-Precision (Singles) can hold numbers with decimals. Singles can hold numbers all the way up to seven decimal places. If that's not enough decimal places you will be needing a double.

Double-Precision

Double-Precision (Doubles) are numbers with decimals all the way up to fifteen decimal places. These should be used only when your program requires very accurate math.

Strings

Strings are the only basic data type you don't do math with. Strings hold characters. That means letters, punctuation, and just about anything you type with the keyboard.





Variable Declaration

DIM

The DIM keyword is short for 'Dimension'. This is what we are doing in our computer's memory. We are dimensioning and setting aside a certain amount of memory. If we can go back to our box and warehouse illustration from earlier I can explain this better. When we declare and give dimension to a variable we are placing a box within the warehouse. The dimensions of a Double-Precision box takes up quite a bit more room than an Integer box. The warehouse is your computer's memory, and is limited. So use the best box for each situation.

Here is a list of sample DIM statements:

DIM A AS INTEGER

DIM B AS LONG

DIM C AS SINGLE

DIM D AS DOUBLE

DIM E AS STRING



In this way you can set aside memory for your variables.



Type-Characters

Another way to declare variables as a certain type of data is with Type-Characters. These are special characters that represent each of the data types.

Data-Type

Type-Character

Integer

%

Long-Integer

&

Single-Precision

!

Double-Precision

#

String

$

Table2.1

You can use type-characters to declare a variable of any basic data type anywhere in your program. Let's just use type-characters to declare a variable of each data-type:

A% = 1

B& = 2

C! = 1.1

D# = 2.2

E$ = “This is a string.”

Here we not only place boxes with labels in our warehouse, we also place items in the boxes. As you can see, using type characters can save you some time and space.

I personally don't like using type-characters at all. They tend to confuse me. Instead I use DIM statements with well named variables so I don't get confused about what my variables are and what they are doing in my program.

The default data-type is Single. If you forget your type-character and you wanted a string variable, you are out of luck.

If you decide to use type-characters to declare all your variables than you will definitely need to use Option Explicit. If you forget your type-character, option explicit will tell you where you've messed up.

Option Explicit

Although I'm just beginning my FreeBASIC adventure, I have programmed in other BASICs before. One of my favorite programming languages, Visual Basic 6, uses something called 'option explicit'. I am glad to tell you that FreeBASIC also uses Option Explicit. This is just a line of code that forces you to declare all variables used in your program. You will save yourself many headaches if you just embrace this one line of code. Write this two word line all by itself toward the beginning of your program, before you've declared or used any variables.

I mentioned in the section on type-characters that if you forget a type-character you've just declared a single whether you wanted to or not. This is one reason to use option explicit. Another reason is that people don't always type perfectly. Some of us have nerve damage and others never really learned to type and are hunting and pecking at their keyboard in darkened rooms with glowing monitors. Without option explicit your typos may be accepted as valid variables and you may never find out why your program isn't working.



Naming Rules and Conventions

Here are the rules for naming variables:

Keep your variable names short and suggestive of their purpose in your program.

The UserName$ and uSERnAME$ both refer to the same variable.

Trying to use PRINT or SLEEP as a variable name just won't work.

UserName$ and UserName are two entirely different variables.



Beyond these rules there are conventions. Conventions are things programmers find useful but are not built into the programming language. For instance, I do not use type-characters, but will often use prefixes to help me keep track of variable types. This means that instead of declaring Name$, I will declare strName as a string.

Other, highly personal conventions surround the use of capitals and separation of words. Some programmers may use all lower case letters and separate words with an underscore ( _ ) like so: user_name. Other programmers will capitalize every new word and use the capitals to separate words like so: UserName. Yet other programmers insist that the first letter of a variable name must be lower-case and each new word after that is capitalized like so: userName.

Each programmer has to find what feels and works best for him or her self. Pick your naming conventions and then stick to them.

More on PRINT

In level one we used the PRINT command to print “Hello World” to the screen. That is the simplest way to use the PRINT command. The PRINT command is more versatile than that though.

PRINT commands may contain variables and perform math functions. Take a look at Code2.1. As you can see, all of the commands we have learned so fare are used. The first line of code is Option Explicit. This forces us to declare all variables, which is the next thing we do in our code. We use DIM to declare two variables named A and B. In larger, more practical programs you will want better variable names, but for this example we can make due with A and B. After we declare the variables we give the variables whatever values we want to give them. We give A the value of 2 and B the value of 3. The PRINT command follws. Each item we want printed is separated by a comma. The PRINT command prints everything between double quotes just as it is typed. The values of variables are printed instead of their names. That means 2 is printed instead of A, and 3 is printed instead of B. The PRINT command in code2.1 also contains the mathematical expression (A+B) which has a value of 5. Just as the PRINT command prints the values of our variables instead of the variable names, it also prints the value of mathematical expressions.

Option Explicit



DIM A AS INTEGER

DIM B AS INTEGER



A = 2

B = 3



PRINT "The sum of ", A, " and ", B, " is ", (A+B)



SLEEP 5000

Code2.1



INPUT



CLS



Line Numbers



Comments



Shortcuts

REM vs. '

PRINT vs. ?





Level Three

Control Structures



Data Structures

arrays

DIM

databases

Type Conversion