UNPKG

blaze-2d

Version:

A fast and simple WebGL 2 2D game engine written in TypeScript

135 lines (134 loc) 4.25 kB
import { vec2 } from "gl-matrix"; import Rect from "../shapes/rect"; import Renderer from "./renderer"; import Entity from "../entity"; import Shape from "../shapes/shape"; import TextureAtlas from "../texture/atlas"; import Circle from "../shapes/circle"; import { ZMap } from "../utils/types"; import Triangle from "../shapes/triangle"; /** * Stores the data needed to render a shape in the world. * * @field `shape` The shape to be rendered * @field `pos` The world position to start drawing the shape at * @field `rot` A base rotation to apply to the shape's vertices, in radians */ interface Renderable<T> { shape: T; pos: vec2; rot: number; } /** * Batch renders instances of shapes and entities. * * A texture atlas must be used with the batch renderer. */ export default abstract class BatchRenderer extends Renderer { static atlas: TextureAtlas; static batchQueue: ZMap<{ [index: string]: Renderable<Shape>[]; }>; /** * Renders all items currently in the batch render queue and clears the queue. * * Should be called at the end of each frame. * * If there is no camera specified in {@link Renderer} then nothing will be rendered. */ static flush(z: number): void; /** * Queue an entity to be batch rendered. * * @param entity The entity to render * @param zIndex The z index of the entity */ static queueEntity(entity: Entity, zIndex?: number): void; /** * Batch render an array of entities. * * @param entities The entities to render * @param zIndex The z position of the rendered rectangle */ static queueEntities(entities: Entity[], zIndex?: number): void; /** * Queue a group of rectangles to be batch rendered. * * @param rects The rects to queue * @param zIndex The z index of the rectangles */ static queueRects(rects: (Rect | Renderable<Rect>)[], zIndex?: number): void; /** * Queue a group of circle to be batch rendered. * * @param circles The circles to queue * @param zIndex The z index of the circles */ static queueCircles(circles: (Circle | Renderable<Circle>)[], zIndex?: number): void; /** * Queue a group of triangle to be batch rendered. * * @param triangles The triangles to queue * @param zIndex The z index of the triangles */ static queueTriangles(triangles: (Triangle | Renderable<Triangle>)[], zIndex?: number): void; private static addRenderablesToQueue; /** * Batch render an array of rectangles. * * @param rects The rects to render * @param zIndex The z index of the rendered rectangles */ private static renderRects; /** * Batch render an array of circles. * * @param circles The circles to render * @param zIndex The z index of the rendered circles */ private static renderCircles; /** * Batch render an array of triangles. * * @param triangles The triangles to render * @param zIndex The z index of the rendered triangles */ private static renderTriangles; /** * Renders geometry using a shape shader. * * @param geometry The geometry to render * @param type The type of shape shader to use * @param zIndex The z position of the rendered rectangle */ private static renderGeometry; /** * Generates geometry data for an array of {@link Renderable} shapes. */ private static getGeometryFromRenderables; private static getRenderableRectsFromRects; private static getRenderableCirclesFromCircles; private static getRenderableTrianglesfromTriangles; private static getRenderableShapesFromEntites; /** * Gets the batch render queue. * * @returns the batch render queue */ static getBatchQueue(): ZMap<{ [index: string]: Renderable<Shape>[]; }>; /** * Gets the maximum zIndex used by the queue. * * @returns The max zIndex of the queue */ static getQueueMax(): number; /** * Gets the minimum zIndex used by the queue. * * @returns The min zIndex of the queue */ static getQueueMin(): number; } export {};