Thursday, January 14, 2016

Tiny BASIC part 5 - Adding color to PSET

It's much more fun to draw in color than with only a black pen, so let's add a third parameter to the PSET statement for color, for example:
PSET x, y, "color"
To do this we need to add some code to our  case "pset"  block:
 CASE "pset"
  IF GWINOPEN = 0 THEN
    E$ = "PSET error - Graphic window is not open"
    GOTO [Ready]
  END IF
  GOSUB [GetExpression]
  IF E$<>"" THEN [Ready]
  PSETX = N
  GOSUB [GetChar]
  IF C$ <> "," THEN
   E$= "Comma expected after x parameter"
   GOTO [Ready]
  END IF
  C = C + 1
  GOSUB [GetExpression]
  IF E$<>"" THEN [Ready]
  PSETY = N
  PSETCOLOR$ = "black"
  GOSUB [GetChar]
  IF C$ = "," THEN
   C = C + 1
   GOSUB [GetStringLiteral]
   IF E$ <> "" THEN [Ready]
   PSETCOLOR$ = B$
  END IF
  #GWIN "color "; PSETCOLOR$

  #GWIN "down ; set "; PSETX; " "; PSETY
  GOTO [FinishStatement]

The way this works is that it will look for a comma and a string expression after it sets PSETX and PSETYIf there is no comma it will skip over the part that gets a color parameter, so the default color will be black in that case.

The string will be the name of a valid Liberty BASIC color, for example red, blue, green, black, etc.    The following code is stolen from the routine that parses for the PRINT statement.  In a later post we will incorporate this into the PRINT code by calling it as a subroutine so that we won't have the same code twice.
[GetStringLiteral]
  GOSUB [SkipSpace]
  GOSUB [GetChar]
  IF C$=G$ THEN
   B$=""
[NextStringMember]
   C = C + 1 : C$=MID$(A$,C,1)
   IF C$="" THEN
    E$="Unterminated string"
    RETURN
   ELSE
   IF C$<>G$ THEN
    B$=B$+C$
    GOTO [NextStringMember]
   END IF
  END IF
  C = C + 1 : C$=MID$(A$,C,1)
  IF C$=G$ THEN
   B$=B$+C$
   GOTO [NextStringMember]
  END IF
 END IF
 RETURN

So we call the [GetStringLiteral] subroutine and check E$ for an error.  If there is none then we set PSETCOLOR$ to the value of B$ as shown here.

   GOSUB [GetStringLiteral]
   IF E$ <> "" THEN [Ready]
   PSETCOLOR$ = B$
  END IF
  #GWIN "color "; PSETCOLOR$


Then we add a drawing command like so to set the color:

  #GWIN "color "; PSETCOLOR$

Here is a sample that uses PSET with color!


5 graphicwin
10 pset x, y, "red"
20 pset x + 10, y, "blue"
30 pset x + 20, y, "green"
40 x = x + 1
50 y = y + 2
60 if x < 100 then goto 10

And here is a screenshot!