Open Source Cross-Platform Game Programming

Graphics2D

Library for drawing 2D graphics to the screen in a game environment, used in conjunction with the Game library.

class Draw[link]

Static class containing utility functions to draw various shape primitives to the screen.

function ellipse(left, top, width, height, red, green, blue, alpha)[link]

Draws a solid color ellipse to the screen at the given coordinates with the the given color and alpha.

Arguments

NameTypeOptionalDescription
left integer

x coordinate of the left side of the bounding box that the ellipse will fill.

top integer

y coordinate of the top side of the bounding box that the ellipse will fill.

width integer

width of the bounding box that the ellipse will fill.

height integer

height of the bounding box that the ellipse will fill.

red integer

red color value of the ellipse (0-255).

green integer

green color value of the ellipse (0-255).

blue integer

blue color value of the ellipse (0-255).

alpha integer Optional

optional alpha value of the ellipse (0-255). Default is 255.

function fill(red, green, blue)[link]

Clears all drawn objects and fills the screen with a solid opaque color.

Arguments

NameTypeDescription
red integer

red color value of the fill (0-255).

green integer

green color value of the fill (0-255).

blue integer

blue color value of the fill (0-255).

function line(startX, startY, endX, endY, strokeWidth, red, green, blue, alpha)[link]

Draws a line to the screen between the two given coordinates, with the given color, alpha, and thickness.

Arguments

NameTypeOptionalDescription
startX integer

x coordinate of the start of the line

startY integer

y coordinate of the start of the line

endX integer

x coordinate of the end of the line

endY integer

y coordinate of the end of the line

strokeWidth integer

thickness of the line

red integer

red color value of the line (0-255).

green integer

green color value of the line (0-255).

blue integer

blue color value of the line (0-255).

alpha integer Optional

optional alpha value of the line (0-255). Default is 255.

function quad(ax, ay, bx, by, cx, cy, dx, dy, red, green, blue, alpha)[link]

Draws a solid color quadrilateral to the screen at the given coordinates with the the given color and alpha. Quadrilateral edges must not cross each other, otherwise behavior is undefined and may vary depending on platform.

Arguments

NameTypeOptionalDescription
ax integer

x coordinate of the first quadrilateral point.

ay integer

y coordinate of the first quadrilateral point.

bx integer

x coordinate of the second quadrilateral point.

by integer

y coordinate of the second quadrilateral point.

cx integer

x coordinate of the third quadrilateral point.

cy integer

y coordinate of the third quadrilateral point.

dx integer

x coordinate of the fourth quadrilateral point.

dy integer

y coordinate of the fourth quadrilateral point.

red integer

red color value of the quadrilateral (0-255).

green integer

green color value of the quadrilateral (0-255).

blue integer

blue color value of the quadrilateral (0-255).

alpha integer Optional

optional alpha value of the quadrilateral (0-255). Default is 255.

function rectangle(left, top, width, height, red, green, blue, alpha)[link]

Draws a solid color rectangle to the screen at the given coordinates with the the given color and alpha.

Arguments

NameTypeOptionalDescription
left integer

x coordinate of the left side of the bounding box that the rectangle will fill.

top integer

y coordinate of the top side of the bounding box that the rectangle will fill.

width integer

width of the bounding box that the rectangle will fill.

height integer

height of the bounding box that the rectangle will fill.

red integer

red color value of the retangle (0-255).

green integer

green color value of the rectangle (0-255).

blue integer

blue color value of the rectangle (0-255).

alpha integer Optional

optional alpha value of the rectangle (0-255). Default is 255.

function triangle(ax, ay, bx, by, cx, cy, red, green, blue, alpha)[link]

Draws a solid color triangle to the screen at the given coordinates with the the given color and alpha.

Arguments

NameTypeOptionalDescription
ax integer

x coordinate of the first triangle point.

ay integer

y coordinate of the first triangle point.

bx integer

x coordinate of the second triangle point.

by integer

y coordinate of the second triangle point.

cx integer

x coordinate of the third triangle point.

