Charger - A General FreeBASIC Framework

User projects written in or related to FreeBASIC.
SoruCoder
Posts: 4
Joined: Mar 20, 2020 10:31

Charger - A General FreeBASIC Framework

Postby SoruCoder » Mar 29, 2020 7:48

It's very early in development, but I wanted to put it out there for those who would like to contribute and/or use.
The Github can be found here
dodicat
Posts: 6363
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Charger - A General FreeBASIC Framework

Postby dodicat » Mar 29, 2020 20:25

I haven't done much work with FreeBASIC frames.
I only got five done.

Code: Select all


Type frame
    Private:
    As Any Ptr i
    As Long x,y
    As Long w,h
    Declare Sub show()
    Declare Function map(a As Single,b As Single,x As Single,c As Single,d As Single) As Single
    Declare Function overlapped(() As frame) As Long
    Public:
    Declare Constructor
    Declare Constructor(W As Long,h As Long,col As Ulong,px As Long,py As Long)
    Declare Sub MoveByMouse(As Long, As Long, As Long,x() As frame)
    Declare Sub showscreen(() As Frame)
    Declare Function InImage(As Long,As Long) As Long
    Declare Sub destroy(() As frame)
End Type

Constructor frame
If Screenptr=0 Then Screen 20,32:Windowtitle "MOVE FRAMES WITH THE MOUSE"
End Constructor

Constructor frame(Wd As Long,hi As Long,col As Ulong,px As Long,py As Long)
If Screenptr=0 Then this.constructor
If i=0 Then i=Imagecreate(wd,hi)
x=px:y=py
w=Wd:h=Hi
Var b=(h+w)/2
For k As Single=1 To b\10
    Var z=map(1,b/10,k,1,.1)
    Var r=Cptr(Ubyte Ptr,@col)[2]
    Var g=Cptr(Ubyte Ptr,@col)[1]
    Var b=Cptr(Ubyte Ptr,@col)[0]
    Line i,(k,k)-(w-k,h-k),Rgb(r*z,g*z,b*z),b
Next k
End Constructor

Sub frame.show()
    Put(x,y),i,trans
End Sub

Function frame.Overlapped(f() As Frame) As Long
    Dim As Long px(1 To 8),py(1 To 8)
    For n As Long=1 To Ubound(f)
        px(1)=f(n).x:py(1)=f(n).y
        px(2)=f(n).x+f(n).w:py(2)=f(n).y
        px(3)=f(n).x+f(n).w:py(3)=f(n).y+f(n).h
        px(4)=f(n).x:py(4)=f(n).y+f(n).h
       
        px(5)=(px(1)+px(2))/2:py(5)=(py(1)+py(2))/2
        px(6)=(px(2)+px(3))/2:py(6)=(py(2)+py(3))/2
        px(7)=(px(3)+px(4))/2:py(7)=(py(3)+py(4))/2
        px(8)=(px(4)+px(1))/2:py(8)=(py(4)+py(1))/2
        For n2 As Long=1 To 8
            For k As Long=1 To Ubound(f)
                If f(k).InImage(px(n2),py(n2)) Then Return -1
            Next k
        Next n2
    Next n
    Return 0
End Function

Function frame.map(a As Single,b As Single,x As Single,c As Single,d As Single) As Single
    Return ((d)-(c))*((x)-(a))/((b)-(a))+(c)
End Function

Sub frame.showscreen(n() As frame)
    Screenlock
    Cls
    For m As Long=1 To Ubound(n)
        n(m).show
    Next m
    Screenunlock
    Sleep 1,1
End Sub

Sub Frame.MoveByMouse(mx As Long,my As Long,button As Long,n() As frame)
    Dim As Long x1=mx,y1=my,dx,dy,b
    Dim As Long idx=x-mx,idy=y-my
    While button = 1
        showscreen(n())
        Getmouse mx,my,,button
        If mx>0 And my>0 Then
            If mx<>x1 Or my<>y1  Then
                dx = mx-x1
                dy = my-y1
                x1 = mx
                y1 = my
                Var i2=This
                x=x1+dx+idx
                y=y1+dy+idy
                If Overlapped(n()) Then This=i2:Exit Sub
            End If
        End If
    Wend
End Sub

Function Frame.InImage(mx As Long,my As Long) As Long
    Return mx<x+w Andalso mx>x Andalso my<y+h And my>y
End Function

Sub frame.destroy(f() As frame)
    For n As Long=1 To Ubound(f)
        Imagedestroy f(n).i
        Print n
    Next n
End Sub


Dim As frame f(1 To 5)
f(1)=frame(200,100,Rgb(200,0,100),40,40)
f(2)=frame(200,150,Rgb(0,200,0),140,140)
f(3)=frame(100,300,Rgb(0,0,200),340,400)
f(4)=frame(200,300,Rgb(0,100,255),540,400)
f(5)=frame(300,50,Rgb(200,100,0),640,40)

Dim As Long mx,my,mb

Do
    Getmouse mx,my,,mb
    For n As Long=1 To Ubound(f)
        If f(n).InImage(mx,my) Then f(n).MoveByMouse(mx,my,mb,f())
    Next n
    Type<frame>.showscreen(f())
Loop Until Len(Inkey)

Type<frame>.destroy(f())
Sleep


 
angros47
Posts: 1614
Joined: Jun 21, 2005 19:04

Re: Charger - A General FreeBASIC Framework

Postby angros47 » Mar 29, 2020 20:54

Are you sure you linked the right repository? All I can see in that github is the license file, and nothing else
badidea
Posts: 1995
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Charger - A General FreeBASIC Framework

Postby badidea » Mar 29, 2020 21:06

