0
\$\begingroup\$

I am using the Slick2D library to draw text. The text draws perfectly, however it is in the wrong location!

I could easily draw it in the correct location, however the problem is that the "correct location" is very different depending on the size of the screen - And my game needs to be able to have the screen size changed.

Here is the render method for the text:

 public static void render() {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();

    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_NORMAL_ARRAY);
    glUseProgram(0);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glMatrixMode(GL_PROJECTION);
    glLoadMatrix(orthographicProjectionMatrix);
    glMatrixMode(GL_MODELVIEW);
    glPushMatrix();
    glLoadIdentity();
    glDisable(GL_LIGHTING);
    font.drawString(0, 0, "OMG ITS TEXT!", Color.green);
    glEnable(GL_LIGHTING);
    glPopMatrix();
    glMatrixMode(GL_PROJECTION);
    glLoadMatrix(perspectiveProjectionMatrix);
    glMatrixMode(GL_MODELVIEW);
}

So, how can I set the text to be in the middle of the screen irrespective of screen size?

Thanks!

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Ok so I haven't used slick2d for a long time but basically you need to find the screens width and height and then divide them by 2 like so...

Paste these at the top of the class in the public variable section

int width;
int height;

Paste these in the method that has the "GameContainer" reference

width = container.getWidth() / 2;
height = container.getHeight() / 2;

If you don't have a GameContainer reference then try this

width = Display.getWidth()/2;
height = Display.getHeight()/2;

These return the centre of the screen... Next you want to use those variables for the drawing of you string like so...

Replace

font.drawString(0, 0, "OMG ITS TEXT", Color.green);

With

font.drawString(width, height, "OMG ITS TEXT", Color.green);

Now it should display the words in the centre of your screen... I can't remember where the anchor point for text is but you may have to find the width of the text and divide that by 2 then subtract it from the "width" variable to have perfectly centred text...

Like I said, I haven't used slick for a long long time and I am on my phone so I didn't get a chance to test it...

I hope this clears it up for you

\$\endgroup\$
11
  • \$\begingroup\$ "Cannot make a static reference to the non-static method getWidth() from the type GameContainer" What the devil is that meant to mean? I just pasted your code in the render() method above the font.drawString(). Thank you for the answer. \$\endgroup\$
    – joehot200
    Jun 6, 2014 at 13:37
  • \$\begingroup\$ Do you use a GameContainer or an AppGameContainer? \$\endgroup\$
    – Savlon
    Jun 6, 2014 at 13:41
  • \$\begingroup\$ GameContainer. I directly copy-pasted your code with no changes. \$\endgroup\$
    – joehot200
    Jun 6, 2014 at 13:45
  • \$\begingroup\$ Try again as I just changed it for you... The getWidth and getHeight are not static methods so you need to have an instance of GameContainer already created to reference them... I think slick does it automatically in a few of the inherited methods \$\endgroup\$
    – Savlon
    Jun 6, 2014 at 13:46
  • \$\begingroup\$ I cannot create an instance of GameContainer. I tried: GameContainer gameContainer = GameContainer; GameContainer gameContainer = new GameContainer(); GameContainer gameContainer = new GameContainer; \$\endgroup\$
    – joehot200
    Jun 6, 2014 at 13:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .