0
\$\begingroup\$

I'm trying to draw a texture (with a crescent moon) in the middle of the y-axis:

public void draw(SpriteBatch batch) {
    batch.draw(lune, 0, Gdx.graphics.getHeight() / 2);
}

It renders this when windowed:

windowed render

And in full screen mode:

full screen render

I don't understand why it doesn't work since I'm always drawing at the top of the middle in the y-axis.


I add a little update to what i do exactly :

        @Override
    public void resize(int width, int height) {
            this.levelDatas.screenWidth = width;
            this.levelDatas.screenHeight = height;
            this.levelDatas.lune.updateSize(width, height);
    }

My render method :

        @Override
    public void render(float delta) {
            Gdx.gl.glClearColor(0, 0, 0, 1);
            Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
            this.levelDatas.batch.begin();
            this.background.draw(this.levelDatas.batch);
            this.levelDatas.lune.draw(this.levelDatas.batch);
            this.levelDatas.batch.end();
            [...]

And this is the updateSize and draw method for the "Lune" object :

public void updateSize(int width, int height) {
    this.widthScreen = width;
    this.heightScreen = height;
}

public void draw(SpriteBatch batch) {
    batch.draw(new TextureRegion(lune), 0, this.heightScreen / 2, 0, 0, this.lune.getWidth(), this.lune.getHeight(), 1f, 1f, 0);
}

Still got the problem !

\$\endgroup\$
  • \$\begingroup\$ Can you also specify the version of LibGDX that you use? \$\endgroup\$ – user39686 Feb 24 '14 at 14:09
  • \$\begingroup\$ I use the 0.9.9 version, i saw that they updated it today. \$\endgroup\$ – Kamigaku Feb 24 '14 at 14:14
1
\$\begingroup\$

I think your problem is , that

Gdx.graphics.getHeight()

doesn't get updated properly. (I tested this behavior and found out, that it's getting updated properly . I am sorry for this wrong information of mine.)

My solution:

create 2 variables

private int w,h; 

Get the height and width of the window:

public void create() {
        w = Gdx.graphics.getWidth();
        h = Gdx.graphics.getHeight();
}

And then update the to every time, the resolution gets changed:

public void resize(int width, int height) {
    w = width;
    h = height;
}

Hope this helps.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ If Gdx.graphics.getHeight() returns the wrong value, it's a bug. Do the developers know? \$\endgroup\$ – Anko Feb 24 '14 at 13:59
  • \$\begingroup\$ I think , that it returns the wrong value only on resize, at least i think it is like that. ( Do you use java 1.7 ? This may causes some problems) \$\endgroup\$ – Klitz Feb 24 '14 at 14:04
  • 1
    \$\begingroup\$ Can you confirm with testing? I've a few projects that would have broke if this was the case. \$\endgroup\$ – user39686 Feb 24 '14 at 14:09
  • 1
    \$\begingroup\$ Okay, i tested it and everything was just fine and I think I was wrong. I posted this because i had this strange behavior once. \$\endgroup\$ – Klitz Feb 24 '14 at 14:21
  • \$\begingroup\$ @Klitz That's important detail that you should add to the answer! \$\endgroup\$ – Anko Feb 24 '14 at 14:26
0
\$\begingroup\$

Looking at the source code for SpriteBatch, it looks like vertices are generated like this:

final float fx2 = x + width;
final float fy2 = y + height;

This means even if you render at the "center" of the screen along the y-axis, it most likely will not do that. Had the SpriteBatch had something like:

final float fy2 = (y - height/2) + height;

I suspect it would be correct?

Imagine a screen height of 100, and a texture height of 50. The middle of the screen would be 50. So if you rendered the texture at 50, what would happen in the SpriteBatch is that y = 50, and fy2 = 100. That's not the middle of the screen. However, if you put y as 50 - (50/2) = 25, fy2 would be 75, and it should render in the middle.

So, (big disclaimer!!) unless libgdx does some translation somewhere else that I dont know about (and that's quite possible, since I don't know libgdx at all),

try:

public void draw(SpriteBatch batch) {
    batch.draw(lune, 0, Gdx.graphics.getHeight() / 2 - (lune.getHeight()/2));
}
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

I finaly found the answer and it was pretty stupid. I don't understand how this work but let's admit that. Game is loaded in 1200x720. I keep this values inside of the Lune classes. And everytime i draw the Lune, i draw it at 720/2 and the scale does the work to place it at the correct place. Less talk, more code, here is my solution to my own problem :
This is the constructor :

this.widthScreen = Gdx.graphics.getWidth();
this.heightScreen = Gdx.graphics.getHeight();

And this is the render method :

batch.draw(new TextureRegion(lune), 0, (this.heightScreen / 2) - (this.lune.getHeight() / 2), 0, 0, this.lune.getWidth(), this.lune.getHeight(), 1f, 1f, 0);

I don't understand why, but the moon is placed correctly, see : Original size : windowed render

Full screen : fullscreen render

Lowered : lower render

If anyone have any explanation, i'm here to listen !

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Try calling this in your resize method and see if it works. It worked for me, but sincerely speaking, I don't know why.

batch.getProjectionMatrix().setToOrtho2D(0, 0, width, height);
| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.