blaze-2d
Version:
A fast and simple WebGL 2 2D game engine written in TypeScript
157 lines (156 loc) • 5.23 kB
TypeScript
import { vec2 } from "gl-matrix";
import { ShaderProgramInfo } from "../utils/gl";
import Color from "../utils/color";
import Camera from "../camera/camera";
import Shape from "../shapes/shape";
import { ZMap } from "../utils/types";
interface RenderQueueItem {
shape: Shape;
position: vec2;
rotation: number;
}
/**
* Renders single instances of shapes at a time.
*/
export default abstract class Renderer {
private static gl;
private static resolutionScale;
private static mode;
private static camera;
/**
* The scale applied to shape vertices to obtain their clip space vertices.
*/
protected static scale: vec2;
protected static queue: ZMap<RenderQueueItem[]>;
static positionBuffer: WebGLBuffer;
static texBuffer: WebGLBuffer;
static uvBuffer: WebGLBuffer;
static indexBuffer: WebGLBuffer;
static rectProgram: WebGLProgram;
static rectProgramInfo: ShaderProgramInfo;
static circleProgram: WebGLProgram;
static circleProgramInfo: ShaderProgramInfo;
static triangleProgram: WebGLProgram;
static triangleProgramInfo: ShaderProgramInfo;
static metaballProgram: WebGLProgram;
static metaballProgramInfo: ShaderProgramInfo;
/**
* Sets up the renderer to be used for rendering.
*
* Creates the webgl2 rendering context for the canvas and clears the webgl buffer.
*
* @throws When browser does not support webgl 2.0
*
* @param canvas The canvas to grab webgl context from
* @param opts {@link WebGLContextAttributes} to pass to the `getContext` call
*/
static init(canvas: HTMLCanvasElement, opts?: WebGLContextAttributes): void;
private static initShaders;
/**
* Resizes the `width` and `height` of the canvas attached to `gl` to the canvas' `clientWidth` and `clientHeight` multiplied by the `resolutionScale` or 1.
*/
static resizeToCanvas(): void;
static clear(clearColor: Color): void;
/**
* Renders all items currently in the 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;
/**
* Adds a shape to the render queue.
*
* @param shape The shape to queue
* @param position The x and y position to render the rectangle at
* @param rotation The rotation to apply to the shape
* @param zIndex The z position of the rendered rectangle
* @param scale The world to clip space scale value
*/
static queueShape(shape: Shape, position?: vec2, rotation?: number, zIndex?: number): void;
/**
* Renders a shape from a {@link RenderQueueItem} object.
*
* @param item The {@link RenderQueueItem} to render
* @param zIndex The z index to render the shape at
*/
private static renderQueueItem;
/**
* Gets the renderer's webgl context.
*
* @returns The renderer's webgl context
*/
static getGL(): WebGL2RenderingContext;
/**
* Sets the resolution scale to use when rendering.
*
* The width and height of the renderer canvas are set to `clientWidth * resolutionScale` and `clientHeight * resolutionScale` respectively.
*
* @param resolutionScale The new resolution scale to use
*/
static setResolutionScale(resolutionScale: number): void;
/**
* Gets the renderer's current resolution scale.
*
* @returns The renderer's current resolution scale
*/
static getResolutionScale(): number;
/**
* Sets the mode the renderer will use for drawing.
*
* @throws When the provided mode is not TRIANGLES, LINES or POINTS
*
* @param mode The mode to use
*/
static setMode(mode: "TRIANGLES" | "LINES" | "POINTS"): void;
/**
* Gets the current webgl rendering mode being used by the renderer.
*
* @returns The rendering mode
*/
static getMode(): "TRIANGLES" | "LINES" | "POINTS";
/**
* Sets the camera to use for rendering.
*
* @param camera The camera to use for rendering
*/
static useCamera(camera: Camera): void;
/**
* Gets the camera that is currently being used for rendering.
*
* @returns The camera that is currently being used for rendering
*/
static getCamera(): Camera;
/**
* Set the scale that is applied to vertices to obtain the vertices in clip space.
*
* @param scale The scaling vector
*/
static setScale(scale: vec2): void;
/**
* Gets the scale that is applied to vertices to obtain the vertices in clip space.
*
* @returns The scaling vector
*/
static getScale(): vec2;
/**
* Gets the render queue.
*
* @returns the render queue
*/
static getQueue(): ZMap<RenderQueueItem[]>;
/**
* 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 {};