0

I have a class that looks something like this:

public class MyResources
{

    public static final Size textureSize = new Size(72, 96);
    public Texture texture;
    public TextureRegion textureRegion;

    private static BGRResources instance = null;

    protected BGRResources()
    {
        // Protected to prevent direct instantiation.

        texture = new Texture(Gdx.files.internal("data/mytex.png"));
        texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);

        textureRegion = new TextureRegion(texture, 0, 0, 72, 96);
        //textureRegion = new TextureRegion(texture, 0, 0, textureSize.width, textureSize.height);

    }

    public static MyResources getInstance()
    {
        if (instance == null)
        {
            instance = new MyResources();
        }
        return instance;
    }
}

Size is a simple class with just public width and height floats.

I want to replace the line where textureRegion is set to the line below it, which is commented out. When I do that, the region no longer works -- I get nothing when it is drawn in an Image. The line above, which to my C programming (i.e. preprocessor) brain should be absolutely identical, works fine.

Why? I tried duplicating the problem in a simpler, non-libGDX class, but couldn't. I thought maybe the static final Size class wasn't actually getting instantiated before it was being used to create the textureRegion, but both the debugger and my attempt at simplifying it seemed to disprove that.

As indicated above, I'm a C guy, where the order of things like this is very clear. It gets muddy for me here, so I could use any education on how to properly declare #define equivalents or singleton classes that might come along with the answer. Thanks.

gkimsey
  • 517
  • 6
  • 13

1 Answers1

3

You mention your Size class has two float fields for width and height. When you instantiate your TextureRegion with your Size class, because of method overloading it actually calls a separate constructor than when you pass in integer constants. That separate constructor expects UV coordinates which are normalized to a range from 0 - 1.0. Check the TextureRegion API and the Textures section of open.gl for more details.

The easiest fix for your use case is to use ints instead of floats in your Size class.

adketuri
  • 196
  • 5
  • Thank you! It would've taken me at least another hour or two to figure this out. I would not have thought to check for typing because I didn't think to look for other constructors. It's something I'll have to remember to watch out for as I get back into Java. Much appreciated, sir. – gkimsey Jan 26 '16 at 00:18
  • @gkimsey glad I can help! I usually use ctrl+click on the method names in my code (with Eclipse at least) to figure out exactly which one gets called. – adketuri Jan 26 '16 at 03:11