How can I use fbgfx in C?

General FreeBASIC programming questions.
Post Reply
xlucas
Posts: 334
Joined: May 09, 2014 21:19
Location: Argentina

How can I use fbgfx in C?

Post by xlucas »

Hi! I would like to use the same functions for graphics when I program in FreeBasic as when I program in C. I know that fbgfx is made in C, so I suppose it should be easy to do. Unfortunately, in C, I'm not as experience as in FreeBasic and, while I can write simple console programs, I don't know much about compiling libraries and getting external definitions working. I'm using GCC and I would like to link fbgfx statically. How can I do it? Do I have to download the source and compile the library first or can I somehow just get it done from FreeBasic?

If you guys could give me an example of a very short program in C calling a FBGFX function and the command line to compile it, I'd appreciate it very much. I've seen in other posts some people have created wrappers, but that adds an extra call in-between, right? Is that the only way? Thanks!
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: How can I use fbgfx in C?

Post by caseih »

What operating system? If you're using the GCC backend in FB, you can create a test program and use the compiler flag "-r" to emit a C file that you can look at. For example, here's a simple C program I created based on the the compiler output from a simple FB example program:

Code: Select all

typedef   signed char       int8;
typedef unsigned char      uint8;
typedef   signed short      int16;
typedef unsigned short     uint16;
typedef   signed int        int32;
typedef unsigned int       uint32;
typedef   signed long long  int64;
typedef unsigned long long uint64;
typedef struct { char *data; int64 len; int64 size; } FBSTRING;
typedef int8 boolean;
void fb_GfxLine( void*, float, float, float, float, uint32, int32, uint32, int32 );
int32 fb_GfxScreen( int32, int32, int32, int32, int32 );
void fb_Sleep( int32 );

int main (int argc, char *argv[])
{
        fb_GfxScreen( 12, 8, 0, 0, 0 );
        fb_GfxLine( (void*)0ull, 0x0p+0f, 0x0p+0f, 0x1.F4p+8f, 0x1.F4p+8f, 4u, 0, 65535u, 0 ); //hmm what's up with these numbers?
        fb_Sleep( -1 );
}
Unfortunately my simple "line (0,0)-(500,500),4" line got translated to some interesting numbers that I don't understand.

I can compile that with:

Code: Select all

gcc -o testfbg testfbg.c /opt/FreeBASIC/lib/freebasic/linux-x86_64/libfbgfx.a /opt/FreeBASIC/lib/freebasic/linux-x86_64/libfb.a -lpthread -ldl -lXpm -lX11 -lncurses -lXext -lXrandr
I'm sure you could create a C header file with all the function defs and typedefs in it. Or maybe you could extract them from the FreeBASIC compiler source code somehow.

Anyway it certainly can be done. I can see that there are good reasons to make wrappers. The FB graphics library was never meant to be used from outside of an FB program. The overhead of a wrapper is negligible, so not worth worrying about if someone has a nice wrapper already.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: How can I use fbgfx in C?

Post by MrSwiss »

xlucas wrote:Hi! I would like to use the same functions for graphics when I program in FreeBasic as when I program in C. <snip> Is that the only way?
No, of course not.

I'd however go the opposite way (so to speak), choosing a GFX-Library that:
  • has all the modern features like e.g. antialiasing (NOT available in FbGFX)
  • is independent of a specific OS
  • is written in C (for easy FB integration and, straight use form C)
  • has existing headers (.bi files) in FBC's distribution
For above reasons I'd propose: cairo (Wikipedia).
Which makes the whole integration process as simple and straight forward as possible.

Result: one library equally usable from two different languages and, minimal fuss.
coderJeff
Site Admin
Posts: 4326
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: How can I use fbgfx in C?

Post by coderJeff »

fbgfx works fine from C. Best place to get the function prototypes is from <fb_gfx.h> in the FreeBASIC gfxlib2 library sources.

Code: Select all

/*
   for example on win32:
   gcc sample.c -o sample.exe -I d:/fb/src/gfxlib2 -L d:/fb/lib/win32 -lfbgfx -lfb -lgdi32
*/

#include <fb_gfx.h>
int main( int argc, char *argv[] ) 
{

	/* init the rtlib, may or may not be needed depending on FB funcs used */
	fb_Init( argc, argv, 0 );

	/* width, height, depth, pages, flags, refresh */
	fb_GfxScreenRes( 1024, 768, 32, 1, 0, 0 );
	for( int x=0; x<1024; x+=16 ) 
	{
		fb_GfxLine(
			NULL,             /* target, NULL = working page */ 
			(float)0, (float)0, (float)x, (float)767, /* coords */
			0xc0c0c0,         /* colour */
			LINE_TYPE_LINE,   /* line type */
			0xffff,           /* line style */
			COORD_TYPE_AA     /* coordinate type */
		);   
	}
	fb_GfxSleep( -1 );        /* wait for key press */        
	return 0;
}
Main Ingredients are:
- fbgfx lib and fbrt lib compiled for your target system (win/lin 32/64bit non/multi-threaded ... etc)
- gfxlib and rtlib headers from sources
- command line options for gcc to let it know where the headers and libs are
- reading fbgfx lib source code, because we don't document the public API or internal functions anywhere.

