Using a freebasic dll in c++

New to FreeBASIC? Post your questions here.
Boromir
Posts: 447
Joined: Apr 30, 2015 19:28
Location: Texas,U.S., Earth,Solar System
Contact:

Using a freebasic dll in c++

Postby Boromir » May 16, 2017 19:54

Vdecampo made a dll of fbgfx for use in c#
viewtopic.php?f=8&t=22103

I think something similar could be done for C++
How hard would this be?
Last edited by Boromir on May 18, 2017 12:55, edited 1 time in total.
St_W
Posts: 1477
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: Using a freebasic dll in c++

Postby St_W » May 17, 2017 7:44

Using fbgfx from C++ should be very easy as the library is written in C, so you don't have to translate headers. To get it working #including the headers and linking the library should be all you need to do.
Boromir
Posts: 447
Joined: Apr 30, 2015 19:28
Location: Texas,U.S., Earth,Solar System
Contact:

Re: Using a freebasic dll in c++

Postby Boromir » May 17, 2017 14:25

fbgfx.bi is a freebasic header. Wouldn't I have to translate it to C?
MrSwiss
Posts: 3305
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Using a freebasic dll in c++

Postby MrSwiss » May 17, 2017 14:42

@Boromir,

please ignore St_W's comment. In the first sentence are 2 grave errors:
    1) the lib is written in FB (not C) -- and --
    2) Yes, the C#-wrapper has to be re-done, for C++ (or straight C).
caseih
Posts: 1386
Joined: Feb 26, 2007 5:32

Re: Using a freebasic dll in c++

Postby caseih » May 17, 2017 15:30

Vdecampo wrote a simple wrapper for the FB Gfx lib that exposes just a few functions to C#. It should be trivial to make a C header file for these few wrapper functions he wrote. Then you just have to link against his dll which itself contains the full fgxlib and FB runtime library (they are statically-linked to the dll he created).
Boromir
Posts: 447
Joined: Apr 30, 2015 19:28
Location: Texas,U.S., Earth,Solar System
Contact:

Re: Using a freebasic dll in c++

Postby Boromir » May 17, 2017 16:20

I'm getting this when trying to compile my c++ test program.

"gcc -L/home/user/Documents/fbgfxdll -Wall -o test fbgfx.cpp -lfbgfx"
result
l -o test fbgfx.cpp -lfbgfx
/usr/bin/ld: skipping incompatible /home/user/Documents/fbgfxdll/libfbgfx.so when searching for -lfbgfx
/usr/bin/ld: cannot find -lfbgfx
collect2: error: ld returned 1 exit status



Code: Select all

        enum RasterOp
        {
            PSet    = 0,
            PReset  = 1,
            Trans   = 2,
            And     = 3,
            Or      = 4,
            Xor     = 5,
            Alpha   = 6
        };

      int GFX_NULL               = -1;
      int GFX_WINDOWED         = 0x00;
      int GFX_FULLSCREEN         = 0x01;
      int GFX_OPENGL            = 0x02;
      int GFX_NO_SWITCH         = 0x04;
      int GFX_NO_FRAME         = 0x08;
      int GFX_SHAPED_WINDOW      = 0x10;
      int GFX_ALWAYS_ON_TOP      = 0x20;
      int GFX_ALPHA_PRIMITIVES   = 0x40;
      int GFX_HIGH_PRIORITY      = 0x80;

        extern int ScreenRes(int dx, int dy, int depth = 32, int pages = 0, int flags = 0);


int main()
{
ScreenRes(640,480);
};

MrSwiss
Posts: 3305
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Using a freebasic dll in c++

Postby MrSwiss » May 17, 2017 16:29

/usr/bin/ld: skipping incompatible /home/user/Documents/fbgfxdll/libfbgfx.so when searching for -lfbgfx
Typical error if: the .so is NOT the same bitness (32/64), than the program/compiler ...
Last edited by MrSwiss on May 17, 2017 16:38, edited 1 time in total.
Boromir
Posts: 447
Joined: Apr 30, 2015 19:28
Location: Texas,U.S., Earth,Solar System
Contact:

