示例#1
0
  public static BufferedImage convertToImage(TextureRegion textureRegion) {
    final int width = textureRegion.getWidth();
    final int height = textureRegion.getHeight();

    final Rect2i pixelRegion = textureRegion.getPixelRegion();
    final Texture texture = textureRegion.getTexture();
    ByteBuffer textureBytes = texture.getData().getBuffers()[0];
    int stride = texture.getWidth() * 4;
    int posX = pixelRegion.minX();
    int posY = pixelRegion.minY();

    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    for (int y = 0; y < height; y++) {
      for (int x = 0; x < width; x++) {
        int r = UnsignedBytes.toInt(textureBytes.get((posY + y) * stride + (posX + x) * 4));
        int g = UnsignedBytes.toInt(textureBytes.get((posY + y) * stride + (posX + x) * 4 + 1));
        int b = UnsignedBytes.toInt(textureBytes.get((posY + y) * stride + (posX + x) * 4 + 2));
        int a = UnsignedBytes.toInt(textureBytes.get((posY + y) * stride + (posX + x) * 4 + 3));

        int argb = (a << 24) + (r << 16) + (g << 8) + b;
        image.setRGB(x, y, argb);
      }
    }
    return image;
  }
示例#2
0
 public HeroActor(int x, int y, int heroNumber, boolean playerControlled, DankGame dankGame) {
   this.playerControlled = playerControlled;
   if (playerControlled) {
     characterList = Dank.chosenHeroesList;
   } else {
     characterList = Dank.enemyHeroesList;
   }
   pixmap = new Pixmap(64, 64, Pixmap.Format.RGBA8888);
   bitmapFont = new BitmapFont();
   bitmapFont.setColor(1, 0, 0, 1);
   this.x = x;
   this.y = y;
   this.heroNumber = heroNumber;
   this.dankGameInstance = dankGame;
   if (playerControlled == true) {
     heroSheet = characterList.get(heroNumber - 1).getCharacterSheet();
     heroFrames = new TextureRegion[4 * 3];
     TextureRegion[][] tmp = TextureRegion.split(heroSheet, 32, 32);
     int index = 0;
     for (int i = 0; i < 4; i++) {
       for (int j = 0; j < 3; j++) {
         heroFrames[index++] = tmp[i][j];
       }
     }
     TextureRegion[] standingFrames = new TextureRegion[2];
     standingFrames[0] = heroFrames[7];
     standingFrames[1] = heroFrames[8];
     standingAnimation = new Animation(1f, standingFrames);
     spriteBatch = new SpriteBatch();
   } else {
     heroSheet = characterList.get(heroNumber - 1).getCharacterSheet();
     heroFrames = new TextureRegion[1 * 1];
     TextureRegion[][] tmp = TextureRegion.split(heroSheet, 64, 64);
     int index = 0;
     for (int i = 0; i < 1; i++) {
       for (int j = 0; j < 1; j++) {
         heroFrames[index++] = tmp[i][j];
       }
     }
     standingAnimation = new Animation(1f, heroFrames);
     spriteBatch = new SpriteBatch();
   }
   setBounds(x, y, 64, 64);
   addCustomListener();
   update();
 }
示例#3
0
 /**
  * Draws a rectangle with the bottom left corner at x,y having the width and height of the region.
  */
 public void draw(TextureRegion region, float x, float y) {
   draw(region, x, y, Math.abs(region.getRegionWidth()), Math.abs(region.getRegionHeight()));
 }
示例#4
0
 /** Disposes the texture used by this BitmapFont's region. */
 public void dispose() {
   region.getTexture().dispose();
 }
