Optimize grid line drawing by precomputing line coordinates for improved performance
This commit is contained in:
parent
43882f4fef
commit
34f790ca18
24
main.py
24
main.py
@ -141,22 +141,36 @@ def draw_grid(screen, camera, showing_grid=True):
|
|||||||
|
|
||||||
# Draw vertical grid lines (only if zoom is high enough to see them clearly)
|
# Draw vertical grid lines (only if zoom is high enough to see them clearly)
|
||||||
if effective_cell_size > 4:
|
if effective_cell_size > 4:
|
||||||
for i in range(GRID_WIDTH + 1):
|
# Precompute grid boundaries
|
||||||
|
vertical_lines = []
|
||||||
|
horizontal_lines = []
|
||||||
|
|
||||||
|
for i in range(max(GRID_WIDTH, GRID_HEIGHT) + 1):
|
||||||
|
# Vertical lines
|
||||||
|
if i <= GRID_WIDTH:
|
||||||
line_x = grid_left + i * effective_cell_size
|
line_x = grid_left + i * effective_cell_size
|
||||||
if 0 <= line_x <= SCREEN_WIDTH:
|
if 0 <= line_x <= SCREEN_WIDTH:
|
||||||
start_y = max(0, grid_top)
|
start_y = max(0, grid_top)
|
||||||
end_y = min(SCREEN_HEIGHT, grid_bottom)
|
end_y = min(SCREEN_HEIGHT, grid_bottom)
|
||||||
if start_y < end_y:
|
if start_y < end_y:
|
||||||
pygame.draw.line(screen, GRAY, (line_x, start_y), (line_x, end_y))
|
vertical_lines.append(((line_x, start_y), (line_x, end_y)))
|
||||||
|
|
||||||
# Draw horizontal grid lines
|
# Horizontal lines
|
||||||
for i in range(GRID_HEIGHT + 1):
|
if i <= GRID_HEIGHT:
|
||||||
line_y = grid_top + i * effective_cell_size
|
line_y = grid_top + i * effective_cell_size
|
||||||
if 0 <= line_y <= SCREEN_HEIGHT:
|
if 0 <= line_y <= SCREEN_HEIGHT:
|
||||||
start_x = max(0, grid_left)
|
start_x = max(0, grid_left)
|
||||||
end_x = min(SCREEN_WIDTH, grid_right)
|
end_x = min(SCREEN_WIDTH, grid_right)
|
||||||
if start_x < end_x:
|
if start_x < end_x:
|
||||||
pygame.draw.line(screen, GRAY, (start_x, line_y), (end_x, line_y))
|
horizontal_lines.append(((start_x, line_y), (end_x, line_y)))
|
||||||
|
|
||||||
|
# Draw all vertical lines in one batch
|
||||||
|
for start, end in vertical_lines:
|
||||||
|
pygame.draw.line(screen, GRAY, start, end)
|
||||||
|
|
||||||
|
# Draw all horizontal lines in one batch
|
||||||
|
for start, end in horizontal_lines:
|
||||||
|
pygame.draw.line(screen, GRAY, start, end)
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user