Classes:Canvas

Canvases are objects that can be used to draw directly on a texture. Each texture can have one canvas and any modifications to a texture's canvas are shared among anything that uses that texture. This is because textures aren't stored individually per entity or surface but are instead indexed and then shared globally. This is necessary in order to save on memory and increase efficiency.

In order to create a canvas texture, there must be a CANVASTEXTURE definition for it in ANIMDEFS. To get a canvas, TexMan's GetCanvas() function has to be used. If that texture doesn't have one it'll return null. From here, canvases have all the same methods as Screen and function the same way. The only difference is that the the Draw functions can be called at any time as opposed to Screen which can only do so when rendering is active. It should be noted that the resolution of the canvas is the size of its texture. Canvases won't clear what was drawn to them between frames so they don't need to be repeatedly drawn to like the Screen.

Example

Modifying a Canvas

In ANIMDEFS:

CANVASTEXTURE MyTexture 200 200

In ZScript:

TextureID tid = TexMan.CheckForTexture("MyTexture");

// Get the canvas
Canvas cv = TexMan.GetCanvas("MyTexture");

// Get the resolution
int w, h;
[w, h] = TexMan.GetSize(tid);

// Get the center taking string size into account
string hello = "Hello World";
int x = (w - bigFont.StringWidth(hello)) / 2;
int y = (h - bigFont.GetHeight()) / 2;

// Draw some text in the middle of it
cv.DrawText(bigFont, Font.CR_RED, x, y, hello);

// Hello World will now show up on anything that uses MyTexture

Setting up a Map Texture

In TEXTURES:

texture MyTexture, 200, 200 {}

In ANIMDEFS:

CANVASTEXTURE MyTexture 200 200

The canvas can now be used in levels for things like placing it on walls. Any drawing done to it will automatically update it everywhere.

This article is issued from Zdoom. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.