示例#5
0
 /**
  * Draws a substring at the specified position.
  *
  * @param x The x position for the left most character.
  * @param y The y position for the top of most capital letters in the font (the {@link
  *     #getCapHeight() cap height}).
  * @param start The first character of the string to draw.
  * @param end The last character of the string to draw (exclusive).
  * @return The bounds of the rendered string (the height is the distance from y to the baseline).
  *     Note the same TextBounds instance is used for all methods that return TextBounds.
  */
 public TextBounds draw(
     SpriteBatch spriteBatch, CharSequence str, float x, float y, int start, int end) {
   float batchColor = spriteBatch.color;
   spriteBatch.setColor(color);
   final Texture texture = region.getTexture();
   y += ascent;
   float startX = x;
   Glyph lastGlyph = null;
   if (scaleX == 1 && scaleY == 1) {
     while (start < end) {
       lastGlyph = getGlyph(str.charAt(start++));
       if (lastGlyph != null) {
         spriteBatch.draw(
             texture, //
             x + lastGlyph.xoffset,
             y + lastGlyph.yoffset, //
             lastGlyph.width,
             lastGlyph.height, //
             lastGlyph.u,
             lastGlyph.v,
             lastGlyph.u2,
             lastGlyph.v2);
         x += lastGlyph.xadvance;
         break;
       }
     }
     while (start < end) {
       char ch = str.charAt(start++);
       Glyph g = getGlyph(ch);
       if (g == null) continue;
       x += lastGlyph.getKerning(ch);
       lastGlyph = g;
       spriteBatch.draw(
           texture, //
           x + lastGlyph.xoffset,
           y + lastGlyph.yoffset, //
           lastGlyph.width,
           lastGlyph.height, //
           lastGlyph.u,
           lastGlyph.v,
           lastGlyph.u2,
           lastGlyph.v2);
       x += g.xadvance;
     }
   } else {
     float scaleX = this.scaleX, scaleY = this.scaleY;
     while (start < end) {
       lastGlyph = getGlyph(str.charAt(start++));
       if (lastGlyph != null) {
         spriteBatch.draw(
             texture, //
             x + lastGlyph.xoffset * scaleX, //
             y + lastGlyph.yoffset * scaleY, //
             lastGlyph.width * scaleX, //
             lastGlyph.height * scaleY, //
             lastGlyph.u,
             lastGlyph.v,
             lastGlyph.u2,
             lastGlyph.v2);
         x += lastGlyph.xadvance * scaleX;
         break;
       }
     }
     while (start < end) {
       char ch = str.charAt(start++);
       Glyph g = getGlyph(ch);
       if (g == null) continue;
       x += lastGlyph.getKerning(ch) * scaleX;
       lastGlyph = g;
       spriteBatch.draw(
           texture, //
           x + lastGlyph.xoffset * scaleX, //
           y + lastGlyph.yoffset * scaleY, //
           lastGlyph.width * scaleX, //
           lastGlyph.height * scaleY, //
           lastGlyph.u,
           lastGlyph.v,
           lastGlyph.u2,
           lastGlyph.v2);
       x += g.xadvance * scaleX;
     }
   }
   spriteBatch.setColor(batchColor);
   textBounds.width = x - startX;
   textBounds.height = capHeight;
   return textBounds;
 }
