Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (480)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (547)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1] 2
  ignore  |  Print  
  Tinting a tile in LibGDX  (Read 3720 times)
0 Members and 1 Guest are viewing this topic.
Offline opiop65

JGO Kernel


Medals: 154
Projects: 7
Exp: 3 years


JumpButton Studios


« Posted 2013-08-26 10:32:17 »

Alright, currently in my project I'm rendering a tile, then rendering another tile that overlays onto the first and sort of provides tile based lighting. The first tile is the texture, then the second tile just has a black texture with the alpha value changed so that it can make the first tile lighter or darker. This isn't very efficient, however, because I have to render twice as many tiles for every tile space. The lighting in my game changes depending on the time of day, and the torches. So, is there anyway to tint the tiles texture using only one tile? I'm using LibGDX and my tiles are objects of the
1  
Sprite
class. I'm not using shaders right now, but if I need to I can.
Thanks!

Offline grevius
« Reply #1 - Posted 2013-08-26 10:56:02 »

if you use sprite and you want only make your tile lighter or darker you can use the .setColor() of your tile.
this is an example :
1  
sprite.setColor(sprite.getColor().r, sprite.getColor().g, sprite.getColor().b, value);

with this you can set the opacity of the tile without change the color of it.
I also think that if you add value to r,g and b channel you can change the darkness or lighter of your sprite, you can try.
I hope this can help  Smiley
Offline opiop65

JGO Kernel


Medals: 154
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #2 - Posted 2013-08-26 10:57:08 »

Thank you! Lots  Grin Ill try it out and report back!

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

JGO Kernel


Medals: 154
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #3 - Posted 2013-08-26 19:05:22 »

That didn't work...
This is my tile class:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
package com.nishu.particaletest.entities;

import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.nishu.particaletest.GameScreen;

public class Tile extends Entity{
   
   private boolean isSolid, isLit;
   Texture tex;
   Sprite tile;
   float lightVal;
   
   public Tile(int x, int y, boolean isSolid, boolean isLit){
      super((x * GameScreen.scale) * 16, (y * GameScreen.scale) * 16);
      this.isSolid = isSolid;
      this.isLit = isLit;
     
      if(!isSolid)
         this.tex = new Texture("data/grass.png");
      else
         this.tex = new Texture("data/stone.png");
     
      tile = new Sprite(tex);
      tile.setColor(0, 0.5f, 1, lightVal);
      tile.setPosition((x * GameScreen.scale) * 16, (y * GameScreen.scale) * 16);
   }
   
   public void render(){
     
   }
   
   public void update(){

   }

   public boolean isSolid() {
      return isSolid;
   }

   public void setSolid(boolean isSolid) {
      this.isSolid = isSolid;
   }

   public boolean isLit() {
      return isLit;
   }

   public void setLit(boolean isLit) {
      this.isLit = isLit;
   }

   public Texture getTex() {
      return tex;
   }

   public void setTex(Texture tex) {
      this.tex = tex;
   }

   public Sprite getTile() {
      return tile;
   }

   public void setTile(Sprite tile) {
      this.tile = tile;
   }

   public float getLightVal() {
      return lightVal;
   }

   public void setLightVal(float lightValue) {
      this.lightVal = lightValue;
   }

}

I set the color up in the constructor, and no matter how much I change the values, the colors of the textures do not change. Do I need to enable something for coloring to work?

Offline Cero
« Reply #4 - Posted 2013-08-26 19:10:39 »

your render method is empty, how do you render ?

I suppose you are actually rendering "tex" and not "tile"

Offline opiop65

JGO Kernel


Medals: 154
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #5 - Posted 2013-08-26 19:12:52 »

Ah yes, sorry. This is just a test project, so my code is pretty messy, but here is how I render:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
   @Override
   public void render(float delta) {
      Gdx.gl.glClearColor(0, 0, 0, 1);
      Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
      batch.begin();
      for (int x = 0; x < ParticaleGame.WIDTH / 16; x++){
         for(int y = 0; y < ParticaleGame.HEIGHT / 16; y++){
            batch.draw(tiles[x][y].getTex(), (float)tiles[x][y].getX(), (float)tiles[x][y].getY());
            tiles[x][y].setColor(0, 0, 0, 0.5f);
         }
      }
      batch.end();
   }

