Author
Topic: sprite color change (Read 6116 times)

I am thinking about doing a simple football kit design app and wondered if there is a a way you can change a colour of a sprite on the fly?

For instance I have a shirt and shorts graphic that have 2 defined default colours (255,0,255 and 0,255,0) can I change it to say 210,30,77 and 1,1,1 for the shirt and on the shorts graphic make the 255,0,255 to say 255,228,0 and then draw it? Obviously the final program would have many kits and many predefined colors but for now im just stuck on doing the one.

The only way I can see to do it is a bit complex and uses sprite2mem, is there an easier way?

CheersGary

« Last Edit: 2011-Jul-12 by Gary_Leeds »

Logged

MrTAToad

Had a look at polyvector and from what I can see it applies a standard colour add to the sprite so would not work for the attached example if I wanted to set one colour to black and one to white for example (or could it?)

Of course I could split it into 2 graphics to allow the different colours but is there an easier way?

Polyvector and every color as a single white/greyscale image would be the best quality, I think. Also make it a png with alpha background, don´t scale it and set alphamode to -0.999. Every pixel based approach would make fuzzy edges.

For your exact situation, I'd split it into 2 graphics, like you said. And use polyvectors.It's simple to implement, and it's fast, but may be more difficult to manage working with and positioning two graphics.

'sprite2mem' would probably work if you didn't want the overhead of managing two separate sprites per item.I've never used it however, so don't know it's speed or features, but you'd only have to do it once per colour change.

I don't know this, but can OpenGL use custom shaders in 2D mode?If so, you could have a shader to substitute the base colours with two colours passed to the shader.This would be at the fragment level? Do a check, if the base pixel is 'colour 1' (say (200,200,200)), set it to the passed colour 1, or else if it's 'colour 2' (say (100,100,100), set it to passed colour 2. Ha, I really have limited shader experience and don't know it it's possible, or how difficult it would be.

Crivens, handy bit of code that but it still does not take away the need to break up my shirt patterns

I had a little mess about with my single shirt I have here (on a PSD file at home I have about 50 different styles and patterns some are single colour, some are 4 colour. I could set a default back colour for the shirt which would remove the need for the 2 colour shirts to have 2 graphics, one colour would be the default colour, the other would be the overlaid colour so this would cut down the number of images by maybe a 3rd

which is currently using the draw routine dreamerman posted on the sprite speed test thread with a change to allow for a different colour to be applied. I know its not memory friendly but I would probably keep all the parts of the kit the same size so all draw routines are done at 0,0 rather than storing the x,y start position for every graphic.

Still, its all learning another part of GLB, even if I dont fully understand it just yet

Have attached the code so far with graphics but doubt it will be of use to anyone lol

You could actually palette change in Div Games Studio. Unfortunately that program went the way of the dodo. However others took up its mantle, including Fenix, Gemix and Bennuu. So there are languages that can do it.

Actually, if ALL your cloths have mirrored patterns, like your sample pic, you may be able to use just one picture.Split the pic by removing one colour, just as you would if you were to use the two pic method. Colour it white.

In your code plot the one side with one colour, then redraw the same polygon but flipped using the other colour.Use the tx, ty to plot the pic backwards.

Of course this wont work if the item isn't perfectly symmetrical.

[Edit] I was just looking at it again, I would save the 1 colour image with the same dimensions as the 2 colour original pic. It will make aligning the two sides so much easier.

I'd use SPRITE2MEM, then split each pixel in RGBA and compare to a given value (draw tricots in red/green and if the pixel is definitely red, recolour.) That way you can also have shades of other colours and keep skin colour untainted. Also, polyvector with colours is slow on mobile devices, usually.

I haven't tried it, but is it also possible to use SPRITE2MEM and MEM2SPRITE with spritesheets that you load with LOADANIM and then can draw with DRAWANIM? So I mean an image with lots of small images for animation.

You could actually palette change in Div Games Studio. Unfortunately that program went the way of the dodo. However others took up its mantle, including Fenix, Gemix and Bennuu. So there are languages that can do it.

True but this would only be for languages that use a 256 colour palette. Plus it would be a total waste of colours because you may have a shirt that visually appears to use 2 colours but may actually be 16 in total with 8 shades of each colour for antialiasing. 16 colour changes for the shirts and your palette is gone. I meant no language would have a command to do this as it would be a pretty unique feature to implement a replace colour command.

Fenix and Bennu both offer 24bit colour - and don't knock 256 colours, I suspect most coders here use far less, with some very impressive results. Anyway you are changing the goalposts, you stated language, not colour depth limited language!

PlayBASIC also has the ability to colour change (forgot about that one despite using it for a couple of years). That offers full 32bit colour. There was a specific example of shirt colour changing functions when Kevin Picone was doing a sports game (rugby IIRC). He's rolled out some wonderful examples with PB.