1
\$\begingroup\$

I have found a great tutorial series on creating a simple 2D side scroller, Super Mario Brothers, in this case, and I believe I can adapt it fairly well to create a Megaman clone(something I am quite familiar with so that I can focus on learning the code and not necessarily the mechanics).

What has been challenging me is how to swap colors on Megaman when the chosen weapon is changed. I've found a few options, but I am unsure which one is viable.

  1. Having each color as a separate sprite sheet
    • PRO:
      • simplest implementation I believe
    • CON:
      • increased footprint size of overall app
  2. Converting the texture to a Pixmap, changing the colors I want, convert back to a texture and render as normal?
    • PRO:
      • no increase in app size
    • CON:
      • Is this done on reach render cycle or can I change it once and forget about it until a new weapon change?
  3. Shaders
    • PRO:
      • ??
    • CON:
      • From what I am reading, it sounds like they can only change on color at a time? Do I use 2 shaders?
      • They can only be applied wholly to the render and not per sprite?

Thanks.

\$\endgroup\$
  • \$\begingroup\$ The second con for the shader is not one, you can then go back to the passthrought shader after rendering a single sprite. \$\endgroup\$ – Cedric Martens Jul 13 '17 at 1:45
1
\$\begingroup\$

It seems that doing the pixmap conversion would be your best bet. Especially if your thinking of using alot of colors. Once you convert it back to a texture the colors are retained so you wouldnt need to continuously swap colors during your runtime. If you wanted to, you could even load separate textures for each color before the game starts to save processing time. Shaders could work, but as you said, it would be applied to everything on the screen. But if you were to use some shaders, they could change two colors through frame buffers and such, it would just be really heavy on your game.

Overall, you should go with your pixmap idea. Good luck with your code :)

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Another option is to create a black and white or grayscale version of the character spritesheet and then tint it using SpriteBatch.setColor() before rendering the character, based on the appropriate color for the currently equipped weapon.

This option doesn't increase the overall size (like option 1) or memory/cpu usage (like option 2) of the app, and is significantly less complex than option 3 for someone who is learning.

Just make sure you setColor() back to Color.WHITE after drawing the player sprite or everything drawn after that will also be tinted.

Regarding the shader option, it is possible to apply a shader to just a single sprite (sort of), but it would involve rendering the player sprite to an offscreen FrameBufferObject while applying the tinting shader and then compositing that back onto the main scene.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ I did look into using setColor(), but I don't think it would be applicable as I am not looking to a monochromatic sprite. Just looking to change the color of the armor(?) but not the face or eyes. \$\endgroup\$ – John Vanek Sep 28 '16 at 9:42
  • \$\begingroup\$ Good point. It is still possible to use setColor() but it would involve separating the armor and non-armor parts of the texture and drawing the non-armor parts first then the armor part tinted to the appropriate color. This saves the pixmap -> GPU upload every time you have to change colors at the cost of some texture work and potentially fiddly alignment stuff for drawing the non-armor part in the right position. The bigger down side is that the color control is limited to tinting versus manually changing pixels as desired... so no rainbow armor. \$\endgroup\$ – Lando System Sep 28 '16 at 21:35
1
\$\begingroup\$

To achieve this behaviour, you should definitely use a specific shader. If you apply the shader to your sprite batch before drawing your sprite and set it back to null, it will just apply to your sprite and not to the entire screen.

You can drive the colours using a few uniforms depending on the power the user have enabled.

See this question for more details:

https://stackoverflow.com/questions/24099103/libgdx-changing-sprite-color-while-hurt

you basically need the same thing with some colour swapping in your shader.

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

To color your sprite simply use the Sprite#setColor(color) method. This will tint your sprite with the selected color. Make sure to have the parts of your sprite that you want to be colored to be completely white. That way the blending will tint them with 100% of the new color.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.