OK, should be known, I have fbc/rtlib/gfxlib development environment set-up that allows me to do this quite easily. If your C and gcc skillz not so great, expect to work at it a bit....
xlucas
Posts: 334
Joined: May 09, 2014 21:19
Location: Argentina

Re: How can I use fbgfx in C?

Post by xlucas »

caseih wrote:What operating system? If you're using the GCC backend in FB, you can create a test program and [...]
Oh! I hadn't thought of that! It's an interesting way to get it working. I'll try that and see how works :)
I'm using GNU/Linux, but I would like my programs to be portable to Windows too. Still, GNU is priority for me. And you're right... it doesn't look so clean that way, so a wrapper would make it look better. But I think the wrapper I saw here in the forum was for C#. Still, I figure I could make my own wrapper if necessary and include only the functions I'm going to be using for each project perhaps.
MrSwiss wrote:[...]I'd propose: cairo (Wikipedia).
Which makes the whole integration process as simple and straight forward as possible.
I've heard about Cairo. Haven't read much yet about it, though. I will. Another library other people have proposed me to use was SDL2. I do understand that these libraries are made already thinking in this portability I seek and that they are more powerful than FBGFX, but I am trying to avoid having to get to that for a number of reasons (which doesn't mean I don't consider them for some projects):

- They are large, very resourceful and most of my projects use simple graphics for which FBGFX is more than enough
- It sounds to me (though I may be mistaken) like it'll be harder to statically compile those libraries or that they would totally bloat my binaries and it's imperative for me to compile statically because I want my programs to be distributed with a portable binary (that people can keep in a USB drive if they want, copy and run whenever they need not even having to download anything from the internet). I've seen many modern programs are following this approach. As a GNU/Linux user, I really dislike when installing programs requires dependencies to be downloaded ans sometimes they can't be easily resolved
- I am very used to FBGFX and I like it. I've seen that other graphics libraries tend to be more OOP styled, which I don't like or that you have to set up a separate thread for a blocking function and similar things. I like a cleaner program flow. I do not know if this is the case of Cairo. I'll check

Of course, thank you. I will definitely take a look at Cairo anyway. I'm just commenting in case these details help to point another possible solution.
coderJeff wrote:OK, should be known, I have fbc/rtlib/gfxlib development environment set-up that allows me to do this quite easily. If your C and gcc skillz not so great, expect to work at it a bit....
Thanks! Indeed, that does look simple in the code, but... I have no idea about how to set up my environment like you say. I have only used GCC for simple console programs so far. I am comfortable with C clauses, because I used to program in Borland C 3.1 for DOS a long time ago (using graphics.h), but I am not skilled with the compiler itself, incorporating external libraries and that. Can you hint me about what kind of setup I should do to be able to just write a program like your example and get it to compile without issues?
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: How can I use fbgfx in C?

Post by dodicat »

Here is an example with cairo and a tiny subset of gfx to run cairo with c++
http://www.mediafire.com/file/etcf5utll ... s.zip/file
TeeEmCee
Posts: 375
Joined: Jul 22, 2006 0:54
Location: Auckland

Re: How can I use fbgfx in C?

Post by TeeEmCee »

xlucas wrote:I have no idea about how to set up my environment like you say. ... Can you hint me about what kind of setup I should do to be able to just write a program like your example and get it to compile without issues?
You only need 1) gcc, 2) a compiled copy of FB, 3) the source code for FB (for the headers). You only need 2 and 3 extracted somewhere on your harddisk. coderJeff gave an example of the commandline to compile his example program:
for example on win32:
gcc sample.c -o sample.exe -I d:/fb/src/gfxlib2 -L d:/fb/lib/win32 -lfbgfx -lfb -lgdi32
12val12newakk
Posts: 35
Joined: Nov 14, 2019 17:04

Re: How can I use fbgfx in C?

Post by 12val12newakk »

is it possible to use fbgfx in C, more correctly in codeblocks-17.12 + mingw (32bit) + fbgfx. ?
I would be grateful for instructions on what to prescribe in the options
codeblocks IDE.
First i need to run the code from post by coderJeff Aug 08, 2020 18:05 (viewtopic.php?p=274795#p274795)
Last edited by fxm on Jul 30, 2023 14:05, edited 1 time in total.
Reason: Added the link to coderJeff's post.
Post Reply