From 00709ba60365f5a26606507381dd205972714428 Mon Sep 17 00:00:00 2001 From: Benjamin Moran Date: Thu, 18 Jan 2024 14:46:30 +0900 Subject: [PATCH] Fix pyglet_demo.py Use pyglet.resource.image instead of image.load --- apps/pyglet_demo.py | 46 +++++++++++++++++++++++--------------------- pytmx/util_pyglet.py | 2 +- 2 files changed, 25 insertions(+), 23 deletions(-) diff --git a/apps/pyglet_demo.py b/apps/pyglet_demo.py index 8a5ae32..d47123f 100644 --- a/apps/pyglet_demo.py +++ b/apps/pyglet_demo.py @@ -19,11 +19,13 @@ import pyglet +from pyglet.sprite import Sprite + from pytmx import * from pytmx.util_pyglet import load_pyglet -class TiledRenderer(object): +class TiledRenderer: """ Super simple way to render a tiled map with pyglet @@ -34,15 +36,16 @@ def __init__(self, filename) -> None: tm = load_pyglet(filename) self.size = tm.width * tm.tilewidth, tm.height * tm.tileheight self.tmx_data = tm - self.batches = [] # list of batches, e.g. layers + self.batch = pyglet.graphics.Batch() self.sprites = [] # container for tiles self.generate_sprites() - self.clock_display = pyglet.clock.ClockDisplay() def draw_rect(self, color, rect, width) -> None: + # TODO: use pyglet.shapes pass def draw_lines(self, color, closed, points, width) -> None: + # TODO: use pyglet.shapes pass def generate_sprites(self) -> None: @@ -57,23 +60,22 @@ def generate_sprites(self) -> None: rect_color = (255, 0, 0) poly_color = (0, 255, 0) - for layer in self.tmx_data.visible_layers: - batch = pyglet.graphics.Batch() # create a new batch - self.batches.append(batch) # add the batch to the list + for i, layer in enumerate(self.tmx_data.visible_layers): + # Use Groups to seperate layers inside the Batch: + group = pyglet.graphics.Group(order=i) + # draw map tile layers if isinstance(layer, TiledTileLayer): - # iterate over the tiles in the layer for x, y, image in layer.tiles(): y = mh - y x = x * tw y = y * th - sprite = pyglet.sprite.Sprite(image, batch=batch, x=x, y=y) + sprite = Sprite(image, x, y, batch=self.batch, group=group) self.sprites.append(sprite) # draw object layers elif isinstance(layer, TiledObjectGroup): - # iterate over all the objects in the layer for obj in layer: logger.info(obj) @@ -95,16 +97,14 @@ def generate_sprites(self) -> None: if layer.image: x = mw // 2 # centers image y = mh // 2 - sprite = pyglet.sprite.Sprite(layer.image, batch=batch, x=x, y=y) + sprite = Sprite(layer.image, x, y, batch=self.batch) self.sprites.append(sprite) - def draw(self) -> None: - for b in self.batches: - b.draw() - self.clock_display.draw() + def draw(self): + self.batch.draw() -class SimpleTest(object): +class SimpleTest: def __init__(self, filename) -> None: self.renderer = None self.running = False @@ -142,13 +142,16 @@ def all_filenames(): class TestWindow(pyglet.window.Window): - def on_draw(self) -> None: - if not hasattr(self, "filenames"): - self.filenames = all_filenames() - self.next_map() + def __init__(self, width, height, vsync): + super().__init__(width=width, height=height, vsync=vsync) + self.fps_display = pyglet.window.FPSDisplay(self, color=(50, 255, 50, 255)) + self.filenames = all_filenames() + self.next_map() + def on_draw(self) -> None: self.clear() self.contents.draw() + self.fps_display.draw() def next_map(self) -> None: try: @@ -165,6 +168,5 @@ def on_key_press(self, symbol, mod): if __name__ == "__main__": window = TestWindow(600, 600, vsync=False) - # Add schedule_interval with a dummy callable to force speeding up fps - pyglet.clock.schedule_interval(int, 1.0 / 240) - pyglet.app.run() + pyglet.clock.schedule_interval(window.draw, 1/120) + pyglet.app.run(None) diff --git a/pytmx/util_pyglet.py b/pytmx/util_pyglet.py index 163599f..6e2334a 100644 --- a/pytmx/util_pyglet.py +++ b/pytmx/util_pyglet.py @@ -46,7 +46,7 @@ def pyglet_image_loader(filename, colorkey, **kwargs): if colorkey: logger.debug("colorkey not implemented") - image = pyglet.image.load(filename) + image = pyglet.resource.image(filename) def load_image(rect=None, flags=None): if rect: