Split acquiring data from rendering, remake run func

master
LEdoian 3 years ago
parent c0acb24491
commit 156e08a3cf

@ -14,7 +14,8 @@ colors = {
BORDERCOLOR = (64,64,64)
W = 1920
W = 1920 // 2 - 2
W = 1920 - 2
H = 1040
CELLSIZE = 8
CELLBORDER = 0
@ -45,22 +46,26 @@ class Visualisation:
self.mapfile = MapFile(fn, **kwargs)
self.mapfile.load()
def draw(self):
self.disp.fill((0,0,0)) # Dark theme :-)
self.squares = None
def gen_squares(self):
self.squares = []
sz = self.mapfile.size
sqsz = max(sz / CELLS, 4096)
print(f"INFO: Each square represents {sqsz} bytes")
for cid in range(CELLS):
start = cid * sqsz
end = (cid + 1) * sqsz
hist = self.mapfile.get_hist(start, end)
symb = get_symb(hist)
color = colors[symb]
self.squares.append(color)
line = cid // COLUMNS
col = cid % COLUMNS
def draw(self):
self.disp.fill((0,0,0)) # Dark theme :-)
if self.squares is None: self.gen_squares()
for cell, color in enumerate(self.squares):
line = cell // COLUMNS
col = cell % COLUMNS
pos = (line, col)
rect = pygame.Rect(col * CELLOFFSET + CELLBORDER, line * CELLOFFSET + CELLBORDER, CELLSIZE+2*CELLBORDER, CELLSIZE+2*CELLBORDER)
@ -71,18 +76,22 @@ class Visualisation:
def reload(self):
self.mapfile.load()
self.squares = None
self.draw()
def run(self, *, refresh=2):
pygame.time.set_timer(pygame.USEREVENT, refresh * 1000)
# pygame.time.set_timer(pygame.USEREVENT, refresh * 1000)
pygame.event.set_blocked(None)
pygame.event.set_allowed([pygame.USEREVENT, pygame.VIDEORESIZE, pygame.VIDEOEXPOSE, pygame.QUIT, pygame.KEYDOWN])
# pygame.event.set_allowed([pygame.USEREVENT, pygame.VIDEORESIZE, pygame.VIDEOEXPOSE, pygame.QUIT, pygame.KEYDOWN])
pygame.event.set_allowed([pygame.VIDEORESIZE, pygame.VIDEOEXPOSE, pygame.QUIT, pygame.KEYDOWN])
self.draw()
while True:
ev = pygame.event.wait()
if ev.type == pygame.QUIT or ev.type == pygame.KEYDOWN and ev.key == pygame.K_q:
return
if ev.type in [pygame.VIDEOEXPOSE, pygame.VIDEORESIZE, pygame.USEREVENT]:
if ev.type in [pygame.VIDEOEXPOSE, pygame.VIDEORESIZE]:
self.draw()
if ev.type == pygame.KEYDOWN and ev.key == pygame.K_r:
self.reload()

@ -30,6 +30,4 @@ args = ap.parse_args()
visu = v.Visualisation(args.file, start=args.start, end=args.end)
visu.run(refresh=35)

Loading…
Cancel
Save