|
how to find and replace colours in a bitmap? |
demonisch
Member #7,581
August 2006
|
I'm making a snake game for multiplayer. I have one bitmap with all the snake body sprites. Each player will have a different coloured snake. Is it possible to colour the inside of the snake sprites white (purple = transparent is used on the outsides of the snakes to let the background show) and use some function to replace the white with the colour of the snake, such as red? Also would I need to make copies of the bitmap in memory for each player, or could I do the find and replace during painting the sprite pieces? Sounds more efficent then creating separate bitmaps for each player, and will allow players to choose which colour they wish to be. Thanks for your help |
orz
Member #565
August 2000
|
You can make multiple copies of the snake bitmap(s), and recolor them manually at run time. Takes more memory, but it works. You can have only one copy of the bitmap, but keep that copy in 8 bits per pixel, keep the screen in 15/16/24/32 bits per pixel, use only drawing functions that support 8 bit sources and truecolor destinations, and switch palettes for each different snake color. You'll have to look up in the docs which drawing functions support 8 -> 15/16/24/32 drawing, I don't remember, but I think several do. And you'd need one palette per snake color. Probably other ways to do it too. |
demonisch
Member #7,581
August 2006
|
I've solved the problem by creating my own blit function void objSnake::paintSeg (BITMAP *imgBuffer, BITMAP *imgSnakes, int sourceX, int sourceY, int desX, int desY) |
ixilom
Member #7,167
April 2006
|
If the bodyparts are just a few small bitmaps you could loop through the pixels replacing white with your color of choice. Maybe something like
Note that MakeBitmapCopy() will return a new BITMAP, dont forget to run destroy_bitmap() on it. ___________________________________________ |
Evert
Member #794
November 2000
|
Quote: It handles transparent pixels by ignoring them, and more importantly it replaces all white pixels with the colour of the snake. A better idea would be use an 8-bit image and use a custom palette for each player, or using a custom blender. |
miran
Member #2,407
June 2002
|
What I like to do is this: 1. Draw the sprites as normal, probably in one colour version or in black and white. -- |
BAF
Member #2,981
December 2002
|
Your custom blit is going to be bloody slow. |
Paul Pridham
Member #250
April 2000
|
---- |
|