*.bi ?

New to FreeBASIC? Post your questions here.
Post Reply
bluatigro
Posts: 660
Joined: Apr 25, 2012 10:35
Location: netherlands

*.bi ?

Post by bluatigro »

i want to split my code into *.bi's and *.bas's

instructions please
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Re: *.bi ?

Post by counting_pine »

I would start by moving things like function declarations and defines/macros into one section at the top of the file. Then once you've made sure it all still works, cut and paste that section into a new file, e.g. headerfile.bi. Replace the text you've cut in the .bas file with the line: #include "headerfile.bi", and it should all continue to work the same.
bluatigro
Posts: 660
Joined: Apr 25, 2012 10:35
Location: netherlands

Re: *.bi ?

Post by bluatigro »

it shoot be done like this i think but i got a strange error
.bi

Code: Select all

type complex
  dim i as double
  dim r as double
  declare constructor()
  declare constructor( a as double , bj as double )
  declare sub fill( a as double , bj as double )
  declare function lenght() as double
end type
.bas

Code: Select all

#include "t_complex.bi"
constructor complex( )
  this.r = 0
  this.i = 0
end constructor 
constructor complex( a as double , bj as double )
  this.r = a
  this.i = bj
end constructor 
sub complex.fill( a as double , bj as double )
  this.r = a
  this.i = bj
end sub
function complex.lenght() as double
  return sqr( this.i ^ 2 + this.r ^ 2 )
end function
operator + ( a as complex , b as complex ) as complex
  return type( a.r + b.r , a.i + b.i )
end operator
operator * ( a as complex , b as complex ) as complex
  return type( a.r * b.r - a.i * b.i , a.r * b.i + a.i * b.r )
end operator
main prog

Code: Select all

#include "t_complex.bi"
const as double pi = atn( 1 ) * 4
declare function rad( deg as double ) as double
declare function rainbow( deg as double ) as integer

dim as double x , y 
dim as integer tel , dept
dim as complex nu , punt

dim as double mx , my , diam

print : print : print

input "mid x = " ; mx
input "mid y = " ; my
input "diameter = " ; diam
input "dept = " ; dept

screen 20 , 32
for x = 0 to 1024
  for y = 0 to 768
    tel = 0
    nu.fill ( x - 1024 / 2 ) / 1024 * diam - mx , ( y - 768 / 2 ) / 768 * diam - my
    punt.fill ( x - 1024 / 2 ) / 1024 * diam - mx , ( y - 768 / 2 ) / 768 * diam - my
    while nu.lenght < 50 and tel < dept
      tel += 1
      nu = nu * nu + punt
    wend
    if tel < 50 then
      pset( x , y ) , rainbow( tel * 36 / 5 )
    end if
  next y
next x

while inkey = "" 
wend
end
function rad( deg as double ) as double
  return  deg * pi / 180
end function
function rainbow( deg as double ) as integer
  return rgb( sin( rad( deg ) ) * 127 + 128 _
            , sin( rad( deg + 120 ) ) * 127 + 128 _
            , sin( rad( deg - 120 ) ) * 127 + 128 )
end function
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Re: *.bi ?

Post by counting_pine »

You're getting a type mismatch error on 'nu = nu * nu + punt' (where nu and punt are type 'complex') because the main prog can't see your custom operators. They should be declared in the .bi file, e.g. 'declare operator + ( a as complex , b as complex ) as complex'

(Note: it's spelled "length" :)
bluatigro
Posts: 660
Joined: Apr 25, 2012 10:35
Location: netherlands

Re: *.bi ?

Post by bluatigro »

changed code of .bi [ see below ]

got : undeclared defenition [ several times ]

can i overload fb comands ?
example :
declare function len( x as complex ) as single

can i do :
#include "type\complex.bi"

Code: Select all

#ifndef COMPLEX_BI
#define COMPLEX_BI

type complex
  dim i as double
  dim r as double
  declare constructor()
  declare constructor( a as double , bj as double )
  declare sub fill( a as double , bj as double )
  declare function length() as double
end type

declare operator + ( a as complex , b as complex ) as complex
declare operator * ( a as complex , b as complex ) as complex

#endif
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Re: *.bi ?

Post by counting_pine »

Hi.
Rather than vague error reports, it would be more helpful if you paste the exact output of the command. The include file you posted compiles OK as-is if you save it as a .bas file.

Some FB operators such as abs/log/... can be overloaded as operators, e.g. operator abs(t as udt) as ....
Some functions like ucase() can be overloaded in the usual way.

len() is a quirk syntax, similar to log() but more complicated internally. It provides the same function as sizeof() function for any non-string type. It theoretically could be made overloadable maybe, but for the time being it isn't.

You can have path name in #include directives, but for cross-platform purposes it is probably better to use the forward-slash, e.g. #include "crt/string.bi".
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: *.bi ?

Post by fxm »

- I realized that bluatigro has two source modules that he calls: '.bas' (secondary module I guess) and 'main prog' (main module).
But I do not think that he compiles these two modules at once (with command: "fbc module1.bas module2.bas").

- It might be more simple for him (using FBIde) to have a single source module, and only another module to include containing the UDT with its member procedures (command: "fbc single_module.bas").
Remark: For this second configuration, the two added lines for operators declaration are useless.
bluatigro
Posts: 660
Joined: Apr 25, 2012 10:35
Location: netherlands

Re: *.bi ?

Post by bluatigro »

i want to take a c++ like way to do this

and store every class into its own *.bi and *.bas file

so i dont have to cut and paste every time i use a object

and opengl stuf in one big one

i m using fbide to code stuf
Post Reply