Luke's BlitzMax Tutorial

Post all your tuts or request for tuts here.
Post Reply
User avatar
Luke111
Posts: 84
Joined: Fri Aug 17, 2012 7:02 am

Luke's BlitzMax Tutorial

Post by Luke111 »

Hi everyone!
I figured I would write an in-depth tutorial on the basic to advanced topics of BlitzMax!
Here you all go!
(Please tell me if I missed anything, or you need help with something.)

---
Part 1: What is BlitzMax?
---
Some of you may be wondering, What is BlitzMax?
I will try to answer that question in this part.

BlitzMax is a Compiled (creates an EXE) programming language created by Mark Sibly (from Blitz Research, Ltd. http://www.blitzbasic.co.nz).
BlitzMax is different to Blitz3D/Plus in many, many ways; but I will go over these topics as if the audience has no idea what those two are,
or has never heard of BlitzMax before.

BlitzMax is an Object-Oriented Basic Dialect.
You can do a lot of really cool stuff with it, that you may not be able to do with, say,
Commodore BASIC. Pretty much, you can do with BlitzMax what you can do with (almost) any other programming language
out there. I have seen compilers made in it, I have seen games made in it (which is what it was designed for), I have seen pretty much everything but an operating
system made in BlitzMax.

---
Part 2: What is the difference between a compiled and an interpreted language?
---
The easiest way to put it is in an interpreted language, code can not be natively made into an executable, but in a compiled language, it can.

Interpreted code is ran through the interpreter, ie. php.exe, python.exe, etc. and that interpreter runs the code right then.

Compiled code, on the other hand, is ran through an interpreter, ie. bmk.exe, gcc.exe, fbc.exe, etc. The compiler creates assembly language code that is
then ran through an assembler (fasm, masm, nasm) and a linker (ld) to create an executable (.exe).

So, as you can see, compiled languages have a lot going on in the background; it is worth it, though! Compiled programs run faster than code being put through an
interpreter. It also allows you to distribute your executable without giving away source code.

There is still ways to compile interpreted languages, but they are normally not officially supported. I think there was/is a PHP2EXE converter somewhere around,
but that name is probably incorrect.

---
Part 3: Obtaining Prerequisites for this tutorial
---
You will need some stuff to get you through this tutorial. Here is what you will need:

*BlitzMax http://www.blitzmax.com/Products/_index_.php
*minib3d http://www.blitzmax.com/Community/posts.php?topic=96521
*MinGW http://experimental.thegamerboard.com/Files/MinGW.rar

The rest I will walk you through how to make. This includes the programs, as well as the framework you can use throughout this tutorial.

---
Part 4a: Getting Started; installing BlitzMax
---
BlitzMax can be obtained by, after buying it from BlitzMax.com, logging in to the website and clicking on Account->Product Updates->BlitzMax (the latest version).

If you don't know how to install a program, this tutorial is going to be too hard for you right now, so you should go learn more about the computer and come back
after learning how to install programs, and, if you haven't already, make powerpoints and word documents.

TIP (Windows 7 & Vista users): Right click on the BlitzMax icon once it is installed, and go to properties. Then, go to compatibility, and enable "Run this program as
administrator". This will probably save you some hassle later on.

---
Part 4b: Getting Started; Installing minib3d
---
once BlitzMax is installed, make sure you are logged in to an administrator account. Next, extract the archive downloaded from BlitzMax.com or google code, which
contains the minib3d code. Then, copy the folder labeled sidesign.mod to C:\BlitzMax\mod or wherever you installed BlitzMax to.

---
Part 4c: Getting Started; Installing MinGW (Windows Vista/7 ONLY, If you are using XP, please tell me how to do this on that OS, so I can update the tutorial.)
---
1. Extract the archive MinGW to C:\
2. Go to Start then type in "System Environment Variables"
3. Click on the "Edit System Environment Variables" link in the options that pop up, then click on the "Environment Variables" button.
4. Find in the Second Listbox the variable PATH (upper or lowercase, I forget) and click Edit. In the box that pops up, append (add to the end) of the textbox
with all the paths in it ";C:\MinGW\bin" without the quotation marks. The semicolon is needed, otherwise it won't work.
5. Click OK, and add a user environment variable to the top listbox named "GCC_EXEC_PREFIX" and the value is "C:\MinGW".
6. Add another environment variable to the same list, named "MinGW" and the value is "C:\MinGW" too.

---
Part 4d: Getting Started; Building Modules
---
Open BlitzMax, and press CTRL+D (Control and D) to build the modules.
Go to the Program menu, and click on "Rebuild All Documentation" to rebuild the docs for the minib3d, and everything else.

---
Part 5: Int Main (Or Not!)
---
This part is a quick rundown of the BlitzMax syntax for users of C/C++/PHP who have no experience in BlitzMax. It is designed to make the transition easy.

There are no semicolons in BlitzMax! Well, there are, but they are just used for putting multiple commands on the same line.

Code: Select all

Write "Hello";Write " World"
Conditional Statements Look Different!
Use the following chart to make the transition:

Code: Select all

!=    <>
==    =
!     Not
Some other operators to make note of:

Code: Select all

+=    :+
-=    :-
/=    :/
*=    :*
<<    Shl
>>    Shr
The following operators are still valid in BlitzMax:

Code: Select all

<
>
<=
>=
&
|
*
-
+
/
=
A Class is refered to as a Type.

Functions can NOT be overloaded. This also applies to type methods, and static type methods.
The only way to make overloading work is to use inheritance in the types.

There are no Curly Brackets in BlitzMax! Well, there are, but they are never actually used in most applications, and this tutorial will (probably) not
outline how to use them. They are used for metadata, and I have only read about it once in a forum post on the Blitz Website.
The replacement is the use of the End X Command.

Code: Select all

If X = 1 Then
	Do Something
End If
The rest will be explained as the tutorial progresses.

---
Part 6: Data Types
---
I would explain this, but it is better off if you start reading some of the parts of the documentation that are actually usefully documented.
Open the Official BlitzMax IDE, and go to Language->Data Types. There is an attempt to document the data types there. It is very important that you read through the data
types to understand why I use certain ones where in the code. This is mainly for the Integer Data Types (Byte, Short, Int, Long).

---
Part 7a: Hello World!
---
Here comes the fun part! You will write your first program in this section!

First off, let's start with the standard Hello World program:

Code: Select all

Print "Hello World!"
Make a new document, put this in, save it, and press F5 (or the Rocket Blasting Off). Look! It says Hello World!

Now, this is great, but what if we want our users to be able to run this. They need the BlitzMax IDE, right? Wrong!

Go to your folder that you saved the code in, and you will see the exe named after the code file. Run it!

Uh Oh, it opens and quickly disappears! This is our first bug! (Well, maybe not a bug, but an undesired effect.)

Add this in to the bottom of the code:

Code: Select all

'Delay for 5000 Milliseconds, or 5 Seconds
Delay 5000
Now press that rocket again, and you will see the same thing, almost. It doesn't say Process Finished until 5 seconds after it says Hello World.

Go run that EXE again, and see what comes up!

Modify the program a bit. Change the 5000 to something else, and maybe make Hello World! something else too.

Go get a beer, or in my case, a soda, and relax. You just made your first program!

---
Part 7b. Code Analysis
---
You may be wondering why something didn't just come out of your printer.
Print basically means show on the screen. So, Print has nothing to do with the printer, and I agree, it is named wrong.
It's name has to do with BlitzMax's roots in BASIC.

---
Part 8a: Variables & Input
---
Okay, so this works fine, but what's a program without user interaction, or input?
To be honest, it isn't a good one!

Let's make our program ask for the user's name, store it in a variable, and say hello to our user.

Here's the code!

Code: Select all

'print our cool message first!
Print "Welcome to my Second Program!"
'then, ask the user for their name, storing it in a variable named "name"
Local name:String = Input("What is your name? ")
'finally, say hello to the user!
Print "Hello, " + name + "!"
'wait so the window doesn't close
Delay 5000
---
Part 8b: Code Analysis
---
Print is the same kind of thing in the first line, but in the second Print statement, we used the + operator to add stings together.
A string is pretty much anything that can be shown on the screen. Well, it is actually more than that, but all you need to know is that it is only stuff that
can be shown on the screen, or typed from the keyboard.

Input first displayed a message, then it Waited for the user to type in something and press enter.
It stored the user's input in a string variable named "name". We then proceeded to print the user's name, and Delay for 5000 milliseconds, or 5 seconds
to stop the window from closing immediately.

---
To Come: Part 9a: Guessing Game
---
I don't believe in platform-specific development, but I also don't believe in Apple.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Luke's BlitzMax Tutorial

Post by Jackolantern »

Good stuff! :)