angros47 wrote:Are you sure you linked the right repository? All I can see in that github is the license file, and nothing else

Development branch: https://github.com/sorucoder/Charger/tree/develop
paul doe
Posts: 1175
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: Charger - A General FreeBASIC Framework

Postby paul doe » Mar 29, 2020 23:25

Oh? A Unicode string class? My, that could prove very useful, indeed... And the 1.5 Mb of source for the class proves why nobody wanted to do this XD

Many thanks!
caseih
Posts: 1484
Joined: Feb 26, 2007 5:32

Re: Charger - A General FreeBASIC Framework

Postby caseih » Mar 30, 2020 0:05

Are you auto-generating the case folding code? Sure seems like most of that giant select case block for case folding could be made into a simple function with a large look-up table. Would be more future-proof also as new languages are added to unicode.
SoruCoder
Posts: 4
Joined: Mar 20, 2020 10:31

Re: Charger - A General FreeBASIC Framework

Postby SoruCoder » Mar 30, 2020 18:34

caseih wrote:Are you auto-generating the case folding code? Sure seems like most of that giant select case block for case folding could be made into a simple function with a large look-up table. Would be more future-proof also as new languages are added to unicode.


Yes, in fact, most of the static functions are. I used a Python script to do so. However, I haven't saved that source, so maybe in the future I shall write that tool again and post it to the Github, that way it could be kept up to date.

EDIT- The lookup table approach I think would be memory hungry, wouldn't it? Also, not every codepoint maps to a single codepoint, so that's a lot of memory either on the stack or the heap.
Last edited by SoruCoder on Mar 30, 2020 18:53, edited 1 time in total.
SoruCoder
Posts: 4
Joined: Mar 20, 2020 10:31

Re: Charger - A General FreeBASIC Framework

Postby SoruCoder » Mar 30, 2020 18:45

paul doe wrote:Oh? A Unicode string class? My, that could prove very useful, indeed... And the 1.5 Mb of source for the class proves why nobody wanted to do this XD

Many thanks!


I know right? However, it's still limited by the platform limitations of WString. But, I have made it so that Strings and WStrings preserve information via escape sequences. By the way, any String or WString passed to UnicodeString is parsed as if it were an "!string" literal; with the addition of <\Uxxxxxx>, which parses codepoints 0x100000 to 0x10FFFF regardless of platform, but without the <\"> or <\'> escape sequences, which admittedly can already be achieved by using <""> and <'> respectively. And yes, you're welcome. I do plan to write an encode method so it can finally be written to a file.
caseih
Posts: 1484
Joined: Feb 26, 2007 5:32

Re: Charger - A General FreeBASIC Framework

Postby caseih » Mar 30, 2020 19:17

SoruCoder wrote:EDIT- The lookup table approach I think would be memory hungry, wouldn't it? Also, not every codepoint maps to a single codepoint, so that's a lot of memory either on the stack or the heap.
No, in fact I would think it would be a lot less memory than the current code. Remember, all that select case code has to be in memory to run! A lookup table would be a lot more compact, and likely much faster (lookups vs jumps). Whether it be code or data, it all sits in memory. Just a wild uneducated guess, but I think a lookup table plus a function would be less than half the space in the binary than the select case code. Anyway something worth considering.
paul doe
Posts: 1175
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: Charger - A General FreeBASIC Framework

Postby paul doe » Mar 30, 2020 23:59

SoruCoder wrote:...
And yes, you're welcome. I do plan to write an encode method so it can finally be written to a file.

Super useful. Terrific work so far.
SoruCoder wrote:...
EDIT- The lookup table approach I think would be memory hungry, wouldn't it? Also, not every codepoint maps to a single codepoint, so that's a lot of memory either on the stack or the heap.

The approach proposed by caseih has its merits. That space can very well be static, so that's not a problem. However, to achieve the same speed as a State Transition Table, you would need a select case as const statement, which isn't actually possible (since such statement supports up to 8192 cases).
Also, by using a STT, you can add support for other codepoints without having to recompile/redistribute the lib, so it is an approach worth considering (particularly since you can't derive from a class that extends WString).
SoruCoder
Posts: 4
Joined: Mar 20, 2020 10:31

Re: Charger - A General FreeBASIC Framework

Postby SoruCoder » Apr 02, 2020 2:49

paul doe wrote:However, to achieve the same speed as a State Transition Table, you would need a select case as const statement, which isn't actually possible (since such statement supports up to 8192 cases).


Actually, this approach is possible because the mapping functions have less than that many cases. Casefolding nets 1,520 codepoints, Lowercasing nets 2,334 codepoints, Uppercasing nets 1,911 codepoints, and Titlecasing nets 1,412 codepoints. Also, I have thought about how to future-proof new Unicode codepoints, and I believe the best solution may be to develop a program which pulls files from the Public Unicode Database and generates the source code necessary. And if it does become to the point where we reach more than 8,192 cases (which doesn't seem likely, as FreeBASIC might have already corrected the issue of the number of cases by the time this happens), then we can retool that program to generate lookup tables. Also, the rest of the methods rely on Boolean statements consisting of individual and ranges of codepoints joined with short-circuiting Boolean operators.
paul doe
Posts: 1175
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: Charger - A General FreeBASIC Framework

Postby paul doe » Apr 02, 2020 3:10

SoruCoder wrote:...
Also, I have thought about how to future-proof new Unicode codepoints, and I believe the best solution may be to develop a program which pulls files from the Public Unicode Database and generates the source code necessary.
...

Indeed, that's a sane approach. Naturally, you're the implementor so you're ultimately entitled to decide how best to lay it out to suit your needs.

Return to “Projects”

Who is online

Users browsing this forum: Google [Bot] and 4 guests