Hires Graphics

Basic and Machine Language

Moderator: Moderators

Post Reply
User avatar
Mike
Herr VC
Posts: 4839
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Two example programs

Post by Mike »

I wrote two small example programs, that use the MINIGRAFIK extension. The first one does 3D-Graphics:

Code: Select all

10 DIMMI(159),MA(159)
11 FORX=0TO159:MI(X)=192:MA(X)=-1:NEXT
12 POKE36879,26:@ON:@CLR
13 FORY=30TO-30STEP-1:XP=INT(2*SQR(900-Y*Y)+.5)
14 FORX=-XPTOXP:XR=X/28:YR=Y/14:R=SQR(XR*XR+YR*YR)
15 F=COS(R)-COS(3*R)/3+COS(5*R)/5-COS(7*R)/7
16 FL=0:XP=80+X-Y:YP=96+2*Y-INT(40*F+.5)
17 IFMI(XP)>YPTHENMI(XP)=YP:FL=1
18 IFMA(XP)<YPTHENMA(XP)=YP:FL=1
19 IFFLANDYP>=0ANDYP<192THEN:@1,XP,YP
20 NEXT:NEXT:POKE36879,27
21 GETA$:IFA$=""THEN21
22 @RETURN
Set aside a pot of coffee, and watch. It runs ~22 Minutes to completion.


The second program portrays the Mandelbrot set:

Code: Select all

10 X=0:Y=0:X2=0:Y2=0:XY=0:R=0:J=0
11 DIMD(2,159),R(159),J(191):A=0:B=1:C=2
12 R0=-2.4:R1=+0.8:DR=(R1-R0)/160:FORS=0TO159:R(S)=R0+DR*S:NEXT
13 J0=-1.25:J1=+1.25:DJ=(J1-J0)/192:FORT=0TO191:J(191-T)=J0+DJ*T:NEXT
14 N=30
15 POKE36879,14:POKE646,1:@ON:@CLR
16 FORT=0TO191:J=J(T):FORS=0TO159:R=R(S):X=R:Y=J
17 FORI=1TON:X2=X*X:Y2=Y*Y:IFX2+Y2<4THENXY=X*Y:X=X2-Y2+R:Y=XY+XY+J:NEXT
18 D(C,S)=I:IFI<=NTHEN:@1,S,T
19 IFS<2ORT<2THEN28
20 IF@(S-1,T-1)=0THEN28
21 M=D(B,S-1)
22 IFD(B,S-2)<MANDD(B,S)<MTHEN27
23 IFD(A,S-2)<MANDD(C,S)<MTHEN27
24 IFD(C,S-2)<MANDD(A,S)<MTHEN27
25 IFD(A,S-1)<MANDD(C,S-1)<MTHEN27
26 GOTO28
27 @0,S-1,T-1
28 NEXTS:Z=A:A=B:B=C:C=Z:NEXT
29 GETA$:IFA$=""THEN29
30 POKE36879,27:POKE646,6:@RETURN
Here you should have more time to spare. It needs roughly 2 1/2 hours to finish. Alternatively run it in VICE and choose 'no speed limit'. ;)

Cheers,

Michael

P.S.: The ':' between THEN and an '@' command is necessary. Otherwise you'll get a ?SYNTAX ERROR. This 'bug' is typical for this kind of BASIC extension.
User avatar
Mike
Herr VC
Posts: 4839
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Another example, with 3 useful sub-routines

Post by Mike »

