Skip to content
This repository was archived by the owner on Dec 23, 2021. It is now read-only.

Commit d98a649

Browse files
committed
cleaned up tile_grid and added threading
1 parent 0d4a549 commit d98a649

File tree

1 file changed

+56
-14
lines changed

1 file changed

+56
-14
lines changed

src/base_circuitpython/displayio/tile_grid.py

Lines changed: 56 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from PIL import Image
22
from . import constants as CONSTANTS
3+
import threading
34

45
# TileGrid implementation loosely based on the
56
# displayio.TileGrid class in Adafruit CircuitPython
@@ -21,6 +22,10 @@
2122
bmp_img = img.load()
2223

2324

25+
def func(x, y):
26+
return x + y
27+
28+
2429
class TileGrid:
2530
def __init__(
2631
self,
@@ -82,22 +87,59 @@ def draw(self, x, y, scale):
8287
# appropriate scale on the global bmp_img
8388
x = self.x * scale + x
8489
y = self.y * scale + y
85-
for i in range(self.tile_height):
86-
for j in range(self.tile_width):
90+
91+
if self.tile_height > 1 and self.tile_width > 1:
92+
y_mid = int(self.tile_height / 2)
93+
x_mid = int(self.tile_width / 2)
94+
thread_1 = threading.Thread(
95+
target=self.draw_group, args=(x, y, 0, y_mid, 0, x_mid, scale,),
96+
)
97+
thread_2 = threading.Thread(
98+
target=self.draw_group,
99+
args=(x, y, 0, y_mid, x_mid, self.tile_width, scale),
100+
)
101+
thread_3 = threading.Thread(
102+
target=self.draw_group,
103+
args=(x, y, y_mid, self.tile_height, 0, x_mid, scale),
104+
)
105+
thread_4 = threading.Thread(
106+
target=self.draw_group,
107+
args=(x, y, y_mid, self.tile_height, x_mid, self.tile_width, scale,),
108+
)
109+
thread_1.start()
110+
thread_2.start()
111+
thread_3.start()
112+
thread_4.start()
113+
114+
thread_1.join()
115+
thread_2.join()
116+
thread_3.join()
117+
thread_4.join()
118+
else:
119+
self.draw_group(
120+
x, y, 0, self.tile_height, 0, self.tile_width, scale,
121+
)
122+
123+
def draw_group(self, x, y, y_start, y_end, x_start, x_end, scale):
124+
# return
125+
for i in range(y_start, y_end):
126+
for j in range(x_start, x_end):
87127
self.fill_pixel(i, j, x, y, scale)
88128

89129
# helper method for drawing pixels on bmp_img
90130
# given the src, offset, and scale
91131
def fill_pixel(self, i, j, x, y, scale):
92-
for i_new in range(scale):
93-
for j_new in range(scale):
94-
try:
95-
if x + (j * scale) + j_new >= 0 and y + (i * scale) + i_new >= 0:
96-
if not self.pixel_shader._Palette__contents[
97-
self.bitmap[j, i]
98-
].transparent:
99-
bmp_img[
100-
x + (j * scale) + j_new, y + (i * scale) + i_new,
101-
] = self.pixel_shader[self.bitmap[j, i]]
102-
except IndexError:
103-
continue
132+
133+
curr_val = self.bitmap[j, i]
134+
transparent = self.pixel_shader._Palette__contents[curr_val].transparent
135+
if not transparent:
136+
x_offset = x + (j * scale)
137+
y_offset = y + (i * scale)
138+
x_max = min(x_offset + scale, 240)
139+
y_max = min(y_offset + scale, 240)
140+
141+
curr_colour = self.pixel_shader[curr_val]
142+
for new_y in range(y_offset, y_max):
143+
for new_x in range(x_offset, x_max):
144+
if curr_val != bmp_img[new_x, new_y]:
145+
bmp_img[new_x, new_y] = curr_colour

0 commit comments

Comments
 (0)