Question about FLTK and FreeBASIC

External libraries (GTK, GSL, SDL, Allegro, OpenGL, etc) questions.
taijinantangXX
Posts: 13
Joined: Dec 19, 2014 5:29

Question about FLTK and FreeBASIC

Postby taijinantangXX » Aug 07, 2018 7:17

Hey, guys! I've wrote a simple programme which is a window showing DATE and TIME second by second using FreeBASIC and FLTK. BUT, The time didn't go second by second (sometime did sometime no). Is there anything wrong with my code? Could someone kindly correct it for me. Thank you.
Here is the code:

Code: Select all

#include once "fltk-c.bi"
#include once "fbthread.bi"
#include once "vbcompat.bi"

DIM Shared AS Fl_Window ptr Window_Main
DIM Shared AS Fl_Widget ptr Box1
DIM Shared AS integer Win_Lenth
DIM Shared AS integer Win_Height
DIM Shared AS String MytimeString
DIM Shared AS Any Ptr MythreadNO

Fl_SetFont (18,"SimHei")

Win_Lenth = 800
Win_Height =600

Sub Create_Window_Main ( )
   Window_Main = Fl_WindowNew2 (Fl_GetW()/2-Win_Lenth/2,Fl_GetH()/2-Win_Height/2,Win_Lenth,Win_Height,"测试")
   Box1 = Fl_BoxNew (100,110,800,600)
   Fl_WidgetSetLabelFont (Box1,18)
   Fl_WidgetSetLabelSize (Box1,16)
END SUB

Sub myhandler (byval userdata as any ptr)
 Dim AS double mytimeNOW
 Dim AS string MyYil, MyAy, MyKun, MyHour, MyMinute, MySecond
  mytimeNOW = NOW ()
  MyYil = str (Year (mytimeNOW))
  MyAy  = str (Month (mytimeNOW))
  MyKun = str (Day (mytimeNOW))
  MyHour= str (Hour (mytimeNOW))
  MyMinute=str (Minute (mytimeNOW))
  MySecond=str (Second (mytimeNOW))
  MytimeString = "现在是:"+MyYil+"年"+MyAy+"月"+MyKun+"日 "+MyHour+"时"+MyMinute+"分"+MySecond+"秒"
 Fl_WidgetSetLabel (Box1, MytimeString)
END SUB

Sub mythread (byval userdata as any ptr)
Start:
  Fl_RepeatTimeout(1, @myhandler)
GOTO Start
END SUB

Create_Window_Main ( )
Fl_WindowShow (Window_Main)
MythreadNO = ThreadCreate(@mythread, 0)
Fl_Run
END
grindstone
Posts: 493
Joined: May 05, 2015 5:35
Location: Germany

Re: Question about FLTK and FreeBASIC

Postby grindstone » Aug 07, 2018 8:11

Sorry, I only get a window with a grey background.
WQ1980
Posts: 32
Joined: Sep 25, 2015 12:04
Location: Russia

Re: Question about FLTK and FreeBASIC

Postby WQ1980 » Aug 07, 2018 11:10

taijinantangXX

Code: Select all

#Include Once "fltk-c.bi"
#Include Once "fbthread.bi"
#Include Once "vbcompat.bi"

Dim Shared As Fl_Window Ptr Window_Main
Dim Shared As Fl_Widget Ptr Box1
Dim Shared As Integer Win_Lenth
Dim Shared As Integer Win_Height
Dim Shared As String MytimeString
Dim Shared As Any Ptr MythreadNO

Fl_SetFont (18,"SimHei")

Win_Lenth = 800
Win_Height =600

Sub Create_Window_Main ( )
   Window_Main = Fl_WindowNew2 (Fl_GetW()/2-Win_Lenth/2,Fl_GetH()/2-Win_Height/2,Win_Lenth,Win_Height,"??")
   Box1 = Fl_BoxNew (100,110,800,600)
   Fl_WidgetSetLabelFont (Box1,18)
   Fl_WidgetSetLabelSize (Box1,16)
End Sub

Sub myhandler (ByVal userdata As Any Ptr)
   
   Dim As Double mytimeNOW = Now()
   Dim As Integer MyYil, MyAy, MyKun, MyHour, MyMinute, MySecond
   
   MyYil = Year (mytimeNOW)
   MyAy  =  Month (mytimeNOW)
   MyKun =  Day (mytimeNOW)
   MyHour=  Hour (mytimeNOW)
   MyMinute = Minute (mytimeNOW)
   MySecond = Second (mytimeNOW)
   MytimeString = "???:" & MyYil & "?" & MyAy & "?" & MyKun & "? " & MyHour & "?" & MyMinute & "?" & MySecond & "?"
   Fl_WidgetSetLabel (Box1, MytimeString)