Time for a bigger program. This example draws a 'minigrafik' logo. First the 'minigrafik' caption is read from DATA statements, and drawn using a line-routine (it's here only used to draw vertical lines).

Then two ellipses are drawn. I've assumed a 1.67:1 ratio for a VIC pixel on a TV set, so these ellipses actually should come out as circles. In VICE they'll appear more flattened.

Finally the gap between these two ellipses is filled with a flood fill routine.

The main program in the lines 1-9 shows, how to call the sub-routines in your own programs.

Greetings,

Michael

Code: Select all

1 REM ** MINIGRAFIK DEMO
2 POKE36879,14:POKE646,7:@ON:@CLR
3 READX:IFX<>-1THENX1=X+31:X2=X1:READA,B:Y1=113-A:Y2=113-B:GOSUB40:GOTO3
4 MX=80:MY=96:A=54:B=90:GOSUB50:A=57:B=95:GOSUB50
5 X=111:Y=173:GOSUB70
6 GETA$:IFA$=""THEN6
7 POKE36879,27:POKE646,6:@RETURN
8 END
9 :
10 DATA1,9,24,2,15,24,3,21,23,4,23,24,5,23,25,6,23,25,7,9,24,8,9,23,9,20,23,10,22,24
11 DATA11,23,25,12,23,25,13,23,25,14,10,24,15,9,22,16,9,10,17,10,11
12 :
13 DATA19,11,22,19,29,30,20,9,24,20,29,30,21,9,10,22,10,11
14 :
15 DATA25,9,24,26,9,24,27,19,22,28,22,24,29,23,25,30,23,25,31,23,25,32,10,24,33,9,22
16 DATA34,9,10,35,10,11
17 :
18 DATA37,11,22,37,29,30,38,9,24,38,29,30,39,9,10,40,10,11
19 :
20 DATA43,13,18,44,2,3,44,11,21,45,1,3,45,10,12,45,20,23,46,1,3,46,9,11,46,22,24,47,1,3
21 DATA47,9,11,47,23,24,48,2,3,48,10,11,48,23,25,49,2,4,49,11,12,49,23,25,50,3,10
22 DATA50,12,15,50,23,25,51,6,25,52,19,24
23 :
24 DATA55,9,24,56,11,24,57,20,23,58,22,24,59,23,25,60,23,25
25 :
26 DATA63,13,18,64,11,22,65,10,12,65,20,23,66,9,11,66,22,24,67,9,11,67,23,24,68,10,12
27 DATA68,23,25,69,11,13,69,23,25,70,10,25,71,9,24,72,9,10,73,10,11
28 :
29 DATA71,1,3,72,1,3,73,1,3,74,2,4,74,23,24,75,3,7,75,23,24,76,4,30,77,11,32,78,23,24
30 DATA78,31,33,79,23,24,79,32,34,80,32,34,81,33,33
31 :
32 DATA82,11,22,82,29,30,83,9,24,83,29,30,84,9,10,85,10,11
33 :
34 DATA88,9,34,89,16,34,90,16,18,90,21,23,91,14,18,91,22,24,92,12,15,92,17,19,92,23,25
35 DATA93,10,13,93,18,20,93,23,25,94,9,11,94,19,24,95,9,11,95,20,23,96,10,11
36 :
37 DATA-1
38 :
39 REM ** LINE DRAW
40 DX=X2-X1:DY=Y2-Y1:IFDX=0ANDDY=0THEN:@1,X1,Y1:RETURN
41 IFABS(DX)<ABS(DY)THEN43
42 M=DY/DX:B=Y1-M*X1+.5:FORX=X1TOX2STEPSGN(X2-X1):@1,X,INT(M*X+B):NEXT:RETURN
43 M=DX/DY:B=X1-M*Y1+.5:FORY=Y1TOY2STEPSGN(Y2-Y1):@1,INT(M*Y+B),Y:NEXT:RETURN
44 :
49 REM ** ELLIPSE DRAW
50 X=0:Y=B:S=B*B:T=A*A*(2*Y-1):U=2*B*B:V=2*A*A:E=0
51 X1=MX+X:F1=X1>=0ANDX1<160
52 Y1=MY+Y:F2=Y1>=0ANDY1<192:IFF1ANDF2THEN:@1,X1,Y1
53 Y2=MY-Y:F3=Y2>=0ANDY2<192:IFF1ANDF3THEN:@1,X1,Y2
54 X2=MX-X:F4=X2>=0ANDX2<160:IFF4ANDF3THEN:@1,X2,Y2
55 IFF4ANDF2THEN:@1,X2,Y1
56 IFX=AANDY=0THENRETURN
57 F=E+S:D=0
58 G=F-T:IFABS(F)>ABS(G)THENF=G:D=1
59 G=E-T:IFABS(F)>ABS(G)THENF=G:D=2
60 E=F
61 IFD<2THENX=X+1:S=S+U
62 IFD>0THENY=Y-1:T=T-V
63 GOTO51
64 :
69 REM ** FLOOD FILL
70 Q$=CHR$(X)+CHR$(Y)
71 IFQ$=""THENRETURN
72 X=ASC(MID$(Q$,1,1)):Y=ASC(MID$(Q$,2,1))
73 Q$=MID$(Q$,3):GOSUB82:IFPTHEN71
74 X=X-1:GOSUB82:IFNOTPTHEN74
75 X=X+1:U=0:D=0
76 @1,X,Y:Y=Y-1:GOSUB82:IFPTHENU=0:GOTO78
77 IFNOTUTHENQ$=Q$+CHR$(X)+CHR$(Y):U=-1
78 Y=Y+2:GOSUB82:IFPTHEND=0:GOTO80
79 IFNOTDTHENQ$=Q$+CHR$(X)+CHR$(Y):D=-1
80 Y=Y-1:X=X+1:GOSUB82:IFNOTPTHEN76
81 GOTO71
82 IFX<0ORX>159ORY<0ORY>191THENP=-1:RETURN
83 P=-@(X,Y):RETURN
84 :
99 REM ** MINIGRAFIK DEMO 2006-03-23 BY MICHAEL KIRCHER
User avatar
Jeff-20
Denial Founder
Posts: 5759
Joined: Wed Dec 31, 1969 6:00 pm

Post by Jeff-20 »

"@"?
High Scores, Links, and Jeff's Basic Games page.
User avatar
Schlowski
NoMess!
Posts: 892
Joined: Tue Jun 08, 2004 12:20 pm

Post by Schlowski »

You need to load and run the "graphik engine" before running Mikes program - see some entries above...

Björg
User avatar
Mike
Herr VC
Posts: 4839
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Post by Mike »

Jeff-20 wrote:"@"?
The reason why the thread runs under 'ML and other programs'. You'll need to run my MINIGRAFIK extension first. This is the DATA loader further up this topic. You can then use the following commands:

Code: Select all

@ON               : initialise 160x192 pixel hires screen
@CLR              : clear hires screen, and set colour RAM
@RETURN           : back to text mode
@<colour>,<x>,<y> : set point in <colour> at <x>,<y>
<colour>, <x>, and <y> can be numbers, variables, or other arbitrary expressions.

Code: Select all

<colour>: =0, clear point; =1, set point
<x>: between 0 and 159
<y>: between 0 and 191
and finally, as a function:

Code: Select all

@(<x>,<y>)        : returns 0 if pixel is clear at <x>,<y>; 1 if it is set.
Greetings,

Michael
User avatar
GreyGhost
Vic 20 Nerd
Posts: 525
Joined: Wed Oct 05, 2005 11:10 pm

Post by GreyGhost »

You have to put a space between the DATA and the -1 or you get a syntax error. Or add it to the end of line 35.
nice little demo.
Rob
User avatar
Mike
Herr VC
Posts: 4839
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Post by Mike »

Strange. My program works without the space. Maybe your tokeniser doesn't translate the '-' sign correctly. Try re-entering the line within the BASIC editor, without the space.

I transferred the program as SEQ file on a *.d64 image, and then ran the following small tool to 'type' it in:

Code: Select all

63995 POKE812,238:OPEN2,8,2,"<program name>,S,R" 
63996 IFST<>0THENPOKE812,239:CLOSE2:END 
63997 PRINT"{CLR}"; 
63998 GET#2,A$:PRINTA$;:IFA$<>CHR$(13)THEN63998 
63999 PRINT"GOTO63996":POKE631,19:POKE632,13:POKE633,13:POKE198,3
This way I'm using BASIC's built-in tokenizer, and that's guaranteed to work. ;)

Michael

P.S.: Thanks for the praise. :)
User avatar
GreyGhost
Vic 20 Nerd
Posts: 525
Joined: Wed Oct 05, 2005 11:10 pm

