4

For some reason, I can't get pyglet to draw sprites. Here's my code:

import pyglet

game = pyglet.window.Window(640, 480, "I'm a window")

batch = pyglet.graphics.Batch()

pyglet.resource.path = ["."]
pyglet.resource.reindex()

image = pyglet.resource.image("hextile.png")
pyglet.sprite.Sprite(image, x=200, y=300, batch=batch)
pyglet.text.Label('DING', font_name='Arial', font_size=24, x=100, y=100, batch=batch)

@game.event
def on_draw():

    game.clear()
    batch.draw()
    #image.blit(0, 0)

pyglet.app.run()

Now, when I draw the batch, the text label is shown correctly. I see "DING" on the window. However, the image "hextile.png" is not shown. I tried drawing the sprite independently, but that didn't work either. Blitting the image (as shown in the commented line), however, seems to work just fine, but obviously that's not quite the functionality I'm after here. I can't figure this one out. What am I missing?

SnowFatal
  • 2,431
  • 1
  • 15
  • 12
  • Do you happen to have an ATI/AMD graphics card? Might be related with this: http://stackoverflow.com/questions/9369357/pyglet-vertex-list-not-rendered-amd-driver See my comment there as well. – Eric Mar 20 '12 at 22:35

2 Answers2

5

Assuming you and your friends have ATI graphics cards:

Sprite.draw() uses the v2i format and VertexDomain.draw() internally. For some reason this combination doesn't work on Windows Vista/7 Catalyst drivers 11.9 and above, and consequently Sprite drawing fails as well. See also: pyglet vertex list not rendered (AMD driver?)

There is a pyglet issue you might want to follow: http://code.google.com/p/pyglet/issues/detail?id=544

Your options for the moment seem to be either to patch pyglet.sprite.Sprite as mentioned in the third comment on that issue or downgrade your video driver.

Update: No need to patch Sprite or downgrade your video driver. This problem seems to be fixed with Catalyst 12.4 (video driver 8.961.0.0).

Community
  • 1
  • 1
Eric
  • 1,356
  • 14
  • 24
1

The sprite is getting garbage collected because you don't hold a reference to it. Do this:

sprite = pyglet.sprite.Sprite(image, x=200, y=300, batch=batch)

For what it's worth, I prefer using a subclass of Window, like this: (this code works for me too)

import pyglet

class Window(pyglet.window.Window):
    def __init__(self, *args, **kwargs):
        super(Window, self).__init__(*args, **kwargs)
        self.batch = pyglet.graphics.Batch()
        image = pyglet.resource.image('hextile.png')
        self.sprite = pyglet.sprite.Sprite(image, batch=self.batch)
    def on_draw(self):
        self.clear()
        self.batch.draw()

def main():
    window = Window(width=640, height=480, caption='Pyglet')
    pyglet.app.run()

if __name__ == '__main__':
    main()
FogleBird
  • 74,300
  • 25
  • 125
  • 131
  • I tested your version and my version. My version worked. How big is hextile.png? Try moving it closer to the origin? – FogleBird Mar 14 '12 at 00:06
  • Here's my png: http://i44.tinypic.com/2wodc7t.png I tried with a few other pngs, and I didn't get different results. What do you mean by "closer to the origin"? – SnowFatal Mar 14 '12 at 00:10
  • Take the code you posted, and put `sprite = ` where you create your sprite. No other changes. Your .png looks fine. I was concerned it might be off the screen. – FogleBird Mar 14 '12 at 00:11
  • Yes, I did try that, I even tried `sprite = pyglet.sprite.Sprite(image, x=200, y=300, batch=batch) @game.event def on_draw(): game.clear() sprite.draw()` to no avail. Also, I thought that this way of drawing stuff was the standard as it's used in pretty much all tutorials I've ran across? – SnowFatal Mar 14 '12 at 00:14
  • This is most strange. I did send my code to two of my friends and they experienced same problem as I did, perhaps there's something wrong with my pyglet? They had the same pyglet setup as I. Thanks, friend. I'll look into this. – SnowFatal Mar 14 '12 at 00:18
  • I'm using Python 2.7.2 and Pyglet 1.1.4 – FogleBird Mar 14 '12 at 00:19
  • Same here. Running on 64 bit Windows 7. I'll look into the matter some more. – SnowFatal Mar 14 '12 at 00:22
  • Oddly this doesn't work, either. I just reinstalled pyglet and double-checked my Python version, so I'm rather sure my libraries are in order. I'm truly at a loss here. Truly, I appreciate your help. I'll try your code tomorrow at school and see how it goes. – SnowFatal Mar 14 '12 at 00:47