Home PageDownloadBeginner's ZoneAmateur's ZoneExpert's ZoneTips PageProgramming LinksE-Mail Me

Tips

I've been programming in GFA Basic for 6 years. In that time you learn a few tricks to speed up programs and other general good programming techniques. On this page I will put a list of quick useful tips to make your programming a bit easier.


Stopping GFA's Paint Interference

When Windows sends a message to your window thankfully the first place GFA sends it is any callback routine you've told it to. Thankfully because when it's finished doing that some messages come in for the GFA Basic 'annoy the hell out of you treatment'. The biggest example of this is WM_PAINT. If you resize your window you will see that, most of the time, the contents will be destroyed. A box, usually white, although sometimes the last color you used in a call to CLS (not always), will be put over the contents. This is not Windows doing, it is GFA Basic and thankfully it's really easy to stop, so you can have full control over what's painted in your window. You have to stop GFA Basic knowing you ever had a message, to do this you first need a callback routine for WM_PAINT:

OPENW #1
CB WIN(1),WM_PAINT,PaintProc
CB WIN(1),WM_NCPAINT,PaintProc

'Put Program here

Then, for the routine you must do this:

PROCEDURE PaintProc(Win&,Mess&,wPar&,lPar%)
  KILLEVENT
  RETVAL DefWindowProc(Win&,Mess&,wPar&,lPar%)
RETURN

In the routine you can put any drawing you want to do and that's all that will be done. The KILLEVENT stops GFA doing most things but to make sure you have to let windows know the callback has processed the messsage. The RETVAL line could just be RETVAL 0, in fact that is what the procedure will always return, but you will find that nothing will be drawn, the title bar and the border will just delete itself. DefWindowProc is an API command that calls the default function to draw the border and title bar of the window. It would probably be less confusing if it was:

~DefWindowProc(win&,mess&,wparam&,lparam%)
RETVAL 0

But the first code will work with ANY Windows message (some of them return important values).


Using Words and Fixed-Point numbers

Use DEFWRD or DEFLNG at the top of your program so you don't have to type & or % at the end of all your variables. These variables are quicker, easier and all that is needed for most tasks. You should avoid using the default variable (floats), they are a lot slower and not required. In fact if you want to use fractions often you can use fixed-point numbers. Fixed-point numbers are words or longs that are divided by a number to give the impression of floating points. You usually only need a few digits after the point. If you download the Bouncing Ball of Joy you may be interested to know that it doesn't use a single floating point variable. The position of the ball is stored in longs which are divided by 64 to give the screen position i.e wx% and wy%. The ball's vectors are also stored in two longs which are divided by 64. This gives the impression of smooth movement without resorting to slow floating points. You should try to use a power of 2 to divide by, it is marginally quicker, also use DIV as it is integer division.


Think Assembler

When it all comes down to it every programming language just ends up creating machine code. Each instruction in machine code is just a number and then numbers after it as the parameters. Try to make your programs as easy for GFA Basic to convert to assembler as possible. Here's an example instead of:

a=(((15*b)+22)/13)+19

Use

a=b
MUL a,15
ADD a,22
DIV a,13
ADD a,19

This will create much faster code with the added benifit of it being easier to understand. You could also use the C style operators (*= += /= -=) to allow you to put it all on one line a=b, a*=15, a+=22, a/=13, a+=19. You can apply this to many commands IF statements being one of the main ones. Instead of this AND that AND this, on and on, put each AND on a separate line. This way as soon as GFA Basic finds a false one, it doesn't have to go through all the others, for example:

IF a=4 AND b=6 AND c=10

change this to

IF a=4
  IF b=6
    IF c=10

and put the least likely one first.


Use API commands

I've mentioned GFA's interference before and it does happen in other areas. Fortunately GFA gives you direct access to the commands of Windows, the API. This means you can write a program using hardly any GFA Basic commands, only structure and variable commands are required. You can use any of the 16-Bit API either immediately or by declaring them from a DLL. So instead of PBOX, use ~Rectangle, instead of BITBLT use ~BitBlt, instead of TEXT, use ExtTextOut. This will make your programs run quicker and will give you full control, which sometimes GFA's 'wrapping' of commands to make them easier, can take away.


Try to become Multi-lingual

GFA Basic is great. It's a very easy to learn but still has the power of any other 16-Bit language. Unfortunatley, barring a miracle, GFA Basic will not be used by business or software developers. This is fine if programming's your hobby and you have no intention of ever using it to get a job, but it's very possible you will learn enough about Windows and computers using GFA Basic to be able to get a job in IT. The problem is the programming jobs. The industry standard language is C++ and if you don't know that you are going to struggle. GFA have been very kind though. They have put in loads of C style stuff into GFA Basic Windows. Here's a few things you can do in GFA Basic to get you ready for C:


Use Dialog Boxes

Windows seems to work much better with GFA dialog boxes than it does with GFA windows. They're also easier to control and a cinch to use as windows. You can also use DS_MODALFRAME to make lovely Windows 95 borders.


Well, I hope this section has come in handy to someone. If you have any useful GFA Basic Windows tips that you'd like to share with people, e-mail them to me and I'll put them on this page with your name in lights (well, in blue) next to them.

Home PageDownloadBeginner's ZoneAmateur's ZoneExpert's ZoneTips PageProgramming LinksE-Mail Me