Re: Using a freebasic dll in c++

Postby Boromir » May 17, 2017 16:37

MrSwiss wrote:
/usr/bin/ld: skipping incompatible /home/user/Documents/fbgfxdll/libfbgfx.so when searching for -lfbgfx
Typical error if: the .so is NOT the same bitness (32/64), than the program ...

Thanks. Using 64bit fb worked.
My c++ code seems to be having a problem using the dll.

l -o test fbgfx.cpp -lfbgfx
/tmp/ccfDgYjL.o: In function `main':
fbgfx.cpp:(.text+0x1f): undefined reference to `ScreenRes(int, int, int, int, int)'
Last edited by Boromir on May 18, 2017 13:11, edited 1 time in total.
MrSwiss
Posts: 3305
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Using a freebasic dll in c++

Postby MrSwiss » May 17, 2017 16:40

Sorry, I don't speak C++ ...
fxm
Posts: 9257
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Using a freebasic dll in c++

Postby fxm » May 17, 2017 16:48

Boromir
Posts: 447
Joined: Apr 30, 2015 19:28
Location: Texas,U.S., Earth,Solar System
Contact:

Re: Using a freebasic dll in c++

Postby Boromir » May 17, 2017 17:01


I tried it with extern "C" and "C++" but it still gives the same error.
I'm assuming it's somehow not able to access the functions in the dll.

This is my dll

Code: Select all

' FBGFX DLL Wrapper
'

extern "C"

Function ScreenRes2(dx As Integer, dy As Integer, depth As Integer, pages As Integer, flags As Integer) As Integer Export

   Return ScreenRes(dx,dy,depth,pages,flags,0)

End Function

end extern



and my c++ prog

Code: Select all

        enum RasterOp
        {
            PSet    = 0,
            PReset  = 1,
            Trans   = 2,
            And     = 3,
            Or      = 4,
            Xor     = 5,
            Alpha   = 6
        };

      int GFX_NULL               = -1;
      int GFX_WINDOWED         = 0x00;
      int GFX_FULLSCREEN         = 0x01;
      int GFX_OPENGL            = 0x02;
      int GFX_NO_SWITCH         = 0x04;
      int GFX_NO_FRAME         = 0x08;
      int GFX_SHAPED_WINDOW      = 0x10;
      int GFX_ALWAYS_ON_TOP      = 0x20;
      int GFX_ALPHA_PRIMITIVES   = 0x40;
      int GFX_HIGH_PRIORITY      = 0x80;

      extern int ScreenRes2(int dx, int dy, int depth = 32, int pages = 0, int flags = 0);


int main()
{
ScreenRes2(640,480);
};



Terminal command executed.
gcc -L/home/user/Documents/fbgfxdll -Wall fbgfx.cpp -lfbgfx
Result
/tmp/ccZLfmHl.o: In function `main':
fbgfx.cpp:(.text+0x1f): undefined reference to `ScreenRes2(int, int, int, int, int)'
collect2: error: ld returned 1 exit status
fxm
Posts: 9257
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Using a freebasic dll in c++

Postby fxm » May 17, 2017 17:55

Have you tried with "Windows-MS"?
Boromir
Posts: 447
Joined: Apr 30, 2015 19:28
Location: Texas,U.S., Earth,Solar System
Contact:

Re: Using a freebasic dll in c++

Postby Boromir » May 17, 2017 18:21

fxm wrote:Have you tried with "Windows-MS"?

It worked on Windows.
I went back to linux and tried putting extern C around the dll wrapper code. Now it works there too.
Last edited by Boromir on May 18, 2017 13:11, edited 2 times in total.
dodicat
Posts: 5984
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Using a freebasic dll in c++

