Maya Grab [Day 10] – UI subroutines

Today I’ve wrote some simple routines for building up the game UI. I’m simply amazed how printing to screen is slow in BASIC. But first let’s take a look at some other modifications I’ve also done.

1500 :
1505 REM – DEFINES ——————–
1510 :
1515 V=53248: S=54272: C=646
1520 MA=780: MX=781: MY=782 MS=783
1525 CB=12: CF=0: FW=27: FC=2: FH=19
1530 PI=1: PD=11: PE=20: PM=22
1535 SL=100: SH=180

Here I’ve stored the address for changing the text color 646 ($0286). Then I needed to access another register in machine code for a routine that I’ve found for placing the cursor at specific position (783). But, the routine was buggy, crashed my emulator, so I’ve replaced it with another one, and this variable will be probably removed at the end. The color of the frame (in which the rooms are drawn into) is stored in FC. And I’ve renamed PO to PI (items Y position) and PI to PE (user input Y position).

4000 :
4005 REM – GAME VARIABLES ————-
4010 :
4015 PR=0: PT=0: PC=0: PP=0
4020 DIM PI$(IS): DIM RF(RS)
4025 :
4030 FOR I=0 TO IS1: PI$(I)=“”: NEXT
4035 FOR I=0 TO RS1: RF(I)=0: NEXT

Last two lines are new; two loops for reseting players inventory and room states if the game should be restarted. The initial plan was to write a routine for this, but after these definitions, the common variables are initialized (line 4500) and the program enters the main loop (line 5000); meaning the whole game can be reseted by just jumping to line 4000, without subroutines.

11000 :
11005 REM – POSITION CURSOR ———–
11010 :
11015 POKE 211,X :POKE 214,Y
11020 SYS 58640
11025 RETURN

This routine places the cursor at position given by X and Y variables. The parameters must be written to addresses 211 ($00D3) and 214 ($00D6) before calling a system machine code routine at address 58640 ($E510).

11500 :
11505 REM – DRAW FRAME —————-
11510 :
11515 X=0: Y=0: GOSUB 11000
11520 POKE C,FC
11525 :
11530 PRINT“[RVS][SPCx12]”;
11535 PRINT“[SPCx17]”
11540 FOR I=0 TO FH2
11545 PRINT“[RVS] “ SPC(FW2) ” “
11550 NEXT
11555 PRINT“[RVS][SPCx12]”;
11560 PRINT“[SPCx17]”
11565 RETURN

This routine draws the frame around a room. The cursor is positioned, the color is set and first the upper bar is drawn, then the side bars in a loop, and at the end the bottom bar. It’s very slow, even if the content of the frame in the middle isn’t cleared.

12500 :
12505 REM – PRINT ITEMS —————
12510 :
12515 X=FW+1: Y=PI: GOSUB 11000
12520 POKE C,14
12525 PRINT IM$(0)
12530 :
12535 POKE C,1
12540 FOR I=0 TO 4
12545 PRINT “[CRDx2][CRLx12]”;
12550 PRINT RO$(PR,I)
12555 NEXT
12560 RETURN

This routine prints items for the current room. Again, the cursor is positioned and the color is set for the label which is printed first. Then, in a loop, the items are printed underneath the label. I’ve tried first to position every item with the subroutine I’ve made, but that was very slow. The fastest way was to do a line feed after every item, and then roll back the cursor to the next item. Tricky but much faster.

13500 :
13505 REM – PRINT DIRECTIONS ———-
13510 :
13515 X=FW+1: Y=PD: GOSUB 11000
13520 POKE C,14
13525 PRINT IM$(1)
13530 :
13535 POKE C,1
13540 FOR I=0 TO 4
13545 PRINT “[CRDx2][CRLx12]”;
13550 PRINT RD$(PR,I)
13555 NEXT
13560 RETURN

Same stuff here for directions for the current room, and of course everything is printed at a different position, underneath the items.

14000 :
14005 REM – CLEAR INPUT —————
14010 :
14015 X=0: Y=PM: GOSUB 11000
14020 PRINT“[SPCx19]”;
14025 PRINT“[SPCx20]”
14030 RETURN

This routine clears the whole line where the player is entering commands.

14500 :
14505 REM – WAIT FOR INPUT ————
14510 :
14515 A$=“”: X$=“”: Y$=” “
14520 A=402LEN(IM$(2))
14525 :
14530 X=0: Y=PE: GOSUB 11000
14535 POKE C,7
14540 PRINT IM$(2);
14545 POKE 204,0
14550 :

I’ve updated the input subroutine to appear at the correct position and to show the correct label with appropriate color.

15000 :
15005 REM – CLEAR MESSAGE ————-
15010 :
15015 X=0: Y=PM: GOSUB 11000
15020 PRINT“[SPCx19]”;
15025 PRINT“[SPCx20]”
15030 RETURN

This routine clears the whole line where the interpreter is writing messages to the user.

15500 :
15505 REM – PRINT MESSAGE ————-
15510 :
15515 X=0: Y=PM: GOSUB 11000
15520 PRINT A$
15525 RETURN

This routine prints a message stored in A$. The colors will be also set in the message itself since multicolor support is needed.

Program size: 14979 bytes, on disk 12446 bytes or 49 blocks
Free BASIC memory: 23932 bytes
Used BASIC commands: SYS, FOR, TO, NEXT, SPC

Source code

* * * * *