Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (498)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
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  
  Repeating Background (libgdx) (Solved)  (Read 6026 times)
0 Members and 1 Guest are viewing this topic.
Offline RUSD

Junior Member


Projects: 2



« Posted 2013-06-11 05:39:34 »

I'm trying to make a repeating background.  I guess you are supposed to use texture.setWrap... or something but I can't seem to find good documentation on how it works.  If someone could point me to some documentation on how to repeat a texture and render it, or give me some pseudo code, I would be grateful.
Offline HeroesGraveDev

JGO Kernel


Medals: 253
Projects: 11
Exp: 2 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #1 - Posted 2013-06-11 05:52:45 »

Use GL_REPEAT instead of GL_CLAMP_TO_EDGE when creating the texture. (or the LibGDX equivalents)

Offline Nate

JGO Kernel


Medals: 147
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #2 - Posted 2013-06-11 09:07:42 »

setTextureWrap(TextureWrap.GL_REPEAT) on the texture. Or specify the wrap when using TexturePacker2. Or use TiledDrawable.

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

Junior Member


Projects: 2



« Reply #3 - Posted 2013-06-11 11:33:44 »

Do I have to do anything different in the render method?

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
Texture background;
background = new Texture("data/background.png");
background.setWrap(TextureWrap.Repeat, TextureWrap.Repeat);


render(){
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

world.update();

batch.begin();
batch.draw(background,0,0);
batch.end();
}


Is this right?  It doesn't seem like I'm telling it to rendering the texture more than once.
Offline Nate

JGO Kernel


Medals: 147
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #4 - Posted 2013-06-11 18:33:58 »

You need to give SpriteBatch UVs using:
draw(Texture texture, float x, float y, float width, float height, float u, float v, float u2, float v2)
The UVs are from 0 to 1, where 0 is the top (v) / left (u) and 1 is the bottom (v) / right (u). You can use values outside this range and it will repeat. Eg, use 0,0,2,1 and it will repeat twice horizontally and once vertically.

You can also use a TextureRegion to specify the UVs. Also see the TextureRegion scroll method.

Offline davedes
« Reply #5 - Posted 2013-06-12 01:27:22 »

Here is an example of using Texture.Repeat:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
... create ...
    ... create texture
    tex.setWrap(TextureWrap.Repeat, TextureWrap.Repeat);

... render ...
    //repeat N times horizontally and vertically
   int tileCount = 3;
    spriteBatch.draw(tex, 0, 0,
         tex.getWidth() * tileCount,
         tex.getHeight() * tileCount,
         0, tileCount,
         tileCount, 0);


However, this means you can't render your background in the same batch as the rest of your sprites.  It also means you can't include the background texture in your texture atlas. The texture needs to be power-of-two sized (unless you are targeting NPOT-supported devices).

Ultimately it might be easier just to render the sprite multiple times in your batch (e.g. nested for loop). This allows you to include the background in the same batch as the rest of your sprites, and overcome the power-of-two issue if needed.

Anyways... The performance difference is pretty much negligible, so choose whichever is easiest for you.

Offline Nate

JGO Kernel


Medals: 147
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #6 - Posted 2013-06-12 02:34:19 »

You could include the region in an atlas if you only need to repeat in one direction, you just need to set the max size of the atlas page the region is on to your POT image size.

To render a region repeatedly without using the repeat wrap mode, see TiledDrawable.

Offline RUSD

Junior Member


Projects: 2



« Reply #7 - Posted 2013-06-12 03:30:29 »

Is there a way that I can repeat the tile infinitely? Draw them as needed?  Or do i just need to set the "N" to be a large number that my player won't reach?

My game is basically going to the right as far as you can before you die

Thank you for all your help.
Offline RUSD

Junior Member


Projects: 2



« Reply #8 - Posted 2013-06-16 20:45:27 »

Incase anyone else ever has this trouble this is what i ended up doing... I don't know if it is the best way to do it or not, but it works.



1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
bgSprite1 = new Sprite(background, 0, 0,width, height);
bgSprite2 = new Sprite(bgSprite1);

render(){

if(stage.getCamera().position.x -width/2> bgSprite2.getX()){
         bgSprite1.setPosition(bgSprite2.getX(),0);
         bgSprite2.setPosition(bgSprite1.getX()+width, 0);
      }
     
      bgSprite1.draw(batch);
     
      bgSprite2.draw(batch);
     
}
Pages: [1]
  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.

BurntPizza (18 views)
2014-09-21 02:42:18

BurntPizza (13 views)
2014-09-21 01:30:30

moogie (13 views)
2014-09-21 00:26:15

UprightPath (25 views)
2014-09-20 20:14:06

BurntPizza (27 views)
2014-09-19 03:14:18

Dwinin (40 views)
2014-09-12 09:08:26

Norakomi (73 views)
2014-09-10 13:57:51

TehJavaDev (96 views)
2014-09-10 06:39:09

Tekkerue (49 views)
2014-09-09 02:24:56

mitcheeb (70 views)
2014-09-08 06:06:29
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!