I actually thought BlitzMax was more along the lines of a Dark Basic Pro competitor, in that it was specifically tooled for game development (you can make DBPro do other things, but there is little sense in doing so, seeing as there is no GUI library, and I think it must run full-screen which does not jive with most modern OS). I didn't know it was really more of a general-purpose programming language.

I realize this is getting way ahead of where you are in the tutorial, but does it have other general-purpose features, like multithreading, TCP/IP support, database access support (could drivers for DBs be written, or does it maybe have a generic connector?), etc.
The indelible lord of tl;dr
User avatar
Luke111
Posts: 84
Joined: Fri Aug 17, 2012 7:02 am

Re: Luke's BlitzMax Tutorial

Post by Luke111 »

Hi Jackolantern. It has all of the things you mentioned.

There is a SQLite wrapper, MySQL too.
It is cross platform out of the box, and it has cross-platform commands for building native GUI applications on Linux, OSX, and Windows.

It has built-in multithreading (POSIX threads on the OSX & Linux, I believe, and I haven't really looked into how it does it in Windows, probably some WinAPI commands).

It has built in socket commands for TCP/UDP, also cross platform, using berkely sockets (I think) and winsock (I think).

There is a wrapper for pretty much everything DLL or open source, and if it isn't available, you can make it work.
Like I said, everything but OSdeving, as that requires some pretty special stuff (your own syscalls, or even no syscalls, and a bunch of other things).

Here, check this out: http://brucey.net/programming/blitz/index.php

That isn't all of his stuff either. He has a bunch on his googlecode.
I don't believe in platform-specific development, but I also don't believe in Apple.
dbest
Posts: 109
Joined: Sun Nov 20, 2011 12:24 pm

Re: Luke's BlitzMax Tutorial

Post by dbest »

Used Blitzmax, but didnt produce anything significant with it. I just buy engines and do nothing with them :(
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Luke's BlitzMax Tutorial

Post by Jackolantern »

Is BlitzMax compiled (I read the part about compiled vs. interpreted above and saw that minGW was required)? If it is, I wonder how it is cross-platform. Do you have to make separate Windows, Linux and OSX versions of the game?
The indelible lord of tl;dr
User avatar
Luke111
Posts: 84
Joined: Fri Aug 17, 2012 7:02 am

Re: Luke's BlitzMax Tutorial

Post by Luke111 »

Yep, it is compiled. There is a preprocessor that allows you to check what OS you are compiling on. You can't cross-compile at the moment.
You use the preprocessor to include code that is platform specific, and write generic code to interface with that other code, making cross-platform code.
I don't believe in platform-specific development, but I also don't believe in Apple.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Luke's BlitzMax Tutorial

Post by Jackolantern »

Very cool! Thanks for answering the questions :)
The indelible lord of tl;dr
Post Reply

Return to “Tutorials”