-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdraw.lua
More file actions
105 lines (84 loc) · 2.11 KB
/
draw.lua
File metadata and controls
105 lines (84 loc) · 2.11 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
backface_culling = true
-- drawing modes
-- 1 = vertices + wireframe
-- 2 = wireframe
-- 3 = solid color
-- 4 = solid color + wireframe
-- 5 = vertices only
drawing_mode = 3
max_draw_mode = 5
function draw_triangle(x0, y0, x1, y1, x2, y2, c)
line(x0, y0, x1, y1, c)
line(x1, y1, x2, y2, c)
line(x2, y2, x0, y0, c)
end
function draw_triangle_filled(x0, y0, z0, w0, x1, y1, z1, w1, x2, y2, z2, w2, c)
-- dumb and lazy sort
if (y0 > y1) then
y0, y1 = y1, y0
x0, x1 = x1, x0
end
if (y1 > y2) then
y1, y2 = y2, y1
x1, x2 = x2, x1
end
if (y0 > y1) then
y0, y1 = y1, y0
x0, x1 = x1, x0
end
-- bottom flat triangle
local inv_slope_1 = 0
local inv_slope_2 = 0
if (y1-y0 != 0) then
inv_slope_1 = (x1-x0) / abs(y1-y0)
end
if (y2-y0 != 0) then
inv_slope_2 = (x2-x0) / abs(y2-y0)
end
if (y1-y0 != 0) then
for y=y0, y1 do
local x_start = x1 + (y-y1) * inv_slope_1
local x_end = x0 + (y-y0) * inv_slope_2
if (x_end < x_start) then
x_start, x_end = x_end, x_start
end
rectfill(x_start, y, x_end, y, c)
end
end
-- top flat triangle
inv_slope_1 = 0
inv_slope_2 = 0
if (y2-y1 != 0) then
inv_slope_1 = (x2-x1) / abs(y2-y1)
end
if (y2-y0 != 0) then
inv_slope_2 = (x2-x0) / abs(y2-y0)
end
if (y2-y1 != 0) then
for y=y1, y2 do
local x_start = x1 + (y-y1) * inv_slope_1
local x_end = x0 + (y-y0) * inv_slope_2
if (x_end < x_start) then
x_start, x_end = x_end, x_start
end
rectfill(x_start, y, x_end, y, c)
end
end
end
-- draws a bg grid for debugging purposes
function draw_grid(w, h, c)
for y=0, 127 do
for x=0, 127 do
if (x%w==0 or y%h==0) then
pset(x,y,c)
end
end
end
end
function draw_dotted_grid(w, h, c)
for y=0, 127, h do
for x=0, 127, w do
pset(x,y,c)
end
end
end