And yes, my names are slightly confusing, tile is the sprite object and tex is the texture object.

Offline elamre

JGO Coder


Medals: 17
Projects: 1


hitar!


« Reply #6 - Posted 2013-08-26 19:26:25 »

if you use sprite and you want only make your tile lighter or darker you can use the .setColor() of your tile.
this is an example :
1  
sprite.setColor(sprite.getColor().r, sprite.getColor().g, sprite.getColor().b, value);

with this you can set the opacity of the tile without change the color of it.
I also think that if you add value to r,g and b channel you can change the darkness or lighter of your sprite, you can try.
I hope this can help  Smiley

dont forget the getColor part.
The thing you do now is you draw it once with color, then you set the sprite color with no red green or blue elements in it. Meaning everything will be black.

My projects:
Tower Defence!]http://www.java-gaming.org/topics/iconified/25690/view.html]Tower Defence! [lll.......] 30%!
Lightsnakerider! [llllll....] 60%!
Offline opiop65

JGO Kernel


Medals: 154
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #7 - Posted 2013-08-26 19:27:46 »

Right, I know that was a test. But the texture is still the same color as before, its not black! I don't understand why its not working :/

Offline elamre

JGO Coder


Medals: 17
Projects: 1


hitar!


« Reply #8 - Posted 2013-08-26 19:29:33 »

What is your setColor function in your entity class?

My projects:
Tower Defence!]http://www.java-gaming.org/topics/iconified/25690/view.html]Tower Defence! [lll.......] 30%!
Lightsnakerider! [llllll....] 60%!
Offline opiop65

JGO Kernel


Medals: 154
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #9 - Posted 2013-08-26 19:30:53 »

It just calls
1  
tile.setColor(r, g, b, a)

Tile is an object of the Sprite class.

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

JGO Kernel


Medals: 154
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #10 - Posted 2013-08-26 22:32:46 »

Bumping because I have another question. Won't changing the tile's RGB values change the actual look of the tile? I feel like the image would look very distorted in terms of color, but if I used an overlay and just changed the alpha value according to the time of day, it might look better. Of course, I have no idea since I can't get the coloring code to work anyway. Any help would be appreciated!

Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 128
Projects: 4
Exp: 3 years



« Reply #11 - Posted 2013-08-26 22:45:56 »

Bumping because I have another question. Won't changing the tile's RGB values change the actual look of the tile?
"Changing" the tile's RGB is like using a mask. It doesn't write those changes to the file.
Offline opiop65

JGO Kernel


Medals: 154
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #12 - Posted 2013-08-26 22:59:17 »

Right, I know. Its just somewhere I heard someone say directly changing the RGB values of a texture looks different than actually using a separate mask. While your here though, could you venture a guess as to why my tiles aren't changing color?

Offline R.D.

Senior Member


Medals: 2
Projects: 1


"For the last time, Hats ARE Awesome"


« Reply #13 - Posted 2013-08-26 23:04:59 »

When drawing a sprite, you should use the sprite.draw() method instead of the batch. I guess that this might be the reason why it is not getting tinted correctly.
You could set the color for the batch too if you want.
Offline opiop65

JGO Kernel


Medals: 154
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #14 - Posted 2013-08-26 23:42:34 »

Ok, that just takes a SpriteBatch right? I'm confused though because on the LibGDX docs, they call batch.draw and change the color of sprites. Here's why I found:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
private Texture texture;
private TextureRegion region;
private Sprite sprite;
...
texture = new Texture(Gdx.files.internal("image.png"));
region = new TextureRegion(texture, 20, 20, 50, 50);
sprite = new Sprite(texture, 20, 20, 50, 50);
sprite.setPosition(100, 10);
sprite.setColor(0, 0, 1, 1);
...
batch.begin();
batch.setColor(1, 0, 0, 1);
batch.draw(texture, 10, 10);
batch.setColor(0, 1, 0, 1);
batch.draw(region, 50, 10);
sprite.draw(batch);
batch.end();

So, what's the difference between my code?

Offline Cero
« Reply #15 - Posted 2013-08-27 00:35:13 »

