FLTK-C-1.3.3 for FreeBASIC

Headers, Bindings, Libraries for use with FreeBASIC, Please include example of use to help ensure they are tested and usable.
Post Reply
owen
Posts: 555
Joined: Apr 19, 2006 10:55
Location: Kissimmee, FL
Contact:

Re: FLTK C for FreeBASIC Jan 09, 2017

Post by owen »

this is what i figure will work for me

Code: Select all

#include once "fltk-c.bi"
#Include Once "fbgfx.bi"
Declare Sub myrgbcol(pc As UByte)

ScreenRes 580,580,,4,fb.GFX_NULL
Circle(290,290),290,2,,,,f


Type myclr
    r As UByte
    g As UByte
    b As UByte
End Type
Dim Shared myrgb As myclr

Dim As Fl_Window Ptr win
Dim As Fl_Box Ptr box
Dim As Fl_Image Ptr img

Dim as ubyte ptr myfbgfx = ScreenPtr
Dim As UByte pixcolor
Dim As UByte Ptr pixels
Dim As Integer i

win = Fl_WindowNew(580,580)
box = Fl_BoxNew(0,0,580,580)

pixels=Allocate(580*580*3)
For i = 0 To 580*580-1
	pixcolor=*(myfbgfx+i)
	myrgbcol(pixcolor)
	*(pixels+i*3)=myrgb.r
	*(pixels+i*3+1)=myrgb.g
	*(pixels+i*3+2)=myrgb.b
Next
img = Fl_RGB_ImageNew(pixels,580,580,3,0)
Fl_WidgetSetImage box,img
Fl_WidgetRedraw box

Fl_WindowShow(win)
Fl_Run

End



Sub myrgbcol(pc As UByte)
	Dim As UByte r,g,b
	Select Case pc
		Case 0 'black
			r=0
			g=0
			b=0
		Case 1 'blue
			r=0
			g=0
			b=255
		Case 2 'green
			r=0
			g=255
			b=0
		Case 3 'cyan
			r=0
			g=255
			b=255
		Case 4 'red
			r=255
			g=0
			b=0
		Case 5 'pink
			r=255
			g=192
			b=203
		Case 6 'yellow
			r=255
			g=255
			b=0
		Case 7 'grey
			r=128
			g=128
			b=128
		Case 8 'dark grey
			r=105
			g=105
			b=105
		Case 9 'bright blue
			r=132
			g=112
			b=255
		Case 10 'bright green
			r=127
			g=255
			b=0
		Case 11 'bright cyan
			r=105
			g=255
			b=255
		Case 12 'bright red
			r=255
			g=105
			b=105
		Case 13 'bright pink
			r=255
			g=222
			b=233
		Case 14 'bright yellow
			r=255
			g=255
			b=128
		Case 15 'white
			r=255
			g=255
			b=255
	End Select
	
	myrgb.r=r
	myrgb.g=g
	myrgb.b=b

End Sub
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: FLTK C for FreeBASIC Jan 09, 2017

Post by MrSwiss »

Hi owen,

you might want to use a Union, to set/get Color's 'full' or 'by Channel' incl. Alpha:

Code: Select all

' AlphaChannelWithUnion.bas -- 2017-04-08, by MrSwiss

Union colour
    As ULong clr    ' color
    Type
        As UByte b  ' blue
        As UByte g  ' green
        As UByte r  ' red
        As UByte a  ' alpha
    End Type
End Union

Dim As colour   c1, c2, c3, c4, c5
' all Color's are 'full Alpha' = opaque
c1.clr = &hFF000000 ' black --> RGBA(0, 0, 0, 255), in HEX= 'AARRGGBB'
c2.clr = &hFFFFFFFF ' white
c3.clr = &hFFFF0000 ' red
c3.clr = &hFF00FF00 ' green
c5.clr = &hFF0000FF ' blue
Last edited by MrSwiss on Apr 29, 2017 9:53, edited 1 time in total.
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: FLTK C for FreeBASIC Jan 09, 2017

Post by D.J.Peters »

Code: Select all

#include once "fltk-c.bi"