End Sub

Sub mythread (ByVal userdata As Any Ptr)
   'Start:
   'GoTo Start
   While Fl_Wait()
      Fl_RepeatTimeout(1,Cast (Any Ptr,@myhandler))
      Sleep 1000\60,1
   Wend
End Sub


Create_Window_Main ( )
Fl_WindowShow (Window_Main)
MythreadNO = ThreadCreate(@mythread, 0)

Fl_Run
End
badidea
Posts: 820
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Question about FLTK and FreeBASIC

Postby badidea » Aug 07, 2018 18:09

Yep, without sleep, the computer is probably to busy looping (throwing all its computing power at this loop), no time for graphics updates. Sleep 1,1 is probably also OK.

Edit: Not sure if sleep is needed if Fl_wait is also used.
Last edited by badidea on Aug 07, 2018 18:29, edited 1 time in total.
taijinantangXX
Posts: 13
Joined: Dec 19, 2014 5:29

Re: Question about FLTK and FreeBASIC

Postby taijinantangXX » Aug 07, 2018 18:22

grindstone

I've REMed some lines with Chinese characters and font settings. Your computer may have not got the Chinese font I used, so you got a grey window with nothing showing on it.

Following is ths code:

Code: Select all

#include once "fltk-c.bi"
#include once "fbthread.bi"
#include once "vbcompat.bi"

DIM Shared AS Fl_Window ptr Window_Main
DIM Shared AS Fl_Widget ptr Box1
DIM Shared AS integer Win_Lenth
DIM Shared AS integer Win_Height
DIM Shared AS String MytimeString
DIM Shared AS Any Ptr MythreadNO

REM Fl_SetFont (18,"SimHei")

Win_Lenth = 800
Win_Height =600

Sub Create_Window_Main ( )
   Window_Main = Fl_WindowNew2 (Fl_GetW()/2-Win_Lenth/2,Fl_GetH()/2-Win_Height/2,Win_Lenth,Win_Height,"TEST")
   Box1 = Fl_BoxNew (100,110,800,600)
REM Fl_WidgetSetLabelFont (Box1,18)
   Fl_WidgetSetLabelSize (Box1,16)
END SUB

Sub myhandler (byval userdata as any ptr)
 Dim AS double mytimeNOW
 Dim AS string MyYil, MyAy, MyKun, MyHour, MyMinute, MySecond
  mytimeNOW = NOW ()
  MyYil = str (Year (mytimeNOW))
  MyAy  = str (Month (mytimeNOW))
  MyKun = str (Day (mytimeNOW))
  MyHour= str (Hour (mytimeNOW))
  MyMinute=str (Minute (mytimeNOW))
  MySecond=str (Second (mytimeNOW))
  MytimeString = "NOW YEAR "+MyYil+"  Month "+MyAy+"  Day "+MyKun+"  Hour "+MyHour+"  Minute "+MyMinute+"  Second"+MySecond
 Fl_WidgetSetLabel (Box1, MytimeString)
END SUB

Sub mythread (byval userdata as any ptr)
Start:
  Fl_RepeatTimeout(1, @myhandler)
GOTO Start
END SUB

Create_Window_Main ( )
Fl_WindowShow (Window_Main)
MythreadNO = ThreadCreate(@mythread, 0)
Fl_Run
END
taijinantangXX
Posts: 13
Joined: Dec 19, 2014 5:29

Re: Question about FLTK and FreeBASIC

Postby taijinantangXX » Aug 07, 2018 18:26

WQ1980

Thank you for your help.
But your code can't run on my machine. The system reported "segment fault, core dumped."
badidea
Posts: 820
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Question about FLTK and FreeBASIC

Postby badidea » Aug 07, 2018 18:35

taijinantangXX wrote:WQ1980

Thank you for your help.
But your code can't run on my machine. The system reported "segment fault, core dumped."

Here too:

Code: Select all

