Package ponkOut.graphics

Source Code of ponkOut.graphics.HUDText

/* Copyright 2010 Christian Matt
*
* This file is part of PonkOut.
*
* PonkOut is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PonkOut is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PonkOut.  If not, see <http://www.gnu.org/licenses/>.
*/

package ponkOut.graphics;

import org.lwjgl.opengl.GL11;
import org.lwjgl.util.vector.Vector2f;

import ponkOut.Settings;

/**
* Text displayed at screen as a head-up display
*/
public class HUDText extends HUDObject {
  private String string;
  private Vector2f position;
  private float size;
  private Font font;

  /**
   * @param string
   *            text to display
   * @param position
   *            top left position of first character
   * @param size
   *            size of the text
   * @param r
   *            red
   * @param g
   *            green
   * @param b
   *            blue
   * @param a
   *            alpha
   * @param visible
   *            true if text shall be rendered
   * @param goManager
   *            GraphicsObjectManager this text will be added to
   */
  public HUDText(String string, float size, float r, float g, float b, float a, Vector2f position, boolean visible,
      GraphicsObjectsManager goManager) {
    super(new Color(r, g, b, a), visible, goManager);
    this.string = string;
    this.size = size;
    this.font = new Font();

    // use negative y-value because y-axis points upwards and origin is in
    // the upper left corner
    this.position = new Vector2f(position.x, -position.y);
  }

  /**
   * creates centred HUD text
   *
   * @param string
   *            text to display
   * @param size
   *            size of the text
   * @param r
   *            red
   * @param g
   *            green
   * @param b
   *            blue
   * @param a
   *            alpha
   * @param visible
   *            true if text shall be rendered
   * @param goManager
   *            GraphicsObjectManager this text will be added to
   */
  public HUDText(String string, float size, float r, float g, float b, float a, boolean visible,
      GraphicsObjectsManager goManager) {
    super(new Color(r, g, b, a), visible, goManager);
    this.string = string;
    this.size = size;
    this.font = new Font();

    float resX = Settings.getInstance().getResolutionX();
    float resY = Settings.getInstance().getResolutionY();
    float length = size * font.getCharacterWidth() * string.length();
    float height = size * font.getCharacterHeight();

    // use negative y-value because y-axis points upwards and origin is in
    // the upper left corner
    this.position = new Vector2f((resX - length) / 2.0f, -(resY - height) / 2.0f);
  }

  public void setText(String string) {
    this.string = string;
  }

  @Override
  public void draw() {
    // store current transformation matrix
    GL11.glPushMatrix();

    // translate to text position
    GL11.glTranslatef(position.x, position.y, 0.0f);

    // scale
    GL11.glScalef(size, size, size);

    // set color
    color.use();

    // draw it
    font.drawString(string, false, false);

    // restore transformation matrix
    GL11.glPopMatrix();
  }

  public void translate(float x, float y) {
    position.x += x;
    position.y += y;
  }
}
TOP

Related Classes of ponkOut.graphics.HUDText

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.