Exemple #1
0
  private void generateFontTexture(Font font) {
    Map<Character, CharImage> charImages = new HashMap<>();

    int imageWidth = 0;
    int imageHeight = 0;

    boolean antiAlias = false;

    /* Start at char #32, because ASCII 0 to 31 are just control codes */
    for (int i = 32; i < 256; i++) {
      if (i == 127) {
          /* ASCII 127 is the DEL control code, so we can skip it */
        continue;
      }
      char c = (char) i;
      CharImage charImage = createCharImage(font, imageWidth, c, antiAlias);
      if (charImage == null) {
        /* If char image is null that font does not contain the char */
        continue;
      }

      /* Create glyph and draw char on image */
      charImages.put(c, charImage);
      imageWidth += charImage.getWidth();
      imageHeight = Math.max(imageHeight, charImage.getHeight());
    }

    BufferedImage image =
        new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_ARGB_PRE);

    Graphics2D g = image.createGraphics();

    if (antiAlias) {
      g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    }
    g.setFont(font);
    g.setPaint(Color.WHITE);

    /* Create BufferedImage from all character images */
    for (Map.Entry<Character, CharImage> entry : charImages.entrySet()) {

      CharImage charImage = entry.getValue();
      Glyph glyph = charImage.getGlyph();

      g.drawString(String.valueOf(entry.getKey()), glyph.getX(), charImage.getAscent());
      glyphs.put(entry.getKey(), glyph);
    }
    g.dispose();

    try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
      PngEncoder encoder = new PngEncoder();
      encoder.write(image, os);
      int textureId = TextureUtils.loadTexture(new ByteArrayInputStream(os.toByteArray()));
      fontTexture = new Texture(textureId, imageWidth, imageHeight);
    } catch (IOException e) {
      throw new IllegalStateException(e);
    }
  }
Exemple #2
0
  @Override
  public void init() {
    int len = text.length();

    TFloatList verticles = new TFloatArrayList(12 * len);
    TFloatList textCoords = new TFloatArrayList(8 * len);
    TIntList indices = new TIntArrayList(6 * len);

    float textureWidth = gameFont.getFontTextureWidth();
    float position = 0.0f;

    for (int i = 0; i < len; i++) {
      Glyph glyph = gameFont.getGlyphs().get(text.charAt(i));

      add(verticles, position, 0.0f, 0.0f);
      add(verticles, position, glyph.getHeight(), 0.0f);
      add(verticles, glyph.getWidth() + position, glyph.getHeight(), 0.0f);
      add(verticles, glyph.getWidth() + position, 0.0f, 0.0f);

      position += glyph.getWidth();

      float textCoordX1 = glyph.getX() / textureWidth;
      float textCoordX2 = (glyph.getX() + glyph.getWidth()) / textureWidth;

      add(textCoords, textCoordX1, 0.0f);
      add(textCoords, textCoordX1, 1.0f);
      add(textCoords, textCoordX2, 1.0f);
      add(textCoords, textCoordX2, 0.0f);

      int firstIndice = i * 4;
      indices.add(firstIndice + 0);
      indices.add(firstIndice + 1);
      indices.add(firstIndice + 3);
      indices.add(firstIndice + 3);
      indices.add(firstIndice + 1);
      indices.add(firstIndice + 2);
    }
    mesh = new Mesh(verticles.toArray(), textCoords.toArray(), indices.toArray());
    mesh.setTextureId(gameFont.getFontTextureId());
  }