Postby dodicat » May 18, 2017 12:25

Extern "C" works well in C
Simple graphics lib:
I call it one.bas --(for one graphics screen)

Code: Select all

'one.bas
'compile with -dll
extern  "C"
Sub screen1 (Byval X As Integer, Byval Y As Integer,xres as integer,yres as integer,b as integer) EXPORT
  Screenres xres,yres,b
  SCREENCONTROL(100, X, Y)
End Sub

function rgbcolor(r as ubyte,g as ubyte,b as ubyte) as uinteger export
    return rgb(r,g,b)
end function

Sub printstring (x as integer,y as integer, text As zString ptr,c as uinteger) EXPORT
 draw string (x,y),*text,c
End Sub

sub printdbl(n as double) export
    print n
    end sub

sub line1(x1 as integer,y1 as integer,x2 as integer,y2 as integer,c as uinteger) EXPORT
    line (x1,y1)-(x2,y2),c
end sub

sub sleep1(t as integer) export
    sleep t
end sub

function randoms(n as integer) as double export
    return rnd*n
    end function

sub circlefill1(x1 as integer,_
    y1 as integer,_
    rad as integer,_
    c as uinteger) export
    circle (x1,y1),rad,c,,,,f
end sub

sub circle1(x1 as integer,_
    y1 as integer,_
    rad as integer,_
    c as uinteger) export
    circle (x1,y1),rad,c
end sub

sub pset1(x1 as double,y1 as double,c as uinteger,im as any pointer=0) EXPORT
    pset im,(x1,y1),c
end sub
sub clearscreen1 export
    cls
end sub
sub endkey1 export
    if inkey=chr(27) then end
end sub
sub screenlock1 export
    screenlock
end sub
sub screenunlock1 export
    screenunlock
end sub
sub getmouse1(byref mx as integer,byref my as integer,byref mw as integer=0,byref mb as integer=0) export
    getmouse(mx,my,mw,mb)
end sub
sub locate1(x as integer,y as integer) export
    locate x,y
end sub

Function framecounter() As Integer export
    dim as double t2=timer
    Static As Double t3,frames,answer
    frames=frames+1
    If (t2-t3)>=1 Then
        t3=t2
        answer=frames
        frames=0
    End If
    Return answer
End Function

end extern
 

The compiler switch for Dev C is
-lone
The C code I use is:

Code: Select all

#include<stdio.h>
#include <stdint.h>
 extern void  screen1(int x, int y,int xres,int yres,int b);
 extern void  printstring (int x,int y,char* text,unsigned int c);
 extern void printdbl(double n);
 extern void circlefill1(int x1,int y1,int rad,unsigned int c);
 extern void line1(int x1,int y1,int x2,int y2,unsigned int c);
 extern unsigned int rgbcolor(int r,int g,int b);
 extern void screenlock1();
 extern void screenunlock1();
 extern void sleep1(int n);
 extern double randoms(int n);
 extern void clearscreen1();
 extern void endket1();
 extern void locate1(int x,int y);
 extern int framecounter();
 



 int h= 1,fps;


 int main(void)

 {
 screen1(50,20,1000,768,32);
 while (h>0)
 {
    h++;
    screenlock1();
 //clearscreen1();
 fps=framecounter();
 printstring(50,100,"Framerate",rgbcolor(255,255,255));
 locate1(15,5);
 printdbl(fps);
 printstring(50,10,"Hello, Press escape key to end",rgbcolor (255,200,0));
circlefill1(50+rand() % 950,50+rand() % 600,5+rand() % 70,rgbcolor(rand() % 255,rand() % 255,rand() % 255));
line1(20,20,900,700,rgbcolor(0,200,0));
screenunlock1();
sleep1(1);
endkey1();
}


 system ("pause");
}
 

