Example #1
0
  /**
   * Draws a rectangle using the given vertices. There must be 4 vertices, each made up of 5
   * elements in this order: x, y, color, u, v.
   */
  public void draw(Texture texture, float[] spriteVertices, int offset, int length) {
    if (!drawing) throw new IllegalStateException("SpriteBatch.begin must be called before draw.");

    if (texture != lastTexture) {
      switchTexture(texture);
    }

    int remainingVertices = vertices.length - idx;
    if (remainingVertices == 0) {
      renderMesh();
      remainingVertices = vertices.length;
    }
    int vertexCount = Math.min(remainingVertices, length - offset);
    System.arraycopy(spriteVertices, offset, vertices, idx, vertexCount);
    offset += vertexCount;
    idx += vertexCount;

    while (offset < length) {
      renderMesh();
      vertexCount = Math.min(vertices.length, length - offset);
      System.arraycopy(spriteVertices, offset, vertices, 0, vertexCount);
      offset += vertexCount;
      idx += vertexCount;
    }
  }
Example #2
0
  /**
   * Draws a rectangle with the bottom left corner at x,y having the width and height of the
   * texture.
   *
   * @param texture the Texture
   * @param x the x-coordinate in screen space
   * @param y the y-coordinate in screen space
   */
  public void draw(Texture texture, float x, float y) {
    if (!drawing) throw new IllegalStateException("SpriteBatch.begin must be called before draw.");

    if (texture != lastTexture) {
      switchTexture(texture);
    } else if (idx == vertices.length) renderMesh();

    final float fx2 = x + texture.getWidth();
    final float fy2 = y + texture.getHeight();

    vertices[idx++] = x;
    vertices[idx++] = y;
    vertices[idx++] = color;
    vertices[idx++] = 0;
    vertices[idx++] = 1;

    vertices[idx++] = x;
    vertices[idx++] = fy2;
    vertices[idx++] = color;
    vertices[idx++] = 0;
    vertices[idx++] = 0;

    vertices[idx++] = fx2;
    vertices[idx++] = fy2;
    vertices[idx++] = color;
    vertices[idx++] = 1;
    vertices[idx++] = 0;

    vertices[idx++] = fx2;
    vertices[idx++] = y;
    vertices[idx++] = color;
    vertices[idx++] = 1;
    vertices[idx++] = 1;
  }
Example #3
0
  /**
   * Draws a rectangle with the bottom left corner at x,y having the given width and height in
   * pixels. The portion of the {@link Texture} given by u, v and u2, v2 are used. These coordinates
   * and sizes are given in texture size percentage. The rectangle will have the given tint {@link
   * Color}.
   *
   * @param texture the Texture
   * @param x the x-coordinate in screen space
   * @param y the y-coordinate in screen space
   * @param width the width in pixels
   * @param height the height in pixels
   */
  public void draw(
      Texture texture,
      float x,
      float y,
      float width,
      float height,
      float u,
      float v,
      float u2,
      float v2) {
    if (!drawing) throw new IllegalStateException("SpriteBatch.begin must be called before draw.");

    if (texture != lastTexture) {
      switchTexture(texture);
    } else if (idx == vertices.length) renderMesh();

    final float fx2 = x + width;
    final float fy2 = y + height;

    vertices[idx++] = x;
    vertices[idx++] = y;
    vertices[idx++] = color;
    vertices[idx++] = u;
    vertices[idx++] = v;

    vertices[idx++] = x;
    vertices[idx++] = fy2;
    vertices[idx++] = color;
    vertices[idx++] = u;
    vertices[idx++] = v2;

    vertices[idx++] = fx2;
    vertices[idx++] = fy2;
    vertices[idx++] = color;
    vertices[idx++] = u2;
    vertices[idx++] = v2;

    vertices[idx++] = fx2;
    vertices[idx++] = y;
    vertices[idx++] = color;
    vertices[idx++] = u2;
    vertices[idx++] = v;
  }
