QB64 Phoenix Edition
drawGO - Printable Version

+- QB64 Phoenix Edition (https://qb64phoenix.com/forum)
+-- Forum: QB64 Rising (https://qb64phoenix.com/forum/forumdisplay.php?fid=1)
+--- Forum: Code and Stuff (https://qb64phoenix.com/forum/forumdisplay.php?fid=3)
+---- Forum: Works in Progress (https://qb64phoenix.com/forum/forumdisplay.php?fid=9)
+---- Thread: drawGO (/showthread.php?tid=863)



drawGO - James D Jarvis - 09-06-2022

drawGO v0.1     
a really simple interpreter to evaluate DRAW commands without having to compile a basic program.
it technically supports all draw commands but entering a few of them can be a bit tricky so the interpreter has a few commands to deal with that and a handful of editing commands. 
The interpreter can save and load lists of cod to and from a text file.
There's a very simple HELP listing.
commands added to draw:  
setX <n> sets the X position of the pen to coordinate n
setY <n> sets the X position of the pen to coordinate n
                     setX and setY will move the draw pen without drawing a line. the program can't track pen movements                           from within draw commands.
setRED <n> set the red value of the 32 bit color
setGREEN <n> set the red value of the 32 bit color
setBLUE <n> set the red value of the 32 bit color
Circle <n>     draw a circle of radius r , restricted to current color and the position established by setX and setY commands    
PRINT   will print all characters on the line following the print command to the coordinated set by setX and setY.

editor commands (not embedded in the saved code)
back          goes back one code step erasing that line of code
list           to list the code
go or redraw to execute the entered code
save and load    to save and load a text file and save it into the drawGO code. 

 
Code: (Select All)
'drawGO v0.1
'
'a simple interpreter to evaluate draw commands  with a little bit extra functionality.
'
Screen _NewImage(800, 500, 32)
'$dynamic
Dim c$(100), tt$(0)
Dim cred&, cgreen&, cblue&
Dim dklr As _Unsigned Long
Dim eklr As _Unsigned Long
cred& = 250
cgreen& = 250
cblue& = 250
dklr = _RGB32(cred&, cgreen&, cblue&)
eklr = _RGB32(250, 250, 250)
D$ = ""
n = 0
T$ = ""
varX = 400
varY = 250
Draw "bm400,250"
Do
    If Len(a$) > 0 Then
        n = n + 1
        c$(n) = a$
    End If
    Draw a$
    Line Input a$
    If a$ = "cls" Then
        Cls
        a$ = ""
    End If
    If a$ = "redraw" Or a$ = "go" Then
        Cls
        varX = 400: varY = 250
        Color eklr
        dklr = _RGB32(250, 250, 250)
        Draw "c" + Str$(_RGB32(250, 250, 250))
        Draw "bm400,250"
        For x = 1 To n
            If LCase$(Left$(c$(x), 4)) = "call" Then 'processing CALL functions

                a$ = Right$(c$(x), Len(c$(x)) - 4)
                If Left$(a$, 6) = "circle" Then
                    B$ = Right$(a$, Len(a$) - 6)
                    r = Val(B$)
                    PSet Step(0, 0)
                    Circle Step(0, 0), r
                End If
                If Left$(a$, 5) = "print" Then
                    B$ = Right$(a$, Len(a$) - 5)
                    Color dklr
                    _PrintString (varX, varY), B$
                    Color eklr
                    n = n + 1
                End If
                If Left$(a$, 4) = "home" Then
                    Draw "bm" + Str$(varX) + "," + Str$(varY)
                    n = n + 1
                End If
                If LCase$(Left$(a$, 4)) = "setx" Then
                    B$ = _Trim$(Right$(a$, Len(a$) - 4))
                    varX = Val(B$)
                    Draw "bm" + Str$(varX) + "," + Str$(varY)
                    n = n + 1
                End If
                If LCase$(Left$(a$, 4)) = "sety" Then
                    B$ = _Trim$(Right$(a$, Len(a$) - 4))
                    varY = Val(B$)
                    Draw "bm" + Str$(varX) + "," + Str$(varY)
                    n = n + 1
                End If
                If LCase$(Left$(a$, 6)) = "setred" Then
                    B$ = _Trim$(Right$(a$, Len(a$) - 6))
                    cred& = Val(B$)
                    dklr = _RGB32(cred&, cgreen&, cblue&)
                    Draw "c" + Str$(_RGB32(cred&, cgreen&, cblue&))
                    n = n + 1
                    a$ = ""
                End If
                If LCase$(Left$(a$, 8)) = "setgreen" Then
                    B$ = _Trim$(Right$(a$, Len(a$) - 8))
                    cgreen& = Val(B$)
                    dklr = _RGB32(cred&, cgreen&, cblue&)
                    Draw "c" + Str$(_RGB32(cred&, cgreen&, cblue&))
                    n = n + 1
                    a$ = ""
                End If
                If LCase$(Left$(a$, 7)) = "setblue" Then
                    B$ = _Trim$(Right$(a$, Len(a$) - 7))
                    cblue& = Val(B$)
                    Draw "c" + Str$(_RGB32(cred&, cgreen&, cblue&))
                    Color dklr
                    n = n + 1
                    a$ = ""
                End If


                a$ = ""
            Else
                Draw c$(x)
            End If
        Next x
        a$ = ""
    End If

    If a$ = "list" Then
        Print "command list"
        For x = 1 To n

            Print x, c$(x)
        Next
        a$ = ""
    End If
    If a$ = "quit" Then
        T$ = "quit"
        a$ = ""
    End If
    If a$ = "clear all" Then
        ReDim c$(100)
        a$ = ""
        n = 0
    End If
    If a$ = "back" Then
        c$(n) = ""
        n = n - 1
        a$ = ""
    End If
    If Left$(a$, 6) = "circle" Then
        B$ = Right$(a$, Len(a$) - 6)
        r = Val(B$)
        PSet Step(0, 0)
        Circle Step(0, 0), r
        n = n + 1
        c$(n) = "CALL" + a$
        a$ = ""
    End If
    If Left$(a$, 5) = "print" Then
        B$ = Right$(a$, Len(a$) - 6)
        _PrintString (varX, varY), B$
        n = n + 1
        c$(n) = "CALL" + a$
        a$ = ""
    End If
    If Left$(a$, 5) = "join" Then
        ReDim tt$(n)
        tn = 1
        For m = 1 To n
            If Left$(c$(m), 4) <> "CALL" Then
                If lst$ = "call" Then tn = tn + 1
                tt$(tn) = tt$(tn) + c$(m)
                lst$ = "draw"
            Else
                tn = tn + 1
                tt$(tn) = c$(m)
                lst$ = "call"
            End If
        Next m
        ReDim c$(100)
        For x = 1 To tn
            c$(x) = tt$(x)
        Next x
        n = tn
        a$ = ""
    End If
    If Left$(a$, 4) = "save" Then
        B$ = _Trim$(Right$(a$, Len(a$) - 4))
        Print B$
        n = n + 1
        c$(n) = "END"
        Open B$ For Output As #1
        For x = 1 To n

            If c$(x) <> "" Then Write #1, c$(x)
        Next x
        Close #1
        a$ = ""
    End If
    If Left$(a$, 4) = "load" Then
        B$ = _Trim$(Right$(a$, Len(a$) - 4))
        Open B$ For Input As #1
        n = 0
        Do
            n = n + 1
            Input #1, c$(n)
        Loop Until c$(n) = "END"
        Close #1
        n = n - 1
        a$ = ""
    End If
    If LCase$(Left$(a$, 4)) = "setx" Then
        B$ = _Trim$(Right$(a$, Len(a$) - 4))
        varX = Val(B$)
        Draw "bm" + Str$(varX) + "," + Str$(varY)
        n = n + 1
        c$(n) = "CALL" + a$

        a$ = ""
    End If
    If LCase$(Left$(a$, 4)) = "sety" Then
        B$ = _Trim$(Right$(a$, Len(a$) - 4))
        varY = Val(B$)
        Draw "bm" + Str$(varX) + "," + Str$(varY)
        n = n + 1
        c$(n) = "CALL" + a$

        a$ = ""
    End If
    If Left$(a$, 4) = "home" Then
        n = n + 1
        Draw "bm" + Str$(varX) + "," + Str$(varY)
        c$(n) = "CALLhome"
        a$ = ""
    End If
    If LCase$(Left$(a$, 7)) = "callcls" Then
        n = n + 1
        c$(n) = "CALLcls"
        a$ = ""
    End If
    If LCase$(Left$(a$, 6)) = "setred" Then
        n = n + 1
        B$ = _Trim$(Right$(a$, Len(a$) - 6))
        c$(n) = "CALLSETRED" + B$
        cred& = Val(B$)
        dklr = _RGB32(cred&, cgreen&, cblue&)
        Draw "c" + Str$(_RGB32(cred&, cgreen&, cblue&))
        a$ = ""
    End If
    If LCase$(Left$(a$, 8)) = "setgreen" Then
        n = n + 1
        B$ = _Trim$(Right$(a$, Len(a$) - 8))
        c$(n) = "CALLSETGREEN" + B$
        cgreen& = Val(B$)
        dklr = _RGB32(cred&, cgreen&, cblue&)
        Draw "c" + Str$(_RGB32(cred&, cgreen&, cblue&))
        a$ = ""
    End If
    If LCase$(Left$(a$, 7)) = "setblue" Then
        n = n + 1
        B$ = _Trim$(Right$(a$, Len(a$) - 7))
        c$(n) = "CALLSETBLUE" + B$
        cblue& = Val(B$)
        dklr = _RGB32(cred&, cgreen&, cblue&)
        Draw "c" + Str$(_RGB32(cred&, cgreen&, cblue&))
        a$ = ""
    End If





    If Left$(a$, 4) = "help" Then
        Cls
        Print "HELP"
        Print "================="
        Print "edit commands"
        Print "================="
        Print "join    compress code by joinign code line that only contain draw statements"
        Print "save     saves code to a file name"
        Print "load     load code from a file name"
        Print "print    print text at varX,varY"
        Print "cls      clear the screen"
        Print "back     step back and erase last line of code"
        Print "list     list program code"
        Print "redraw or go      to execute code"
        Print ""
        Print "Command words"
        Print "==============================="
        Print "setX     set the value of X"
        Print "setY     set the value of Y"
        Print "circle n draw a circle with a radius of numerical value n at current position"
        Print "home     sets the draw postions to varX and VarY"
        Print "<draw commands> supports all draw commands"
        Print "setred n     set red value to n"
        Print "setgreen n   set green value to n"
        Print "setblue n    set blue value to n"
        Print "CALLcls   call cls from code"

        a$ = ""

    End If


Loop Until T$ = "quit"
Print
Print "command list"
For x = 1 To n

    Print x, c$(x)
Next



RE: drawGO - mnrvovrfc - 09-07-2022

You know, I've been fighting with myself to post here a program that does something similar. My program has a draw area at the top and an user input area at the bottom. The user could type in the string as he/she would in a BASIC program, and my program would react to it straight away, like the QB64 editor in older times. Punch a key and the user could use the arrow keys to draw, and my program would employ some A.I. to keep the "DRAW" string tidy about it. Punch the same key and it returned to boring user input mode.

It's still W.I.P. and I have to go find it in my backups. I decided to do it in 32-bit color, which affected how the "C" commands were interpreted. If I'm not mistaken I decided to treat it like SCREEN 12, having my program load in a separate text file which indicates the red, green, blue attributes for each of 16 colors. It was to keep the program simple and in case the "DRAW" string was to be included for SCREEN 12 or something else.


RE: drawGO - James D Jarvis - 09-07-2022

I've been messing about with means to save mouse and direction-key created vector drawings into DRAW statements and it's a bear, the biggest issue I've had is keeping track of all the inputs and cleaning up redundant points. draw GO is likely to get more elaborate but is just meant to be a little handy tool to create block of code to use elsewhere. Just after I posted this I realized I could "embed" the output portion of interpreter in another program to make the use of that code even easier.

The 32-bit colors can be a bother to use in DRAW strings because of all the commas and quotation marks used in the notation.