cy integer

y coordinate of the third triangle point.

red integer

red color value of the triangle (0-255).

green integer

green color value of the triangle (0-255).

blue integer

blue color value of the triangle (0-255).

alpha integer Optional

optional alpha value of the triangle (0-255). Default is 255.

class GraphicsTexture[link]

Represents an image that can be drawn to the screen.

Fields

NameTypeDescription
.width integer

Width of the image in pixels.

.height integer

Height of the image in pixels.

function draw(x, y)[link]

Draws the texture to the screen at the given coordinates. The image is positioned such that the top left corner is located at the (x, y) coordinates provided.

Arguments

NameTypeDescription
x integer

x coordinate on the screen to draw to

y integer

y coordinate on the screen to draw to

function drawRegion(x, y, sourceX, sourceY, sourceWidth, sourceHeight)[link]

Draws a portion of an image to the screen. The first x and y coordinates represent where on the screen to draw the image. The next 4 coordinates define a rectangular region on this texture to draw from.

Arguments

NameTypeDescription
x integer

x coordinate on the screen to draw to

y integer

y coordinate on the screen to draw to

sourceX integer

x coordinate on the texture of the left side of the region to be drawn

sourceY integer

y coordinate on the texture of the top side of the region to be drawn

sourceWidth integer

width of the region on the texture to be drawn

sourceHeight integer

height of the region on the texture to be drawn

function drawRegionStretched(targetX, targetY, targetWidth, targetHeight, sourceX, sourceY, sourceWidth, sourceHeight)[link]

Draws a portion of an image to screen, stretching or shrinking it to fill a given region. The first 4 coordinates define a region on the screen to draw to. The last 4 coordinates define a region on the texture to sample the pixels from.

Arguments

NameTypeDescription
targetX integer

x coordinate on the screen to draw to

targetY integer

y coordinate on the screen to draw to

targetWidth integer

width on the screen that the drawn region should stretch or shrink to

targetHeight integer

height on the screen that the drawn region should stretch or shrink to

sourceX integer

x coordinate on the texture of the left side of the region to be drawn

sourceY integer

y coordinate on the texture of the top side of the region to be drawn

sourceWidth integer

width of the region on the texture to be drawn

sourceHeight integer

height of the region on the texture to be drawn

function drawRegionStretchedWithAlpha(targetX, targetY, targetWidth, targetHeight, sourceX, sourceY, sourceWidth, sourceHeight, alpha)[link]

Draws a portion of an image to screen, stretching or shrinking it to fill a given region and applies the given alpha. The first 4 coordinates define a region on the screen to draw to. The last 4 coordinates define a region on the texture to sample the pixels from. If the image contains per-pixel alpha values, those are compounded with the overall alpha.

Arguments

NameTypeDescription
targetX integer

x coordinate on the screen to draw to

targetY integer

y coordinate on the screen to draw to

targetWidth integer

width on the screen that the drawn region should stretch or shrink to

targetHeight integer

height on the screen that the drawn region should stretch or shrink to

sourceX integer

x coordinate on the texture of the left side of the region to be drawn

sourceY integer

y coordinate on the texture of the top side of the region to be drawn

sourceWidth integer

width of the region on the texture to be drawn

sourceHeight integer

height of the region on the texture to be drawn

alpha integer

alpha value applied to the blitted result (0 to 255)

function drawRotated(targetX, targetY, theta)[link]

Draws an image to the screen and rotates it the given amount. Increasing rotation values will rotate the image counter-clockwise. The image is centered over the given x and y coordinates and rotates from the center.

Arguments

NameTypeDescription
targetX integer

x coordinate on the screen to draw to

targetY integer

y coordinate on the screen to draw to

theta float

angle to rotate the image (radians)

function drawRotatedWithAlpha(targetX, targetY, theta, alpha)[link]

Draws an image to the screen and rotates it the given amount and applies the given alpha. Increasing rotation values will rotate the image counter-clockwise. The image is centered over the given x and y coordinates and rotates from the center. If the image contains per-pixel alpha values, those are compounded with the overall alpha.

