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

Re: [pygame] Alpha blending



> I myself have a few problems related to alpha blending
> but it is related to surface created with Font.render
> 
> Basically, if i antialias my text i get a free per pixel alpha (it
> looks like) but I cannot apply a global alpha on the surface.
> If I do not antialias my text i do not manage to get a transparent
> background but then the global alpha works?

yes, this is a "feature" of SDL. it specifically does not
mix per-pixel alpha and the global-surface alpha. once a
surface has a pixel alpha channel, the overall surface alpha
stops being used.

there's not much we can do here. perhaps later version of SDL
will support both, but for now it's one or the other. unless...

you can use the recently added alpha routines in the surfarray
module. here is an example of how you could render smooth text
with an alpha channel, when set the overall alpha to 50%.

>>> import pygame.surfarray as surfarray
>>> mytext = myfont.render('Ghosted', 1, (255, 255, 255))
>>> alpha = surfarray.pixels_alpha(mytext)
>>> alpha /= 2

makes sense? we render the text with an alpha channel, then
divide all the alpha values by two. this gives us transparent
text. the other way to cheat it will be a little faster, but
not quite as nice. use smooth rendering, but choose an opaque
background. then enable colorkeying and you can set the overall
surface transparancy.

>>> mytext = myfont.render('Ghosted', 1, (255, 255, 255), (0, 0, 0))
>>> mytext.set_alpha(128)
>>> mytext.set_colorkey((0, 0, 0), RLEACCEL)

this version may have a slightly halo'd edge around the text, but
as long as you pick a background color that matches the real
background in your game, it will be close enough.


unfortunately, both these aren't as simple as we'd hope.
i've mentioned this to the SDL developers before, and it doesn't
sound like it will be changed immediately.




> I try to blit a Surface into the screen with a bit of the original
> surface being offscreen at the top left. I was surprised to see that
> the clipping only works in the bottom right!
> Basically unless the topleft of the surface to blit into the screen
> is in the screen, nothing will be blit!

hmm, are you sure about this? attached is a program i tested with
early on that does the job for me. my guess is that maybe the
rectangles you pass to display.update() are not being clipped
properly? try just calling display.flip() to update the your whole
display area and see if they are there.

there could be a bug in the display.update() call. it is supposed
to fix any rectangles you pass it, but it may be broken. i'll look
into this a little more.




> last, I had some trouble compiling the SMPEG library so I left
> it aside but it looks very similar to what exists for music.mixer
> and would be then easy to integrate. If I find a bit of time, I
> will try to do it.

i believe SMPEG renders into SDL_Overlay objects. these overlays
are very similar to SDL_Surfaces, but use different color formats.
and work with different hardware. most of the work will be adding
support for these Overlay objects. it wouldn't be too hard to just
do the basics, but full support for overlays and color conversions
will be a bit more effort. once those are ready adding the calls
for SMPEG should be pretty easy.

also, SMPEG is included with the precompiled binaries for windows.
there should be a "prebuilt" directory in your windows installation
of pygame. you can use the libraries and headers from there, no
need to compile it. (this assumes i remember correctly that you
are on windows) :]

negblit.zip