Ok, that just takes a SpriteBatch right? I'm confused though because on the LibGDX docs, they call batch.draw and change the color of sprites. Here's why I found:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
private Texture texture;
private TextureRegion region;
private Sprite sprite;
...
texture = new Texture(Gdx.files.internal("image.png"));
region = new TextureRegion(texture, 20, 20, 50, 50);
sprite = new Sprite(texture, 20, 20, 50, 50);
sprite.setPosition(100, 10);
sprite.setColor(0, 0, 1, 1);
...
batch.begin();
batch.setColor(1, 0, 0, 1);
batch.draw(texture, 10, 10);
batch.setColor(0, 1, 0, 1);
batch.draw(region, 50, 10);
sprite.draw(batch);
batch.end();

So, what's the difference between my code?

THIS:
1  
2  
3  
batch.setColor(1, 0, 0, 1);
batch.draw(texture, 10, 10);
batch.setColor(0, 1, 0, 1);

is what sprite.render does and it uses the color you have setted.

Sprite is a convenience class, either use it or not, but make up your mind :D
meaning you call sprite.setColor, preferably only once, unless it changes every frame and then sprite.draw(batch)

Offline opiop65

JGO Kernel


Medals: 154
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #16 - Posted 2013-08-27 00:44:15 »

That's the code from the official docs Tongue I only call sprite.setColor and batch.draw, I don't see how that's a bad thing to use both. Sprite.draw seems like it gives the same results as batch.draw, so why does it matter?

Offline opiop65

JGO Kernel


Medals: 154
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #17 - Posted 2013-08-27 11:16:21 »

So, I was playing with my code, and I realized that the sprite only serves to render my texture. If I try to set the position of the sprite or rotate it or anything, it doesn't work. When I call sprite.draw(batch) and assign the sprites into a grid, the sprites still only render at coords 0, 0. I specifically set sprite.set position so I have no idea why that's not working. However, if I just draw the tiles like this:
1  
2  
3  
4  
for (int x = 0; x < width; x++){
//loop through other dimension
batch.draw(tiles[x][y].getTex(), tiles[x][y].getX(), tiles[x][y].getY());
}

It works fine and the tiles arrange into a grid. This is the first time I've used LibGDX, so I imagine this is a rookie mistake, but I really don't understand why working with the sprites doesn't work.

Offline opiop65

JGO Kernel


Medals: 154
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #18 - Posted 2013-08-27 18:10:06 »

Well, I've discovered some more info. Every example code  ive seen has a camera as well as a sprite. Do I need to have a camera to properly render a sprite? This doesn't make sense, but I'm grasping at straws here.

Offline Cero
« Reply #19 - Posted 2013-08-27 19:51:24 »

guy... do you ever listen ? :D

maybe you should look into the source code of sprite to understand whats happening

