Skip to content

Commit 308509c

Browse files
authored
Unroll & reduce dot access in rectangle drawing helpers (#2198)
* Optimize draw_rect_outline by reducing dot access * Reduce dot access by fetching values once * Add inline diagram of the rectangle points * Unroll rotated portion of draw_rect_outline * Use direct indexed access instead of slower properties in texture drawing * Attempt to work around broken mypy * Use Point2List * Fix bug in closing rectangle corner ---------
1 parent 4dbaacf commit 308509c

File tree

1 file changed

+63
-19
lines changed

1 file changed

+63
-19
lines changed

arcade/draw/rect.py

Lines changed: 63 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def draw_texture_rect(
7070
atlas.use_uv_texture(unit=1)
7171

7272
geometry = ctx.geometry_empty
73-
program["pos"] = rect.center_x, rect.center_y, 0
73+
program["pos"] = rect.x, rect.y, 0
7474
program["color"] = color.normalized
7575
program["size"] = rect.width, rect.height
7676
program["angle"] = angle
@@ -299,27 +299,71 @@ def draw_rect_outline(
299299
"""
300300

301301
HALF_BORDER = border_width / 2
302+
# Extremely unrolled code below
302303

303304
# fmt: off
304-
i_lb = rect.bottom_left.x + HALF_BORDER, rect.bottom_left.y + HALF_BORDER
305-
i_rb = rect.bottom_right.x - HALF_BORDER, rect.bottom_right.y + HALF_BORDER
306-
i_rt = rect.top_right.x - HALF_BORDER, rect.top_right.y - HALF_BORDER
307-
i_lt = rect.top_left.x + HALF_BORDER, rect.top_left.y - HALF_BORDER
308-
o_lb = rect.bottom_left.x - HALF_BORDER, rect.bottom_left.y - HALF_BORDER
309-
o_rb = rect.bottom_right.x + HALF_BORDER, rect.bottom_right.y - HALF_BORDER
310-
o_rt = rect.top_right.x + HALF_BORDER, rect.top_right.y + HALF_BORDER
311-
o_lt = rect.top_left.x - HALF_BORDER, rect.top_right.y + HALF_BORDER
305+
left = rect.left
306+
right = rect.right
307+
bottom = rect.bottom
308+
top = rect.top
309+
x = rect.x
310+
y = rect.y
311+
312+
# o = outer, i = inner
313+
#
314+
# o_left, o_top o_right, o_top
315+
# +----------------------------------------+
316+
# | i_left, i_top i_right, i_top |
317+
# | +----------------------------------+ |
318+
# | | | |
319+
# | | | |
320+
# | | | |
321+
# | +----------------------------------+ |
322+
# | i_left, i_bottom i_right , i_bottom |
323+
# +----------------------------------------+
324+
# o_left, o_bottom o_right, o_bottom
325+
326+
i_left = left + HALF_BORDER
327+
i_bottom = bottom + HALF_BORDER
328+
i_right = right - HALF_BORDER
329+
i_top = top - HALF_BORDER
330+
331+
o_left = left - HALF_BORDER
332+
o_bottom = bottom - HALF_BORDER
333+
o_right = right + HALF_BORDER
334+
o_top = top + HALF_BORDER
335+
336+
# Declared separately because the code below seems to break mypy
337+
point_list: Point2List
338+
339+
# This is intentionally unrolled to minimize repacking tuples
340+
if tilt_angle == 0:
341+
point_list = (
342+
(o_left , o_top ),
343+
(i_left , i_top ),
344+
(o_right , o_top ),
345+
(i_right , i_top ),
346+
(o_right , o_bottom),
347+
(i_right , i_bottom),
348+
(o_left , o_bottom),
349+
(i_left , i_bottom),
350+
(o_left , o_top ),
351+
(i_left , i_top )
352+
)
353+
else:
354+
point_list = (
355+
rotate_point(o_left , o_top , x, y, tilt_angle),
356+
rotate_point(i_left , i_top , x, y, tilt_angle),
357+
rotate_point(o_right , o_top , x, y, tilt_angle),
358+
rotate_point(i_right , i_top , x, y, tilt_angle),
359+
rotate_point(o_right , o_bottom, x, y, tilt_angle),
360+
rotate_point(i_right , i_bottom, x, y, tilt_angle),
361+
rotate_point(o_left , o_bottom, x, y, tilt_angle),
362+
rotate_point(i_left , i_bottom, x, y, tilt_angle),
363+
rotate_point(o_left , o_top , x, y, tilt_angle),
364+
rotate_point(i_left , i_top , x, y, tilt_angle)
365+
)
312366
# fmt: on
313-
314-
point_list: Point2List = (o_lt, i_lt, o_rt, i_rt, o_rb, i_rb, o_lb, i_lb, o_lt, i_lt)
315-
316-
if tilt_angle != 0:
317-
point_list_2 = []
318-
for point in point_list:
319-
new_point = rotate_point(point[0], point[1], rect.x, rect.y, tilt_angle)
320-
point_list_2.append(new_point)
321-
point_list = point_list_2
322-
323367
_generic_draw_line_strip(point_list, color, gl.TRIANGLE_STRIP)
324368

325369

0 commit comments

Comments
 (0)