Exemple #1
0
    def resetEng(self, eng):
        self._engine = eng
        self.animated = dict()
        self.visibleAnimated = dict()
        del self.domain
        self.domain = vertexdomain.create_domain(*('v2i', 't3f'))
        self.nullDomain = vertexdomain.create_domain(*('v2i', 't3f'))

        if self.regions is not None:
            for row in xrange(self.rows):
                for column in xrange(self.columns):
                    self.regions[row][column].delete()

        if eng is None:
            return
        self.rows = int(math.ceil(float(eng.getWidth()) / self.regionSize))
        self.columns = int(math.ceil(float(eng.getHeight()) / self.regionSize))

        self.regions = create2dArray(self.rows, self.columns)
        self.regionsVisible = create2dArray(self.rows, self.columns, True)
        for row in xrange(self.rows):
            for column in xrange(self.columns):
                texCoords = []
                vertices = []
                sX = row * self.regionSize
                sY = column * self.regionSize
                for x in xrange(sX, sX + self.regionSize):
                    for y in xrange(sY, sY + self.regionSize):
                        x1 = int(x * TILESIZE)
                        y1 = int(y * TILESIZE)
                        x2 = int(x1 + TILESIZE)
                        y2 = int(y1 + TILESIZE)
                        if eng.testBounds(x, y):
                            vertices.extend([x1, y1, x2, y1, x2, y2, x1, y2])
                            tImg = self.tileImagesLoader.getTileImage(
                                eng.getTile(x, y))
                            try:
                                texCoords.extend(tImg.tex_coords)
                            except AttributeError:
                                assert isinstance(tImg, TileAnimation)
                                texCoords.extend(
                                    tImg.frames[0].image.tex_coords)
                                self.visibleAnimated[(x, y)] = tImg
                        else:
                            vertices.extend([0, 0, 0, 0, 0, 0, 0, 0])
                            texCoords.extend([
                                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.
                            ])
                self.regions[row][column] = self._add(
                    len(vertices) // 2, vertices, texCoords)
Exemple #2
0
    def _get_domain(self, indexed, mode, group, formats):
        if group is None:
            group = null_group

        # Batch group
        if group not in self.group_map:
            self._add_group(group)

        domain_map = self.group_map[group]

        # Find domain given formats, indices and mode
        key = (formats, mode, indexed)
        try:
            domain = domain_map[key]
        except KeyError:
            # Create domain
            if indexed:
                domain = vertexdomain.create_indexed_domain(*formats)
            else:
                domain = vertexdomain.create_domain(*formats)
            domain.__formats = formats
            domain_map[key] = domain
            self._draw_list_dirty = True

        return domain
Exemple #3
0
    def _get_domain(self, indexed, mode, group, formats):
        if group is None:
            group = null_group
        
        # Batch group
        if group not in self.group_map:
            self._add_group(group)

        domain_map = self.group_map[group]

        # Find domain given formats, indices and mode
        key = (formats, mode, indexed)
        try:
            domain = domain_map[key]
        except KeyError:
            # Create domain
            if indexed:
                domain = vertexdomain.create_indexed_domain(*formats)
            else:
                domain = vertexdomain.create_domain(*formats)
            domain.__formats = formats
            domain_map[key] = domain
            self._draw_list_dirty = True 

        return domain
Exemple #4
0
    def _get_domain(self, indexed, mode, group, formats):
        if group is None:
            group = get_default_group()

        # Batch group
        if group not in self.group_map:
            self._add_group(group)

        # If not a ShaderGroup, use the default ShaderProgram
        shader_program = getattr(group, 'program', get_default_shader())

        # Find domain given formats, indices and mode
        domain_map = self.group_map[group]
        key = (formats, mode, indexed, shader_program.id)
        try:
            domain = domain_map[key]
        except KeyError:
            # Create domain
            if indexed:
                domain = vertexdomain.create_indexed_domain(
                    shader_program.id, *formats)
            else:
                domain = vertexdomain.create_domain(shader_program.id,
                                                    *formats)
            domain.__formats = formats
            domain_map[key] = domain
            self._draw_list_dirty = True

        return domain
Exemple #5
0
    def resetEng(self, eng):
        self._engine = eng
        self.animated = dict()
        self.visibleAnimated = dict()
        del self.domain
        self.domain = vertexdomain.create_domain(*('v2i', 't3f'))
        self.nullDomain = vertexdomain.create_domain(*('v2i', 't3f'))

        if self.regions is not None:
            for row in xrange(self.rows):
                for column in xrange(self.columns):
                    self.regions[row][column].delete()

        if eng is None:
            return
        self.rows = int(math.ceil(float(eng.getWidth()) / self.regionSize))
        self.columns = int(math.ceil(float(eng.getHeight()) / self.regionSize))

        self.regions = create2dArray(self.rows, self.columns)
        self.regionsVisible = create2dArray(self.rows, self.columns, True)
        for row in xrange(self.rows):
            for column in xrange(self.columns):
                texCoords = []
                vertices = []
                sX = row * self.regionSize
                sY = column * self.regionSize
                for x in xrange(sX, sX + self.regionSize):
                    for y in xrange(sY, sY + self.regionSize):
                        x1 = int(x * TILESIZE)
                        y1 = int(y * TILESIZE)
                        x2 = int(x1 + TILESIZE)
                        y2 = int(y1 + TILESIZE)
                        if eng.testBounds(x, y):
                            vertices.extend([x1, y1, x2, y1, x2, y2, x1, y2])
                            tImg = self.tileImagesLoader.getTileImage(eng.getTile(x, y))
                            try:
                                texCoords.extend(tImg.tex_coords)
                            except AttributeError:
                                assert isinstance(tImg, TileAnimation)
                                texCoords.extend(tImg.frames[0].image.tex_coords)
                                self.visibleAnimated[(x, y)] = tImg
                        else:
                            vertices.extend([0, 0, 0, 0, 0, 0, 0, 0])
                            texCoords.extend([0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.])
                self.regions[row][column] = self._add(len(vertices) // 2, vertices, texCoords)
Exemple #6
0
 def create_vertexlist(self):
     usage = create_attribute_usage(f'{self.draw_mode}/dynamic')
     vd = create_domain(usage)
     vertex_list = vd.create(self._n_vertices)
     if self._tex_coords:
         vertex_list.tex_coords = self._tex_coords
     vertex_list.vertices = self._vertices
     vertex_list.normals = self._normals
     return vertex_list
Exemple #7
0
    def __init__(self, tileImagesLoader, group):
        self._engine = None
        self.tileImagesLoader = tileImagesLoader
        self.texture = self.tileImagesLoader.getTexture()
        self.animations = self.tileImagesLoader.getAnimations()

        self.animated = dict()
        self.visibleAnimated = dict()

        self.domain = vertexdomain.create_domain(*('v2i', 't3f'))
        self.nullDomain = None
        self.group = group

        self.regionSize = 6
        self.regions = None
        self.regionsVisible = None
Exemple #8
0
    def __init__(self, tileImagesLoader, group):
        self._engine = None
        self.tileImagesLoader = tileImagesLoader
        self.texture = self.tileImagesLoader.getTexture()
        self.animations = self.tileImagesLoader.getAnimations()

        self.animated = dict()
        self.visibleAnimated = dict()

        self.domain = vertexdomain.create_domain(*('v2i', 't3f'))
        self.nullDomain = None
        self.group = group

        self.regionSize = 6
        self.regions = None
        self.regionsVisible = None
Exemple #9
0
def draw_sprites(domain, texture):
    glPushAttrib(GL_CURRENT_BIT | GL_ENABLE_BIT)

    glEnable(GL_BLEND)
    glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA)
    glColor4f(.2, .2, .2, .2)
    glEnable(GL_TEXTURE_2D)
    glBindTexture(GL_TEXTURE_2D, texture.id)
    domain.draw(GL_QUADS)

    glPopAttrib()

    domain = vertexdomain.create_domain('v2f/static', 't2f/static')

    sprites = [Sprite(domain) for i in range(SPRITES)]
    fps = clock.ClockDisplay(color=(1, 1, 1, 1))

    texture = image.load(SPRITE_IMAGE).texture

    if SPRITE_UPDATE_N:
        update_n = 0

    while not win.has_exit:
        dt = clock.tick()
        if dt == 0:
            dt = 0.01

        win.dispatch_events()

        if SPRITE_UPDATE_N > 1:
            # Update small number of sprites
            for sprite in sprites[update_n:update_n + SPRITE_UPDATE_N]:
                sprite.update(dt)
            update_n = (update_n + SPRITE_UPDATE_N) % len(sprites)
        elif SPRITE_UPDATE_N:
            # Update all sprites
            for sprite in sprites:
                sprite.update(dt)
        # Otherwise, update no sprites (static)

        win.clear()
        draw_sprites(domain, texture)
        fps.draw()
        win.flip()
Exemple #10
0
def draw_sprites(domain, texture):
    glPushAttrib(GL_CURRENT_BIT | GL_ENABLE_BIT)

    glEnable(GL_BLEND)
    glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA)
    glColor4f(.2, .2, .2, .2)
    glEnable(GL_TEXTURE_2D)
    glBindTexture(GL_TEXTURE_2D, texture.id)
    domain.draw(GL_QUADS)

    glPopAttrib()

    domain = vertexdomain.create_domain('v2f/static', 't2f/static')

    sprites = [Sprite(domain) for i in range(SPRITES)]
    fps = clock.ClockDisplay(color=(1, 1, 1, 1))

    texture = image.load(SPRITE_IMAGE).texture

    if SPRITE_UPDATE_N:
        update_n = 0

    while not win.has_exit:
        dt = clock.tick()
        if dt == 0:
            dt = 0.01

        win.dispatch_events()

        if SPRITE_UPDATE_N > 1:
            # Update small number of sprites
            for sprite in sprites[update_n:update_n + SPRITE_UPDATE_N]:
                sprite.update(dt)
            update_n = (update_n + SPRITE_UPDATE_N) % len(sprites)
        elif SPRITE_UPDATE_N:
            # Update all sprites
            for sprite in sprites:
                sprite.update(dt)
        # Otherwise, update no sprites (static)

        win.clear()
        draw_sprites(domain, texture)
        fps.draw()
        win.flip()
Exemple #11
0
def draw_sprites(domain, texture):
    glPushAttrib(GL_CURRENT_BIT | GL_ENABLE_BIT)

    glEnable(GL_BLEND)
    glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA)
    glColor4f(.2, .2, .2, .2)
    glEnable(GL_TEXTURE_2D)
    glBindTexture(GL_TEXTURE_2D, texture.id)
    domain.draw(GL_QUADS)

    glPopAttrib()


if __name__ == '__main__':
    domain = vertexdomain.create_domain('v2f/static', 't2f/static')

    sprites = [Sprite(domain) for i in range(SPRITES)]
    fps = clock.ClockDisplay(color=(1, 1, 1, 1))

    texture = image.load(SPRITE_IMAGE).texture

    if SPRITE_UPDATE_N:
        update_n = 0

    while not win.has_exit:
        dt = clock.tick()
        if dt == 0:
            dt = 0.01

        win.dispatch_events()