示例#6
0
  private void init(FileHandle fontFile, TextureRegion region, boolean flip) {
    flipped = flip;
    BufferedReader reader = new BufferedReader(new InputStreamReader(fontFile.read()), 512);
    try {
      reader.readLine(); // info

      String line = reader.readLine();
      if (line == null) throw new GdxRuntimeException("Invalid font file: " + fontFile);
      String[] common = line.split(" ", 4);
      if (common.length < 4) throw new GdxRuntimeException("Invalid font file: " + fontFile);

      if (!common[1].startsWith("lineHeight="))
        throw new GdxRuntimeException("Invalid font file: " + fontFile);
      lineHeight = Integer.parseInt(common[1].substring(11));

      if (!common[2].startsWith("base="))
        throw new GdxRuntimeException("Invalid font file: " + fontFile);
      int baseLine = Integer.parseInt(common[2].substring(5));

      if (region != null) reader.readLine(); // page
      else {
        line = reader.readLine();
        if (line == null) throw new GdxRuntimeException("Invalid font file: " + fontFile);
        String[] page = line.split(" ", 4);
        if (!page[2].startsWith("file="))
          throw new GdxRuntimeException("Invalid font file: " + fontFile);
        String imgFilename = null;
        if (page[2].endsWith("\"")) {
          imgFilename = page[2].substring(6, page[2].length() - 1);
        } else {
          imgFilename = page[2].substring(5, page[2].length());
        }
        FileHandle imageFile = fontFile.parent().child(imgFilename);
        region = new TextureRegion(new Texture(imageFile, false));
      }

      this.region = region;
      float invTexWidth = 1.0f / region.getTexture().getWidth();
      float invTexHeight = 1.0f / region.getTexture().getHeight();
      float u = region.u;
      float v = region.v;

      descent = 0;
      //			descent = g != null? baseLine + g.yoffset:0;

      while (true) {
        line = reader.readLine();
        if (line == null) break;
        if (line.startsWith("kernings ")) break;
        if (!line.startsWith("char ")) continue;

        Glyph glyph = new Glyph();

        StringTokenizer tokens = new StringTokenizer(line, " =");
        tokens.nextToken();
        tokens.nextToken();
        int ch = Integer.parseInt(tokens.nextToken());
        if (ch <= Character.MAX_VALUE) {
          Glyph[] page = glyphs[ch / PAGE_SIZE];
          if (page == null) glyphs[ch / PAGE_SIZE] = page = new Glyph[PAGE_SIZE];
          page[ch & PAGE_SIZE - 1] = glyph;
        } else continue;
        tokens.nextToken();
        int srcX = Integer.parseInt(tokens.nextToken());
        tokens.nextToken();
        int srcY = Integer.parseInt(tokens.nextToken());
        tokens.nextToken();
        glyph.width = Integer.parseInt(tokens.nextToken());
        tokens.nextToken();
        glyph.height = Integer.parseInt(tokens.nextToken());
        tokens.nextToken();
        glyph.xoffset = Integer.parseInt(tokens.nextToken());
        tokens.nextToken();
        if (flip) glyph.yoffset = Integer.parseInt(tokens.nextToken());
        else glyph.yoffset = -(glyph.height + Integer.parseInt(tokens.nextToken()));
        tokens.nextToken();
        glyph.xadvance = Integer.parseInt(tokens.nextToken());

        glyph.u = u + srcX * invTexWidth;
        glyph.u2 = u + (srcX + glyph.width) * invTexWidth;
        if (flip) {
          glyph.v = v + srcY * invTexHeight;
          glyph.v2 = v + (srcY + glyph.height) * invTexHeight;
        } else {
          glyph.v2 = v + srcY * invTexHeight;
          glyph.v = v + (srcY + glyph.height) * invTexHeight;
        }

        descent = Math.min(baseLine + glyph.yoffset, descent);
      }

      while (true) {
        line = reader.readLine();
        if (line == null) break;
        if (!line.startsWith("kerning ")) break;

        StringTokenizer tokens = new StringTokenizer(line, " =");
        tokens.nextToken();
        tokens.nextToken();
        int first = Integer.parseInt(tokens.nextToken());
        tokens.nextToken();
        int second = Integer.parseInt(tokens.nextToken());
        if (first < 0 || first > Character.MAX_VALUE || second < 0 || second > Character.MAX_VALUE)
          continue;
        Glyph glyph = getGlyph((char) first);
        tokens.nextToken();
        int amount = Integer.parseInt(tokens.nextToken());
        glyph.setKerning(second, amount);
      }

      Glyph g = getGlyph(' ');
      if (g == null) {
        g = new Glyph();
        g.xadvance = getGlyph('l').xadvance;
        Glyph[] page = glyphs[' ' / PAGE_SIZE];
        if (page == null) glyphs[' ' / PAGE_SIZE] = page = new Glyph[PAGE_SIZE];
        page[' ' & PAGE_SIZE - 1] = g;
      }
      spaceWidth = g != null ? g.xadvance + g.width : 1;

      g = getGlyph('x');
      xHeight = g != null ? g.height : 1;

      g = getGlyph('M');
      capHeight = g != null ? g.height : 1;

      ascent = baseLine - capHeight;
      down = -lineHeight;
      if (flip) {
        ascent = -ascent;
        down = -down;
      }
    } catch (Exception ex) {
      throw new GdxRuntimeException("Error loading font file: " + fontFile, ex);
    } finally {
      try {
        reader.close();
      } catch (IOException ignored) {
      }
    }
  }