I get a framerate of > 600 with sleep1(1)
Note: I have declared a few routines in the C code.
But they still work without declaring them?
(I am very rusty with my C)
Boromir
Posts: 447
Joined: Apr 30, 2015 19:28
Location: Texas,U.S., Earth,Solar System
Contact:

Re: Using a freebasic dll in c++

Postby Boromir » May 18, 2017 12:54

I've already got quite the library going too. :)
I'm still trying to find a way to use byref functions. C++ "&" doesn't seem to work. That's why my getmouse is separated into 4 commands.

Code: Select all

' FBGFX DLL Wrapper
'
extern "C"

Function cScreenRes(dx As Integer, dy As Integer, depth As Integer, pages As Integer, flags As Integer) As Integer Export

   Return ScreenRes(dx,dy,depth,pages,flags,0)

End Function

Function cImageCreate(dx As Integer, dy As Integer, colors As Integer,depth As integer) As Any Ptr Export
   Return ImageCreate(dx,dy,colors)
End Function
Function cImageDestroy(img As Any ptr) As Integer Export
   ImageDestroy(img)
   Return 0
End Function

Function cPset(target As Any Ptr,x As Integer, y As Integer, colors As integer) As Integer Export

   PSet target,(x,y),colors
   Return 1
End Function

Function cPoint(target As Any Ptr,x As Integer, y As Integer) As Integer Export
   Return Point(x,y,target)
End Function

Function cSleep(num As Integer) As Integer Export

   If num=0 Then Sleep Else Sleep num
   Return 1
End Function

Function cRGB(r As Integer, g As Integer, b As integer) As Integer Export

   Return RGB(r,g,b)
End Function

Function cRGBA(r As Integer, g As Integer, b As Integer, a As Integer) As Integer Export

   Return RGBA(r,g,b,a)
End Function

Function cMultikey(key As Integer) As Integer Export

   Return MultiKey(key)
End Function

Function cCircle(target As Any Ptr,x As integer,y As integer,size As integer,colors As Integer,fill As integer) As Integer Export

   If fill=0 Then Circle target,(x,y),size,colors
   If fill=1 Then Circle target,(x,y),size,colors,,,,f
   Return 0
End Function

Function cLine(target As Any Ptr,x As integer,y As Integer,x2 As integer,y2 As integer,colors As Integer,style As integer) As Integer Export

   Line target,(x, y)-(x2, y2),colors,,style
   Return 0
End Function
Function cBox(target As Any Ptr,x As integer,y As Integer,x2 As integer,y2 As integer,colors As Integer,fill As Integer,style As integer) As Integer Export

   If fill=0 Then Line target,(x, y)-(x2, y2),colors,b,style
   If fill=1 Then Line target,(x, y)-(x2, y2),colors,bf,style
   Return 0
End Function

Function cPaint(target As Any Ptr,x As integer,y As Integer,colors As Integer,border As integer) As Integer Export

   Paint target,(x, y),colors,border
   Return 0
End Function

Function cScreenlock(key As Integer) As Integer Export
   If key=0 Then ScreenUnLock
   If key=1 Then ScreenLock
   Return 1
End Function

Function cBload(file As ZString ptr,target As Any Ptr) As Integer Export
   BLoad *file,target
   Return 0
End Function

Function cPrint(text As ZString ptr,mode As Integer) As Integer Export
   If mode=0 Then print *text
   If mode=1 Then print *text;
   Return 0
End Function

Function cLocate(row As Integer,column As Integer) As Integer Export
   locate row,column
   Return 0
End Function

Function cColor(fore As integer,back As Integer) As Integer Export
   Color fore,back
   Return 0
End Function

Function cCls As Integer Export
   Cls
   Return 1
End Function

Function cGetmousex As Integer Export
   Dim As Integer x,y
   GetMouse x,y
   Return x
End Function

Function cGetmousey As Integer Export
   Dim As Integer x,y
   GetMouse x,y
   Return y
End Function

Function cGetmousew As Integer Export
   Dim As Integer x,y,w
   GetMouse x,y,w
   Return w