Post by GreyGhost »

I thought it was odd. I knew I'd seen it done before(-1 by itself in data). I did what you said and just cursored over the line and hit return. It worked fine after that. I copy/pasted the text file with BaseEdit and saved it. So, there might be a little error in BaseEdit. Or might have been me screwing things up again.
Rob
User avatar
Schlowski
NoMess!
Posts: 892
Joined: Tue Jun 08, 2004 12:20 pm

Post by Schlowski »

Error in BasEdit? Never! :roll:

Just tested a simple Basic porgram

Code: Select all

10 DATA-1
and guess what: My BasEdit screwed up the - sign , made $AB instead of $2D out of it.

(Some minutes later)
What the... If used in calculations as A=A-1 the minus is treated as an operator and codes as $AB, but, if used in Data lines it is coded as $2D, i.e. pure ASCII.
Think I have to investigate this further if some other operators like + behave the same way...

Björg

PS: And such an error even after I had coded the complete Videopoker Basic version in BasEdit...
carlsson
Class of '6502
Posts: 5516
Joined: Wed Mar 10, 2004 1:41 am

Post by carlsson »

I encountered a couple of bugs in bastext too. I mailed my friend Peter about it, but while waiting for someone to fix them, I switched to petcat. Can you borrow some tokenizer code from that application in case your own isn't complete?
Anders Carlsson

