Description
When displaying tiles in OpenGL formatted coordinate systems (y=0 occurs at bottom of page instead of top of page), there is a simple transformation needed to ensure data appearing at the top of the page has coordinates starting from max(y) rather than 0. Would it make sense to incorporate an image loader that handles this simple, yet common transformation?
See below where I had to subclass TiledMap and override the reload_images
method just to set the image_loader
to one that sets the y ordinate based on the current tileset
's tileheight
and height
. Is there a cleaner, more future-version-compatible-robust way for loading images with correct coordinates for OpenGL displays?
class OpenGLTiledMap(TiledMap):
def reload_images(self):
def GL_image_loader(filename, flags, **kwargs):
""" convert the y coordinate to opengl format where
the bottom-most coordinate in the image is 0
"""
ts = self.fname2ts[filename]
def load((x,y,w,h)=None, flags=None):
GL_y = ts.height - ts.tileheight - y
return filename, (x,GL_y,w,h), flags
return load
self.fname2ts = dict((ts.source, ts) for ts in self.tilesets)
self.image_loader = GL_image_loader
super(OpenGLTiledMap, self).reload_images()
Also, this issue comes up again after you've loaded the images from the tileset and now need to play them as widgets in your OpenGL display window. Again, the conversion is needing to know tileset
specific dimensions. Is there a convenient way to look this up?
For instance, I do something kinda ugly like this:
layer = self.map.get_layer_by_name('ground')
max_y = len(layer.data)
for (x, y, ts_props) in layer.tiles():
# correct the y value for openGL displays
y = max_y - y