Skip to content

Commit b9cdb0e

Browse files
committed
switch to using pygame
1 parent 1044594 commit b9cdb0e

File tree

3 files changed

+935
-804
lines changed

3 files changed

+935
-804
lines changed

elkplot/renderer.py

Lines changed: 28 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55

66
import numpy as np
77
import shapely
8-
from pyglet import window, gl, app, canvas
9-
from pyglet.graphics import Batch, Group
10-
from pyglet import shapes
8+
from os import environ
9+
10+
environ["PYGAME_HIDE_SUPPORT_PROMPT"] = "1"
11+
import pygame
1112

1213
COLORS = [
1314
(0, 0, 255, 255), # blue
@@ -26,37 +27,12 @@ def _random_color(rng: np.random.Generator) -> tuple[int, int, int, int]:
2627
return (r, g, b, 255)
2728

2829

29-
def _batch_drawings(
30-
layers: list[shapely.MultiLineString], height: float, dpi: float
31-
) -> Batch:
32-
rng = np.random.default_rng()
33-
my_colors = COLORS + [_random_color(rng) for _ in range(len(layers) - len(COLORS))]
34-
batch = Batch()
35-
for i, layer in enumerate(layers):
36-
color = my_colors[i]
37-
path: shapely.LineString
38-
for path in shapely.get_parts(layer):
39-
grp = Group()
40-
screen_coords = [(dpi * x, dpi * (height - y)) for x, y in path.coords]
41-
vertices = (
42-
screen_coords[0] + tuple(chain(*screen_coords)) + screen_coords[-1]
43-
)
44-
batch.add(
45-
len(vertices) // 2,
46-
gl.GL_LINE_STRIP,
47-
grp,
48-
("v2f", vertices),
49-
("c4B", color * (len(vertices) // 2)),
50-
)
51-
return batch
52-
53-
5430
def render(
55-
drawings: list[shapely.MultiLineString],
31+
layers: list[shapely.MultiLineString],
5632
width: float,
5733
height: float,
5834
dpi: float = 64,
59-
bg_color: tuple[float, ...] = (0, 0, 0),
35+
bg_color: tuple[int, int, int] = (0, 0, 0),
6036
) -> None:
6137
"""
6238
NOTE: You will probably not want to call this directly and instead use elkplot.draw
@@ -69,22 +45,25 @@ def render(
6945
:param dpi: How large would you like the preview shown in screen pixels per plotter-inch
7046
:return:
7147
"""
72-
batch = _batch_drawings(drawings, height, dpi)
73-
config = gl.Config(sample_buffers=1, samples=8, double_buffer=True)
74-
win = window.Window(
75-
int(width * dpi),
76-
int(height * dpi),
77-
"plot preview",
78-
config=config,
79-
)
80-
81-
@win.event
82-
def on_draw():
83-
gl.glEnable(gl.GL_LINE_SMOOTH)
84-
win.clear()
85-
shapes.Rectangle(
86-
0, int(height * dpi), int(width * dpi), int(height * dpi), color=bg_color, batch=None
87-
).draw()
88-
batch.draw()
89-
90-
app.run()
48+
pygame.init()
49+
win = pygame.display.set_mode((int(dpi * width), int(dpi * height)))
50+
rng = np.random.default_rng()
51+
my_colors = COLORS + [_random_color(rng) for _ in range(len(layers) - len(COLORS))]
52+
run = True
53+
first_run = True
54+
while run:
55+
for event in pygame.event.get():
56+
if event.type == pygame.QUIT:
57+
run = False
58+
if not first_run:
59+
continue
60+
win.fill(bg_color)
61+
for i, layer in enumerate(layers):
62+
color = my_colors[i]
63+
path: shapely.LineString
64+
for path in shapely.get_parts(layer):
65+
screen_coords = [(dpi * x, dpi * (height - y)) for x, y in path.coords]
66+
pygame.draw.lines(win, color, False, screen_coords)
67+
pygame.display.flip()
68+
first_run = False
69+
pygame.quit()

0 commit comments

Comments
 (0)