summaryrefslogtreecommitdiff
path: root/external/pytmx/util_pyglet.py
blob: 6b4a38abe070e6f97d28742870b28a9c8ad0f888 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import logging

logger = logging.getLogger(__name__)
ch = logging.StreamHandler()
ch.setLevel(logging.INFO)
logger.addHandler(ch)
logger.setLevel(logging.INFO)

try:
    import pyglet
except ImportError:
    logger.error('cannot import pyglet (is it installed?)')
    raise

import pytmx


def pyglet_image_loader(filename, colorkey, **kwargs):
    """basic image loading with pyglet

    returns pyglet Images, not textures

    This is a basic proof-of-concept and is likely to fail in some situations.

    Missing:
        Transparency
        Tile Rotation

    This is slow as well.
    """
    if colorkey:
        logger.debug('colorkey not implemented')

    image = pyglet.image.load(filename)

    def load_image(rect=None, flags=None):
        if rect:
            try:
                x, y, w, h = rect
                y = image.height - y - h
                tile = image.get_region(x, y, w, h)
            except:
                logger.error('cannot get region %s of image', rect)
                raise
        else:
            tile = image

        if flags:
            logger.error('tile flags are not implemented')

        return tile

    return load_image


def load_pyglet(filename, *args, **kwargs):
    kwargs['image_loader'] = pyglet_image_loader
    kwargs['invert_y'] = True
    return pytmx.TiledMap(filename, *args, **kwargs)