Geneartion of cycled background texture with TDrawableImage

I understand you wanted to ask “possibility to do it with DrawFrom of TDrawableImage” ? I’ll answer assuming that “yes”.

The TDrawableImage.DrawFrom allows to specify any subset of the source image using the SourceRect,

procedure TDrawableImage.DrawFrom(const SourceImage: TDrawableImage;
  const DestinationRect, SourceRect: TFloatRectangle);

The SourceRect specifies which portion of the source to take – any origin, any size.

For example, this would render 4x the part of TestImage that starts at (100,100) and has size (150,150):

  function CreateDrawableImage: TDrawableImage;
  var
    TestImage: TDrawableImage;
    I: Integer;
  begin
    Result := TDrawableImage.Create(1024, 1024, TRGBImage, true);
    Result.RenderToImageBegin;
    RenderContext.Clear([cbColor], Yellow);
    Result.RenderToImageEnd;
    TestImage := TDrawableImage.Create('castle-data:/test_texture.png');
    try
      for I := 0 to 3 do
        Result.DrawFrom(TestImage,
          // destination rectangle (origin and size in target, i.e. in Result)
          FloatRectangle(100, 100 + I * 200, 200, 200),
          // source rectangle (origin and size in source, i.e. in TestImage)
          FloatRectangle(100, 100, 150, 150));
    finally FreeAndNil(TestImage); end;
  end;

You can test above by using it in image_generate_and_use example in gameviewmain.pas instead of the current CreateDrawableImage.

BTW: I just noticed that the feature TCastleImageTransform.LoadFromImage was temporarily broken (I broke it when implementing another TCastleImageTransform feature requested in this thread, https://forum.castle-engine.io/t/solved-bug-with-shadows-on-tcastleimagetransform/ ). Now it’s all fixed, naturally, with both features working :slight_smile: If you want to upgrade CGE, be sure to either take sources from GitHub or wait until commit titled “Fix TCastleImageTransform.LoadFromImage…” is no longer visible on this page, it means that it is part of downloads.

1 Like