Skip to content

Commit

Permalink
Merge pull request #181 from benmoran56/pyglet_fixes
Browse files Browse the repository at this point in the history
Fix pyglet_demo.py
  • Loading branch information
bitcraft authored Jan 19, 2024
2 parents da50ec0 + 00709ba commit 7af805b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 23 deletions.
46 changes: 24 additions & 22 deletions apps/pyglet_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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)
2 changes: 1 addition & 1 deletion pytmx/util_pyglet.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 7af805b

Please sign in to comment.