Skip to content

Commit 4af8ebe

Browse files
PLOTQUAD and textured cube
1 parent 77bbde3 commit 4af8ebe

File tree

7 files changed

+402
-49
lines changed

7 files changed

+402
-49
lines changed

include/basic/graphics.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,24 @@ void autoflip_statement(struct basic_ctx* ctx);
9797
* @param ctx The current BASIC context.
9898
*/
9999
void flip_statement(struct basic_ctx* ctx);
100+
101+
/**
102+
* @brief Implements the BASIC `PLOTQUAD` statement.
103+
*
104+
* Parses and executes the `PLOTQUAD` keyword from the BASIC interpreter.
105+
* This statement expects a sprite handle followed by four (x,y) coordinate
106+
* pairs, defining the quadrilateral to which the sprite should be mapped.
107+
*
108+
* The coordinates may describe any convex quad, allowing sprites to be drawn
109+
* with skew and perspective. Internally this delegates to the textured-quad
110+
* blitter (`plot_sprite_quad`) to perform the actual rendering.
111+
*
112+
* Example usage in BASIC:
113+
* @code
114+
* PLOTQUAD SPRITEHANDLE, 100,100, 200,120, 190,220, 90,210
115+
* @endcode
116+
*
117+
* @param ctx Pointer to the BASIC execution context, containing parser state,
118+
* sprite table, and current program environment.
119+
*/
120+
void plotquad_statement(struct basic_ctx* ctx);

include/basic/tokenizer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@
145145
T(MOUNT) \
146146
T(SETTIMEZONE) \
147147
T(ENDIF) \
148+
T(PLOTQUAD) \
148149
T(KGET)
149150

150151
GENERATE_ENUM_LIST(TOKEN, token_t)

os/images/s1.png

180 KB
Loading

os/programs/cube.rrbasic

Lines changed: 86 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,64 @@
1-
REM =======================
2-
REM Rotating Wireframe Cube
3-
REM =======================
1+
REM ======================
2+
REM Rotating Textured Cube
3+
REM ======================
44

55
CX# = GRAPHICS_WIDTH / 2
66
CY# = GRAPHICS_HEIGHT / 2
77
SCALE# = 250
88
DIST# = 3
99

10+
SPRITELOAD s1, "/images/s1.png"
11+
1012
DIM VX#,8
1113
DIM VY#,8
1214
DIM VZ#,8
15+
DIM Xr#,8
16+
DIM Yr#,8
17+
DIM Zr#,8
1318
DIM PX#,8
1419
DIM PY#,8
15-
DIM EA,12
16-
DIM EB,12
20+
21+
REM Faces as 4 vertex indices each (6 faces * 4 = 24)
22+
DIM F,24
23+
24+
REM Back (+Z)
25+
F(0) = 4
26+
F(1) = 5
27+
F(2) = 6
28+
F(3) = 7
29+
30+
REM Front (-Z)
31+
F(4) = 0
32+
F(5) = 1
33+
F(6) = 2
34+
F(7) = 3
35+
36+
REM Left (-X)
37+
F(8) = 0
38+
F(9) = 3
39+
F(10) = 7
40+
F(11) = 4
41+
42+
REM Right (+X)
43+
F(12) = 1
44+
F(13) = 2
45+
F(14) = 6
46+
F(15) = 5
47+
48+
REM Top (+Y)
49+
F(16) = 3
50+
F(17) = 2
51+
F(18) = 6
52+
F(19) = 7
53+
54+
REM Bottom (-Y)
55+
F(20) = 0
56+
F(21) = 1
57+
F(22) = 5
58+
F(23) = 4
59+
60+
DIM DEPTH#,6
61+
DIM IDX,6
1762

1863
REM Cube vertices
1964
VX#(0) = -1
@@ -48,58 +93,28 @@ VX#(7) = -1
4893
VY#(7) = 1
4994
VZ#(7) = 1
5095

51-
REM Edges
52-
EA(0) = 0
53-
EB(0) = 1
54-
EA(1) = 1
55-
EB(1) = 2
56-
EA(2) = 2
57-
EB(2) = 3
58-
EA(3) = 3
59-
EB(3) = 0
60-
61-
EA(4) = 4
62-
EB(4) = 5
63-
EA(5) = 5
64-
EB(5) = 6
65-
EA(6) = 6
66-
EB(6) = 7
67-
EA(7) = 7
68-
EB(7) = 4
69-
70-
EA(8) = 0
71-
EB(8) = 4
72-
EA(9) = 1
73-
EB(9) = 5
74-
EA(10) = 2
75-
EB(10) = 6
76-
EA(11) = 3
77-
EB(11) = 7
78-
7996
ANGLE# = 0
8097

8198
CLS
8299
AUTOFLIP FALSE
100+
REM for rectangle cull only
101+
GCOL RGB(0,0,0)
83102

84103
REPEAT
85104

86105
min_x# = PX#(0)
87106
max_x# = PX#(0)
88107
min_y# = PY#(0)
89108
max_y# = PY#(0)
90-
91109
FOR I = 1 TO 7
92110
IF PX#(I) < min_x# THEN min_x# = PX#(I) - 16
93111
IF PX#(I) > max_x# THEN max_x# = PX#(I) + 16
94112
IF PY#(I) < min_y# THEN min_y# = PY#(I) - 16
95113
IF PY#(I) > max_y# THEN max_y# = PY#(I) + 16
96114
NEXT
97-
98-
GCOL RGB(0,0,0)
99115
RECTANGLE min_x#, min_y#, max_x#, max_y#
100116

101117
ANGLE# = ANGLE# + 0.03
102-
103118
SY# = SIN(ANGLE#)
104119
CYR# = COS(ANGLE#)
105120
SX# = SIN(ANGLE# * 0.7)
@@ -116,18 +131,47 @@ REPEAT
116131
Y1# = Y# * CXR# - Z1# * SX#
117132
Z2# = Y# * SX# + Z1# * CXR#
118133

134+
Xr#(I) = X1#
135+
Yr#(I) = Y1#
136+
Zr#(I) = Z2#
137+
119138
DEN# = Z2# + DIST#
120139
IF DEN# = 0 THEN DEN# = 0.000001
121140

122141
PX#(I) = CX# + (X1# * SCALE# / DEN#)
123142
PY#(I) = CY# + (Y1# * SCALE# / DEN#)
143+
124144
NEXT
125145

126-
GCOL RGB(255,255,255)
127-
FOR E = 0 TO 11
128-
V1 = EA(E)
129-
V2 = EB(E)
130-
LINE PX#(V1), PY#(V1), PX#(V2), PY#(V2)
146+
FOR f = 0 TO 5
147+
a = F(f * 4 + 0)
148+
b = F(f * 4 + 1)
149+
c = F(f * 4 + 2)
150+
d = F(f * 4 + 3)
151+
DEPTH#(f) = (Zr#(a) + Zr#(b) + Zr#(c) + Zr#(d)) / 4
152+
IDX(f) = f
153+
NEXT
154+
155+
FOR i = 0 TO 4
156+
FOR j = 0 TO 4 - i
157+
left = IDX(j)
158+
right = IDX(j + 1)
159+
IF DEPTH#(left) < DEPTH#(right) THEN temp = IDX(j)
160+
IF DEPTH#(left) < DEPTH#(right) THEN IDX(j) = IDX(j + 1)
161+
IF DEPTH#(left) < DEPTH#(right) THEN IDX(j + 1) = temp
162+
NEXT
163+
NEXT
164+
165+
FOR k = 0 TO 5
166+
f = IDX(k)
167+
168+
a = F(f * 4 + 0)
169+
b = F(f * 4 + 1)
170+
c = F(f * 4 + 2)
171+
d = F(f * 4 + 3)
172+
173+
PLOTQUAD s1, PX#(a),PY#(a), PX#(b),PY#(b), PX#(c),PY#(c), PX#(d),PY#(d)
174+
131175
NEXT
132176

133177
FLIP

0 commit comments

Comments
 (0)