LibGDX and Getting Pixels From Sprite

I want to implement the ability for people to change the way players characters look, and by that I mean that I want to give the players the ability to change the colors of their given hero. Which is an png. The only problem is that I need the image as a sprite to make use of LibGDX’s flip and scale functions to the nearest pixel, so I retain that awesome pixel-y look. No methods, not even Pixmap, are giving me what I need. I thought about taking a BufferedImage, saving the data, writing the data as a new player png (Which I may have to do so I dont keep loading in the same stupid thing and messing up) and then just assigning that to the player. But what if the player makes 10’s or hundreds of characters? I would have 10’s of hundreds of .pngs of different heroes my player would probably not use. I also noticed that when Notch made Minicraft for LD22 a long time ago, he performed exactly what I would like to do!

My question is, with LibGDX’s given library, is there a way I could copy Notch’s Minicraft attempt at saving the images as grayscale, and just applying colors at runtime?

Not really.

Not with libgdx… At least performance wise. The way he did it is he took 4 gray colors (0, 85, 170, 255) and when rendering he would assign each of these colors a color he wanted with code.

Have you watched vanzeben’s tutorials? I’m pretty sure he explains just that method.

Are you using a SpriteBatch to render your characters? If you simply to batch.setColor(color) the texture you render after that will be tinted in the color you specify. Thats what I use for these kinds of customizations, works like a charm.

One trick (that I never used, but it should work) is to render your characters to the screen (without swapping the buffers so nothing is shown) and then copying the rendered character to a Texture e.g. using the ScreenUtils methods.

You can get the pixels from the texture, unless I’m thinking a pixmap. You can then store them in a 2D array but like you I have no idea if you manipulate them from there.

Just have all your images shades of grey, then apply a tint.

@grunt and gibbo

You don’t seem to understand what he wants.

He doesn’t want tinting. He wants to replace certain gray colors with custom colors in code.

I understand what he wants, I told him you can get the pixels of a texture but if there is a way to manipulate them with libgdx, no idea.

I then suggested an alternative, is all.

Could you do something along the lines of:
• Load an image as a BufferedImage (RGBA)
• Loop through its pixel data
• Each Integer value that has an RGBA can be replaced with an RGBA of the custom colour.
• Store this image as the player sprite.

Scratch that, googled and found out its a bad idea.
Source: http://www.java-gaming.org/index.php?topic=29243.0

That said, could you render the sprites in layers? Tint only the bits that need to be tinted?

[quote=“trollwarrior1,post:5,topic:48065”]
Where precisely do you read that he “does not want tinting”? I’m curious.

I dont see how my solution does not give him what he wants. You can flip and scale a tinted texture to your heart’s content.

[quote=“Grunnt,post:8,topic:48065”]

It’s because he wants to do it like notch did it in minicraft. And notch didn’t tint the textures in minicraft :wink:

Use a fragment shader.

[quote=“matheus23,post:9,topic:48065”]
In case the OP wants to know how Notch did it, here’s the code:

Its in Java2D, and its also 100% officially silly to try to do this in OpenGL and LibGDX. It is extremely slow, error-prone, terribly overcomplicated and pretty much worst practice (unless you enjoy making life difficult for yourself). Much better do someting like:

batch.setColor(bodyColor);
batch.draw(bodyTexture, ...
batch.setColor(pantsColor);
batch.draw(pantsTexture, ...
...

Easy peasy.

Thanks Grunnt, but I wasnt saying I wanted to use Java2D with my current LibGDX setup, I was simply asking if there was a possible way to get it going. I would use a tint, but the tint does not give me the same look I was going for. Tint gives me some crazy dark colors, and I can only go so far with some things.

I’ll take a look at it again, and If I run into more trouble, I’ll post my code after work.