loadImage method

Images loadImage(
  1. String path, {
  2. int frameWidth = 0,
  3. int frameHeight = 0,
  4. int paddingX = 0,
  5. int paddingY = 0,
  6. int textureFlags = TextureFlags.defaultFlags,
  7. double pivotX = 0.5,
  8. double pivotY = 0.5,
  9. void onLoad()?,
})

Loads an image or a sequence of image frames (Images) from the given path.

If frameWidth and frameHeight are specified (and greater than 0), the image is treated as a spritesheet, and multiple Image frames are extracted. Otherwise, a single Image representing the entire texture is created.

  • path: The path to the image file.
  • frameWidth: The width of each frame in the spritesheet. If 0, the entire texture width is used.
  • frameHeight: The height of each frame in the spritesheet. If 0, the entire texture height is used.
  • paddingX: Horizontal padding between frames in the spritesheet.
  • paddingY: Vertical padding between frames in the spritesheet.
  • textureFlags: Optional flags for the Texture.
  • pivotX: The horizontal pivot point (0.0 to 1.0) for the image(s).
  • pivotY: The vertical pivot point (0.0 to 1.0) for the image(s).
  • onLoad: An optional callback function that is executed when the image (and its frames) has finished loading and processing.

Returns an Images list.

Implementation

Images loadImage(
  String path, {
  int frameWidth = 0,
  int frameHeight = 0,
  int paddingX = 0,
  int paddingY = 0,
  int textureFlags = TextureFlags.defaultFlags,
  double pivotX = 0.5,
  double pivotY = 0.5,
  void Function()? onLoad,
}) {
  Images result = [];

  var texture = loadTexture(path, textureFlags);

  if (frameWidth == 0 && frameHeight == 0) {
    var image = Image(
      texture: texture,
      sourceRect: Rect(0, 0, texture.width, texture.height),
      pivotX: pivotX,
      pivotY: pivotY,
    );
    texture.dispose();

    texture.onLoad((Texture texture) {
      image.sourceRect.set(0, 0, texture.width, texture.height);
      onLoad?.call();
    });

    result.add(image);
  } else {
    texture.onLoad((Texture texture) {
      result.addAll(
        Image.loadFrames(
          texture: texture,
          frameWidth: frameWidth,
          frameHeight: frameHeight,
          paddingX: paddingX,
          paddingY: paddingY,
          pivotX: pivotX,
          pivotY: pivotY,
        ),
      );
      onLoad?.call();
    });
  }
  return result;
}