Advertisement
dermetfan

repeating infinite background

Dec 14th, 2014
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.50 KB | None | 0 0
  1. package net.dermetfan.someLibgdxTests.screens;
  2.  
  3. import com.badlogic.gdx.Gdx;
  4. import com.badlogic.gdx.ScreenAdapter;
  5. import com.badlogic.gdx.graphics.GL20;
  6. import com.badlogic.gdx.graphics.Texture;
  7. import com.badlogic.gdx.graphics.Texture.TextureWrap;
  8. import com.badlogic.gdx.graphics.g2d.Batch;
  9. import com.badlogic.gdx.graphics.g2d.SpriteBatch;
  10. import com.badlogic.gdx.utils.viewport.ScreenViewport;
  11. import com.badlogic.gdx.utils.viewport.Viewport;
  12. import net.dermetfan.someLibgdxTests.Assets;
  13. import net.dermetfan.someLibgdxTests.SomeLibgdxTests;
  14.  
  15. public class RepeatingBackgroundTest extends ScreenAdapter {
  16.  
  17.     Viewport viewport = new ScreenViewport();
  18.     final Batch batch = new SpriteBatch();
  19.     Texture texture = SomeLibgdxTests.assets.get(Assets.testImage);
  20.  
  21.     {
  22.         texture.setWrap(TextureWrap.Repeat, TextureWrap.Repeat);
  23.     }
  24.  
  25.     @Override
  26.     public void render(float delta) {
  27.         Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
  28.         int w = texture.getWidth(), h = texture.getHeight();
  29.         float y = viewport.getCamera().position.y += delta * 100;
  30.         viewport.getCamera().update();
  31.         batch.setProjectionMatrix(viewport.getCamera().combined);
  32.         batch.begin();
  33.         int repeat = (int) viewport.getWorldHeight() / h + 2;
  34.         batch.draw(texture, 0, -viewport.getWorldHeight() / 2 + h * (int) (y / h), w, h * repeat, 0, repeat, 1, 0);
  35.         batch.end();
  36.     }
  37.  
  38.     @Override
  39.     public void resize(int width, int height) {
  40.         viewport.update(width, height, true);
  41.     }
  42.  
  43.     @Override
  44.     public void dispose() {
  45.         batch.dispose();
  46.     }
  47.  
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement