Hello,

I'm trying to *efficiently* draw a tilemap in my game. At first i had
a Sprite instance for each of my tiles, and i thought that adding &
removing them from the viewport was the cause of those freezes but it
appears that i'm having the same problem with a lower-level version of
it, here is my code :

import pyglet
from pyglet.gl import *
from pyglet.window import key

window = pyglet.window.Window(width=800,height=800,vsync=0)
batch = pyglet.graphics.Batch()
keys = pyglet.window.key.KeyStateHandler()
window.push_handlers(keys)
fps_display = pyglet.clock.ClockDisplay()

class TextureState:
    def __init__(self, texture):
        self.texture = texture
        self.parent = None

    def set_state(self):
        glEnable(self.texture.target)
        glBindTexture(self.texture.target, self.texture.id)

    def unset_state(self):
        glDisable(self.texture.target)

# Loading texture
spritesheet = pyglet.image.load('zelda2nestiles.png')
tile_seq = pyglet.image.ImageGrid(spritesheet, 12, 15)
tile_tex = pyglet.image.TextureGrid(tile_seq)
mytex = tile_tex[(5,5)]

rows = cols = 60
chunk_size = 128
width = height = 16

def gen_vertex(x,y1):
    vertex_data = []
    texture_data = []
    for row in range(rows):
        y2 = y1 + height
        x1 = x
        for col in range(cols):
            x2 = x1 + width
            vertex_data.extend([x1, y1, x2, y1, x2, y2, x1, y2])
            texture_data.extend(mytex.tex_coords)
            x1 = x2
        y1 = y2
    return vertex_data, texture_data

vertex_data, texture_data = gen_vertex(0,0)
v = batch.add(cols * rows * 4, GL_QUADS, TextureState(mytex),
    ('v2f', vertex_data),
    ('t3f', texture_data))

# Init vars
camx = camy = 800/2
step = 5
active_chunk = (424242,424242)

def update(dt):
    global camx, camy, active_chunk

    chunk = (-camx / chunk_size, -camy /chunk_size)
    if not chunk == active_chunk:
        active_chunk = chunk
        x,y = chunk[0]*chunk_size, chunk[1]*chunk_size
        vertex_data, texture_data = gen_vertex(x,y)
        v.vertices = vertex_data

    if keys[key.Z]:
        camy -= step
    if keys[key.S]:
        camy += step
    if keys[key.Q]:
        camx += step
    if keys[key.D]:
        camx -= step

@window.event
def on_draw():
    window.clear()

    glPushMatrix()
    glTranslatef(camx, camy, 0)
    batch.draw()
    glPopMatrix()

    fps_display.draw()

pyglet.clock.schedule_interval(update, 1/60.)
pyglet.app.run()

More readable version : http://paste2.org/p/1801849
The spritesheet : http://www.dwedit.org/sprites/zelda2nestiles.png

Micro freezes appear several times per second when moving the camera
(with ZQSD). My code regenerate the vertex array every 128 pixels, so
it doesn't appear to be linked to my problem, i also tried with the -O
argument.

-- 
You received this message because you are subscribed to the Google Groups 
"pyglet-users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/pyglet-users?hl=en.

Reply via email to