Class Renderer

  • All Implemented Interfaces:
    java.lang.AutoCloseable

    
    public abstract class Renderer
     implements AutoCloseable
                        

    The renderer is responsible for drawing all components to the screen, handling frame buffers, and more. Please make sure to implement all the functions in this class, and you may want to familiarize yourself with how org.polyfrost.polyui.PolyUI works.

    It is also responsible for loading and caching all images and fonts, but this is down to you as a rendering implementation to implement. for these functions, such as image and text, an initialized Font or PolyImage instance will be given. You can access the data using Resource.stream, and cache it for future use (ideally).

    • Constructor Detail

    • Method Detail

      • getAlphaCap

         final Float getAlphaCap()

        set a maximum alpha value for all future draw calls, in the range (0-1), until reset. This is useful for fading in/out all of PolyUI, for example.

        Note that this itself will not set the global alpha, so use globalAlpha to do that.

      • setAlphaCap

         final Unit setAlphaCap(Float alphaCap)

        set a maximum alpha value for all future draw calls, in the range (0-1), until reset. This is useful for fading in/out all of PolyUI, for example.

        Note that this itself will not set the global alpha, so use globalAlpha to do that.

      • getPixelRatio

         final Float getPixelRatio()

        the pixel ratio of the screen, used mainly on Apple screens which use high-dpi.

      • init

         abstract Unit init()

        This function is called during very early initialization, and should be used to prepare the renderer for use. This is always the first function called.

        Since:

        0.21.2

      • beginFrame

         abstract Unit beginFrame()

        Begin a frame. this is called before all drawing calls.

      • endFrame

         abstract Unit endFrame()

        End a frame. this is called after all drawing calls.

      • globalAlpha

         final Unit globalAlpha(Float alpha)

        Set the alpha for all future draw calls, in the range (0-1), until reset.

        Note that this call is capped by alphaCap, so if a value higher than alphaCap's value is set, it will just set it to that.

      • resetGlobalAlpha

         final Unit resetGlobalAlpha()

        reset the global alpha to normal.

        Respects the alphaCap, so may not actually reset fully if the alphaCap is set to a value lower than 1.

      • translate

         abstract Unit translate(Float x, Float y)

        translate the origin of all future draw calls by the given amount.

        you must do all your transforms inside a push, pop pair!

      • scale

         abstract Unit scale(Float x, Float y)

        scales all future draw calls by the given amount.

        you must do all your transforms inside a push, pop pair!

      • rotate

         abstract Unit rotate(Double angleRadians)

        rotate all future draw calls by the given amount.

        you must do all your transforms inside a push, pop pair!

      • skewX

         abstract Unit skewX(Double angleRadians)

        Skew all future draw calls by the given amount.

        you must do all your transforms inside a push, pop pair!

        Since:

        0.16.1

      • skewY

         abstract Unit skewY(Double angleRadians)

        Skew all future draw calls by the given amount.

        you must do all your transforms inside a push, pop pair!

        Since:

        0.16.1

      • pushScissor

         abstract Unit pushScissor(Float x, Float y, Float width, Float height)

        begin a scissor rectangle, that will clip rendering to the given rectangle.

        The scissor is affected by translate, scale, and rotate, and is intersected with the previous scissor.

        you must call popScissor after you are done with this scissor!

      • pushScissorIntersecting

         abstract Unit pushScissorIntersecting(Float x, Float y, Float width, Float height)

        begin a scissor rectangle, that will clip rendering to the given rectangle. The rectangle is intersected with the previous scissor.

        The scissor is affected by translate, scale, and rotate, and is intersected with the previous scissor.

        you must call popScissor after you are done with this scissor!

      • push

         abstract Unit push()

        Push the current state, saving all the current transforms before creating a new one with the same parameters as the current one.

        This is also called saveState or pushMatrix in many frameworks.

        pop - you must call this after you are finished!

        Since:

        0.17.2

      • pop

         abstract Unit pop()

        pop the current state, reverting all transforms to the previous values.

        This is also called restoreState or popMatrix in many frameworks.

        push - you must call this before you are begin!

        Since:

        0.17.2

      • text

         abstract Unit text(Font font, Float x, Float y, String text, Color color, Float fontSize)

        draw text to the screen, per the given parameters. The string will already be wrapped to the given width.

      • textBounds

         abstract Vec2<Unit.Pixel> textBounds(Font font, String text, Float fontSize)

        calculate the bounds of this text, per the given parameters.

        Returns:

        a Vec2 containing the width and height of the given string. If your API does not support returning string heights, just return the font size. The discrepancy should be negligible.

      • initImage

         abstract Unit initImage(PolyImage image)

        Function that can be called to explicitly initialize an image. This is used mainly for getting the size of an image, or to ensure an SVG has been rasterized.

      • rect

         abstract Unit rect(Float x, Float y, Float width, Float height, Color color, Float topLeftRadius, Float topRightRadius, Float bottomLeftRadius, Float bottomRightRadius)

        draw a rectangle to the screen, per the given parameters.

        If the radii are 0, this will just draw a normal rectangle. If they are not 0, it will draw a rounded rectangle.

      • circle

         Unit circle(Float x, Float y, Float radius, Color color)

        Draw a circle to the screen, as per the given parameters. Note that the x,y is the top left of the circle box, and this is intentional; so it is in-line with the rest of the PolyUI rendering methods.

      • line

         abstract Unit line(Float x1, Float y1, Float x2, Float y2, Color color, Float width)

        Draws a line between two points with the specified color and width.

        Parameters:
        x1 - The x-coordinate of the starting point.
        y1 - The y-coordinate of the starting point.
        x2 - The x-coordinate of the ending point.
        y2 - The y-coordinate of the ending point.
        color - The color of the line.
        width - The width of the line.
      • createFramebuffer

         abstract Framebuffer createFramebuffer(Float width, Float height)

        Create a new framebuffer. It is down to you (as a rendering implementation) to cache this, and dispose of it as necessary.

        Returns:

        a PolyUI framebuffer object using the width and height passed to this method. This is used by PolyUI to identify it.

      • delete

         abstract Unit delete(Font font)

        Delete the given font. Use this to free any native resources.

        Since:

        0.20.1

      • delete

         abstract Unit delete(PolyImage image)

        Delete the given image. Use this to free any native resources.

        Since:

        0.20.1

      • cleanup

         abstract Unit cleanup()

        Cleanup the PolyUI instance. Use this to free any native resources.