ScreenRes 580,580,,,-1
dim as integer w,h,pitch_src
screeninfo w,h,,,pitch_src

Circle (290,290),280,2,,,,f
for x as integer=0 to 15
  Line (x*36,0)-Step(17,h-1),x,BF
next

dim as integer   bytes_img=3 ' RGB
dim as integer   pitch_img=w*bytes_img
dim as ubyte ptr rgb_img=allocate(h*pitch_img)
dim as ubyte ptr rs=screenptr()  ' row source
dim as ubyte ptr ri=rgb_img      ' row image
dim as ulong     r,g,b
for y as integer=0 to h-1
  dim as integer i
  for x as integer=0 to w-1
    palette get rs[x],r,g,b
    ri[i+0]=r: ri[i+1]=g : ri[i+2]=b : i+=3
  next
  rs+=pitch_src :ri+=pitch_img ' next row of source and image
next

var win = Fl_WindowNew(w,h)
Fl_WidgetSetImage(Fl_BoxNew(0,0,w,h),Fl_RGB_ImageNew(rgb_img,w,h,bytes_img,pitch_img))
Fl_WindowShow(win)
Fl_Run

deallocate rgb_img
owen
Posts: 555
Joined: Apr 19, 2006 10:55
Location: Kissimmee, FL
Contact:

Re: FLTK C for FreeBASIC Jan 09, 2017

Post by owen »

@ MrSwiss thank you
@ Joshy nice. that is much better. I will use that instead of my solution.
owen
Posts: 555
Joined: Apr 19, 2006 10:55
Location: Kissimmee, FL
Contact:

Re: FLTK C for FreeBASIC Jan 09, 2017

Post by owen »

i can't get this to work. ri+=pitch_img is giving me a problem i think.
all i'm trying to do is show the image when the button is clicked.

Code: Select all

#include once "fltk-c.bi"

Declare Sub Create_Window_Main ()
Declare Sub Button_Click_Event Cdecl (widget As FL_Widget Ptr)

'Windows and widgets:
Dim Shared As Fl_Window Ptr win
Dim Shared As Fl_Box Ptr box
Dim Shared As Fl_Button Ptr btn
Dim Shared As Fl_Image Ptr img
Dim Shared As Integer w,h,pitch_src
dim as Integer x

ScreenRes 580,580,,,-1
screeninfo w,h,,,pitch_src

Circle (290,290),280,2,,,,f
for x as integer=0 to 15
  Line (x*36,0)-Step(17,h-1),x,BF
next




'Main program:
Create_Window_Main ()
Fl_WidgetSetCallback0(btn, @Button_Click_Event())
Fl_WindowShow(win)
Fl_Run

End



Sub Create_Window_Main ()
	win = Fl_WindowNew (800, 600, "my_fbgfx_to_fltk_image_test-2")
	box = Fl_BoxNew(120, 0, 580, 580)
	btn = Fl_ButtonNew (10, 10, 100, 20, "Click me!")
End Sub


Sub Button_Click_Event Cdecl (widget As FL_Widget Ptr)
	Dim As Integer i,x,y
	Dim as integer   bytes_img=3 ' RGB
	Dim as integer   pitch_img=w*bytes_img
	Dim as ubyte ptr rgb_img=allocate(h*pitch_img)
	Dim as ubyte ptr rs=screenptr()  ' row source
	Dim as ubyte ptr ri=rgb_img      ' row image
	Dim as ulong     r,g,b
	For y = 0 to h-1
	  for x = 0 to w-1
	    palette get rs[x],r,g,b
	    ri[i+0]=r: ri[i+1]=g : ri[i+2]=b : i+=3
	  next
	  rs+=pitch_src
	  ri+=pitch_img
	Next
	img = Fl_RGB_ImageNew(rgb_img,w,h,bytes_img,pitch_img)
	Fl_WidgetSetImage box,img
	Fl_WidgetRedraw box
	deallocate rgb_img
End Sub

owen
Posts: 555
Joined: Apr 19, 2006 10:55
Location: Kissimmee, FL
Contact:

Re: FLTK C for FreeBASIC Jan 09, 2017

Post by owen »

ok. got it

Code: Select all

