Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
sprites.qdoc
Go to the documentation of this file.
1
// Copyright (C) 2017 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4
/*!
5
\page qtquick-effects-sprites.html
6
\title Sprite Animations
7
\brief Sprite-based animations with flexible transitioning
8
9
\section1 Sprite Engine
10
11
The \l {Qt Quick} sprite engine is a stochastic state machine combined with the ability
12
to chop up images containing multiple frames of an animation.
13
14
\section2 State Machine
15
16
A primary function of the sprite engine is its internal state machine. This is not the same as
17
the states and transitions in Qt Quick, and is more like a conventional state machine. Sprites
18
can have weighted transitions to other sprites, or back to themselves. When a sprite animation
19
finishes, the sprite engine will choose the next sprite randomly, based on the weighted transitions
20
available for the sprite that just finished.
21
22
You can affect the currently playing sprite in two ways. You can arbitrarily force it to immediately
23
start playing any sprite, or you can tell it to gradually transition to a given sprite. If you
24
instruct it to gradually transition, then it will reach the target sprite by going through valid
25
state transitions using the fewest number of intervening sprites (but ignoring relative weightings).
26
This allows you to easily insert a transitional animation between two different sprites.
27
28
\image spriteenginegraph.png
29
{State diagram showing sprite transitions between standing, waiting,
30
walking, and jumping states}
31
32
As an example, consider the above diagram which illustrates the sprites for a hypothetical 2D
33
platform game character. The character starts by displaying the \e standing state. From this state,
34
barring external input, he will transition to either the \e waiting animation, the \e walking animation,
35
or play the \e standing animation again. Because the weights for those transitions are one, zero and three
36
respectively, he has a one in four chance of playing the \e waiting animation when the \e standing animation
37
finishes, and a three in four chance of playing the \e standing animation again. This allows for a character
38
who has a slightly animated and variable behavior while waiting.
39
40
Because there is a zero weight transition to the \e walking animation, the \e standing animation will not normally
41
transition there. But if you set the goal animation to be the \e walking animation, it would play the \e walking
42
animation when it finished the \e standing animation. If it was previously in the \e waiting animation, it would
43
finish playing that, then play the \e standing animation, then play the \e walking animation. It would then continue to
44
play the \e walking animation until the goal animation is unset, at which point it would switch to the \e standing
45
animation after finishing the \e walking animation.
46
47
If you then set the goal state to the \e jumping animation, it would finish the \e walking animation before
48
playing the \e jumping animation. Because the \e jumping animation does not transition to other states, it will still
49
keep playing the \e jumping animation until the state is forced to change. In this example, you could set it back to
50
\e walking and change the goal animation to \e walking or to nothing (which would lead it to play the \e standing animation
51
after the \e walking animation). Note that by forcibly setting the animation, you can start playing the animation
52
immediately.
53
54
\section2 Input Format
55
56
The file formats accepted by the sprite engine are the same as the file formats accepted by other QML types,
57
such as \l Image. In order to animate the image, however, the sprite engine requires the image file to contain
58
all of the frames of the animation. They should be arranged in a contiguous line, which may wrap from the right
59
edge of the file to a lower row starting from the left edge of the file (and which is placed directly below the
60
previous row).
61
62
\image spritecutting.png
63
{Grid showing frame numbering order in a sprite sheet}
64
65
As an example, take the above image. For now, just consider the black numbers, and assume the squares are 40x40 pixels.
66
Normally, the image is read from the top-left corner. If you specified the frame size as 40x40 pixels, and a frame count
67
of 8, then it would read in the frames as they are numbered. The frame in the top left would be the first frame, the frame
68
in the top right would be the fifth frame, and then it would wrap to the next row (at pixel location 0,40 in the file) to read
69
the sixth frame. It would stop reading after the frame marked 8, and if there was any image data in the square below frame four
70
then it would not be included in the animation.
71
72
It is possible to load animations from an arbitrary offset, but they will still follow the same pattern.
73
Consider now the red numbers. If we specify that the animation begins at pixel location 120,0, with a
74
frame count of 5 and the same frame size as before, then it will load the frames as they are numbered in red.
75
The first 120x40 of the image will not be used, as it starts reading 40x40 blocks from the location of 120,0.
76
When it reaches the end of the file at 160,0, it then starts to read the next row from 0,40.
77
78
The blue numbers show the frame numbers if you tried to load two frames of that size, starting from 40,40. Note
79
that it is possible to load multiple sprites from one image file. The red, blue and black numbers can all
80
be loaded as separate animations to the same sprite engine. The following code loads the animations as per the image.
81
It also specifies that animations are to be played at 20 frames per second.
82
83
\code
84
Sprite {
85
name: "black"
86
source: "image.png"
87
frameCount: 8
88
frameWidth: 40
89
frameHeight: 40
90
frameRate: 20
91
}
92
Sprite {
93
name: "red"
94
source: "image.png"
95
frameX: 120
96
frameCount: 5
97
frameWidth: 40
98
frameHeight: 40
99
frameRate: 20
100
}
101
Sprite {
102
name: "blue"
103
source: "image.png"
104
frameX: 40
105
frameX: 40
106
frameCount: 2
107
frameWidth: 40
108
frameHeight: 40
109
frameRate: 20
110
}
111
\endcode
112
113
Frames within one animation must be the same size. However, multiple animations within the same file
114
do not. Sprites without a \l {Sprite::}{frameCount} specified assume that they take the entire file, and you must specify
115
the frame size. Sprites without a frame size assume that they are square and take the entire file without wrapping,
116
and you must specify a frame count.
117
118
The sprite engine internally copies and cuts up images to fit in an easier-to-read internal format, which leads
119
to some graphics memory limitations. Because it requires all the sprites for a single engine to be in the same
120
texture, attempting to load many different animations can run into texture memory limits on embedded devices. In
121
these situations, a warning will be output to the console containing the maximum texture size.
122
123
There are several tools to help turn a set of images into sprite sheets. Here are some examples:
124
\list
125
\li Photoshop plugin: \l http://www.johnwordsworth.com/projects/photoshop-sprite-sheet-generator-script
126
\li Gimp's SpriteSheet plugin
127
\li Cmd-line tool: \l http://www.imagemagick.org/script/montage.php
128
\endlist
129
130
\section2 QML Types Using the Sprite Engine
131
132
Sprites for the sprite engine can be defined using the \l Sprite type. This type includes the input parameters,
133
as well as the length of the animation and weighted transitions to other animations. It is purely a data class, and
134
does not render anything.
135
136
\l SpriteSequence is a type which uses a sprite engine to draw the sprites defined in it. It is a single and
137
self-contained sprite engine, and does not interact with other sprite engines. \l Sprite types can be shared between
138
sprite engine-using types, but this is not done automatically. So, if you have defined a sprite in one \l SpriteSequence
139
you will need to redefine it (or reference the same \l Sprite type) in the sprites property of another \l SpriteSequence
140
in order to transition to that animation.
141
142
Additionally, \l ImageParticle can use \l Sprite types to define sprites for each particle. This is again a single
143
sprite engine per type. This works similarly to \c SpriteSequence, but it also has the parameterized variability provided
144
by the \l ImageParticle type.
145
146
\section1 AnimatedSprite Type
147
148
For use-cases which do not need to transition between animations, consider the \l AnimatedSprite type.
149
This type displays sprite animations with the same input format, but only one at a time. It also provides more fine-grained
150
manual control, as there is no sprite engine managing the timing and transitions behind the scenes.
151
152
*/
qtdeclarative
src
quick
doc
src
concepts
effects
sprites.qdoc
Generated on
for Qt by
1.16.1