Classes:Canvas

From ZDoom Wiki
Jump to navigation Jump to search

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

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.

Setting up a Map Texture

In TEXTURES:

texture MyTexture, 200, 200 {}

In ANIMDEFS:

CANVASTEXTURE MyTexture 200 200

Methods

  • void Clear(int left, int top, int right, int bottom, Color color, int palcolor = -1)
  • void Dim(Color col, double amount, int x, int y, int w, int h, ERenderStyle style = STYLE_Translucent)
  • vararg void DrawTexture(TextureID tex, bool animate, double x, double y, ...)
  • vararg void DrawShape(TextureID tex, bool animate, Shape2D s, ...)
  • vararg void DrawShapeFill(Color col, double amount, Shape2D s, ...)
  • vararg void DrawChar(Font font, int normalcolor, double x, double y, int character, ...)
  • vararg void DrawText(Font font, int normalcolor, double x, double y, String text, ...)
  • void DrawLine(double x0, double y0, double x1, double y1, Color color, int alpha = 255)
  • void DrawLineFrame(Color color, int x0, int y0, int w, int h, int thickness = 1)
  • void DrawThickLine(double x0, double y0, double x1, double y1, double thickness, Color color, int alpha = 255)
  • Vector2, Vector2 VirtualToRealCoords(Vector2 pos, Vector2 size, Vector2 vsize, bool vbottom=false, bool handleaspect=true)
  • void SetClipRect(int x, int y, int w, int h)
  • void ClearClipRect()
  • int, int, int, int GetClipRect()
  • double, double, double, double GetFullscreenRect(double vwidth, double vheight, int fsmode)
  • Vector2 SetOffset(double x, double y)
  • void ClearScreen(color col = 0)
  • void SetScreenFade(double factor)
  • void EnableStencil(bool on)
  • void SetStencil(int offs, int op, int flags = -1)
  • void ClearStencil()
  • void SetTransform(Shape2DTransform transform)
  • void ClearTransform()

See also