io.flob.blackheart.FontRenderer.java Source code

Java tutorial

Introduction

Here is the source code for io.flob.blackheart.FontRenderer.java

Source

/*
 *    ______     __         ______     ______     __  __     __  __     ______     ______     ______     ______  
 *   /\  == \   /\ \       /\  __ \   /\  ___\   /\ \/ /    /\ \_\ \   /\  ___\   /\  __ \   /\  == \   /\__  _\ 
 *   \ \  __<   \ \ \____  \ \  __ \  \ \ \____  \ \  _"-.  \ \  __ \  \ \  __\   \ \  __ \  \ \  __<   \/_/\ \/ 
 *    \ \_____\  \ \_____\  \ \_\ \_\  \ \_____\  \ \_\ \_\  \ \_\ \_\  \ \_____\  \ \_\ \_\  \ \_\ \_\    \ \_\ 
 *     \/_____/   \/_____/   \/_/\/_/   \/_____/   \/_/\/_/   \/_/\/_/   \/_____/   \/_/\/_/   \/_/ /_/     \/_/ 
 * 
 *      (August 10th-17th 2013) 
 *      <http://7dfps.calvert.io>
 *
 *      blackheart
 *      Copyright (c) 2013 Robert Calvert <http://robert.calvert.io>
 *
 *      This program 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 2 of the License, or
 *      (at your option) any later version.
 *
 *      This program 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 this program.  If not, see <http://www.gnu.org/licenses/>
 *
 */
package io.flob.blackheart;

import java.awt.Font;
import org.lwjgl.opengl.GL11;
import org.newdawn.slick.Color;
import org.newdawn.slick.TrueTypeFont;

/**
 *
 * @author rob
 */
public final class FontRenderer {

    private final TrueTypeFont _font;
    private final Color font_colour = Color.lightGray;

    public FontRenderer(String resource, float size) throws Exception {
        Font awtFont;
        awtFont = Font.createFont(Font.TRUETYPE_FONT, getClass().getResourceAsStream(resource));
        awtFont = awtFont.deriveFont(size);
        _font = new TrueTypeFont(awtFont, true);
    }

    public TrueTypeFont font() {
        return _font;
    }

    public void render(int x, int y, Object text) {
        _font.drawString(x, y, "" + text, font_colour);
        GL11.glColor3d(1, 1, 1);
    }

    public void render_centred(int x, int y, Object text) {
        render(x - (_font.getWidth("" + text) / 2), y - (_font.getHeight("" + text) / 2), text);
    }

    public void render(int x, int y, Object text, Color _font_colour) {
        _font.drawString(x, y, "" + text, _font_colour);
        GL11.glColor3d(1, 1, 1);
    }

    public void render_centred(int x, int y, Object text, Color _font_colour) {
        render(x - (_font.getWidth("" + text) / 2), y - (_font.getHeight("" + text) / 2), text, _font_colour);
    }
}