Example #4
0
  /**
   * Draws a rectangle with the bottom left corner at x,y having the given width and height in
   * pixels. The portion of the {@link Texture} given by srcX, srcY and srcWidth, srcHeight are
   * used. These coordinates and sizes are given in texels.
   *
   * @param texture the Texture
   * @param x the x-coordinate in screen space
   * @param y the y-coordinate in screen space
   * @param srcX the x-coordinate in texel space
   * @param srcY the y-coordinate in texel space
   * @param srcWidth the source with in texels
   * @param srcHeight the source height in texels
   */
  public void draw(
      Texture texture, float x, float y, int srcX, int srcY, int srcWidth, int srcHeight) {
    if (!drawing) throw new IllegalStateException("SpriteBatch.begin must be called before draw.");

    if (texture != lastTexture) {
      switchTexture(texture);
    } else if (idx == vertices.length) renderMesh();

    final float u = srcX * invTexWidth;
    final float v = (srcY + srcHeight) * invTexHeight;
    final float u2 = (srcX + srcWidth) * invTexWidth;
    final float v2 = srcY * invTexHeight;
    final float fx2 = x + srcWidth;
    final float fy2 = y + srcHeight;

    vertices[idx++] = x;
    vertices[idx++] = y;
    vertices[idx++] = color;
    vertices[idx++] = u;
    vertices[idx++] = v;

    vertices[idx++] = x;
    vertices[idx++] = fy2;
    vertices[idx++] = color;
    vertices[idx++] = u;
    vertices[idx++] = v2;

    vertices[idx++] = fx2;
    vertices[idx++] = fy2;
    vertices[idx++] = color;
    vertices[idx++] = u2;
    vertices[idx++] = v2;

    vertices[idx++] = fx2;
    vertices[idx++] = y;
    vertices[idx++] = color;
    vertices[idx++] = u2;
    vertices[idx++] = v;
  }
Example #5
0
  /**
   * Draws a rectangle with the bottom left corner at x,y and stretching the region to cover the
   * given width and height. The rectangle is offset by originX, originY relative to the origin.
   * Scale specifies the scaling factor by which the rectangle should be scaled around originX,
   * originY. Rotation specifies the angle of counter clockwise rotation of the rectangle around
   * originX, originY.
   */
  public void draw(
      TextureRegion region,
      float x,
      float y,
      float originX,
      float originY,
      float width,
      float height,
      float scaleX,
      float scaleY,
      float rotation) {
    if (!drawing) throw new IllegalStateException("SpriteBatch.begin must be called before draw.");

    Texture texture = region.texture;
    if (texture != lastTexture) {
      switchTexture(texture);
    } else if (idx == vertices.length) //
    renderMesh();

    // bottom left and top right corner points relative to origin
    final float worldOriginX = x + originX;
    final float worldOriginY = y + originY;
    float fx = -originX;
    float fy = -originY;
    float fx2 = width - originX;
    float fy2 = height - originY;

    // scale
    if (scaleX != 1 || scaleY != 1) {
      fx *= scaleX;
      fy *= scaleY;
      fx2 *= scaleX;
      fy2 *= scaleY;
    }

    // construct corner points, start from top left and go counter clockwise
    final float p1x = fx;
    final float p1y = fy;
    final float p2x = fx;
    final float p2y = fy2;
    final float p3x = fx2;
    final float p3y = fy2;
    final float p4x = fx2;
    final float p4y = fy;

    float x1;
    float y1;
    float x2;
    float y2;
    float x3;
    float y3;
    float x4;
    float y4;

    // rotate
    if (rotation != 0) {
      final float cos = MathUtils.cosDeg(rotation);
      final float sin = MathUtils.sinDeg(rotation);

      x1 = cos * p1x - sin * p1y;
      y1 = sin * p1x + cos * p1y;

      x2 = cos * p2x - sin * p2y;
      y2 = sin * p2x + cos * p2y;

      x3 = cos * p3x - sin * p3y;
      y3 = sin * p3x + cos * p3y;

      x4 = x1 + (x3 - x2);
      y4 = y3 - (y2 - y1);
    } else {
      x1 = p1x;
      y1 = p1y;

      x2 = p2x;
      y2 = p2y;

      x3 = p3x;
      y3 = p3y;

      x4 = p4x;
      y4 = p4y;
    }

    x1 += worldOriginX;
    y1 += worldOriginY;
    x2 += worldOriginX;
    y2 += worldOriginY;
    x3 += worldOriginX;
    y3 += worldOriginY;
    x4 += worldOriginX;
    y4 += worldOriginY;

    final float u = region.u;
    final float v = region.v2;
    final float u2 = region.u2;
    final float v2 = region.v;

    vertices[idx++] = x1;
    vertices[idx++] = y1;
    vertices[idx++] = color;
    vertices[idx++] = u;
    vertices[idx++] = v;

    vertices[idx++] = x2;
    vertices[idx++] = y2;
    vertices[idx++] = color;
    vertices[idx++] = u;
    vertices[idx++] = v2;

    vertices[idx++] = x3;
    vertices[idx++] = y3;
    vertices[idx++] = color;
    vertices[idx++] = u2;
    vertices[idx++] = v2;

    vertices[idx++] = x4;
    vertices[idx++] = y4;
    vertices[idx++] = color;
    vertices[idx++] = u2;
    vertices[idx++] = v;
  }