[xcb] Unknown request in queue while dequeuing
[xcb] Most likely this is a multi-threaded client and XInitThreads has not been called
[xcb] Aborting, sorry about that.
clock: ../../src/xcb_io.c:179: dequeue_pending_request: Assertion `!xcb_xlib_unknown_req_in_deq' failed.
Aborted (core dumped)
MrSwiss
Posts: 2679
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Question about FLTK and FreeBASIC

Postby MrSwiss » Aug 07, 2018 22:02

The reason for seg-fault(ing) may be: there is nothing stopping, the "active" thread,
before closing the program (End -- statement).

Code: Select all

ThreadWait(MythreadNO)  ' waits for thread termination
End
grindstone
Posts: 493
Joined: May 05, 2015 5:35
Location: Germany

Re: Question about FLTK and FreeBASIC

Postby grindstone » Aug 08, 2018 10:27

On my machine (WinXP/32) WQ1980's code works.

Commenting out the Fl_SetFont statement causes a segmentation fault.
taijinantangXX
Posts: 13
Joined: Dec 19, 2014 5:29

Re: Question about FLTK and FreeBASIC

Postby taijinantangXX » Aug 14, 2018 12:32

grindstone wrote:On my machine (WinXP/32) WQ1980's code works.

Commenting out the Fl_SetFont statement causes a segmentation fault.


I've tested : Yes, On WinXP it works fine (just as my wish - parsing time second by second). But, it didn't work on Linux.

(BTW: The error is not caused by the REM...)
taijinantangXX
Posts: 13
Joined: Dec 19, 2014 5:29

Re: Question about FLTK and FreeBASIC

Postby taijinantangXX » Aug 14, 2018 12:33

badidea wrote:Yep, without sleep, the computer is probably to busy looping (throwing all its computing power at this loop), no time for graphics updates. Sleep 1,1 is probably also OK.

Edit: Not sure if sleep is needed if Fl_wait is also used.

Yes sleep is VERY IMPORTANT in such programme.
taijinantangXX
Posts: 13
Joined: Dec 19, 2014 5:29

Re: Question about FLTK and FreeBASIC

Postby taijinantangXX » Aug 14, 2018 12:36

badidea wrote:
taijinantangXX wrote:WQ1980

Thank you for your help.
But your code can't run on my machine. The system reported "segment fault, core dumped."

Here too:

Code: Select all

[xcb] Unknown request in queue while dequeuing
[xcb] Most likely this is a multi-threaded client and XInitThreads has not been called
[xcb] Aborting, sorry about that.
clock: ../../src/xcb_io.c:179: dequeue_pending_request: Assertion `!xcb_xlib_unknown_req_in_deq' failed.
Aborted (core dumped)


I've found what is wrong with the program by surfing internet day by day, reading articles about THREAD and FLTK.
taijinantangXX
Posts: 13
Joined: Dec 19, 2014 5:29

Re: Question about FLTK and FreeBASIC

Postby taijinantangXX » Aug 14, 2018 12:41

I've found the best solution by surfing the Internet day by day. I combined my previous code with WQ1980's code. NOW, this programme can be compiled on both Windows and Linux machines, and works fine(just as my wish - parsing time second by second).

Code: Select all

REM (Windows) fbc -s gui xxx.bas
REM (Linux)   fbc -s gui xxx.bas -l X11

#include once "fltk-c.bi"
#include once "vbcompat.bi"
#IFDEF __FB_LINUX__
#include once "X11/Xlib.bi"
#include once "crt/pthread.bi"
#ENDIF

DIM Shared AS Fl_Window ptr Window_Main
DIM Shared AS Fl_Widget ptr Box1
DIM Shared AS integer Win_Lenth
DIM Shared AS integer Win_Height
DIM Shared AS String MytimeString
DIM Shared AS Any Ptr MythreadNO

#IFDEF __FB_LINUX__
XInitThreads()
#ENDIF

Win_Lenth = 800
Win_Height =600

Sub Create_Window_Main ( )
   Window_Main = Fl_WindowNew2 (Fl_GetW()/2-Win_Lenth/2,Fl_GetH()/2-Win_Height/2,Win_Lenth,Win_Height,"Test")
   Box1 = Fl_BoxNew (100,110,800,600)
   Fl_WidgetSetLabelSize (Box1,16)
END SUB

Sub myhandler (byval userdata as any ptr)
 Dim AS double mytimeNOW
 Dim AS string MyYil, MyAy, MyKun, MyHour, MyMinute, MySecond
  mytimeNOW = NOW ()
  MyYil = str (Year (mytimeNOW))
  MyAy  = str (Month (mytimeNOW))
  MyKun = str (Day (mytimeNOW))
  MyHour= str (Hour (mytimeNOW))
  MyMinute=str (Minute (mytimeNOW))
  MySecond=str (Second (mytimeNOW))
  MytimeString = "NOW is: "+MyYil+Chr(45)+MyAy+Chr(45)+MyKun+" "+MyHour+Chr(58)+MyMinute+Chr(58)+MySecond
 Fl_WidgetSetLabel (Box1, MytimeString)
