Skip to content

Using Sprites to store transformations

Ryan G edited this page Sep 18, 2017 · 1 revision

As we saw from our last step, writing out the different transformations of an image can get pretty clunky pretty fast. The solution is to use Sprites, which combine a Texture and some transformations.

import dgt;

void main()
{
    Window window = Window("Your First Window", 800, 600, WindowConfig());
    Texture ball = Texture("ball.png");
    Sprite sprite = Sprite(ball);
    sprite.x = 400;
    sprite.y = 300;
    sprite.originX = ball.size.width / 2;
    sprite.originY = ball.size.height / 2;
    while(window.isOpen)
    {
        window.begin(Color.white);
        window.draw(sprite);
        sprite.rotation++;
        window.end();
    }
}

The new window should be identical to the last, with a spinning ball in the middle.

New Lines

Sprite sprite = Sprite(ball); constructs a sprite using the ball texture.

Sprites have properties for each transform, so you can set the transformations without having to specify each one.

Clone this wiki locally