Arguments

NameTypeDescription
targetX integer

x coordinate on the screen to draw to

targetY integer

y coordinate on the screen to draw to

theta float

angle to rotate the image (radians)

alpha integer

alpha value applied to the blitted result (0 to 255)

function drawStretched(targetX, targetY, targetWidth, targetHeight)[link]

Draws the entire texture to the given portion of the screen, stretching or shrinking it to fill that given region.

Arguments

NameTypeDescription
targetX integer

x coordinate on the screen to draw to

targetY integer

y coordinate on the screen to draw to

targetWidth integer

width on the screen that the drawn region should stretch or shrink to

targetHeight integer

height on the screen that the drawn region should stretch or shrink to

function drawWithAlpha(x, y, alpha)[link]

Draws the texture to the screen at the given coordinates and alpha value. The image is positioned such that the top left corner is located at the (x, y) coordinates provided. If the image contains per-pixel alpha values, those are compounded with the overall alpha.

Arguments

NameTypeDescription
x integer

x coordinate on the screen to draw to

y integer

y coordinate on the screen to draw to

alpha integer

alpha value applied to the blitted result (0 to 255)

function drawWithOptions(targetX, targetY, targetWidth, targetHeight, sourceX, sourceY, sourceWidth, sourceHeight, theta, alpha)[link]

Draws a portion of an image to screen, stretching or shrinking it to fill a given region and applies the given alpha. The first 4 coordinates define a region on the screen to draw to. The last 4 coordinates define a region on the texture to sample the pixels from. Increasing rotation values will rotate the image counter-clockwise. The rotation point is the center of the rectangle defined by the region denoted by the first 4 arguments. If the image contains per-pixel alpha values, those are compounded with the overall alpha.

Arguments

NameTypeDescription
targetX integer

x coordinate on the screen to draw to

targetY integer

y coordinate on the screen to draw to

targetWidth integer

width on the screen that the drawn region should stretch or shrink to

targetHeight integer

height on the screen that the drawn region should stretch or shrink to

sourceX integer

x coordinate on the texture of the left side of the region to be drawn

sourceY integer

y coordinate on the texture of the top side of the region to be drawn

sourceWidth integer

width of the region on the texture to be drawn

sourceHeight integer

height of the region on the texture to be drawn

theta float

angle to rotate the image (radians)

alpha integer

alpha value applied to the blitted result (0 to 255)

function flip(flipHorizontal, flipVertical)[link]

Generates a new GraphicsTexture object that has been flipped. Passing in false for both arguments will return the original GraphicsTexture instance.

Arguments

NameTypeDescription
flipHorizontal boolean

true to flip the image horizontally

flipVertical boolean

true to flip the image vertically

Return Value

TypeDescription
Graphics2D.GraphicsTexture

A flipped texture.

function load(imageResource)[link]

Loads a raw image resource into the graphics engine so that it can be drawn to the screen. What specifically happens under the hood depends on the platform implementation. For some platforms the raw image resource and the graphics texture will wrap identical native platform objects (such as JavaScript, which uses a regular HTML Canvas as its underlying implementation for both an ImageResource and GraphicsTexture). Other platforms make a distinction. For example, OpenGL-based platforms will use bitmap objects for the ImageResource, but the GraphicsTexture will be a handle to a GPU-loaded texture. These differences are abstracted away by the GraphicsTexture.

Arguments

NameTypeDescription
imageResource ImageResources.ImageResource

An image resource to convert to a texture.

Return Value

TypeDescription
Graphics2D.GraphicsTexture

A texture object

function scale(newWidth, newHeight)[link]

Generates a new GraphicsTexture object that has been scaled to a new size. Passing in the current width and height of the texture will return the original GraphicsTexture instance.

Arguments

NameTypeDescription
newWidth integer

new width to stretch or shrink the image to

newHeight integer

new height to stretch or shrink the image to

Return Value

TypeDescription
Graphics2D.GraphicsTexture

A resized texture.