Window-like TextureRegion problem.

Anything libgdx related goes here!

Window-like TextureRegion problem.

Postby Varriount » Fri Aug 03, 2012 8:18 am

Ok, so I am trying to create a simple moving actor in scene2d which acts like a window, displaying parts of a larger, static texture as it moves.
(If anyone uses clipping masks in photoshop, this is the same effect)
Example:
Image Image
With the background texture :
Image

After some thinking, I decided that the best way to go about doing using the scene2d framework was to have one actor, containing two textureregions, and have the actor draw them, one after the other . Using the above images as an example, the first region would contain a transparent circles surrounded by black, and the second would contain the rainbow pattern.
The second region's coordinates would match the first region, while the texture the region contain would be the same size as the stage (1024 by 1024).

In this manner I could draw the rainbow portion and have it match the dimensions of the circle, and then draw the circle above it, covering over the unwanted bits of the rainbow.

I managed to get this working, to a certain degree, however it seems that I am specifying the rainbow's x and y values incorrectly, as I am always viewing the bottom right (or top left) corner of the region.

Below is the rendering code, as well as the actor class code and the screen class code on how things are loaded.
Code: Select all
public class MenuScreen implements Screen {
   public Texture buttonTexture;
   public Skin buttonSkin;
   public Skin prefSkin;
   public AssetManager assetManager;
   public SpriteBatch spriteBatch;
   public Stage stage;
   public Table table;
   

   public MenuScreen(AssetManager manager, SpriteBatch spriteBatch) {
      // Instantiation
      this.assetManager = manager;
      this.stage = new Stage(1024f, 1024f, true, spriteBatch);
      
      //Object Configuration
      Gdx.input.setInputProcessor(stage);
       
      // Asset Loading
      assetManager.load("data/skins/pack.json", Skin.class);
      assetManager.load("data/skins/pack.atlas", TextureAtlas.class);
      assetManager.finishLoading();

      // Actor Setup   
      TextureAtlas primaryTexture = assetManager.get(Constants.ATLAS_PATH, TextureAtlas.class);
      DualTextureActor dualTextureActor = new DualTextureActor(primaryTexture.createSprite("Square"), primaryTexture.createSprite("Rainbow"));

      stage.addActor(dualTextureActor);
      dualTextureActor.setPosition(500,500);
   }

   public void render(float delta, SpriteBatch spriteBatch) {
      stage.act();
      stage.draw();
   }
}


Code: Select all
public class DualTextureActor extends Actor {

   private TextureRegion mainTexture;
   private TextureRegion secondaryTexture;

   public DualTextureActor(Texture mainTexture, Texture secondaryTexture) {
      this.mainTexture = new TextureRegion(mainTexture);
      this.secondaryTexture = new TextureRegion(secondaryTexture);

      this.setSize(mainTexture.getWidth(), mainTexture.getHeight());
   }

   public DualTextureActor(TextureRegion mainTexture,
         TextureRegion secondaryTexture) {
      this.mainTexture = mainTexture;
      this.secondaryTexture = secondaryTexture;

      this.setSize(mainTexture.getRegionWidth(),
            mainTexture.getRegionHeight());
   }

   @Override
   public void draw(SpriteBatch batch, float parentAlpha) {
      // Ok, so first we need to modify the region to fit the size of
      // maintexture, and have the position of the sprite within
      float frameX = secondaryTexture.getRegionX() - getX();
      float frameY = secondaryTexture.getRegionY() - getY();
      secondaryTexture.setRegion((int)frameX, (int)frameY, (int)mainTexture.getRegionWidth(), (int)mainTexture.getRegionHeight());
      batch.draw(secondaryTexture, getX(), getY(), getWidth(), getHeight());
      batch.draw(mainTexture,getX(), getY());
   }
}


And the result is.. red:
Image

Please forgive me if I'm unclear, I tend to be a bit drawn on.. so don't hesitate to ask for any other details.
Think of the unanswered posters : search.php?search_id=unanswered
If I'm you and you're me, then who's he?
Varriount
 
Posts: 249
Joined: Tue Jun 05, 2012 2:48 pm

Re: Window-like TextureRegion problem.

Postby Varriount » Sun Aug 05, 2012 4:59 am

As much as I hate to do this, I haven't been able to figure anything else out so... *cringe*
...
Bump.
Think of the unanswered posters : search.php?search_id=unanswered
If I'm you and you're me, then who's he?
Varriount
 
Posts: 249
Joined: Tue Jun 05, 2012 2:48 pm


Return to Libgdx

Who is online

Users browsing this forum: Bing [Bot] and 8 guests