-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcube.si
More file actions
172 lines (170 loc) · 5.4 KB
/
Copy pathcube.si
File metadata and controls
172 lines (170 loc) · 5.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
\\ @extern InitWindow (Int, Int, String);
\\ @extern SetTargetFPS (Int);
\\ @extern WindowShouldClose () -> Bool;
\\ @extern IsKeyDown (Int) -> Bool;
\\ @extern BeginDrawing ();
\\ @extern EndDrawing ();
\\ @extern CloseWindow ();
\\ @extern GetScreenWidth () -> Int;
\\ @extern GetScreenHeight () -> Int;
\\ @extern rlDisableBackfaceCulling ();
\\ @extern rlClearColor (Int, Int, Int, Int);
\\ @extern rlClearScreenBuffers ();
\\ @extern rlBegin (Int);
\\ @extern rlEnd ();
\\ @extern rlColor4ub (Int, Int, Int, Int);
\\ @extern rlVertex2f (Float, Float);
\\ @extern sinf (Float) -> Float;
\\ @extern cosf (Float) -> Float;
# examples/cube.si — a rotating, six-colour cube rendered with raylib.
#
# Silicon's FFI passes only scalars (Int / Int64 / Float / pointers), so the
# cube talks to raylib through its primitive-only `rlgl` immediate-mode API and
# does all the 3-D maths (rotation, perspective projection, back-face culling)
# itself; raylib just opens the window and rasterises the triangles.
#
# Controls: arrow keys or WASD rotate the cube, Esc quits.
#
# NOTE: raylib's IsKeyDown / WindowShouldClose return a C `bool`, which sets
# only the low byte of the return register. They MUST be declared `:Bool`
# (not `:Int`) so the backend masks the result to a clean 0/1 — declared
# `:Int`, Silicon reads the garbage upper bits and every key tests as pressed,
# so the directions cancel and the cube never moves.
#
# Build & run (needs raylib + libm; install the QBE backend with `sgl setup`):
# bun run sgl build --native examples/cube.si -lraylib -lm
# ./examples/cube
@mut gAngleX := 0.0;
@mut gAngleY := 0.0;
@mut gSinX := 0.0;
@mut gCosX := 1.0;
@mut gSinY := 0.0;
@mut gCosY := 1.0;
pos := 1.0;
@mut neg := 0.0;
@mut gCx := 450.0;
@mut gCy := 350.0;
@mut gFocal := 320.0;
\\ rx (Float, Float, Float) -> Float
@fn rx x, y, z := {
x * gCosY + (z * gSinY)
};
\\ ry (Float, Float, Float) -> Float
@fn ry x, y, z := {
z1 := z * gCosY - (x * gSinY);
y * gCosX - (z1 * gSinX)
};
\\ rz (Float, Float, Float) -> Float
@fn rz x, y, z := {
z1 := z * gCosY - (x * gSinY);
y * gSinX + (z1 * gCosX)
};
\\ sx (Float, Float, Float) -> Float
@fn sx x, y, z := {
ox := rx(x, y, z);
oz := rz(x, y, z);
scale := gFocal / (4.0 - oz);
gCx + (ox * scale)
};
\\ sy (Float, Float, Float) -> Float
@fn sy x, y, z := {
oy := ry(x, y, z);
oz := rz(x, y, z);
scale := gFocal / (4.0 - oz);
gCy - (oy * scale)
};
\\ normalZ (Float, Float, Float, Float, Float, Float, Float, Float, Float) -> Float
@fn normalZ ax, ay, az, bx, by, bz, cx, cy, cz := {
rax := rx(ax, ay, az);
ray := ry(ax, ay, az);
rbx := rx(bx, by, bz);
rby := ry(bx, by, bz);
rcx := rx(cx, cy, cz);
rcy := ry(cx, cy, cz);
e1x := rbx - rax;
e1y := rby - ray;
e2x := rcx - rax;
e2y := rcy - ray;
e1x * e2y - (e1y * e2x)
};
\\ drawFace (Float, Float, Float, Float, Float, Float, Float, Float, Float, Float, Float, Float, Int, Int, Int)
@fn drawFace ax, ay, az, bx, by, bz, cx, cy, cz, dx, dy, dz, r, g, b := {
nz := normalZ(ax, ay, az, bx, by, bz, cx, cy, cz);
@if(nz > 0.0, {
ax2 := sx(ax, ay, az);
ay2 := sy(ax, ay, az);
bx2 := sx(bx, by, bz);
by2 := sy(bx, by, bz);
cx2 := sx(cx, cy, cz);
cy2 := sy(cx, cy, cz);
dx2 := sx(dx, dy, dz);
dy2 := sy(dx, dy, dz);
rlBegin(4);
rlColor4ub(r, g, b, 255);
rlVertex2f(ax2, ay2);
rlVertex2f(bx2, by2);
rlVertex2f(cx2, cy2);
rlVertex2f(ax2, ay2);
rlVertex2f(cx2, cy2);
rlVertex2f(dx2, dy2);
rlEnd();
0
});
0
};
@fn drawCube := {
drawFace(neg, neg, pos, pos, neg, pos, pos, pos, pos, neg, pos, pos, 230, 41, 55);
drawFace(pos, neg, neg, neg, neg, neg, neg, pos, neg, pos, pos, neg, 0, 228, 48);
drawFace(pos, neg, pos, pos, neg, neg, pos, pos, neg, pos, pos, pos, 0, 121, 241);
drawFace(neg, neg, neg, neg, neg, pos, neg, pos, pos, neg, pos, neg, 253, 249, 0);
drawFace(neg, pos, pos, pos, pos, pos, pos, pos, neg, neg, pos, neg, 255, 161, 0);
drawFace(neg, neg, neg, pos, neg, neg, pos, neg, pos, neg, neg, pos, 200, 122, 255);
0
};
\\ main Int
@fn main := {
neg = 0.0 - 1.0;
InitWindow(900, 700, 'Silicon + raylib: rotating cube');
gCx = @toFloat(GetScreenWidth()) / 2.0;
gCy = @toFloat(GetScreenHeight()) / 2.0;
gFocal = @toFloat(GetScreenHeight()) * 0.45;
SetTargetFPS(60);
rlDisableBackfaceCulling();
@loop(WindowShouldClose() == 0, {
@if(IsKeyDown(263) != 0, {
gAngleY = gAngleY - 0.03;
});
@if(IsKeyDown(65) != 0, {
gAngleY = gAngleY - 0.03;
});
@if(IsKeyDown(262) != 0, {
gAngleY = gAngleY + 0.03;
});
@if(IsKeyDown(68) != 0, {
gAngleY = gAngleY + 0.03;
});
@if(IsKeyDown(265) != 0, {
gAngleX = gAngleX - 0.03;
});
@if(IsKeyDown(87) != 0, {
gAngleX = gAngleX - 0.03;
});
@if(IsKeyDown(264) != 0, {
gAngleX = gAngleX + 0.03;
});
@if(IsKeyDown(83) != 0, {
gAngleX = gAngleX + 0.03;
});
gCosX = cosf(gAngleX);
gSinX = sinf(gAngleX);
gCosY = cosf(gAngleY);
gSinY = sinf(gAngleY);
BeginDrawing();
rlClearColor(24, 24, 32, 255);
rlClearScreenBuffers();
drawCube();
EndDrawing();
});
CloseWindow();
0
};