drawTexture method

void drawTexture(
  1. Texture tex, {
  2. double? srcX,
  3. double? srcY,
  4. double? srcWidth,
  5. double? srcHeight,
  6. double? x,
  7. double? y,
  8. double? width,
  9. double? height,
  10. ColorList? colors,
})

Draws a Texture or a portion of it onto a quad.

  • tex: The Texture to draw.
  • srcX, srcY: Top-left x,y coordinates of the source rectangle within the texture. Defaults to (0,0).
  • srcWidth, srcHeight: Width and height of the source rectangle. Defaults to texture dimensions.
  • x, y: Top-left x,y coordinates of the destination rectangle on the canvas. Defaults to (0,0).
  • width, height: Width and height of the destination rectangle. Defaults to source dimensions.
  • colors: An optional list of Color objects for per-vertex coloring (tinting). Colors are applied in order: top-left, bottom-left, top-right, bottom-right of the quad.

Implementation

void drawTexture(
  Texture tex, {
  double? srcX,
  double? srcY,
  double? srcWidth,
  double? srcHeight,
  double? x,
  double? y,
  double? width,
  double? height,
  ColorList? colors,
}) {
  _prepareRenderState(_PrimitiveType.quads, texture: tex);

  final texW = tex.width.toDouble();
  final texH = tex.height.toDouble();

  srcX ??= 0;
  srcY ??= 0;
  srcWidth ??= texW;
  srcHeight ??= texH;
  x ??= 0;
  y ??= 0;
  width ??= texW;
  height ??= texH;

  final u1 = srcX / texW;
  final v1 = srcY / texH;
  final u2 = (srcX + srcWidth) / texW;
  final v2 = (srcY + srcHeight) / texH;

  final v0x = x, v0y = y;
  final v1x = x, v1y = y + height;
  final v2x = x + width, v2y = y;
  final v3x = x + width, v3y = y + height;

  _transfromAndAddVertices(v0x, v0y, u1, v1, getColorFromList(colors, 0, _encodedColor));
  _transfromAndAddVertices(v1x, v1y, u1, v2, getColorFromList(colors, 1, _encodedColor));
  _transfromAndAddVertices(v2x, v2y, u2, v1, getColorFromList(colors, 2, _encodedColor));
  _transfromAndAddVertices(v3x, v3y, u2, v2, getColorFromList(colors, 3, _encodedColor));
}