#include once "fltk-c.bi"

Declare Sub Create_Window_Main ()
Declare Sub Button_Click_Event Cdecl (widget As FL_Widget Ptr)

'Windows and widgets:
Dim Shared As Fl_Window Ptr win
Dim Shared As Fl_Box Ptr box
Dim Shared As Fl_Button Ptr btn
Dim Shared As Fl_Image Ptr img
Dim Shared As Integer w,h,pitch_src
Dim Shared As Integer bytes_img=3 ' RGB
ScreenRes 580,580,,,-1
ScreenInfo w,h,,,pitch_src
Dim Shared As Integer pitch_img
pitch_img=w*bytes_img
Dim Shared As UByte Ptr rgb_img
rgb_img=allocate(h*pitch_img)

dim as Integer x



Circle (290,290),280,2,,,,f
for x as integer=0 to 15
  Line (x*36,0)-Step(17,h-1),x,BF
next




'Main program:
Create_Window_Main ()
Fl_WidgetSetCallback0(btn, @Button_Click_Event())
Fl_WindowShow(win)
Fl_Run
DeAllocate rgb_img

End



Sub Create_Window_Main ()
	win = Fl_WindowNew (800, 600, "my_fbgfx_to_fltk_image_test-2")
	box = Fl_BoxNew(120, 0, 580, 580)
	btn = Fl_ButtonNew (10, 10, 100, 20, "Click me!")
End Sub


Sub Button_Click_Event Cdecl (widget As FL_Widget Ptr)
	Dim As Integer i,x,y
	Dim as ubyte ptr rs=screenptr()  ' row source
	Dim as ubyte ptr ri=rgb_img      ' row image
	Dim as ulong     r,g,b
	For y = 0 to h-1
		i=0
		For x = 0 to w-1
			Palette get rs[x],r,g,b
			ri[i+0]=r: ri[i+1]=g : ri[i+2]=b : i+=3
		Next
		rs+=pitch_src
		ri+=pitch_img
	Next
	img = Fl_RGB_ImageNew(rgb_img,w,h,bytes_img,pitch_img)
	Fl_WidgetSetImage box,img
	Fl_WidgetRedraw box
End Sub

D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: FLTK C for FreeBASIC Jan 09, 2017

Post by D.J.Peters »

@owen I don't understand why you need screenres if you create a 2D CAD GUI program ?

However why do you create the RGB image again and again if you click the button ?

You have to create it only once and only if pixels are changed in the fbgfx screen
you have to copy it from screenptr to rgb_img and call Fl_WidgetRedraw() thats all.

With other words the box will use the RGB image the whole lifetime of the program
and the RGB image will use your allocated rgb_img the whole lifetime of the program also

You know what i'm mean ?

Joshy
owen
Posts: 555
Joined: Apr 19, 2006 10:55
Location: Kissimmee, FL
Contact:

Re: FLTK C for FreeBASIC Jan 09, 2017

Post by owen »

clicking the button evertime to update fltk's image with fbgfx is not my intention. i was just testing to see how fltk works.
fltk's image will need to be updated with fbgfx using FL_ADDIDLE. that is what im working on now but having a problem with some kind of time out or system memory / caching issue.


it's because in my cad project. i use fbgfx page flipping to animate my line drawing.
ie. click once starts the line, move the mouse and the line is drawn from start point to where ever you move the mouse.
note only one line is being drawn, not multiple lines from start to mouxe x,y {as you move the mouse}
the animation is achieved by using screencopy from original screen to work page.

my goal today is to convert this gtk to fltk: http://fbcadcam.org/gtk3/source_code/my ... tk-8-1.bas

I added a counter which is printed to fbgfx to see how fast fltk's image can be updated. here's what i got so far

Code: Select all

#include once "fltk-c.bi"

Declare Sub Create_Window_Main ()
Declare Sub Button_Click_Event Cdecl (widget As FL_Widget Ptr)
Declare Sub myfbgfxtofltkimg()

'Windows and widgets:
Dim Shared As Fl_Window Ptr win
Dim Shared As Fl_Box Ptr box
Dim Shared As Fl_Button Ptr btn
Dim Shared As Fl_Image Ptr img
Dim Shared As Integer w,h,pitch_src
Dim Shared As Integer bytes_img=3 ' RGB
ScreenRes 580,580,,,-1
ScreenInfo w,h,,,pitch_src
Dim Shared As Integer pitch_img
pitch_img=w*bytes_img
Dim Shared As UByte Ptr rgb_img
rgb_img=allocate(h*pitch_img)

dim as Integer x
Dim Shared As Integer c



Circle (290,290),280,2,,,,f
for x as integer=0 to 15
  Line (x*36,0)-Step(17,h-1),x,BF
next




'Main program:
Create_Window_Main ()
Fl_WidgetSetCallback(btn, @Button_Click_Event())

Fl_AddIdle @myfbgfxtofltkimg

Fl_WindowShow(win)
Fl_Run
DeAllocate rgb_img

End

Sub Create_Window_Main ()
	win = Fl_WindowNew (800, 600, "my_fbgfx_to_fltk_image_test-3")
	box = Fl_BoxNew(120, 0, 580, 580)
	btn = Fl_ButtonNew (10, 10, 100, 20, "Click me!")
End Sub

Sub Button_Click_Event Cdecl (widget As FL_Widget Ptr)
End Sub

Sub myfbgfxtofltkimg()
	c+=1
	Locate 1,1
	Print c
	Dim As Integer i,x,y
	Dim as ubyte ptr rs=screenptr()  ' row source
	Dim as ubyte ptr ri=rgb_img      ' row image
	Dim as ulong     r,g,b
	For y = 0 to h-1
		i=0
		For x = 0 to w-1
			Palette get rs[x],r,g,b
			ri[i+0]=r: ri[i+1]=g : ri[i+2]=b : i+=3
		Next
		rs+=pitch_src
		ri+=pitch_img
	Next
	img = Fl_RGB_ImageNew(rgb_img,w,h,bytes_img,pitch_img)
	Fl_WidgetSetImage box,img
	Fl_WidgetRedraw box
	Sleep 1
End Sub

owen
Posts: 555
Joined: Apr 19, 2006 10:55
Location: Kissimmee, FL
Contact:

Re: FLTK C for FreeBASIC Jan 09, 2017

Post by owen »

i use screenres to draw lines and circles into an fbgfx screen that has a few pages, workpage, background page, etc... I didn't realize i could draw to a buffer ie. LINE buffer,(x1,y1)-(x2,y2) until you recently demonstrated that. However due to the need to use screenset and screencopy to achieve my animation and zooming / panning etc.. I'm not sure how to do that by just using a buffer.
fbcad will work as is using fltk without having to rework to much of it by just getting this fbgfx to fltkimage stuff down.
owen
Posts: 555
Joined: Apr 19, 2006 10:55
Location: Kissimmee, FL
Contact:

Re: FLTK C for FreeBASIC Jan 09, 2017

Post by owen »

ps, it is quite refreshing to have you coaching me along the way with fltk. i didn't have any assistance really when using gtk.
note: i am a terrible programmer. case in point: didn't know about how to use the pallet command ie. Palette get rs[x],r,g,b
i learn something every day from this community which is unlike any other.
owen
Posts: 555
Joined: Apr 19, 2006 10:55
Location: Kissimmee, FL
Contact:

Re: FLTK C for FreeBASIC Jan 09, 2017

Post by owen »

Fl_ImageDelete was what i was missing. got it now.
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: FLTK C for FreeBASIC Jan 09, 2017

Post by D.J.Peters »

D.J.Peters wrote:don't cast callback pointers !!! NEVER !!!
If you get compile warnings something must be wrong !
@owen your myfbgfxtofltkimg sub are wrong !

sub myfbgfxtofltkimg()

and must be:

sub myfbgfxtofltkimg CDECL (byval UserData as any ptr)

I compile all my stuff with the -w all option and FreeBASIC will write out a warning

Joshy
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: FLTK C for FreeBASIC Jan 09, 2017

Post by D.J.Peters »

Hello owen this is only for fun but may be you can learn something from it.

