D2Transform, D2TransformAdd and D2TransformOffset command

Requires: Direct2D.lg32

Purpose

Applies the specified transformation to the render target.

Syntax

D2Transform mode [, p0 [, p1 [, p2 [, p3]]]
D2TransformAdd mode [, p0 [, p1 [, p2 [, p3]]]
D2TransformOffset xOff!, yOff! [,fAdd?]

mode : integer expression
p0, p1, p2, p3 : float expression
xOff!, yOff!: single expression
fAdd?: bool expression

Description

D2Transform applies the specified (linear) transform to the render target, replacing the existing transformation. All subsequent drawing operations occur in the transformed space. A linear transformation can offset, scale, rotate, or skew a render target.

The mode argument specifies the type of transformation; rotate, scale, skew, and move (translate) a render target, it can be one of the following constants:

D2TF_OFF (0)Disables transformation, returns the transformation to the identity matrix. (When an identity transform is applied to an object, it does not change the position, shape, or size of the object.) Using D2Transform without any argument resets the transformation as well.
D2TF_ROTATE (1)Rotate transformation, p0 must specify the angle in degrees of the rotation and p1,p2 the center of the rotation.
D2TF_SKEW (2)Skew transformation, p0 must specify the skew angle in x-direction in degrees, p1 specifies the skew angle in y-direction, and (p2,p3) define the skew's center point.
D2TF_SCALE (3)Scale (multiplication) transformation, p0 defines scaling in the x-direction, p1 defines the scaling in the y-direction, and (p2,p3) define the center of the scaling.
D2TF_MOVE (4)Offset transformation (move or translate), p0 specifies the offset in x-direction and p1 the offset in y-direction.
D2TF_INVERT (5)Invert transformation, this inverts the current active transformation.

The mode% argument can also specify the address of a D2D1_MATRIX_3X2_F structure. In this case the matrix is passed to the ID2D1RenderTarget::SetTransform method directly.

The different transformations may be combined by adding the value 8 to the mode argument. When the corresponding bit is set the new transformation is 'added' to the current transformation.

D2TransformAdd adds the specified transformation to the current transformation of the render target. D2TransformAdd is wrapper for:

D2Transform mode | D2TF_Add, ... .

D2TransformOffset sets the origin to xOff, yOff. The previous transformation setting is destroyed, to apply the offset to the current transformation set fAdd? To True. D2TransformOffset is a wrapper for:

D2Transform D2TF_MOVE + (fAdd ? D2TF_ADD : 0), xOff!, yOff!

Before applying a new transformation the current setting can be saved to a stack using D2TransformSave. The saved transformation can be restored very quickly using D2TransformRestore.

Example

'

' D2Transform sample (dpi-unaware)

'

$Library "direct2d"

Global Object Win1RT

OpenW 1, 0, 0, 320, 300, ~15

Set Win1RT = D2GetRT()

Do

Sleep

Until Me Is Nothing

 

Sub Win_1_Paint

D2BeginDraw Win1RT, D2C_White

' Show a rotation

D2DefLine 1

D2ForeColor = D2C_Brown

D2Text 40, 10, "Rotation"

D2Box 40, 40, 100, 100      ' original rectangle

D2Transform D2TF_ROTATE, 45, 70, 70   ' rotate 45 degrees around 70,70

D2DefLine 0                 ' straight line

D2Box 40, 40, 100, 100      ' original rectangle, rotated

' Add an offset to the rotation (both box and text)

D2TransformAdd D2TF_MOVE, 90, 0    ' Add offset (translation)

D2Text 40, 10, "Rotated"#10"Translated"

D2Box 40, 40, 100, 100      ' original rectangle

' Skew box

D2Transform D2TF_OFF               ' disable transformation

D2Text 40, 120, "Skewed"

D2DefLine 1

D2Box 40, 140, 100, 200     ' original rectangle

D2Transform D2TF_SKEW , 30, 0, 40, 140 ' skew around (40,140)

' Multiply shape by 1.2 (x-axis) and 1.2 (y-axis)

D2TransformAdd D2TF_SCALE, 1.2, 1.2, 40, 140  ' add to current transform

D2DefLine 0

D2Box 40, 140, 100, 200     ' original rectangle, skewed scaled

' Invert the current transform (the skewed, scaled transform)

D2Transform D2TF_INVERT

D2ForeColor = D2C_BlueViolet

D2Box 40, 140, 100, 200     ' original rectangle, inverted skew

D2EndDraw

EndSub

 

Sub Win_1_ReSize

D2ResizeRT Win1RT, _X, _Y

NOTE: The above example will not run correctly as it relies on an embedded file which is not present in the syntax. The programme with full resources can be found in GFABASIC/Samples/Direct2D/D2Transform.G32.

Remarks

D2Transform calls the ID2D1RenderTarget::SetTransform method to apply the specified transform to the render target. All subsequent drawing operations occur in the transformed space. For more information, see here.

D2Transform must be placed between the D2BeginDraw and D2EndDraw commands. The transformation is reset by D2EndDraw after applying the drawing commands.

Some drawing commands like D2DrawGeometry and D2FillGeometry lack the arguments to position the geometry figure. Before rendering a geometry to a specific position use D2TransformOffset to set the output location.

See Also

D2Clip, D2EndDraw, D2DrawGeometry, D2FillGeometry, D2TransformSave, D2TransformRestore.

{Created by Sjouke Hamstra; Last updated: 19/03/2021 by James Gaite}