End Function

Function cGetmouseb As Integer Export
   Dim As Integer x,y,b
   GetMouse x,y,,b
   Return b
End Function

Function cPut(dst As Any Ptr, x As Integer, y As Integer, himage As Any Ptr, mode As Integer) As Integer Export
   Select Case mode
      Case 0
         Put dst,(x,y),himage,PSet
      Case 1
         Put dst,(x,y),himage,PReset
      Case 2
         Put dst,(x,y),himage,Trans
      Case 3
         Put dst,(x,y),himage,And
      Case 4
         Put dst,(x,y),himage,Or
      Case 5
         Put dst,(x,y),himage,Xor
      Case 6
         Put dst,(x,y),himage,Alpha
   End Select
   Return 0
End Function

End extern



Code: Select all

extern "C" {
        enum RasterOp
        {
            PSet    = 0,
            PReset  = 1,
            Trans   = 2,
            And     = 3,
            Or      = 4,
            Xor     = 5,
            Alpha   = 6
        };

      int GFX_NULL               = -1;
      int GFX_WINDOWED         = 0x00;
      int GFX_FULLSCREEN         = 0x01;
      int GFX_OPENGL            = 0x02;
      int GFX_NO_SWITCH         = 0x04;
      int GFX_NO_FRAME         = 0x08;
      int GFX_SHAPED_WINDOW      = 0x10;
      int GFX_ALWAYS_ON_TOP      = 0x20;
      int GFX_ALPHA_PRIMITIVES   = 0x40;
      int GFX_HIGH_PRIORITY      = 0x80;

      extern int cScreenRes(int dx, int dy, int depth = 32, int pages = 0, int flags = 0);
      extern void * cImageCreate(int x, int y, int color = 0, int depth = 32);
      extern int cImageDestroy(void * img);
      extern int cBload(char * file,void * img);
      extern int cPset(void * target,int x, int y, int color = 0);
      extern int cPoint(void * target,int x, int y);
      extern int cRGB(int r, int g, int b);
      extern int cRGBA(int r, int g, int b, int a);
      extern int cCircle(void * target, int x, int y, int size, int color=0, int fill=0);
      extern int cLine(void * target,int x1,int y1,int x2,int y2,int color=0,int style=0b1111111111111111);
     extern int cBox(void * target,int x1,int y1,int x2,int y2,int color=0,int fill=0,int style=0b1111111111111111);
     extern int cPrint(char * text,int mode=0);
     #define printas cPrint
     extern int cLocate(int column, int row);
     extern int cColor(int fore, int back);
     extern int cPaint(void * target, int x, int y, int color, int border);
      extern int cSleep(int num = 0);
      extern int cMultikey(int key);
      extern int cScreenlock(int key);
      extern int cCls();
      extern int cPut(void * dest,int x,int y,void * img,int mode);
      extern int cGetmousex();
      extern int cGetmousey();
      extern int cGetmousew();
      extern int cGetmouseb();
}

#include <string.h>

int main()
{
cScreenRes(640,480,32,GFX_WINDOWED);
cColor(cRGB(255,0,255),cRGB(255,255,255));
cCls();
cLocate(5,5);
printas(strdup("hello"));
cSleep();
void * pic (cImageCreate(640,480,cRGB(255,0,255),32));
cBload(strdup("new.bmp"),pic);
int x(2),y(2),wheel,buttons;

   while(not cMultikey(1)){
      x=cGetmousex()-1;
      y=cGetmousey()-1;
      cScreenlock(1);
      cCls();
      cPut(0,0,0,pic,PSet);
      cPset(0,x,y,cRGB(255,0,255));
      cCircle(0,x,y,10,cRGB(0,0,0));
      cBox(0,x,y,x+10,y+10,cRGB(255,0,255));
      cScreenlock(0);

      cSleep(1);
   };
cImageDestroy(pic);
};


Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 1 guest