libgdx Background TextureRegion repeat

老子叫甜甜 提交于 2019-12-10 19:15:41

问题


I'm new in libdgx developement and also in game developement. I am reading the book "Learning Libgdx Game Development" from Andreas Oehlke and I am trying to develop my own game in parallel. I have a problem when I try to add the background. In the book, he uses a color, so it's very simple. But I want to add an image from a texture atlas. The image is to small to recover all the screen, so I want to repeat it. I can't use regBackground.setWrap(TextureWrap.Repeat, TextureWrap.Repeat) because regBackground is not a texture. How i can resolve my problem properly?

   public class Background extends AbstractGameObject {

   private TextureRegion regBackground;

   public Background () {
      init();
   }

   private void init () {
      dimension.set(1f, 1f);
      regBackground = Assets.instance.levelDecoration.background;
   }

   public void render (SpriteBatch batch) {
      TextureRegion reg = null;
      reg = regBackground;
      batch.draw(reg.getTexture(), 
            position.x, position.y, 
            origin.x, origin.y, 
            dimension.x, dimension.y, 
            scale.x, scale.y,
            rotation, 
            reg.getRegionX(), reg.getRegionY(), 
            reg.getRegionWidth(), reg.getRegionHeight(), 
            false, false);
   }
}

In my Assets class, I have this code to find the region in the texture atlas :

     public class AssetLevelDecoration {
         public final AtlasRegion background;

         public AssetLevelDecoration (TextureAtlas atlas) {
             background = atlas.findRegion("background");
      }
   }

回答1:


I progressed in solving my problem. I use the setWrap method to repeat my texture :

   public class Background extends AbstractGameObject {

   private TextureRegion regBackground;

   public Background (int width, int heigth) {
      init(width, heigth);
   }

   private void init (int width, int heigth) {
      dimension.set(width, heigth);
      regBackground = Assets.instance.levelDecoration.background;

      origin.x = -dimension.x/2;
      origin.y = -dimension.y/2;
   }

   public void render (SpriteBatch batch) {
      TextureRegion reg = null;
      reg = regBackground;

      Texture test = reg.getTexture();
      test.setWrap(TextureWrap.Repeat, TextureWrap.Repeat);
      batch.draw(test, 
            position.x + origin.x, position.y + origin.y, 
            test.getWidth(), test.getHeight(), 
            reg.getRegionX(), reg.getRegionY(), 
            reg.getRegionWidth(), reg.getRegionHeight()       
            );

   }
}

Now, I obtain this, but I just want to repeat my background image (wood square).

http://s24.postimg.org/c1m92ffwx/Capture_du_2013_11_12_15_49_03.jpg

The problem is that the getTexture() recover the all image and not only my background. How can I fix this?




回答2:


I would just add a comment but I don't have the rep.

To solve the issue of the repeating of the whole texture instead of only the wooden square you have 2 choices. A) separate the textureregion onto a separate texture. B) loop over the to repeat the draw, which should be negligible in terms of performance.

http://badlogicgames.com/forum/viewtopic.php?f=11&t=8883




回答3:


I have created an introduction to images including repeating texture here: https://libgdx.info/basic_image/

I hope it helps

This creates an image along this line:




回答4:


a simpler approach will be

 batch.draw(imageReference,startX,startY,widthOfScreen,heightOfScreen);

batch.draw() is an overloaded method so use only those parameter u need the syntax is just a pseudo code

MOst importantlu This may give image stretching(depending on image).



来源:https://stackoverflow.com/questions/19905117/libgdx-background-textureregion-repeat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!