Image Image Image Image Image
User avatar
Schlowski
NoMess!
Posts: 892
Joined: Tue Jun 08, 2004 12:20 pm

Post by Schlowski »

I could but I won't :-)

I will look into this and try to fix it, this is part of the fun for me... at least I want to be able to say that all bugs belong to me 8)

Björg
carlsson
Class of '6502
Posts: 5516
Joined: Wed Mar 10, 2004 1:41 am

Post by carlsson »

I didn't mean you neccessarily should copy code, only have a look in case there are situations where a different tokenization than you'd expect should be used. For example, dos BasEdit properly handle a colon after a DATA statement, allowing other instructions on the same row?

10 DATA 20,7,-1:A=2:POKE36879,8
Anders Carlsson

Image Image Image Image Image
User avatar
Schlowski
NoMess!
Posts: 892
Joined: Tue Jun 08, 2004 12:20 pm

Post by Schlowski »

Just tested, worked fine beside the known 'minus-bug'. Hex dump of program saved in vice and saved from BasEdit are identical :-)
That's simply because I have no special handling for data lines, which is good for appended commands, but bad for the minus sign...

I'm always astonished to learn such specialities of a very old and (basically) very simple Basic. I think these anomalities are because the interpreter was hand-coded and optimized and not designed by Niklaus Wirth :wink:

More anomalies you want me to check?

Björg

Edit: Just had an idea and proved it: After a DATA statement the tokenizer is off until the next colon (or new line).

Code: Select all

10 DATAPOKE36876,8:POKE36876,8
will not tokenize the first POKE after DATA, but of course the POKE after the colon. Same is valid for my precious '-'. Now I have an idea how to solve this in BasEdit, of course!
carlsson
Class of '6502
Posts: 5516
Joined: Wed Mar 10, 2004 1:41 am

Post by carlsson »

Same goes for +, * and any Basic keyword (like you just figured out). In this manner, DATA and REM operate differently:

DATA does not tokenize any keywords, and graphic symbols outside quotes will be dropped straight away. The part about dropping graphic symbols is true for all other cases of tokenization too.

REM also does not tokenize keywords, but graphic symbols are listed as the Basic keyword their code represents, i.e. REM SHIFT+N lists as REM NEXT.

It is however possible to POKE graphic symbols/keywords into DATA statements afterwards:

10 DATA1
POKE4102,206
LIST
10 DATANEXT
READB$
PRINTB$
(shift+N)
Anders Carlsson

Image Image Image Image Image
User avatar
Mike
Herr VC
Posts: 4839
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Post by Mike »

Schlowski wrote:Edit: Just had an idea and proved it: After a DATA statement the tokenizer is off until the next colon (or new line).

Code: Select all

10 DATAPOKE36876,8:POKE36876,8
will not tokenize the first POKE after DATA, but of course the POKE after the colon. Same is valid for my precious '-'. Now I have an idea how to solve this in BasEdit, of course!
It gets even better, the colon is ignored, i.e. the DATA statement is continued, when it is enclosed in quotes:

Code: Select all

10 DATA"POKE36876,8:POKE36876,8"
...will retain the complete string as plain text.

But a long way from hires graphics... maybe we should continue this discussion in another thread (what a simple DATA-1 statement can do ;) ).

Michael
Post Reply