Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

2D All the way! / DBPro color swapping for sprites?

Author
Message
Sylon
21
Years of Service
User Offline
Joined: 10th Jun 2005
Location:
Posted: 11th Jun 2005 01:36
I'm a total newbie and ironically I purchased DBPro because there is more to learn and implement in it.

I was wondering, is there a way to code in-game color palette-swapping for sprites?

Like, say I assign 5 numbers to 5 shades of green, red, and blue. All lightest shades would be 1, all darkest would be 5, etc.

Instead of making a totally different-colored sprite for the game, if I just wanted its color scheme to be different, couldn't I code it so that, for example, if it was green (palette #1), and I wanted it to become red (palette #2). I could make a function to say something like, "if yada yada then change "sprite color scheme" to "palette #2". All color shades assigned number 1 in palette 1 (green) will be swapped with color 1 in palette 2 (red), all colors assigned the number 2,3,4,5 in palette 1 would respectively switch to colors 2,3,4,5 in palette 2.

I want to know if it is possible, because I am working on the game art right now for my game and I need to know if I can save myself some effort so I don't have to actually design all sprite color combinations etc. Long story, but anyway. Also, it would save memory wouldn't it I hope?! Soon I will actually learn how to do this stuff, but for now I am playing artist boy.

Thanks for your help!

-[Sylon Shanings]-
pizzaman
22
Years of Service
User Offline
Joined: 18th Feb 2004
Location: Gateshead, UK
Posted: 11th Jun 2005 09:01 Edited at: 11th Jun 2005 09:02
Yes this is certainly possible, I've even coded an example to show you.



Note: The code must be run in 32 bit colour mode to work, due to how colour data is stored in different colour modes.

The code works by making an image into a memblock; the memblock is then scanned for certain colour values of pixels which are stored as dwords (4 bytes). If that certain colour is found then it changes that value to a specified colour value. The memblock is then made back into an image to be used.

There is only one problem with this method and that is it could be slow if you are doing many images, or if the images are large. This would mean you might have to have loading screens. One way to get around this is use the above code to change your image to your specific colours and save the images. Then just use the newly created images in your game - it will not save space but it means you might not have to having loading screens.

Anyways hope it helps.

Sylon
21
Years of Service
User Offline
Joined: 10th Jun 2005
Location:
Posted: 12th Jun 2005 00:52
Oh thanks! Looks like you really put a lot of effort into that example!!

But, it really has to be in 32 bit mode to work? I was planning on making my game in 16 bit to save memory!! Ahh!! Color data isn't stored the same in 16 bit? Man. Game creation is so...frustrating! Hopefully when my game might be finished the computers will be so fast that 32 bit isn't a problem for 2d!!

-[Sylon Shanings]-
Robot
22
Years of Service
User Offline
Joined: 27th May 2004
Location:
Posted: 12th Jun 2005 02:38 Edited at: 12th Jun 2005 02:39
red=((word/2048)&31)*8
green=((word/32)&63)*4
blue=(word&31)*8

I think those might be the formulae for 16 bit

And to put them all together

word=((red&248)*256)+((green&252)*8)+((blue/8))

The happenings of tommorow are behind us now
Sylon
21
Years of Service
User Offline
Joined: 10th Jun 2005
Location:
Posted: 13th Jun 2005 10:12
Phew! Super. Thanks man! BIIIG help, both of you!

-[Sylon Shanings]-
pizzaman
22
Years of Service
User Offline
Joined: 18th Feb 2004
Location: Gateshead, UK
Posted: 13th Jun 2005 11:15
@ Sylon

Yeah you have to be in 32 bit mode for the code to work, however your images don't have to be 32 bit, as your images when loaded in 32 bit mode get automatically converted to 32 bit by DBP, therefore the above code would still work.

Also I'm wondering why you want to save memory? Do you mean internal memory like RAM or VRAM, or just hard drive space? If its hard drive space then just use 8 bit or 16 bit images or an image format like png which compresses the image loss-lessly. If its RAM or VRAM then theres nothing that can be done, but I'm sure you have enough RAM or VRAM to handle whatever your doing.

Quote: "Hopefully when my game might be finished the computers will be so fast that 32 bit isn't a problem for 2d"


Unless you have a really rubish graphics card (no offense meant), then 2d in 32 bit mode shouldn't be a problem. I have a 32 MB graphics which was the normal to have about 4 years ago, and I can manage about a 1000 sprites being drawn on screen all moving at 60 fps at a resolution of 1024 x 768 x 32; so unless your doing something in code thats slowing your program down or have a worse graphics card than mine I can't see there being any speed issues.

Sorry if this sounded like a lecture, I'm just trying to help

@ Robot

Thats quite interesting, I would try and alter my code to compensate but I believe theres not much point to it.

Login to post a reply

Server time is: 2026-06-12 09:26:10
Your offset time is: 2026-06-12 09:26:10