Sunday, December 17, 2017

sdl - When should I use color keying vs PNG transparency?


SDL (and other toolkits) can use color keying to make a certain color in an image transparent. Alternatively, you could load a PNG image that supports transparency.


When should I use which?



Answer



JPG is lossy. Don't use that for sprites -- you will end up with nasty artifacts that will look bad. There's a couple reasons you might want to use colur-keying, but they're a bit lost in todays hardware. Taking a quick look at color key advantages:


Pros




  • They use up less disk space -- there is no alpha channel to store

  • By consequence, their memory foot print is lower because of this

  • Can work for older technologies that do not support alpha channels


Cons



  • Transparency cannot be spotted easily in more subtle spots of images

  • You lose a color you can use from your palette

  • This can be computationally expensive in some cases.



A note about CPU complexity


When you use a color key mapped image, you are still converting this to an image with alpha channels when it reaches the video RAM in all cases. You will need an additional channel to record these pixels as an alpha of 0. Due to this, a pass must be made on the image at load time to account for this. Unfortunately, the consequence for this is a higher overhead during loading. The time may be of no consequence for few images, but it is worth taking note.


In the end, PNG transparency is used mostly in todays standards game as it easy to use and widely decodable for the most part (such as with SDL). Most popular game engines no longer support color keying, this method was mostly used in older engines and systems.


Off the top of my head, libGDX and XNA both do not support color keying. However, the latter does support it on its content pipeline. Which leads into another interesting option...


You can process it at build time


If you really wanted to use color keying to save disk space because you were working on a tight budget disk space wise, you can always choose to store the assets in a color key format and then have them changed and built at build time to the format that is most appropriate.


Concluding


However, in the end do not use JPG for something like a sprite. If you want to use JPGs, use them on something that lossy behavior is acceptable, like 3D textures or photographs of real objects where picture blur is more or less unnoticed.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...