Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (487)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (553)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  LibGDX and Getting Pixels From Sprite  (Read 521 times)
0 Members and 1 Guest are viewing this topic.
Offline Atheistzilla

Junior Member


Medals: 2
Projects: 1
Exp: 2 years


Rojar Studios Programmer


« Posted 2014-04-16 03:03:14 »

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?
Online trollwarrior1
« Reply #1 - Posted 2014-04-16 04:55:03 »

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.
https://www.youtube.com/watch?v=VE7ezYCTPe4
Offline Grunnt

JGO Wizard


Medals: 66
Projects: 8
Exp: 5 years


Complex != complicated


« Reply #2 - Posted 2014-04-16 09:12:16 »

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?

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.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Gibbo3771
« Reply #3 - Posted 2014-04-16 09:17:48 »

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.

"This code works flawlessly first time and exactly how I wanted it"
Said no programmer ever
Online trollwarrior1
« Reply #4 - Posted 2014-04-16 09:24:29 »

@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.
Offline Gibbo3771
« Reply #5 - Posted 2014-04-16 09:28:47 »

@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.

"This code works flawlessly first time and exactly how I wanted it"
Said no programmer ever
Offline Coldstream24
« Reply #6 - Posted 2014-04-16 09:31:02 »

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?

Yes, I am that same incredibly famous Internet musician.
Offline Grunnt

JGO Wizard


Medals: 66
Projects: 8
Exp: 5 years


Complex != complicated


« Reply #7 - Posted 2014-04-16 09:36:47 »

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

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.

Offline matheus23

JGO Kernel


Medals: 106
Projects: 3


You think about my Avatar right now!


« Reply #8 - Posted 2014-04-16 11:45:35 »

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

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.

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

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline jonjava
« Reply #9 - Posted 2014-04-16 12:25:00 »

Use a fragment shader.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Grunnt

JGO Wizard


Medals: 66
Projects: 8
Exp: 5 years


Complex != complicated


« Reply #10 - Posted 2014-04-16 13:52:57 »

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

In case the OP wants to know how Notch did it, here's the code:
https://github.com/Miserlou/Minicraft

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:
1  
2  
3  
4  
5  
batch.setColor(bodyColor);
batch.draw(bodyTexture, ...
batch.setColor(pantsColor);
batch.draw(pantsTexture, ...
...


Easy peasy.

Offline Atheistzilla

Junior Member


Medals: 2
Projects: 1
Exp: 2 years


Rojar Studios Programmer


« Reply #11 - Posted 2014-04-16 14:23:30 »

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.
Pages: [1]
  ignore  |  Print  
 
 

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

TehJavaDev (17 views)
2014-08-28 18:26:30

CopyableCougar4 (26 views)
2014-08-22 19:31:30

atombrot (39 views)
2014-08-19 09:29:53

Tekkerue (36 views)
2014-08-16 06:45:27

Tekkerue (33 views)
2014-08-16 06:22:17

Tekkerue (22 views)
2014-08-16 06:20:21

Tekkerue (33 views)
2014-08-16 06:12:11

Rayexar (67 views)
2014-08-11 02:49:23

BurntPizza (45 views)
2014-08-09 21:09:32

BurntPizza (36 views)
2014-08-08 02:01:56
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!