[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [pygame] Introductions and question about transparency



> > you say you are loading from XPM images. i'm not sure if XPM images
> > can store a colorkey value like GIF and PNG can. i'm guessing it does
> > not, since the image loaders in SDL_image will usually preserve any
> > transparancy/colorkey values in the image.
> 
> I thought the same thing at first, so I decided to investigate. The Gimp 
> seems to believe the background is transparent (checkered pattern) and 
> displays some truly-white pixels in some tiles (ie arctic).

ok, it sounds like it is time to investigate this image and SDL_image
one step further.

    img = pygame.image.load(tile_filename)  #no convert call
    print 'COLORKEY:', img.get_colorkey()

that will either print "None" or give you a color values of the
colorkey value SDL_image is setting for the image when loaded.
note we don't "convert" the image here.


> I surmised maybe SDL_Image does not support XPM transparency. So I tried 
> loading my bigtile from the converted GIF and PNG images. That did not work 
> either. I am a bit puzzled.

hmm, i've had problems saving colorkeys from gimp before, make sure the
background color is set properly before saving the image. try the above
test of printing the colorkey value on your saved GIF and PNG images.


> I would really like the image's transparency to work normally, but I did try 
> your advice. I learned a bit doing it too. :-) It did not work immediately, 
> but I remembered your docs and called set_colorkey on the subsurfaces 
> instead. That worked.

doh, i was under the impression subsurfaces would inherit their parent
surface flags when created, owell, guess again. i'll have too look into
the pygame code on this one.

here is another way you can set your colormap. take a look at the image
in any paint package. find the colormap index of the color being used
as the colorkey. when you load the image, you can use this value for
the colormap before calling convert. also because you need to set the
colorkey in the subsurfaces, you'll probably need to make a couple other
small changes to the loading...

    colorkey_value = 255 # this is the index the map uses for the colorkey
    bigtile_surface = pygame.image.load(tile_filename) #again, no convert
    dx, dy = metadata["dx"], metadata["dy"] # Information from the spec file
    # Iterate over all tile names
    for tag in coords.keys():
        x, y = coords[tag][0] * dx, coords[tag][1] * dy
        tile = bigtile_surface.subsurface((x, y, dx, dy))
        tile.set_colorkey(colorkey_value, RLEACCEL)
        tiles[tag] = tile.convert()

note, the RLEACCEL will speed things up a bit when using software surfaces.
the convert() call on the last line will make the tiles use their own copy
of the bigtile image. they will no longer directly share data with the
bigtile_surface. in fact you can delete that bigtile after everything has
been loaded. as always, you will want to convert() the loaded images after
you have gotten them setup however you want. un-converted images will be
a lot slower when blitting to mismatched formats.

well, that is still not "automatic" but it should properly preserve
the colormapping that whoever created the image intended. as long as
all your maps use the same color index for the colorkey, it will work
with everything.

i'm guessing the real problem is that SDL_image is not using the colorkey
from XPM files. and i'm hoping the reason it doesn't work with GIF/PNG is
because gimp wasn't saving them correctly ?



____________________________________
pygame mailing list
pygame-users@seul.org
http://pygame.seul.org