END SUB

#IFDEF __FB_LINUX__
Sub mythread (byval userdata as any ptr)
Start:
  Fl_RepeatTimeout(1, @myhandler)
  sleep 1000\20, 1
GOTO Start
END SUB
#ENDIF

#IFDEF __FB_WIN32__
Sub mythread (ByVal userdata As Any Ptr)
   'Start:
   'GoTo Start
   While Fl_Wait()
      Fl_RepeatTimeout(1,Cast (Any Ptr,@myhandler))
      Sleep 1000\20, 1
   Wend
End Sub
#ENDIF

Create_Window_Main ( )
Fl_WindowShow (Window_Main)
MythreadNO = ThreadCreate(@mythread, 0)
Fl_Run
END
taijinantangXX
Posts: 13
Joined: Dec 19, 2014 5:29

Re: Question about FLTK and FreeBASIC

Postby taijinantangXX » Aug 14, 2018 12:43

And another version with Chinese characters (So you can see the error is not caused by REMs)

Code: Select all

REM 编译方法(Windows) fbc -s gui xxx.bas
REM 编译方法(Linux)   fbc -s gui xxx.bas -l X11

#include once "fltk-c.bi"
#include once "vbcompat.bi"
#IFDEF __FB_LINUX__
#include once "X11/Xlib.bi"
#include once "crt/pthread.bi"
#ENDIF

DIM Shared AS Fl_Window ptr Window_Main
DIM Shared AS Fl_Widget ptr Box1
DIM Shared AS integer Win_Lenth
DIM Shared AS integer Win_Height
DIM Shared AS String MytimeString
DIM Shared AS Any Ptr MythreadNO

#IFDEF __FB_LINUX__
XInitThreads()
#ENDIF

Fl_SetFont (18,"SimHei")

Win_Lenth = 800
Win_Height =600

Sub Create_Window_Main ( )
   Window_Main = Fl_WindowNew2 (Fl_GetW()/2-Win_Lenth/2,Fl_GetH()/2-Win_Height/2,Win_Lenth,Win_Height,"测试")
   Box1 = Fl_BoxNew (100,110,800,600)
   Fl_WidgetSetLabelFont (Box1,18)
   Fl_WidgetSetLabelSize (Box1,16)
END SUB

Sub myhandler (byval userdata as any ptr)
 Dim AS double mytimeNOW
 Dim AS string MyYil, MyAy, MyKun, MyHour, MyMinute, MySecond
  mytimeNOW = NOW ()
  MyYil = str (Year (mytimeNOW))
  MyAy  = str (Month (mytimeNOW))
  MyKun = str (Day (mytimeNOW))
  MyHour= str (Hour (mytimeNOW))
  MyMinute=str (Minute (mytimeNOW))
  MySecond=str (Second (mytimeNOW))
  MytimeString = "现在是:"+MyYil+"年"+MyAy+"月"+MyKun+"日 "+MyHour+"时"+MyMinute+"分"+MySecond+"秒"
 Fl_WidgetSetLabel (Box1, MytimeString)
END SUB

#IFDEF __FB_LINUX__
Sub mythread (byval userdata as any ptr)
Start:
  Fl_RepeatTimeout(1, @myhandler)
  sleep 1000\20, 1
GOTO Start
END SUB
#ENDIF

#IFDEF __FB_WIN32__
Sub mythread (ByVal userdata As Any Ptr)
   'Start:
   'GoTo Start
   While Fl_Wait()
      Fl_RepeatTimeout(1,Cast (Any Ptr,@myhandler))
      Sleep 1000\20, 1
   Wend
End Sub
#ENDIF

Create_Window_Main ( )
Fl_WindowShow (Window_Main)
MythreadNO = ThreadCreate(@mythread, 0)
Fl_Run
END
MrSwiss
Posts: 2679
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Question about FLTK and FreeBASIC

Postby MrSwiss » Aug 14, 2018 13:44

Just a reminder:
the thread, you are starting (the one extern, to FLTK) should still be stopped,
before program exit, unless you want to on purpose, create memory leaks ...

Using either:
ThreadDetach(), or ThreadWait()

Then:
using a Mutex, is also considered good practice, whenever threads are involved.
See relevant parts in the FB-manual and the attached examples.

Return to “Libraries”

Who is online

Users browsing this forum: No registered users and 1 guest