Browse code

(cube3d.tal) Optimizations

neauoire authored on 30/10/2023 22:32:02
Showing 1 changed files
... ...
@@ -1,121 +1,110 @@
1
-( Cube3d:
2
-	Just a cube, y'know )
1
+( Cube3d: Just a cube, y'know )
3 2
 
4 3
 |00 @System &vector $2 &wst $1 &rst $1 &eaddr $2 &ecode $1 &pad $1 &r $2 &g $2 &b $2 &debug $1 &halt $1
5 4
 |20 @Screen &vector $2 &width $2 &height $2 &auto $1 &pad $1 &x $2 &y $2 &addr $2 &pixel $1 &sprite $1
6 5
 
7 6
 |0000
8 7
 
9
-@timer $1
10
-@color $1
11
-@cube &v0 $8 &v4 $8
12
-@line &x $2 &y $2 &dx $2 &dy $2 &e1 $2
13
-@center &x $2 &y $2
8
+	@timer $1
9
+	@cube &v0 $8 &v4 $8
10
+	@center &x $2 &y $2
14 11
 
15
-( program )
12
+|0100
16 13
 
17
-|0100 ( -> )
18
-
19
-	( theme )
14
+@on-reset ( -> )
15
+	( | theme )
20 16
 	#4fcd .System/r DEO2
21 17
 	#4fc3 .System/g DEO2
22 18
 	#dfc2 .System/b DEO2
23
-	( center )
19
+	( | center )
24 20
 	.Screen/width DEI2 #01 SFT2 #0040 SUB2 .center/x STZ2
25 21
 	.Screen/height DEI2 #01 SFT2 #0040 SUB2 .center/y STZ2
26
-	( begin. )
22
+	( | begin. )
27 23
 	;on-frame .Screen/vector DEO2
28 24
 
29 25
 @on-frame ( -> )
30
-
31
-	#00 ;draw-cube JSR2
26
+	( | clear )
27
+	#0000 DUP2 .Screen/x DEO2
28
+	.Screen/y DEO2
29
+	#80 .Screen/pixel DEO
30
+	( | draw )
32 31
 	.timer LDZk INC SWP STZ
33
-	#05 ;draw-cube JSR2
34
-
35
-BRK
36
-
37
-@draw-cube ( frame color -- )
32
+	<draw-cube>
33
+	BRK
38 34
 
39
-	.color STZ
40
-	( create box )
35
+@<draw-cube> ( frame -- )
36
+	( | create box )
41 37
 	#0800
42
-	&loop
43
-		STHk
44
-		#00 .timer LDZ [ #00 STHkr INC #07 AND #60 SFT ADD2 ] #00ff AND2 ;table ADD2 LDA #01 SFT
45
-		#00 .timer LDZ [ #00 STHkr #60 SFT ADD2 ] #00ff AND2 ;table ADD2 LDA #02 SFT [ #00 STHkr #62 SFT2 ADD2 ]
46
-		.cube/v0 STHr DUP ADD ADD STZ2
47
-		INC GTHk ,&loop JCN
38
+	&loop ( -- )
39
+		STHk #00 .timer LDZ #00 STHkr INC #07 AND #60 SFT ADD2 #00ff AND2 ;table ADD2 LDA #01 SFT #00 .timer LDZ #00 STHkr #60 SFT ADD2 #00ff AND2 ;table ADD2 LDA #02 SFT #00 STHkr #62 SFT2 ADD2 .cube/v0 STHr DUP ADD ADD STZ2
40
+		INC GTHk ?&loop
48 41
 	POP2
49
-	( vertices )
42
+	( | vertices )
50 43
 	#0800
51
-	&ver-loop
52
-		DUP DUP ADD .cube ADD LDZ2 ;draw-vertex JSR2
53
-		INC GTHk ,&ver-loop JCN
44
+	&ver-loop ( -- )
45
+		DUP DUP ADD .cube ADD LDZ2 <draw-vertex>
46
+		INC GTHk ?&ver-loop
54 47
 	POP2
55
-	( lines )
56
-	#0400
57
-	&line-loop
58
-		STHk
59
-		.cube/v0 STHkr DUP ADD ADD .cube/v0 STHkr INC #03 AND DUP ADD ADD ,trace JSR
60
-		.cube/v0 STHkr DUP ADD ADD .cube/v4 STHkr DUP ADD ADD ,trace JSR
61
-		.cube/v4 STHkr DUP ADD ADD .cube/v4 STHr INC #03 AND DUP ADD ADD ,trace JSR
62
-		INC GTHk ,&line-loop JCN
63
-	POP2
64
-
65
-JMP2r
66
-
67
-@trace ( a b -- )
68
-
48
+	( lines ) #0400
49
+	&line-loop ( -- )
50
+		STHk .cube/v0 STHkr DUP ADD ADD .cube/v0 STHkr INC #03 AND DUP ADD ADD <draw-edge>
51
+		.cube/v0 STHkr DUP ADD ADD .cube/v4 STHkr DUP ADD ADD <draw-edge>
52
+		.cube/v4 STHkr DUP ADD ADD .cube/v4 STHr INC #03 AND DUP ADD ADD <draw-edge>
53
+		INC GTHk ?&line-loop
54
+	POP2 JMP2r
55
+
56
+@<draw-edge> ( a b -- )
69 57
 	STH STH
70
-	#00 STHkr LDZ .center/x LDZ2 ADD2 #00 STHr INC LDZ .center/y LDZ2 ADD2
71
-	#00 STHkr LDZ .center/x LDZ2 ADD2 #00 STHr INC LDZ .center/y LDZ2 ADD2
72
-		.color LDZ ;draw-line JSR2
73
-
74
-JMP2r
75
-
76
-@draw-vertex ( x y -- )
58
+	( ) #00 STHkr LDZ .center/x LDZ2 ADD2
59
+	( ) #00 STHr INC LDZ .center/y LDZ2 ADD2
60
+	( ) #00 STHkr LDZ .center/x LDZ2 ADD2
61
+	( ) #00 STHr INC LDZ .center/y LDZ2 ADD2 #05 <draw-line>
62
+	JMP2r
77 63
 
64
+@<draw-vertex> ( x y -- )
78 65
 	#00 SWP #0004 SUB2 .center/y LDZ2 ADD2 .Screen/y DEO2
79 66
 	#00 SWP #0003 SUB2 .center/x LDZ2 ADD2 .Screen/x DEO2
80 67
 	;&icn .Screen/addr DEO2
81
-	.color LDZ .Screen/sprite DEO
82
-
83
-JMP2r
84
-	&icn 0000 387c 7c7c 3800
85
-
86
-@draw-line ( x1* y1* x2* y2* color -- )
87
-
88
-	( load ) STH ,&y STR2 ,&x STR2 .line/y STZ2 .line/x STZ2
89
-	,&x LDR2 .line/x LDZ2 SUB2 ;abs2 JSR2 .line/dx STZ2
90
-	#0000 ,&y LDR2 .line/y LDZ2 SUB2 ;abs2 JSR2 SUB2 .line/dy STZ2
91
-	#ffff #00 .line/x LDZ2 ,&x LDR2 ;lts2 JSR2 DUP2 ADD2 ADD2 ,&sx STR2
92
-	#ffff #00 .line/y LDZ2 ,&y LDR2 ;lts2 JSR2 DUP2 ADD2 ADD2 ,&sy STR2
93
-	.line/dx LDZ2 .line/dy LDZ2 ADD2 .line/e1 STZ2
94
-	&loop
95
-		.line/x LDZ2 DUP2 .Screen/x DEO2 [ LIT2 &x $2 ] EQU2
96
-		.line/y LDZ2 DUP2 .Screen/y DEO2 [ LIT2 &y $2 ] EQU2
97
-			STHkr .Screen/pixel DEO
98
-			AND ,&end JCN
99
-		.line/e1 LDZ2 DUP2 ADD2 DUP2
100
-		.line/dy LDZ2 ;lts2 JSR2 ,&skipy JCN
101
-			.line/e1 LDZ2 .line/dy LDZ2 ADD2 .line/e1 STZ2
102
-			.line/x LDZ2 [ LIT2 &sx $2 ] ADD2 .line/x STZ2
103
-		&skipy
104
-		.line/dx LDZ2 ;gts2 JSR2 ,&skipx JCN
105
-			.line/e1 LDZ2 .line/dx LDZ2 ADD2 .line/e1 STZ2
106
-			.line/y LDZ2 [ LIT2 &sy $2 ] ADD2 .line/y STZ2
107
-		&skipx
108
-		,&loop JMP
109
-	&end
110
-	POPr
111
-
112
-JMP2r
113
-
114
-@abs2 DUP2 #0f SFT2 EQU #05 JCN #0000 SWP2 SUB2 JMP2r
115
-@lts2 #8000 STH2k ADD2 SWP2 STH2r ADD2 GTH2 JMP2r
116
-@gts2 #8000 STH2k ADD2 SWP2 STH2r ADD2 LTH2 JMP2r
68
+	#05 .Screen/sprite DEO
69
+	JMP2r
70
+	&icn [ 0000 387c 7c7c 3800 ]
71
+
72
+@<draw-line> ( x1* y1* x2* y2* color -- )
73
+	,&color STR
74
+	,&y STR2
75
+	,&x STR2
76
+	,&y2 STR2
77
+	,&x2 STR2
78
+	,&x LDR2 ,&x2 LDR2 SUB2 abs2 ,&dx STR2
79
+	#0000 ,&y LDR2 ,&y2 LDR2 SUB2 abs2 SUB2 ,&dy STR2
80
+	#ffff [ LIT2 00 _&x2 ] LDR2 ,&x LDR2 lts2 DUP2 ADD2 ADD2 ,&sx STR2
81
+	#ffff [ LIT2 00 _&y2 ] LDR2 ,&y LDR2 lts2 DUP2 ADD2 ADD2 ,&sy STR2
82
+	[ LIT2 &dx $2 ] [ LIT2 &dy $2 ] ADD2 STH2
83
+	&while ( -- )
84
+		[ LIT2 &x2 $2 ] DUP2 .Screen/x DEO2
85
+		[ LIT2 &x $2 ] EQU2 [ LIT2 &y2 $2 ] DUP2 .Screen/y DEO2
86
+		[ LIT2 &y $2 ] EQU2 [ LIT2 &color $1 -Screen/pixel ] DEO
87
+		AND ?&end
88
+		STH2kr DUP2 ADD2 DUP2 ,&dy LDR2 lts2 ?&skipy
89
+		STH2r ,&dy LDR2 ADD2 STH2 ,&x2 LDR2 [ LIT2 &sx $2 ] ADD2 ,&x2 STR2
90
+	&skipy ( -- )
91
+		,&dx LDR2 gts2 ?&while
92
+	STH2r ,&dx LDR2 ADD2 STH2 ,&y2 LDR2 [ LIT2 &sy $2 ] ADD2 ,&y2 STR2
93
+	!&while
94
+	&end POP2r JMP2r
95
+
96
+@abs2 ( a* -- f )
97
+	DUP2 #0f SFT2 EQU ?{ #0000 SWP2 SUB2 }
98
+	JMP2r
99
+
100
+@lts2 ( a* b* -- f )
101
+	#8000 STH2k ADD2 SWP2 STH2r ADD2 GTH2 JMP2r
102
+
103
+@gts2 ( a* b* -- f )
104
+	#8000 STH2k ADD2 SWP2 STH2r ADD2 LTH2 JMP2r
117 105
 
118 106
 @table ( 256 xy )
107
+	[
119 108
 	f7f8 f9fa fbfc fcfd fefe ffff ffff ffff
120 109
 	ffff ffff fffe fefd fcfc fbfa f9f8 f7f6
121 110
 	f5f3 f2f0 efed ecea e8e6 e4e2 e0de dcda
... ...
@@ -131,4 +120,5 @@ JMP2r
131 120
 	5154 575a 5d60 6367 6a6d 7073 7679 7c7f
132 121
 	8386 898c 8f92 9598 9c9f a2a5 a8ab aeb0
133 122
 	b3b6 b9bc bfc1 c4c7 c9cc ced1 d3d5 d8da
134
-	dcde e0e2 e4e6 e8ea eced eff0 f2f3 f5f6
123
+	dcde e0e2 e4e6 e8ea eced eff0 f2f3 f5f6 ]
124
+