Package playn.core

Class Surface

java.lang.Object
playn.core.Surface
All Implemented Interfaces:
AutoCloseable, Closeable
Direct Known Subclasses:
TextureSurface

public class Surface extends Object implements Closeable
A surface provides a simple drawing API to a GPU accelerated render target. This can be either the main frame buffer, or a frame buffer bound to a texture.

Note: all rendering operations to a surface must be enclosed in calls to begin() and end(). This ensures that the batch into which the surface is rendering is properly flushed to the GPU at the right times.

  • Nested Class Summary

    Nested classes/interfaces inherited from interface react.Closeable

    Closeable.Set, Closeable.Util
  • Constructor Summary

    Constructors
    Constructor
    Description
    Surface(Graphics gfx, RenderTarget target, QuadBatch defaultBatch)
    Creates a surface which will render to target using defaultBatch as its default quad renderer.
  • Method Summary

    Modifier and Type
    Method
    Description
    float
    Returns the currently configured alpha.
    Starts a series of drawing commands to this surface.
    Clears the entire surface to transparent blackness.
    clear(float red, float green, float blue, float alpha)
    Clears the entire surface to the specified color.
    void
     
    int
    combineTint(int tint)
    Combines tint with the current tint via Tint.combine(int, int).
    concatenate(AffineTransform xf, float originX, float originY)
    Concatenates xf onto this surface's transform, accounting for the origin.
    draw(Tile tile, float x, float y)
    Draws a tile at the specified location: x, y.
    draw(Tile tile, float x, float y, float w, float h)
    Draws a tile at the specified location (x, y) and size (w x h).
    draw(Tile tile, float dx, float dy, float dw, float dh, float sx, float sy, float sw, float sh)
    Draws a scaled subset of an image (defined by (sx, sy) and (w x h)) at the specified location (dx, dy) and size (dw x dh).
    draw(Tile tile, int tint, float x, float y, float w, float h)
    Draws a tile at the specified location (x, y) and size (w x h), with tint tint.
    draw(Tile tile, int tint, float dx, float dy, float dw, float dh, float sx, float sy, float sw, float sh)
    Draws a scaled subset of an image (defined by (sx, sy) and (w x h)) at the specified location (dx, dy) and size (dw x dh), with tint tint.
    drawCentered(Tile tile, float x, float y)
    Draws a texture tile, centered at the specified location.
    drawLine(float x0, float y0, float x1, float y1, float width)
    Fills a line between the specified coordinates, of the specified display unit width.
    drawLine(XY a, XY b, float width)
    Fills a line between the specified coordinates, of the specified display unit width.
    end()
    Completes a series of drawing commands to this surface.
    void
    Ends a series of drawing commands that were clipped per a call to startClipped(int, int, int, int).
    fillRect(float x, float y, float width, float height)
    Fills the specified rectangle.
    boolean
    intersects(float x, float y, float w, float h)
    Returns whether the given rectangle intersects the render target area of this surface.
    void
    popBatch(QuadBatch oldBatch)
    Restores the batch that was in effect prior to a pushBatch(playn.core.QuadBatch) call.
    Pre-concatenates xf onto this surface's transform.
    pushBatch(QuadBatch newBatch)
    Configures this surface to use batch, if non-null.
    Restores the transform previously stored by saveTx().
    rotate(float angle)
    Rotates the current transformation matrix by the specified angle in radians.
    Saves the current transform.
    scale(float sx, float sy)
    Scales the current transformation matrix by the specified amount on each axis.
    setAlpha(float alpha)
    Set the alpha component of this surface's current tint.
    void
    setCheckIntersection(boolean checkIntersection)
    Configures this surface to check the bounds of drawn Tiles to ensure that they intersect our visible bounds before adding them to our GPU batch.
    setFillColor(int color)
    Sets the color to be used for fill operations.
    Sets the texture to be used for fill operations.
    setTint(int tint)
    Sets the tint to be applied to draw operations, as ARGB.
    boolean
    startClipped(int x, int y, int width, int height)
    Starts a series of drawing commands that are clipped to the specified rectangle (in view coordinates, not OpenGL coordinates).
    int
    Returns the currently configured tint.
    transform(float m00, float m01, float m10, float m11, float tx, float ty)
    Multiplies the current transformation matrix by the given matrix.
    translate(float x, float y)
    Translates the current transformation matrix by the given amount.
    tx()
    Returns the current transform.

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Surface

      public Surface(Graphics gfx, RenderTarget target, QuadBatch defaultBatch)
      Creates a surface which will render to target using defaultBatch as its default quad renderer.
  • Method Details

    • setCheckIntersection

      public void setCheckIntersection(boolean checkIntersection)
      Configures this surface to check the bounds of drawn Tiles to ensure that they intersect our visible bounds before adding them to our GPU batch. If you draw a lot of totally out of bounds images, this may increase your draw performance.
    • begin

      public Surface begin()
      Starts a series of drawing commands to this surface.
    • end

      public Surface end()
      Completes a series of drawing commands to this surface.
    • pushBatch

      public QuadBatch pushBatch(QuadBatch newBatch)
      Configures this surface to use batch, if non-null. NOOPs otherwise.
      Returns:
      a batch which should be passed to popBatch(playn.core.QuadBatch) when rendering is done with this batch.
    • popBatch

      public void popBatch(QuadBatch oldBatch)
      Restores the batch that was in effect prior to a pushBatch(playn.core.QuadBatch) call.
    • tx

      public AffineTransform tx()
      Returns the current transform.
    • saveTx

      public Surface saveTx()
      Saves the current transform.
    • restoreTx

      public Surface restoreTx()
      Restores the transform previously stored by saveTx().
    • startClipped

      public boolean startClipped(int x, int y, int width, int height)
      Starts a series of drawing commands that are clipped to the specified rectangle (in view coordinates, not OpenGL coordinates). Thus must be followed by a call to endClipped() when the clipped drawing commands are done.
      Returns:
      whether the resulting clip rectangle is non-empty. Note: the caller may wish to skip their drawing if this returns false, but they must still call endClipped().
    • endClipped

      public void endClipped()
      Ends a series of drawing commands that were clipped per a call to startClipped(int, int, int, int).
    • translate

      public Surface translate(float x, float y)
      Translates the current transformation matrix by the given amount.
    • scale

      public Surface scale(float sx, float sy)
      Scales the current transformation matrix by the specified amount on each axis.
    • rotate

      public Surface rotate(float angle)
      Rotates the current transformation matrix by the specified angle in radians.
    • transform

      public Surface transform(float m00, float m01, float m10, float m11, float tx, float ty)
      Multiplies the current transformation matrix by the given matrix.
    • concatenate

      public Surface concatenate(AffineTransform xf, float originX, float originY)
      Concatenates xf onto this surface's transform, accounting for the origin.
    • preConcatenate

      public Surface preConcatenate(AffineTransform xf)
      Pre-concatenates xf onto this surface's transform.
    • alpha

      public float alpha()
      Returns the currently configured alpha.
    • setAlpha

      public Surface setAlpha(float alpha)
      Set the alpha component of this surface's current tint. Note that this value will be quantized to an integer between 0 and 255. Also see setTint(int).

      Values outside the range [0,1] will be clamped to the range [0,1].

      Parameters:
      alpha - value in range [0,1] where 0 is transparent and 1 is opaque.
    • tint

      public int tint()
      Returns the currently configured tint.
    • setTint

      public Surface setTint(int tint)
      Sets the tint to be applied to draw operations, as ARGB. NOTE: this will overwrite any value configured via setAlpha(float). Either include your desired alpha in the high bits of tint or call setAlpha(float) after calling this method.
    • combineTint

      public int combineTint(int tint)
      Combines tint with the current tint via Tint.combine(int, int).
      Returns:
      the tint prior to combination.
    • setFillColor

      public Surface setFillColor(int color)
      Sets the color to be used for fill operations. This replaces any existing fill color or pattern.
    • setFillPattern

      public Surface setFillPattern(Texture texture)
      Sets the texture to be used for fill operations. This replaces any existing fill color or pattern.
    • intersects

      public boolean intersects(float x, float y, float w, float h)
      Returns whether the given rectangle intersects the render target area of this surface.
    • clear

      public Surface clear()
      Clears the entire surface to transparent blackness.
    • clear

      public Surface clear(float red, float green, float blue, float alpha)
      Clears the entire surface to the specified color. The channels are values in the range [0,1].
    • draw

      public Surface draw(Tile tile, float x, float y)
      Draws a tile at the specified location: x, y.
    • draw

      public Surface draw(Tile tile, float x, float y, float w, float h)
      Draws a tile at the specified location (x, y) and size (w x h).
    • draw

      public Surface draw(Tile tile, int tint, float x, float y, float w, float h)
      Draws a tile at the specified location (x, y) and size (w x h), with tint tint. Note: this will override any tint and alpha currently configured on this surface.
    • draw

      public Surface draw(Tile tile, float dx, float dy, float dw, float dh, float sx, float sy, float sw, float sh)
      Draws a scaled subset of an image (defined by (sx, sy) and (w x h)) at the specified location (dx, dy) and size (dw x dh).
    • draw

      public Surface draw(Tile tile, int tint, float dx, float dy, float dw, float dh, float sx, float sy, float sw, float sh)
      Draws a scaled subset of an image (defined by (sx, sy) and (w x h)) at the specified location (dx, dy) and size (dw x dh), with tint tint. Note: this will override any tint and alpha currently configured on this surface.
    • drawCentered

      public Surface drawCentered(Tile tile, float x, float y)
      Draws a texture tile, centered at the specified location.
    • drawLine

      public Surface drawLine(XY a, XY b, float width)
      Fills a line between the specified coordinates, of the specified display unit width.
    • drawLine

      public Surface drawLine(float x0, float y0, float x1, float y1, float width)
      Fills a line between the specified coordinates, of the specified display unit width.
    • fillRect

      public Surface fillRect(float x, float y, float width, float height)
      Fills the specified rectangle.
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable