Compiling a Big QB program in FB

FreeBASIC

Compiling a Big QB program in FB
 
Let's try to compile a big (4000+ lines) graphical QB program in FreeBasic, to see how compatible FB is with QB.
As an example I will use Jark's TCRay a great raytracer with quadric, cubic and quadratic shapes, perlin noise programmed in 2004. You can get TCRay.zip from
http://www.mandelbrot-dazibao.com/Programs/Programs.htm

Notice TCRay is a QB4.5 interpreted program, Jark never had the patience to compile his work,
he just tested it interpreted and went on adding features.


The program is made of has 3 files:

TcRay21C.bas - The Main file.
TcLib17L.bas - The SVGA graphics library.
Tclib17.bi - The include file for the library.

Porting TCLib17.bas

In TCLib17.bas
It is a "pure QB" SVGA library. Most of its functions are obsoleted by FB as they are implemented as QB-style keywords. I had my share in developing that lib so you can trust me for this part ;)

Comment out the contents of the ClearScreen sub and add this
CLS

Comment out the contents of the Point24 sub and add this:
a& = Point(x%,y%)
red% = a& Shr 16
green% = (a& Shr 8) And 255
blue% = a& And 255


Comment out the contents of the Pset24 sub and add this:
PSet (x%,y%), red% Shl 16 Or green Shl 8 Or blue

Comment out the contents of the Screenshot sub and add this:
BSave Name$+".bmp"

Comment out the contents of the SelectVga sub, we will work with a fixed size most pc's will support. Comment out the contents of the SetText sub, we are able to output text in HiRes graphics so mode switching is not required.

Comment out the contents of the SetVGA Sub excluding the Powers of two calculation at the end and add these four lines:
Screen 20,32 '1024x768, 32 bits
scrheight=768
scrwidth=1024
Fullscreen



In TCRay17.bas
Add a SetSVGA as the first line in the Menu sub (we are not switching modes so mode must be set before outputting text),

Compiling
Ok, stop trusting me, now you can start trying to compile. You'll receive some errors.

Compile with: fbc -s gui -w 1 -lang qb TcRay21C.bas TcLib17L.bas

I warn you all changes required except two come from a couple of (wise) limitations in the FB syntax:
  • A variable name can't be a keyword plus a type suffix
  • A simple variable can't have the same name as an array

In TCLib17.bi
ERROR: Duplicated definition, found 'RGB' (RGB is a keyword in FB)
Add:
#undefine RGB
Before the line giving the error.

ERROR: Duplicated definition, found 'ScreenRes' (ScreenRes is a keyword in FB)
Add:
#undefine ScreenRes
Before the line giving the error.

ERROR: Duplicated definition, found 'Name' (Name is a keyword in QB)
Add
#undefine Name
Before the line giving the error.

In TCRay17.bas
ERROR: Duplicated definition, found 'Acos' (Acos is a keyword in QB)
Add
#undefine Acos
Before the line giving the error.

ERROR: Argument count mismatch Clear (Clear is not required in FB, the keyword has been reused (not a clever decision?))
'comment out CLEAR

ERROR: Illegal specification, at parameter 2 (Type) of Init.Cubic() (Type is a keyword in QB)
We can undefine it so search and replace type$ to _type$

ERROR: Expected 'END IF', found 'END' END FUNCTION
This is an error caused by a quirk introduced in FB. Single line If's having a colon after THEN require an ENDIF, it has to do with macros... What reason had Jark to put colons past his THEN's escapes me. QB does not require them at all and FB behaves as expected without them. Remove all colons after THEN keywords. Search and Replace THEN : to THEN

ERROR: Array access, index expected, before '=' xn = x * x - y * y + zx0
We have an array names xn, and a variable named xn. Substitute xn with _xn in the lines which error when you try to compile.

The same error with x0, we have an array called x0.
Substitute xo with _xo in the lines which error when you try to compile.

ERROR: Array access, index expected, before '*' dadY = Amplitude * dAdR * drdY. Same problem with Amplitude.
Substitute Amplitude with _Amplitude in the lines which error when you try to compile.

Ok. At this point all modules compile. We're now going to fix a few linker errors.

Linker Errors
After compiling the linker ties together all the modules with a runtime library, finds the final addresses of every sub/function and substitutes the labels in the calls with these addresses. If a sub/function is called in the code and its nowhere to be found, the linker complains and gives us the name of the offending function. It can't give us the line numbers (the linker doesn't work with the source) so we will have to do a text search to find where the problem occurs. Notice the linker gives us "mangled" function names (an ampersand and the size of the parameters passed is added to the end), just ignore the ampersand and what's after.

TcRay21C.o:fake:(.text+0x174d): undefined reference to `LINE24@20'
A call to an undefined Line24 sub is made in the program, you can find this call inside Draw.Axis, in TCRay21.bas, a sub that's it's never called (you can do a search to confirrm it)
Probably the QB4.5 compiler would complain too about this. (Remember: this program never compiled in QB4.5) Just comment out the contents of the sub Draw.Axis

TcRay21C.o:fake:(.text+0x181b3): undefined reference to `FFIX@0'
Ffix was that useful v1ctor's floating point patch for QB 4.5. It's not needed in FreeBASIC. Just comment out the line calling it just after the declarations in tcray21c.bas

And that's all, the program compiles and works. Not a lot of changes for 4000+ lines...
Enjoy!