if you have a sprite
then do sprite.setcolor(bla)
and then batch.draw(sprite.getTex()
there will be no color change

same with rotation, position, what have you

Offline opiop65

JGO Kernel


Medals: 154
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #20 - Posted 2013-08-29 10:36:52 »

Well, now I'm confused. I redid my code to look like this:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
package com.nishu.particaletest;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.nishu.particaletest.entities.Tile;

public class GameScreen implements Screen {
   OrthographicCamera camera;
   SpriteBatch batch;
   Sprite sprite;

   Tile[][] tiles;

   int sWidth = ParticaleGame.getWIDTH();
   int sHeight = ParticaleGame.getHEIGHT();
   int width = sWidth / Tile.SIZE;
   int height = sHeight / Tile.SIZE;

   @Override
   public void show() {
      camera = new OrthographicCamera();
      camera.setToOrtho(false, ParticaleGame.getWIDTH(),
            ParticaleGame.getHEIGHT());
      batch = new SpriteBatch();
      tiles = new Tile[width][height + Tile.SIZE];

      for (int x = 0; x < width; x++) {
         for (int y = 0; y < height + Tile.SIZE; y++) {
            tiles[x][y] = new Tile(x, y, false, false);
            System.out.println(tiles[x][y].getTile().getX() + " , " + tiles[x][y].getTile().getY());        
         }
      }
   }

   @Override
   public void render(float delta) {
      Gdx.gl.glClearColor(0, 0, 0.2f, 1);
      Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

      camera.update();
      batch.setProjectionMatrix(camera.combined);

      batch.begin();
      for (int x = 0; x < width; x++) {
         for (int y = 0; y < height + Tile.SIZE; y++) {
            //tiles[x][y].setPos(x, y);
           tiles[x][y].getTile().draw(batch);
            System.out.println("Render: " + tiles[x][y].getTile().getX() + " , " + tiles[x][y].getTile().getY());        
         }
      }
      batch.end();
   }

   @Override
   public void dispose() {
      batch.dispose();
   }

   @Override
   public void resize(int width, int height) {
   }

   @Override
   public void pause() {
   }

   @Override
   public void resume() {
   }

   @Override
   public void hide() {

   }
}

The output for the rendering coords are this(it works, I'm only showing a section of it):
1  
2  
3  
4  
5  
6  
7  
Render: 752.0 , 288.0
Render: 752.0 , 304.0
Render: 752.0 , 320.0
Render: 752.0 , 336.0
Render: 752.0 , 352.0
Render: 752.0 , 368.0
Render: 752.0 , 384.0

Yet, I still end up with a blank screen. Why?

Offline opiop65

JGO Kernel


Medals: 154
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #21 - Posted 2013-08-30 02:25:22 »

Anyone? Please I've been stuck on this for days and it ridiculous.

Offline Longarmx
« Reply #22 - Posted 2013-08-30 03:59:39 »

Remember that when you use an orthographic camera, your origin is set to the center of the screen. Try to zoom the camera out
camera.zoom = .5f;
to see if your tiles are just being rendered off screen.

Offline JESTERRRRRR

Senior Member


Medals: 7
Exp: 1 year



« Reply #23 - Posted 2013-08-30 04:29:19 »

I re-read the thread a few times...  Undecided If you're still having problems and would like help, pastebin or w/e your entire code, tell me what is going wrong and  I will run it and have a go at fixing it as soon as I wake up
Offline opiop65

JGO Kernel


Medals: 154
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #24 - Posted 2013-08-30 10:25:50 »

Ah! I love you this worked! Can you explain why though? If I set the zoom level to 0, I cannot see any tiles. Anything above 0, and it renders fine! I'm thinking that 0 means its fully zoomed in. Am I correct? Still, thank you so much!

Edit: also, I tried calling sprite.scroll and it did nothing. I increased the values of the scroll and then drew the tile, but it didn't do anything. However, sprite.setColor worked fine. Why is it only some sprite functions seem to work? :/

Offline Longarmx
« Reply #25 - Posted 2013-08-30 12:08:30 »

Your zoom should automatically be set at 1 whenever you create an orthographic camera. Weren't your tiles just off screen?  Also, you should use
camera.translate(x, y);
to make your sprites scroll.

Offline opiop65

JGO Kernel


Medals: 154
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #26 - Posted 2013-08-30 15:47:00 »

I honestly have no idea what was wrong with my camera and tiles, this is my first foray into LibGDX. And ok that should work but why isn't the sprite.scroll method working? I just don't understand...

Offline JESTERRRRRR

Senior Member


Medals: 7
Exp: 1 year



« Reply #27 - Posted 2013-08-30 21:49:00 »

I'm sure scroll works fine...

Really not sure what bits of code we're looking at now so I can't help you at all, but

http://code.google.com/p/libgdx-users/wiki/ScrollingTexture

in particular,

spriteTexture.setWrap(!TextureWrap.Repeat, !TextureWrap.Repeat);

you tried that on your texture?
Offline opiop65

JGO Kernel


Medals: 154
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #28 - Posted 2013-08-31 00:46:09 »

No I haven't but I managed to get scrolling to work by just repositioning the camera, and it works great! Finally, all my problems are solved, thanks everyone!

Offline JESTERRRRRR

Senior Member


Medals: 7
Exp: 1 year



« Reply #29 - Posted 2013-08-31 03:35:02 »

I... well good work have fun with it...    Roll Eyes
Pages: [1] 2
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

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

The first screenshot will be displayed as a thumbnail.

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

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

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

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

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

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

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

BurntPizza (30 views)
2014-08-08 02:01:56

Norakomi (37 views)
2014-08-06 19:49:38

BurntPizza (67 views)
2014-08-03 02:57:17
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!