示例#1
0
  public static UIFont createGuiFont(String name, int font_size) {
    Font font = load_ttf(StringUtils.concat(name, ".ttf"), font_size);
    UIFont tmp = new UIFont();

    tmp.font_texture = new Texture();

    Graphics2D graphics = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB).createGraphics();
    graphics.setFont(font);

    tmp.fontMetrics = graphics.getFontMetrics();

    WritableRaster raster;
    BufferedImage bufferedImage;

    raster =
        Raster.createInterleavedRaster(
            DataBuffer.TYPE_BYTE,
            (int) tmp.getFontImageWidth(),
            (int) tmp.getFontImageHeight(),
            4,
            null);
    bufferedImage = new BufferedImage(glAlphaColorModel, raster, false, null);

    // Draw the characters on our image
    Graphics2D imageGraphics = bufferedImage.createGraphics();
    imageGraphics.setRenderingHint(
        RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
    imageGraphics.setRenderingHint(
        RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    imageGraphics.setFont(font);
    imageGraphics.setColor(Color.white.getAWTColor());

    // draw every CHAR by line...
    for (int i : key_table.keySet())
      imageGraphics.drawString(
          key_table.get(i), 0, (int) (tmp.fontMetrics.getMaxAscent() + (tmp.getHeight() * i)));

    // Generate texture data
    byte[] data = ((DataBufferByte) bufferedImage.getRaster().getDataBuffer()).getData();

    ByteBuffer imageData = BufferUtils.create_byte_buffer(data.length);
    imageData.order(ByteOrder.nativeOrder());
    imageData.put(data, 0, data.length);
    imageData.flip();

    glBindTexture(GL_TEXTURE_2D, tmp.font_texture.id);

    // Setup wrap mode
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

    glTexImage2D(
        GL_TEXTURE_2D,
        0,
        GL_RGBA8,
        (int) tmp.getFontImageWidth(),
        (int) tmp.getFontImageHeight(),
        0,
        GL_RGBA,
        GL_UNSIGNED_BYTE,
        imageData);

    return tmp;
  }
示例#2
0
  public static UIFont getDefault() {
    if (default_font == null) default_font = UIFont.createGuiFont("Kasper_R", 32);

    return default_font;
  }