you don't need an extra pixel buffer
you don't need an Fl_RGB_Image at all
you need only swap the red and blue triples
you can use use RGB(blue, green, red) instead of RGB(red, green, blue)

test of:
DrawImage(ScreenPtr(),0,0,w,h,bpp,pitch)
and while FL_Wait():wend instead of Fl_Run()

Joshy

Code: Select all

#include once "fltk-c.bi"

Sub myIdleHandler cdecl (byval win as any ptr)
  static as  ubyte r,g,b
  static as integer counter
  static as single x1=100,y1=100,x2=100,y2=100,x1s=1.1,y1s=2.1,x2s=-3.3,y2s=-4.4
  dim as integer w,h,bpp,pitch
  ' no active screen something must be wrong !  
  if screenptr()=0 then return
  
  screeninfo w,h,,bpp,pitch
  counter+=1 : locate 1,1 : print counter

  ' only for fun FreeBASIC used BGR and FLTK RGB
  line (x1,y1)-(x2,y2),RGB(b,g,r)
  x1+=x1s :if x1<0 or x1>w then x1s*=-1
  y1+=y1s :if y1<0 or y1>h then y1s*=-1
  x2+=x2s :if x2<0 or x2>w then x2s*=-1
  y2+=y2s :if y2<0 or y2>h then y2s*=-1
  r+=2:g+=3:b+=5
  
  ' make the window the current target for all kinds of drawing
  Fl_WindowMakeCurrent(win)
  ' copy the FBGFX screen to the FLTK window
  DrawImage(ScreenPtr(),0,0,w,h,bpp,pitch)
End Sub

'
' main
'
ScreenRes 580,580,32,,-1
var win = Fl_Double_WindowNew(580, 580, "myIdleHandler")
Fl_AddIdle(@myIdleHandler,win)
Fl_WindowShow(win)
while FL_Wait():wend
Last edited by D.J.Peters on Apr 30, 2017 14:04, edited 1 time in total.
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: FLTK C for FreeBASIC Jan 09, 2017

Post by D.J.Peters »

Here are an example how you can use the Fl_Wait() loop for other tasks.

Joshy

Code: Select all

#include once "fltk-c.bi"

Sub myIdleHandler cdecl (byval win as any ptr)
  static as ubyte r,g,b
  static as integer counter
  static as single x1=100,y1=100,x2=100,y2=100,x1s=1.1,y1s=2.1,x2s=-3.3,y2s=-4.4
  dim as integer w,h,bpp,pitch
  ' no active screen something must be wrong !  
  if screenptr()=0 then return
  
  screeninfo w,h,,bpp,pitch
  counter+=1 : locate 1,1 : print counter

  ' only for fun FreeBASIC used BGR and FLTK RGB
  line (x1,y1)-(x2,y2),RGB(b,g,r)
  x1+=x1s :if x1<1 or x1>w-1 then x1s*=-1
  y1+=y1s :if y1<1 or y1>h-1 then y1s*=-1
  x2+=x2s :if x2<1 or x2>w-1 then x2s*=-1
  y2+=y2s :if y2<1 or y2>h-1 then y2s*=-1
  r+=1:g+=2:b+=3
  
  ' make the window the current target for all kinds of drawing
  Fl_WindowMakeCurrent(win)
  ' draw the FBGFX screen to the FLTK window
  DrawImage(ScreenPtr(),0,0,w,h,bpp,pitch)
End Sub

'
' main
'
ScreenRes 580,580,24,,not 0
var win = Fl_Double_WindowNew(580, 580, "myIdleHandler")
Fl_AddIdle(@myIdleHandler,win)
Fl_WindowShow(win)

' optional: you can do any task in "quasi" parallel
dim as integer char=32
open err for output as #99
while FL_Wait()
  char+=1:if char=256 then char=33
  print #99,chr(char);
  sleep 10 ' don't eat all CPÜ cycles :-)
wend
close #99



owen
Posts: 555
Joined: Apr 19, 2006 10:55
Location: Kissimmee, FL
Contact:

Re: FLTK C for FreeBASIC Jan 09, 2017

Post by owen »

Joshy thank you for all this information.
how do i get mouse x,y from image or the box containing the image?
Post Reply