Author Topic: Flixtra - A Flixel library (with Palette Swapping)  (Read 3313 times)

pixelomatic

  • Active Member
  • ***
  • Posts: 131
  • Karma: +0/-0
    • View Profile


Hello guys! I was thinking about palette swapping and how awesome it could be for people making games with customizable colors, maybe strategy games etc...

For a retro game library like flixel, palette swapping looks like a good way to achieve this, and I made a utility class with static functions (like FlxU) for making it easy. At first, it made sense to make a MutliplePaletteSprite class which extends FlxSprite class, but then I said "There's no need for using inheritance for a simple thing like that." There is no need to get complicated.

I created this utility class within flixtra package, and I think I'll add any other useful classes/functions to this library which I may develop.

For now, palette swapping works like this:
You give function an array of colors, and you give it another array of colors, then function swaps the colors it find in first array with second array, like a[0] = b[0]. I mean, if you gave red to first array's first element, then it will swap every red color in source bitmap with second arrays first element! And it caches the swapped sprite sheet for further use and I believe it's more performance efficient than a real-time swap-blitting solution. I'm sure there are more elegant solutions. (I found waay more shorter code after fiddling with it for some hours.)

This function may be basis for more higher level functions. I'm thinking of, selecting a range of colors and HSV shifting and stuff like that.

BTW, it felt kinda painful to achieve some ideas without hacking into flixel source code.

Edit: If you want a real-time swap, you just need to override your sprite's draw function like that:
Code: [Select]
override public function draw():void
{
FlixtraU.swapColors(framePixels, 0xff9e009e, 0xffff0000);
super.draw();
}
This code does the swap in-place with sprite's current frame buffer!

Demo
Repo & DL
« Last Edit: Thu, Jul 28, 2011 by pixelomatic »

pippinbarr

  • Member
  • **
  • Posts: 26
  • Karma: +0/-0
    • View Profile
Re: Flixtra - A Flixel library (with Palette Swapping)
« Reply #1 on: Tue, Aug 16, 2011 »
Hey, I was just looking for something precisely like this - I'll be giving it a try in my next project. Thanks for making it!

MRKSTWRT

  • Active Member
  • ***
  • Posts: 116
  • Karma: +0/-0
    • View Profile
Re: Flixtra - A Flixel library (with Palette Swapping)
« Reply #2 on: Wed, Aug 24, 2011 »
Great work, using this in my current project.

initials

  • Contributor
  • ****
  • Posts: 378
  • Karma: +0/-0
  • Initials
    • View Profile
    • Initials Blog. Code and other things.
Re: Flixtra - A Flixel library (with Palette Swapping)
« Reply #3 on: Wed, Aug 24, 2011 »
What sort of performance hit does this cause a flash game?
Initials: Super Lemonade Factory, Super Lemonade Factory Part Two, Above The Clouds, Revvolvver, Four Chambers of the Human Heart

pixelomatic

  • Active Member
  • ***
  • Posts: 131
  • Karma: +0/-0
    • View Profile
Re: Flixtra - A Flixel library (with Palette Swapping)
« Reply #4 on: Thu, Aug 25, 2011 »
I don't have a proper testbed so I will make some guessing. Pre-cached spirtes should not cause too much trouble, but real time swapping should be done with care.
« Last Edit: Thu, Aug 25, 2011 by pixelomatic »

larsiusprime

  • New Member
  • *
  • Posts: 4
  • Karma: +0/-0
    • View Profile
Re: Flixtra - A Flixel library (with Palette Swapping)
« Reply #5 on: Tue, Sep 27, 2011 »
I implemented a similar method in my own game. Are you using lots of getPixel()/setPixel() calls to make the function work?

If so, you could optimize it by using Adobe Pixel bender kernels and and using a PBJ shader. For big sprites with lots of animations, it increased performance by some 100x for me.