Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/qtdeclarative/src/qtdeclarative/src/quick/items/qquickspriteengine.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | /**************************************************************************** | - | ||||||||||||
2 | ** | - | ||||||||||||
3 | ** Copyright (C) 2016 The Qt Company Ltd. | - | ||||||||||||
4 | ** Contact: https://www.qt.io/licensing/ | - | ||||||||||||
5 | ** | - | ||||||||||||
6 | ** This file is part of the QtQuick module of the Qt Toolkit. | - | ||||||||||||
7 | ** | - | ||||||||||||
8 | ** $QT_BEGIN_LICENSE:LGPL$ | - | ||||||||||||
9 | ** Commercial License Usage | - | ||||||||||||
10 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||||||||
11 | ** accordance with the commercial license agreement provided with the | - | ||||||||||||
12 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||||||||
13 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||||||||
14 | ** and conditions see https://www.qt.io/terms-conditions. For further | - | ||||||||||||
15 | ** information use the contact form at https://www.qt.io/contact-us. | - | ||||||||||||
16 | ** | - | ||||||||||||
17 | ** GNU Lesser General Public License Usage | - | ||||||||||||
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||||||||
19 | ** General Public License version 3 as published by the Free Software | - | ||||||||||||
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the | - | ||||||||||||
21 | ** packaging of this file. Please review the following information to | - | ||||||||||||
22 | ** ensure the GNU Lesser General Public License version 3 requirements | - | ||||||||||||
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. | - | ||||||||||||
24 | ** | - | ||||||||||||
25 | ** GNU General Public License Usage | - | ||||||||||||
26 | ** Alternatively, this file may be used under the terms of the GNU | - | ||||||||||||
27 | ** General Public License version 2.0 or (at your option) the GNU General | - | ||||||||||||
28 | ** Public license version 3 or any later version approved by the KDE Free | - | ||||||||||||
29 | ** Qt Foundation. The licenses are as published by the Free Software | - | ||||||||||||
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 | - | ||||||||||||
31 | ** included in the packaging of this file. Please review the following | - | ||||||||||||
32 | ** information to ensure the GNU General Public License requirements will | - | ||||||||||||
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and | - | ||||||||||||
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. | - | ||||||||||||
35 | ** | - | ||||||||||||
36 | ** $QT_END_LICENSE$ | - | ||||||||||||
37 | ** | - | ||||||||||||
38 | ****************************************************************************/ | - | ||||||||||||
39 | - | |||||||||||||
40 | #include "qquickspriteengine_p.h" | - | ||||||||||||
41 | #include "qquicksprite_p.h" | - | ||||||||||||
42 | #include <qqmlinfo.h> | - | ||||||||||||
43 | #include <qqml.h> | - | ||||||||||||
44 | #include <QDebug> | - | ||||||||||||
45 | #include <QPainter> | - | ||||||||||||
46 | #include <QRandomGenerator> | - | ||||||||||||
47 | #include <QSet> | - | ||||||||||||
48 | #include <QtGui/qopengl.h> | - | ||||||||||||
49 | #include <QOpenGLFunctions> | - | ||||||||||||
50 | - | |||||||||||||
51 | QT_BEGIN_NAMESPACE | - | ||||||||||||
52 | - | |||||||||||||
53 | /* | - | ||||||||||||
54 | \internal Stochastic/Sprite engine implementation docs | - | ||||||||||||
55 | - | |||||||||||||
56 | Nomenclature: 'thing' refers to an instance of a running sprite or state. It could be renamed. | - | ||||||||||||
57 | States and Transitions are referred to in the state machine sense here, NOT in the QML sense. | - | ||||||||||||
58 | - | |||||||||||||
59 | The Stochastic State engine takes states with stochastic state transitions defined and transitions them. | - | ||||||||||||
60 | When a state is started, it's added to a list of pending updates sorted by their time they want to update. | - | ||||||||||||
61 | An external driver calls the update function with an elapsed time, which becomes the new time offset. | - | ||||||||||||
62 | The pending update stack is popped until all entries are past the current time, which simulates all intervening time. | - | ||||||||||||
63 | - | |||||||||||||
64 | The Sprite Engine subclass has two major differences. Firstly all states are sprites (and there's a new vector with them | - | ||||||||||||
65 | cast to sprite). Secondly, it chops up images and states to fit a texture friendly format. | - | ||||||||||||
66 | Before the Sprite Engine starts running, its user requests a texture assembled from all the sprite images. This | - | ||||||||||||
67 | texture is made by pasting the sprites into one image, with one sprite animation per row (in the future it is planned to have | - | ||||||||||||
68 | arbitrary X/Y start ends, but they will still be assembled and recorded here and still have to be contiguous lines). | - | ||||||||||||
69 | This cut-up allows the users to calcuate frame positions with a texture percentage width and elapsed time. | - | ||||||||||||
70 | It also means that large sprites cover multiple lines to fit inside the texture memory limit (which is a square). | - | ||||||||||||
71 | - | |||||||||||||
72 | Large sprites covering multiple lines breaks this simple interface for the users, so each line is treated as a pseudostate | - | ||||||||||||
73 | and it's mostly hidden from the spriteengine users (except that they'll get advanced signals where the state is the same | - | ||||||||||||
74 | but the visual parameters changed). These are not real states because that would get very complex with bindings. Instead, | - | ||||||||||||
75 | when sprite attributes are requested from a sprite that has multiple pseudostates, it returns the values for the psuedostate | - | ||||||||||||
76 | it is in. State advancement is intercepted and hollow for pseudostates, except the last one. The last one transitions as the | - | ||||||||||||
77 | state normally does. | - | ||||||||||||
78 | */ | - | ||||||||||||
79 | - | |||||||||||||
80 | static const int NINF = -1000000;//magic number for random start time - should be more negative than a single realistic animation duration | - | ||||||||||||
81 | //#define SPRITE_IMAGE_DEBUG | - | ||||||||||||
82 | #ifdef SPRITE_IMAGE_DEBUG | - | ||||||||||||
83 | #include <QFile> | - | ||||||||||||
84 | #include <QDir> | - | ||||||||||||
85 | #endif | - | ||||||||||||
86 | /* TODO: | - | ||||||||||||
87 | make sharable? | - | ||||||||||||
88 | solve the state data initialization/transfer issue so as to not need to make friends | - | ||||||||||||
89 | */ | - | ||||||||||||
90 | - | |||||||||||||
91 | QQuickStochasticEngine::QQuickStochasticEngine(QObject *parent) : | - | ||||||||||||
92 | QObject(parent), m_timeOffset(0), m_addAdvance(false) | - | ||||||||||||
93 | { | - | ||||||||||||
94 | //Default size 1 | - | ||||||||||||
95 | setCount(1); | - | ||||||||||||
96 | } executed 102 times by 6 tests: end of block Executed by:
| 102 | ||||||||||||
97 | - | |||||||||||||
98 | QQuickStochasticEngine::QQuickStochasticEngine(const QList<QQuickStochasticState *> &states, QObject *parent) : | - | ||||||||||||
99 | QObject(parent), m_states(states), m_timeOffset(0), m_addAdvance(false) | - | ||||||||||||
100 | { | - | ||||||||||||
101 | //Default size 1 | - | ||||||||||||
102 | setCount(1); | - | ||||||||||||
103 | } never executed: end of block | 0 | ||||||||||||
104 | - | |||||||||||||
105 | QQuickStochasticEngine::~QQuickStochasticEngine() | - | ||||||||||||
106 | { | - | ||||||||||||
107 | } | - | ||||||||||||
108 | - | |||||||||||||
109 | QQuickSpriteEngine::QQuickSpriteEngine(QObject *parent) | - | ||||||||||||
110 | : QQuickStochasticEngine(parent), m_startedImageAssembly(false), m_loaded(false) | - | ||||||||||||
111 | { | - | ||||||||||||
112 | } executed 96 times by 5 tests: end of block Executed by:
| 96 | ||||||||||||
113 | - | |||||||||||||
114 | QQuickSpriteEngine::QQuickSpriteEngine(const QList<QQuickSprite *> &sprites, QObject *parent) | - | ||||||||||||
115 | : QQuickSpriteEngine(parent) | - | ||||||||||||
116 | { | - | ||||||||||||
117 | for (QQuickSprite* sprite : sprites) | - | ||||||||||||
118 | m_states << (QQuickStochasticState*)sprite; executed 166 times by 5 tests: m_states << (QQuickStochasticState*)sprite; Executed by:
| 166 | ||||||||||||
119 | } executed 96 times by 5 tests: end of block Executed by:
| 96 | ||||||||||||
120 | - | |||||||||||||
121 | QQuickSpriteEngine::~QQuickSpriteEngine() | - | ||||||||||||
122 | { | - | ||||||||||||
123 | } | - | ||||||||||||
124 | - | |||||||||||||
125 | - | |||||||||||||
126 | int QQuickSpriteEngine::maxFrames() const | - | ||||||||||||
127 | { | - | ||||||||||||
128 | return m_maxFrames; executed 312 times by 1 test: return m_maxFrames; Executed by:
| 312 | ||||||||||||
129 | } | - | ||||||||||||
130 | - | |||||||||||||
131 | /* States too large to fit in one row are split into multiple rows | - | ||||||||||||
132 | This is more efficient for the implementation, but should remain an implementation detail (invisible from QML) | - | ||||||||||||
133 | Therefore the below functions abstract sprite from the viewpoint of classes that pass the details onto shaders | - | ||||||||||||
134 | But States maintain their listed index for internal structures | - | ||||||||||||
135 | TODO: All these calculations should be pre-calculated and cached during initialization for a significant performance boost | - | ||||||||||||
136 | TODO: Above idea needs to have the varying duration offset added to it | - | ||||||||||||
137 | */ | - | ||||||||||||
138 | //TODO: Should these be adding advanceTime as well? But only if advanceTime was added to your startTime... | - | ||||||||||||
139 | /* | - | ||||||||||||
140 | To get these working with duration=-1, m_startTimes will be messed with should duration=-1 | - | ||||||||||||
141 | m_startTimes will be set in advance/restart to 0->(m_framesPerRow-1) and can be used directly as extra. | - | ||||||||||||
142 | This makes it 'frame' instead, but is more memory efficient than two arrays and less hideous than a vector of unions. | - | ||||||||||||
143 | */ | - | ||||||||||||
144 | int QQuickSpriteEngine::pseudospriteProgress(int sprite, int state, int* rowDuration) const | - | ||||||||||||
145 | { | - | ||||||||||||
146 | int myRowDuration = m_duration[sprite] * m_sprites[state]->m_framesPerRow / m_sprites[state]->m_frames; | - | ||||||||||||
147 | if (rowDuration)
| 1168-2840 | ||||||||||||
148 | *rowDuration = myRowDuration; executed 1168 times by 1 test: *rowDuration = myRowDuration; Executed by:
| 1168 | ||||||||||||
149 | - | |||||||||||||
150 | if (m_sprites[state]->reverse()) //shift start-time back by the amount of time the first frame is smaller than rowDuration
| 0-4008 | ||||||||||||
151 | return (m_timeOffset - (m_startTimes[sprite] - (myRowDuration - (m_duration[sprite] % myRowDuration))) ) never executed: return (m_timeOffset - (m_startTimes[sprite] - (myRowDuration - (m_duration[sprite] % myRowDuration))) ) / myRowDuration; | 0 | ||||||||||||
152 | / myRowDuration; never executed: return (m_timeOffset - (m_startTimes[sprite] - (myRowDuration - (m_duration[sprite] % myRowDuration))) ) / myRowDuration; | 0 | ||||||||||||
153 | else | - | ||||||||||||
154 | return (m_timeOffset - m_startTimes[sprite]) / myRowDuration; executed 4008 times by 1 test: return (m_timeOffset - m_startTimes[sprite]) / myRowDuration; Executed by:
| 4008 | ||||||||||||
155 | } | - | ||||||||||||
156 | - | |||||||||||||
157 | int QQuickSpriteEngine::spriteState(int sprite) const | - | ||||||||||||
158 | { | - | ||||||||||||
159 | if (!m_loaded)
| 0-1200 | ||||||||||||
160 | return 0; never executed: return 0; | 0 | ||||||||||||
161 | int state = m_things[sprite]; | - | ||||||||||||
162 | if (!m_sprites[state]->m_generatedCount)
| 0-1200 | ||||||||||||
163 | return state; executed 1200 times by 1 test: return state; Executed by:
| 1200 | ||||||||||||
164 | - | |||||||||||||
165 | int extra; | - | ||||||||||||
166 | if (m_sprites[state]->frameSync())
| 0 | ||||||||||||
167 | extra = m_startTimes[sprite]; never executed: extra = m_startTimes[sprite]; | 0 | ||||||||||||
168 | else if (!m_duration[sprite])
| 0 | ||||||||||||
169 | return state; never executed: return state; | 0 | ||||||||||||
170 | else | - | ||||||||||||
171 | extra = pseudospriteProgress(sprite, state); never executed: extra = pseudospriteProgress(sprite, state); | 0 | ||||||||||||
172 | if (m_sprites[state]->reverse())
| 0 | ||||||||||||
173 | extra = (m_sprites[state]->m_generatedCount - 1) - extra; never executed: extra = (m_sprites[state]->m_generatedCount - 1) - extra; | 0 | ||||||||||||
174 | - | |||||||||||||
175 | return state + extra; never executed: return state + extra; | 0 | ||||||||||||
176 | } | - | ||||||||||||
177 | - | |||||||||||||
178 | int QQuickSpriteEngine::spriteStart(int sprite) const | - | ||||||||||||
179 | { | - | ||||||||||||
180 | if (!m_duration[sprite] || !m_loaded)
| 0-2893 | ||||||||||||
181 | return m_timeOffset; executed 326 times by 2 tests: return m_timeOffset; Executed by:
| 326 | ||||||||||||
182 | int state = m_things[sprite]; | - | ||||||||||||
183 | if (!m_sprites[state]->m_generatedCount)
| 580-2313 | ||||||||||||
184 | return m_startTimes[sprite]; executed 2313 times by 3 tests: return m_startTimes[sprite]; Executed by:
| 2313 | ||||||||||||
185 | int rowDuration; | - | ||||||||||||
186 | int extra = pseudospriteProgress(sprite, state, &rowDuration); | - | ||||||||||||
187 | if (m_sprites[state]->reverse())
| 0-580 | ||||||||||||
188 | return m_startTimes[sprite] + (extra ? (extra - 1)*rowDuration + (m_duration[sprite] % rowDuration) : 0); never executed: return m_startTimes[sprite] + (extra ? (extra - 1)*rowDuration + (m_duration[sprite] % rowDuration) : 0); | 0 | ||||||||||||
189 | return m_startTimes[sprite] + extra*rowDuration; executed 580 times by 1 test: return m_startTimes[sprite] + extra*rowDuration; Executed by:
| 580 | ||||||||||||
190 | } | - | ||||||||||||
191 | - | |||||||||||||
192 | int QQuickSpriteEngine::spriteFrames(int sprite) const | - | ||||||||||||
193 | { | - | ||||||||||||
194 | if (!m_loaded)
| 0-6604 | ||||||||||||
195 | return 1; never executed: return 1; | 0 | ||||||||||||
196 | int state = m_things[sprite]; | - | ||||||||||||
197 | if (!m_sprites[state]->m_generatedCount)
| 1626-4978 | ||||||||||||
198 | return m_sprites[state]->frames(); executed 4978 times by 4 tests: return m_sprites[state]->frames(); Executed by:
| 4978 | ||||||||||||
199 | - | |||||||||||||
200 | int extra; | - | ||||||||||||
201 | if (m_sprites[state]->frameSync())
| 490-1136 | ||||||||||||
202 | extra = m_startTimes[sprite]; executed 490 times by 2 tests: extra = m_startTimes[sprite]; Executed by:
| 490 | ||||||||||||
203 | else if (!m_duration[sprite])
| 0-1136 | ||||||||||||
204 | return m_sprites[state]->frames(); never executed: return m_sprites[state]->frames(); | 0 | ||||||||||||
205 | else | - | ||||||||||||
206 | extra = pseudospriteProgress(sprite, state); executed 1136 times by 1 test: extra = pseudospriteProgress(sprite, state); Executed by:
| 1136 | ||||||||||||
207 | if (m_sprites[state]->reverse())
| 0-1626 | ||||||||||||
208 | extra = (m_sprites[state]->m_generatedCount - 1) - extra; never executed: extra = (m_sprites[state]->m_generatedCount - 1) - extra; | 0 | ||||||||||||
209 | - | |||||||||||||
210 | - | |||||||||||||
211 | if (extra == m_sprites[state]->m_generatedCount - 1) {//last state
| 326-1300 | ||||||||||||
212 | const int framesRemaining = m_sprites[state]->frames() % m_sprites[state]->m_framesPerRow; | - | ||||||||||||
213 | if (framesRemaining > 0)
| 0-326 | ||||||||||||
214 | return framesRemaining; executed 326 times by 1 test: return framesRemaining; Executed by:
| 326 | ||||||||||||
215 | } never executed: end of block | 0 | ||||||||||||
216 | return m_sprites[state]->m_framesPerRow; executed 1300 times by 2 tests: return m_sprites[state]->m_framesPerRow; Executed by:
| 1300 | ||||||||||||
217 | } | - | ||||||||||||
218 | - | |||||||||||||
219 | int QQuickSpriteEngine::spriteDuration(int sprite) const //Full duration, not per frame | - | ||||||||||||
220 | { | - | ||||||||||||
221 | if (!m_duration[sprite] || !m_loaded)
| 12-9037 | ||||||||||||
222 | return m_duration[sprite]; executed 338 times by 2 tests: return m_duration[sprite]; Executed by:
| 338 | ||||||||||||
223 | int state = m_things[sprite]; | - | ||||||||||||
224 | if (!m_sprites[state]->m_generatedCount)
| 588-8437 | ||||||||||||
225 | return m_duration[sprite]; executed 8437 times by 4 tests: return m_duration[sprite]; Executed by:
| 8437 | ||||||||||||
226 | int rowDuration; | - | ||||||||||||
227 | int extra = pseudospriteProgress(sprite, state, &rowDuration); | - | ||||||||||||
228 | if (m_sprites[state]->reverse())
| 0-588 | ||||||||||||
229 | extra = (m_sprites[state]->m_generatedCount - 1) - extra; never executed: extra = (m_sprites[state]->m_generatedCount - 1) - extra; | 0 | ||||||||||||
230 | - | |||||||||||||
231 | if (extra == m_sprites[state]->m_generatedCount - 1) {//last state
| 121-467 | ||||||||||||
232 | const int durationRemaining = m_duration[sprite] % rowDuration; | - | ||||||||||||
233 | if (durationRemaining > 0)
| 0-121 | ||||||||||||
234 | return durationRemaining; executed 121 times by 1 test: return durationRemaining; Executed by:
| 121 | ||||||||||||
235 | } never executed: end of block | 0 | ||||||||||||
236 | return rowDuration; executed 467 times by 1 test: return rowDuration; Executed by:
| 467 | ||||||||||||
237 | } | - | ||||||||||||
238 | - | |||||||||||||
239 | int QQuickSpriteEngine::spriteY(int sprite) const | - | ||||||||||||
240 | { | - | ||||||||||||
241 | if (!m_loaded)
| 0-6352 | ||||||||||||
242 | return 0; never executed: return 0; | 0 | ||||||||||||
243 | int state = m_things[sprite]; | - | ||||||||||||
244 | if (!m_sprites[state]->m_generatedCount)
| 1398-4954 | ||||||||||||
245 | return m_sprites[state]->m_rowY; executed 4954 times by 4 tests: return m_sprites[state]->m_rowY; Executed by:
| 4954 | ||||||||||||
246 | - | |||||||||||||
247 | int extra; | - | ||||||||||||
248 | if (m_sprites[state]->frameSync())
| 262-1136 | ||||||||||||
249 | extra = m_startTimes[sprite]; executed 262 times by 2 tests: extra = m_startTimes[sprite]; Executed by:
| 262 | ||||||||||||
250 | else if (!m_duration[sprite])
| 0-1136 | ||||||||||||
251 | return m_sprites[state]->m_rowY; never executed: return m_sprites[state]->m_rowY; | 0 | ||||||||||||
252 | else | - | ||||||||||||
253 | extra = pseudospriteProgress(sprite, state); executed 1136 times by 1 test: extra = pseudospriteProgress(sprite, state); Executed by:
| 1136 | ||||||||||||
254 | if (m_sprites[state]->reverse())
| 0-1398 | ||||||||||||
255 | extra = (m_sprites[state]->m_generatedCount - 1) - extra; never executed: extra = (m_sprites[state]->m_generatedCount - 1) - extra; | 0 | ||||||||||||
256 | - | |||||||||||||
257 | - | |||||||||||||
258 | return m_sprites[state]->m_rowY + m_sprites[state]->m_frameHeight * extra; executed 1398 times by 2 tests: return m_sprites[state]->m_rowY + m_sprites[state]->m_frameHeight * extra; Executed by:
| 1398 | ||||||||||||
259 | } | - | ||||||||||||
260 | - | |||||||||||||
261 | int QQuickSpriteEngine::spriteX(int sprite) const | - | ||||||||||||
262 | { | - | ||||||||||||
263 | if (!m_loaded)
| 0-5729 | ||||||||||||
264 | return 0; never executed: return 0; | 0 | ||||||||||||
265 | int state = m_things[sprite]; | - | ||||||||||||
266 | if (!m_sprites[state]->m_generatedCount)
| 830-4899 | ||||||||||||
267 | return m_sprites[state]->m_rowStartX; executed 4899 times by 4 tests: return m_sprites[state]->m_rowStartX; Executed by:
| 4899 | ||||||||||||
268 | - | |||||||||||||
269 | int extra; | - | ||||||||||||
270 | if (m_sprites[state]->frameSync())
| 262-568 | ||||||||||||
271 | extra = m_startTimes[sprite]; executed 262 times by 2 tests: extra = m_startTimes[sprite]; Executed by:
| 262 | ||||||||||||
272 | else if (!m_duration[sprite])
| 0-568 | ||||||||||||
273 | return m_sprites[state]->m_rowStartX; never executed: return m_sprites[state]->m_rowStartX; | 0 | ||||||||||||
274 | else | - | ||||||||||||
275 | extra = pseudospriteProgress(sprite, state); executed 568 times by 1 test: extra = pseudospriteProgress(sprite, state); Executed by:
| 568 | ||||||||||||
276 | if (m_sprites[state]->reverse())
| 0-830 | ||||||||||||
277 | extra = (m_sprites[state]->m_generatedCount - 1) - extra; never executed: extra = (m_sprites[state]->m_generatedCount - 1) - extra; | 0 | ||||||||||||
278 | - | |||||||||||||
279 | if (extra)
| 344-486 | ||||||||||||
280 | return 0; executed 486 times by 1 test: return 0; Executed by:
| 486 | ||||||||||||
281 | return m_sprites[state]->m_rowStartX; executed 344 times by 2 tests: return m_sprites[state]->m_rowStartX; Executed by:
| 344 | ||||||||||||
282 | } | - | ||||||||||||
283 | - | |||||||||||||
284 | QQuickSprite* QQuickSpriteEngine::sprite(int sprite) const | - | ||||||||||||
285 | { | - | ||||||||||||
286 | return m_sprites[m_things[sprite]]; executed 137521 times by 4 tests: return m_sprites[m_things[sprite]]; Executed by:
| 137521 | ||||||||||||
287 | } | - | ||||||||||||
288 | - | |||||||||||||
289 | int QQuickSpriteEngine::spriteWidth(int sprite) const | - | ||||||||||||
290 | { | - | ||||||||||||
291 | int state = m_things[sprite]; | - | ||||||||||||
292 | return m_sprites[state]->m_frameWidth; executed 6634 times by 4 tests: return m_sprites[state]->m_frameWidth; Executed by:
| 6634 | ||||||||||||
293 | } | - | ||||||||||||
294 | - | |||||||||||||
295 | int QQuickSpriteEngine::spriteHeight(int sprite) const | - | ||||||||||||
296 | { | - | ||||||||||||
297 | int state = m_things[sprite]; | - | ||||||||||||
298 | return m_sprites[state]->m_frameHeight; executed 5699 times by 4 tests: return m_sprites[state]->m_frameHeight; Executed by:
| 5699 | ||||||||||||
299 | } | - | ||||||||||||
300 | - | |||||||||||||
301 | int QQuickSpriteEngine::spriteCount() const //TODO: Actually image state count, need to rename these things to make sense together | - | ||||||||||||
302 | { | - | ||||||||||||
303 | return m_imageStateCount; never executed: return m_imageStateCount; | 0 | ||||||||||||
304 | } | - | ||||||||||||
305 | - | |||||||||||||
306 | void QQuickStochasticEngine::setGoal(int state, int sprite, bool jump) | - | ||||||||||||
307 | { | - | ||||||||||||
308 | if (sprite >= m_things.count() || state >= m_states.count()
| 0-1206 | ||||||||||||
309 | || sprite < 0 || state < 0)
| 0-1206 | ||||||||||||
310 | return; never executed: return; | 0 | ||||||||||||
311 | if (!jump){
| 4-1202 | ||||||||||||
312 | m_goals[sprite] = state; | - | ||||||||||||
313 | return; executed 4 times by 1 test: return; Executed by:
| 4 | ||||||||||||
314 | } | - | ||||||||||||
315 | - | |||||||||||||
316 | if (m_things.at(sprite) == state)
| 0-1202 | ||||||||||||
317 | return;//Already there never executed: return; | 0 | ||||||||||||
318 | m_things[sprite] = state; | - | ||||||||||||
319 | m_duration[sprite] = m_states.at(state)->variedDuration(); | - | ||||||||||||
320 | m_goals[sprite] = -1; | - | ||||||||||||
321 | restart(sprite); | - | ||||||||||||
322 | emit stateChanged(sprite); | - | ||||||||||||
323 | emit m_states.at(state)->entered(); | - | ||||||||||||
324 | return; executed 1202 times by 2 tests: return; Executed by:
| 1202 | ||||||||||||
325 | } | - | ||||||||||||
326 | - | |||||||||||||
327 | QQuickPixmap::Status QQuickSpriteEngine::status() const //Composed status of all Sprites | - | ||||||||||||
328 | { | - | ||||||||||||
329 | if (!m_startedImageAssembly)
| 40-128 | ||||||||||||
330 | return QQuickPixmap::Null; executed 40 times by 5 tests: return QQuickPixmap::Null; Executed by:
| 40 | ||||||||||||
331 | int null, loading, ready; | - | ||||||||||||
332 | null = loading = ready = 0; | - | ||||||||||||
333 | for (QQuickSprite* s : m_sprites) { | - | ||||||||||||
334 | switch (s->m_pix.status()) { | - | ||||||||||||
335 | // ### Maybe add an error message here, because this null shouldn't be reached but when it does, the image fails without an error message. | - | ||||||||||||
336 | case QQuickPixmap::Null : null++; break; never executed: break; never executed: case QQuickPixmap::Null : | 0 | ||||||||||||
337 | case QQuickPixmap::Loading : loading++; break; never executed: break; never executed: case QQuickPixmap::Loading : | 0 | ||||||||||||
338 | case QQuickPixmap::Error : return QQuickPixmap::Error; never executed: return QQuickPixmap::Error; never executed: case QQuickPixmap::Error : | 0 | ||||||||||||
339 | case QQuickPixmap::Ready : ready++; break; executed 144 times by 4 tests: break; Executed by:
executed 144 times by 4 tests: case QQuickPixmap::Ready : Executed by:
| 144 | ||||||||||||
340 | } | - | ||||||||||||
341 | } executed 144 times by 4 tests: end of block Executed by:
| 144 | ||||||||||||
342 | if (null)
| 0-128 | ||||||||||||
343 | return QQuickPixmap::Null; never executed: return QQuickPixmap::Null; | 0 | ||||||||||||
344 | if (loading)
| 0-128 | ||||||||||||
345 | return QQuickPixmap::Loading; never executed: return QQuickPixmap::Loading; | 0 | ||||||||||||
346 | if (ready)
| 0-128 | ||||||||||||
347 | return QQuickPixmap::Ready; executed 128 times by 4 tests: return QQuickPixmap::Ready; Executed by:
| 128 | ||||||||||||
348 | return QQuickPixmap::Null; never executed: return QQuickPixmap::Null; | 0 | ||||||||||||
349 | } | - | ||||||||||||
350 | - | |||||||||||||
351 | void QQuickSpriteEngine::startAssemblingImage() | - | ||||||||||||
352 | { | - | ||||||||||||
353 | if (m_startedImageAssembly)
| 0-44 | ||||||||||||
354 | return; never executed: return; | 0 | ||||||||||||
355 | m_loaded = false; | - | ||||||||||||
356 | m_errorsPrinted = false; | - | ||||||||||||
357 | - | |||||||||||||
358 | //This could also trigger the start of the image loading in Sprites, however that currently happens in Sprite::setSource | - | ||||||||||||
359 | - | |||||||||||||
360 | QList<QQuickStochasticState*> removals; | - | ||||||||||||
361 | - | |||||||||||||
362 | for (QQuickStochasticState* s : qAsConst(m_states)) { | - | ||||||||||||
363 | QQuickSprite* sprite = qobject_cast<QQuickSprite*>(s); | - | ||||||||||||
364 | if (sprite) {
| 0-50 | ||||||||||||
365 | m_sprites << sprite; | - | ||||||||||||
366 | } else { executed 50 times by 5 tests: end of block Executed by:
| 50 | ||||||||||||
367 | removals << s; | - | ||||||||||||
368 | qDebug() << "Error: Non-sprite in QQuickSpriteEngine"; | - | ||||||||||||
369 | } never executed: end of block | 0 | ||||||||||||
370 | } | - | ||||||||||||
371 | for (QQuickStochasticState* s : qAsConst(removals)) | - | ||||||||||||
372 | m_states.removeAll(s); never executed: m_states.removeAll(s); | 0 | ||||||||||||
373 | m_startedImageAssembly = true; | - | ||||||||||||
374 | } executed 44 times by 5 tests: end of block Executed by:
| 44 | ||||||||||||
375 | - | |||||||||||||
376 | QImage QQuickSpriteEngine::assembledImage(int maxSize) | - | ||||||||||||
377 | { | - | ||||||||||||
378 | QQuickPixmap::Status stat = status(); | - | ||||||||||||
379 | if (!m_errorsPrinted && stat == QQuickPixmap::Error) {
| 0-44 | ||||||||||||
380 | for (QQuickSprite* s : qAsConst(m_sprites)) | - | ||||||||||||
381 | if (s->m_pix.isError())
| 0 | ||||||||||||
382 | qmlWarning(s) << s->m_pix.error(); never executed: qmlWarning(s) << s->m_pix.error(); | 0 | ||||||||||||
383 | m_errorsPrinted = true; | - | ||||||||||||
384 | } never executed: end of block | 0 | ||||||||||||
385 | - | |||||||||||||
386 | if (stat != QQuickPixmap::Ready)
| 0-44 | ||||||||||||
387 | return QImage(); never executed: return QImage(); | 0 | ||||||||||||
388 | - | |||||||||||||
389 | int h = 0; | - | ||||||||||||
390 | int w = 0; | - | ||||||||||||
391 | m_maxFrames = 0; | - | ||||||||||||
392 | m_imageStateCount = 0; | - | ||||||||||||
393 | qreal pixelRatio = 1.0; | - | ||||||||||||
394 | - | |||||||||||||
395 | for (QQuickSprite* state : qAsConst(m_sprites)) { | - | ||||||||||||
396 | if (state->frames() > m_maxFrames)
| 4-46 | ||||||||||||
397 | m_maxFrames = state->frames(); executed 46 times by 4 tests: m_maxFrames = state->frames(); Executed by:
| 46 | ||||||||||||
398 | - | |||||||||||||
399 | QImage img = state->m_pix.image(); | - | ||||||||||||
400 | - | |||||||||||||
401 | { | - | ||||||||||||
402 | const QSize frameSize(state->m_frameWidth, state->m_frameHeight); | - | ||||||||||||
403 | if (!(img.size() - frameSize).isValid()) {
| 10-40 | ||||||||||||
404 | qmlWarning(state).nospace() << "SpriteEngine: Invalid frame size " << frameSize << "." | - | ||||||||||||
405 | " It's bigger than image size " << img.size() << "."; | - | ||||||||||||
406 | return QImage(); executed 10 times by 1 test: return QImage(); Executed by:
| 10 | ||||||||||||
407 | } | - | ||||||||||||
408 | } | - | ||||||||||||
409 | - | |||||||||||||
410 | //Check that the frame sizes are the same within one sprite | - | ||||||||||||
411 | if (!state->m_frameWidth)
| 6-34 | ||||||||||||
412 | state->m_frameWidth = img.width() / state->frames(); executed 34 times by 4 tests: state->m_frameWidth = img.width() / state->frames(); Executed by:
| 34 | ||||||||||||
413 | - | |||||||||||||
414 | if (!state->m_frameHeight)
| 6-34 | ||||||||||||
415 | state->m_frameHeight = img.height(); executed 34 times by 4 tests: state->m_frameHeight = img.height(); Executed by:
| 34 | ||||||||||||
416 | - | |||||||||||||
417 | pixelRatio = qMax(pixelRatio, state->devicePixelRatio()); | - | ||||||||||||
418 | - | |||||||||||||
419 | if (state->frames() * state->frameWidth() > maxSize){
| 6-34 | ||||||||||||
420 | struct helper{ | - | ||||||||||||
421 | static int divRoundUp(int a, int b){return (a+b-1)/b;} executed 6 times by 2 tests: return (a+b-1)/b; Executed by:
| 6 | ||||||||||||
422 | }; | - | ||||||||||||
423 | int rowsNeeded = helper::divRoundUp(state->frames(), (maxSize / state->frameWidth())); | - | ||||||||||||
424 | if (h + rowsNeeded * state->frameHeight() > maxSize){
| 0-6 | ||||||||||||
425 | if (rowsNeeded * state->frameHeight() > maxSize)
| 0 | ||||||||||||
426 | qmlWarning(state) << "SpriteEngine: Animation too large to fit in one texture:" << state->source().toLocalFile(); never executed: qmlWarning(state) << "SpriteEngine: Animation too large to fit in one texture:" << state->source().toLocalFile(); | 0 | ||||||||||||
427 | else | - | ||||||||||||
428 | qmlWarning(state) << "SpriteEngine: Animations too large to fit in one texture, pushed over the edge by:" << state->source().toLocalFile(); never executed: qmlWarning(state) << "SpriteEngine: Animations too large to fit in one texture, pushed over the edge by:" << state->source().toLocalFile(); | 0 | ||||||||||||
429 | qmlWarning(state) << "SpriteEngine: Your texture max size today is " << maxSize; | - | ||||||||||||
430 | return QImage(); never executed: return QImage(); | 0 | ||||||||||||
431 | } | - | ||||||||||||
432 | state->m_generatedCount = rowsNeeded; | - | ||||||||||||
433 | h += state->frameHeight() * rowsNeeded; | - | ||||||||||||
434 | w = qMax(w, ((int)(maxSize / state->frameWidth())) * state->frameWidth()); | - | ||||||||||||
435 | m_imageStateCount += rowsNeeded; | - | ||||||||||||
436 | }else{ executed 6 times by 2 tests: end of block Executed by:
| 6 | ||||||||||||
437 | h += state->frameHeight(); | - | ||||||||||||
438 | w = qMax(w, state->frameWidth() * state->frames()); | - | ||||||||||||
439 | m_imageStateCount++; | - | ||||||||||||
440 | } executed 34 times by 4 tests: end of block Executed by:
| 34 | ||||||||||||
441 | } | - | ||||||||||||
442 | - | |||||||||||||
443 | if (h > maxSize){
| 0-34 | ||||||||||||
444 | qWarning() << "SpriteEngine: Too many animations to fit in one texture..."; | - | ||||||||||||
445 | qWarning() << "SpriteEngine: Your texture max size today is " << maxSize; | - | ||||||||||||
446 | return QImage(); never executed: return QImage(); | 0 | ||||||||||||
447 | } | - | ||||||||||||
448 | - | |||||||||||||
449 | //maxFrames is max number in a line of the texture | - | ||||||||||||
450 | QImage image(w * pixelRatio, h * pixelRatio, QImage::Format_ARGB32_Premultiplied); | - | ||||||||||||
451 | image.setDevicePixelRatio(pixelRatio); | - | ||||||||||||
452 | image.fill(0); | - | ||||||||||||
453 | QPainter p(&image); | - | ||||||||||||
454 | int y = 0; | - | ||||||||||||
455 | for (QQuickSprite* state : qAsConst(m_sprites)) { | - | ||||||||||||
456 | QImage img(state->m_pix.image()); | - | ||||||||||||
457 | const int frameWidth = state->m_frameWidth; | - | ||||||||||||
458 | const int frameHeight = state->m_frameHeight; | - | ||||||||||||
459 | const int imgHeight = img.height() / img.devicePixelRatioF(); | - | ||||||||||||
460 | const int imgWidth = img.width() / img.devicePixelRatioF(); | - | ||||||||||||
461 | if (imgHeight == frameHeight && imgWidth < maxSize){ //Simple case
| 2-36 | ||||||||||||
462 | p.drawImage(QRect(0, y, state->m_frames * frameWidth, frameHeight), | - | ||||||||||||
463 | img, | - | ||||||||||||
464 | QRect(state->m_frameX * img.devicePixelRatioF(), 0, state->m_frames * frameWidth * img.devicePixelRatioF(), frameHeight * img.devicePixelRatioF())); | - | ||||||||||||
465 | state->m_rowStartX = 0; | - | ||||||||||||
466 | state->m_rowY = y; | - | ||||||||||||
467 | y += frameHeight; | - | ||||||||||||
468 | } else { //Chopping up image case executed 34 times by 4 tests: end of block Executed by:
| 34 | ||||||||||||
469 | state->m_framesPerRow = w/frameWidth; | - | ||||||||||||
470 | state->m_rowY = y; | - | ||||||||||||
471 | int x = 0; | - | ||||||||||||
472 | int curX = state->m_frameX; | - | ||||||||||||
473 | int curY = state->m_frameY; | - | ||||||||||||
474 | int framesLeft = state->frames(); | - | ||||||||||||
475 | while (framesLeft > 0){
| 6-224 | ||||||||||||
476 | if (w - x + curX <= imgWidth){//finish a row in image (dest)
| 72-152 | ||||||||||||
477 | int copied = w - x; | - | ||||||||||||
478 | framesLeft -= copied/frameWidth; | - | ||||||||||||
479 | p.drawImage(QRect(x, y, copied, frameHeight), | - | ||||||||||||
480 | img, | - | ||||||||||||
481 | QRect(curX * img.devicePixelRatioF(), curY * img.devicePixelRatioF(), copied * img.devicePixelRatioF(), frameHeight * img.devicePixelRatioF())); | - | ||||||||||||
482 | y += frameHeight; | - | ||||||||||||
483 | curX += copied; | - | ||||||||||||
484 | x = 0; | - | ||||||||||||
485 | if (curX == imgWidth){
| 10-62 | ||||||||||||
486 | curX = 0; | - | ||||||||||||
487 | curY += frameHeight; | - | ||||||||||||
488 | } executed 10 times by 2 tests: end of block Executed by:
| 10 | ||||||||||||
489 | }else{//finish a row in img (src) executed 72 times by 2 tests: end of block Executed by:
| 72 | ||||||||||||
490 | int copied = imgWidth - curX; | - | ||||||||||||
491 | framesLeft -= copied/frameWidth; | - | ||||||||||||
492 | p.drawImage(QRect(x, y, copied, frameHeight), | - | ||||||||||||
493 | img, | - | ||||||||||||
494 | QRect(curX * img.devicePixelRatioF(), curY * img.devicePixelRatioF(), copied * img.devicePixelRatioF(), frameHeight * img.devicePixelRatioF())); | - | ||||||||||||
495 | curY += frameHeight; | - | ||||||||||||
496 | x += copied; | - | ||||||||||||
497 | curX = 0; | - | ||||||||||||
498 | } executed 152 times by 1 test: end of block Executed by:
| 152 | ||||||||||||
499 | } | - | ||||||||||||
500 | if (x)
| 2-4 | ||||||||||||
501 | y += frameHeight; executed 4 times by 1 test: y += frameHeight; Executed by:
| 4 | ||||||||||||
502 | } executed 6 times by 2 tests: end of block Executed by:
| 6 | ||||||||||||
503 | } | - | ||||||||||||
504 | - | |||||||||||||
505 | #ifdef SPRITE_IMAGE_DEBUG | - | ||||||||||||
506 | QString fPath = QDir::tempPath() + "/SpriteImage.%1.png"; | - | ||||||||||||
507 | int acc = 0; | - | ||||||||||||
508 | while (QFile::exists(fPath.arg(acc))) acc++; | - | ||||||||||||
509 | image.save(fPath.arg(acc), "PNG"); | - | ||||||||||||
510 | qDebug() << "Assembled image output to: " << fPath.arg(acc); | - | ||||||||||||
511 | #endif | - | ||||||||||||
512 | - | |||||||||||||
513 | m_loaded = true; | - | ||||||||||||
514 | m_startedImageAssembly = false; | - | ||||||||||||
515 | return image; executed 34 times by 4 tests: return image; Executed by:
| 34 | ||||||||||||
516 | } | - | ||||||||||||
517 | - | |||||||||||||
518 | //TODO: Add a reset() function, for completeness in the case of a SpriteEngine being restarted from 0 | - | ||||||||||||
519 | void QQuickStochasticEngine::setCount(int c) | - | ||||||||||||
520 | { | - | ||||||||||||
521 | m_things.resize(c); | - | ||||||||||||
522 | m_goals.resize(c); | - | ||||||||||||
523 | m_duration.resize(c); | - | ||||||||||||
524 | m_startTimes.resize(c); | - | ||||||||||||
525 | } executed 127 times by 6 tests: end of block Executed by:
| 127 | ||||||||||||
526 | - | |||||||||||||
527 | void QQuickStochasticEngine::start(int index, int state) | - | ||||||||||||
528 | { | - | ||||||||||||
529 | if (index >= m_things.count())
| 0-5184 | ||||||||||||
530 | return; never executed: return; | 0 | ||||||||||||
531 | m_things[index] = state; | - | ||||||||||||
532 | m_duration[index] = m_states.at(state)->variedDuration(); | - | ||||||||||||
533 | if (m_states.at(state)->randomStart())
| 0-5184 | ||||||||||||
534 | m_startTimes[index] = NINF; never executed: m_startTimes[index] = NINF; | 0 | ||||||||||||
535 | else | - | ||||||||||||
536 | m_startTimes[index] = 0; executed 5184 times by 5 tests: m_startTimes[index] = 0; Executed by:
| 5184 | ||||||||||||
537 | m_goals[index] = -1; | - | ||||||||||||
538 | m_addAdvance = false; | - | ||||||||||||
539 | restart(index); | - | ||||||||||||
540 | m_addAdvance = true; | - | ||||||||||||
541 | } executed 5184 times by 5 tests: end of block Executed by:
| 5184 | ||||||||||||
542 | - | |||||||||||||
543 | void QQuickStochasticEngine::stop(int index) | - | ||||||||||||
544 | { | - | ||||||||||||
545 | if (index >= m_things.count())
| 0-380 | ||||||||||||
546 | return; never executed: return; | 0 | ||||||||||||
547 | //Will never change until start is called again with a new state (or manually advanced) - this is not a 'pause' | - | ||||||||||||
548 | for (int i=0; i<m_stateUpdates.count(); i++)
| 380-5024 | ||||||||||||
549 | m_stateUpdates[i].second.removeAll(index); executed 5024 times by 1 test: m_stateUpdates[i].second.removeAll(index); Executed by:
| 5024 | ||||||||||||
550 | } executed 380 times by 2 tests: end of block Executed by:
| 380 | ||||||||||||
551 | - | |||||||||||||
552 | void QQuickStochasticEngine::restart(int index) | - | ||||||||||||
553 | { | - | ||||||||||||
554 | bool randomStart = (m_startTimes.at(index) == NINF); | - | ||||||||||||
555 | m_startTimes[index] = m_timeOffset; | - | ||||||||||||
556 | if (m_addAdvance)
| 2771-18349 | ||||||||||||
557 | m_startTimes[index] += m_advanceTime.elapsed(); executed 18349 times by 1 test: m_startTimes[index] += m_advanceTime.elapsed(); Executed by:
| 18349 | ||||||||||||
558 | if (randomStart)
| 0-21120 | ||||||||||||
559 | m_startTimes[index] -= QRandomGenerator::global()->bounded(m_duration.at(index)); never executed: m_startTimes[index] -= QRandomGenerator::global()->bounded(m_duration.at(index)); | 0 | ||||||||||||
560 | int time = m_duration.at(index) + m_startTimes.at(index); | - | ||||||||||||
561 | for (int i=0; i<m_stateUpdates.count(); i++)
| 21120-345041 | ||||||||||||
562 | m_stateUpdates[i].second.removeAll(index); executed 345041 times by 1 test: m_stateUpdates[i].second.removeAll(index); Executed by:
| 345041 | ||||||||||||
563 | if (m_duration.at(index) >= 0)
| 0-21120 | ||||||||||||
564 | addToUpdateList(time, index); executed 21120 times by 1 test: addToUpdateList(time, index); Executed by:
| 21120 | ||||||||||||
565 | } executed 21120 times by 1 test: end of block Executed by:
| 21120 | ||||||||||||
566 | - | |||||||||||||
567 | void QQuickSpriteEngine::restart(int index) //Reimplemented to recognize and handle pseudostates | - | ||||||||||||
568 | { | - | ||||||||||||
569 | bool randomStart = (m_startTimes.at(index) == NINF); | - | ||||||||||||
570 | if (m_loaded && m_sprites.at(m_things.at(index))->frameSync()) {//Manually advanced
| 12-3722 | ||||||||||||
571 | m_startTimes[index] = 0; | - | ||||||||||||
572 | if (randomStart && m_sprites.at(m_things.at(index))->m_generatedCount)
| 0-38 | ||||||||||||
573 | m_startTimes[index] += QRandomGenerator::global()->bounded(m_sprites.at(m_things.at(index))->m_generatedCount); never executed: m_startTimes[index] += QRandomGenerator::global()->bounded(m_sprites.at(m_things.at(index))->m_generatedCount); | 0 | ||||||||||||
574 | } else { executed 38 times by 2 tests: end of block Executed by:
| 38 | ||||||||||||
575 | m_startTimes[index] = m_timeOffset; | - | ||||||||||||
576 | if (m_addAdvance)
| 1200-2496 | ||||||||||||
577 | m_startTimes[index] += m_advanceTime.elapsed(); executed 1200 times by 1 test: m_startTimes[index] += m_advanceTime.elapsed(); Executed by:
| 1200 | ||||||||||||
578 | if (randomStart)
| 0-3696 | ||||||||||||
579 | m_startTimes[index] -= QRandomGenerator::global()->bounded(m_duration.at(index)); never executed: m_startTimes[index] -= QRandomGenerator::global()->bounded(m_duration.at(index)); | 0 | ||||||||||||
580 | int time = spriteDuration(index) + m_startTimes.at(index); | - | ||||||||||||
581 | if (randomStart) {
| 0-3696 | ||||||||||||
582 | int curTime = m_timeOffset + (m_addAdvance ? m_advanceTime.elapsed() : 0);
| 0 | ||||||||||||
583 | while (time < curTime) //Fast forward through psuedostates as needed
| 0 | ||||||||||||
584 | time += spriteDuration(index); never executed: time += spriteDuration(index); | 0 | ||||||||||||
585 | } never executed: end of block | 0 | ||||||||||||
586 | - | |||||||||||||
587 | for (int i=0; i<m_stateUpdates.count(); i++)
| 3696-130735 | ||||||||||||
588 | m_stateUpdates[i].second.removeAll(index); executed 130735 times by 4 tests: m_stateUpdates[i].second.removeAll(index); Executed by:
| 130735 | ||||||||||||
589 | addToUpdateList(time, index); | - | ||||||||||||
590 | } executed 3696 times by 4 tests: end of block Executed by:
| 3696 | ||||||||||||
591 | } | - | ||||||||||||
592 | - | |||||||||||||
593 | void QQuickStochasticEngine::advance(int idx) | - | ||||||||||||
594 | { | - | ||||||||||||
595 | if (idx >= m_things.count())
| 0-18432 | ||||||||||||
596 | return;//TODO: Proper fix(because this has happened and I just ignored it) never executed: return; | 0 | ||||||||||||
597 | int nextIdx = nextState(m_things.at(idx), idx); | - | ||||||||||||
598 | m_things[idx] = nextIdx; | - | ||||||||||||
599 | m_duration[idx] = m_states.at(nextIdx)->variedDuration(); | - | ||||||||||||
600 | restart(idx); | - | ||||||||||||
601 | emit m_states.at(nextIdx)->entered(); | - | ||||||||||||
602 | emit stateChanged(idx); | - | ||||||||||||
603 | } executed 18432 times by 1 test: end of block Executed by:
| 18432 | ||||||||||||
604 | - | |||||||||||||
605 | void QQuickSpriteEngine::advance(int idx) //Reimplemented to recognize and handle pseudostates | - | ||||||||||||
606 | { | - | ||||||||||||
607 | if (!m_loaded) {
| 0-60 | ||||||||||||
608 | qWarning() << QLatin1String("QQuickSpriteEngine: Trying to advance sprites before sprites finish loading. Ignoring directive"); | - | ||||||||||||
609 | return; never executed: return; | 0 | ||||||||||||
610 | } | - | ||||||||||||
611 | - | |||||||||||||
612 | if (idx >= m_things.count())
| 0-60 | ||||||||||||
613 | return;//TODO: Proper fix(because this has happened and I just ignored it) never executed: return; | 0 | ||||||||||||
614 | if (m_duration.at(idx) == 0) {
| 28-32 | ||||||||||||
615 | if (m_sprites.at(m_things.at(idx))->frameSync()) {
| 0-32 | ||||||||||||
616 | //Manually called, advance inner substate count | - | ||||||||||||
617 | m_startTimes[idx]++; | - | ||||||||||||
618 | if (m_startTimes.at(idx) < m_sprites.at(m_things.at(idx))->m_generatedCount) {
| 12-20 | ||||||||||||
619 | //only a pseudostate ended | - | ||||||||||||
620 | emit stateChanged(idx); | - | ||||||||||||
621 | return; executed 12 times by 1 test: return; Executed by:
| 12 | ||||||||||||
622 | } | - | ||||||||||||
623 | } executed 20 times by 2 tests: end of block Executed by:
| 20 | ||||||||||||
624 | //just go past the pseudostate logic | - | ||||||||||||
625 | } else if (m_startTimes.at(idx) + m_duration.at(idx) executed 20 times by 2 tests: end of block Executed by:
| 12-20 | ||||||||||||
626 | > int(m_timeOffset + (m_addAdvance ? m_advanceTime.elapsed() : 0))) {
| 12-16 | ||||||||||||
627 | //only a pseduostate ended | - | ||||||||||||
628 | emit stateChanged(idx); | - | ||||||||||||
629 | addToUpdateList(spriteStart(idx) + spriteDuration(idx) + (m_addAdvance ? m_advanceTime.elapsed() : 0), idx); | - | ||||||||||||
630 | return; executed 12 times by 1 test: return; Executed by:
| 12 | ||||||||||||
631 | } | - | ||||||||||||
632 | int nextIdx = nextState(m_things.at(idx), idx); | - | ||||||||||||
633 | m_things[idx] = nextIdx; | - | ||||||||||||
634 | m_duration[idx] = m_states.at(nextIdx)->variedDuration(); | - | ||||||||||||
635 | restart(idx); | - | ||||||||||||
636 | emit m_states.at(nextIdx)->entered(); | - | ||||||||||||
637 | emit stateChanged(idx); | - | ||||||||||||
638 | } executed 36 times by 2 tests: end of block Executed by:
| 36 | ||||||||||||
639 | - | |||||||||||||
640 | int QQuickStochasticEngine::nextState(int curState, int curThing) | - | ||||||||||||
641 | { | - | ||||||||||||
642 | int nextIdx = -1; | - | ||||||||||||
643 | int goalPath = goalSeek(curState, curThing); | - | ||||||||||||
644 | if (goalPath == -1){//Random
| 0-18468 | ||||||||||||
645 | qreal r = QRandomGenerator::global()->generateDouble(); | - | ||||||||||||
646 | qreal total = 0.0; | - | ||||||||||||
647 | for (QVariantMap::const_iterator iter=m_states.at(curState)->m_to.constBegin(); | - | ||||||||||||
648 | iter!=m_states.at(curState)->m_to.constEnd(); ++iter)
| 1330-18468 | ||||||||||||
649 | total += (*iter).toReal(); executed 1330 times by 2 tests: total += (*iter).toReal(); Executed by:
| 1330 | ||||||||||||
650 | r*=total; | - | ||||||||||||
651 | for (QVariantMap::const_iterator iter= m_states.at(curState)->m_to.constBegin(); | - | ||||||||||||
652 | iter!=m_states.at(curState)->m_to.constEnd(); ++iter){
| 1330-17138 | ||||||||||||
653 | if (r < (*iter).toReal()){
| 0-1330 | ||||||||||||
654 | bool superBreak = false; | - | ||||||||||||
655 | for (int i=0; i<m_states.count(); i++){
| 0-1332 | ||||||||||||
656 | if (m_states.at(i)->name() == iter.key()){
| 2-1330 | ||||||||||||
657 | nextIdx = i; | - | ||||||||||||
658 | superBreak = true; | - | ||||||||||||
659 | break; executed 1330 times by 2 tests: break; Executed by:
| 1330 | ||||||||||||
660 | } | - | ||||||||||||
661 | } executed 2 times by 1 test: end of block Executed by:
| 2 | ||||||||||||
662 | if (superBreak)
| 0-1330 | ||||||||||||
663 | break; executed 1330 times by 2 tests: break; Executed by:
| 1330 | ||||||||||||
664 | } never executed: end of block | 0 | ||||||||||||
665 | r -= (*iter).toReal(); | - | ||||||||||||
666 | } never executed: end of block | 0 | ||||||||||||
667 | }else{//Random out of shortest paths to goal executed 18468 times by 3 tests: end of block Executed by:
| 18468 | ||||||||||||
668 | nextIdx = goalPath; | - | ||||||||||||
669 | } never executed: end of block | 0 | ||||||||||||
670 | if (nextIdx == -1)//No 'to' states means stay here
| 1330-17138 | ||||||||||||
671 | nextIdx = curState; executed 17138 times by 3 tests: nextIdx = curState; Executed by:
| 17138 | ||||||||||||
672 | return nextIdx; executed 18468 times by 3 tests: return nextIdx; Executed by:
| 18468 | ||||||||||||
673 | } | - | ||||||||||||
674 | - | |||||||||||||
675 | uint QQuickStochasticEngine::updateSprites(uint time)//### would returning a list of changed idxs be faster than signals? | - | ||||||||||||
676 | { | - | ||||||||||||
677 | //Sprite State Update; | - | ||||||||||||
678 | m_timeOffset = time; | - | ||||||||||||
679 | m_addAdvance = false; | - | ||||||||||||
680 | int i = 0; | - | ||||||||||||
681 | for (; i < m_stateUpdates.count() && time >= m_stateUpdates.at(i).first; ++i) {
| 357-2660 | ||||||||||||
682 | const auto copy = m_stateUpdates.at(i).second; | - | ||||||||||||
683 | for (int idx : copy) | - | ||||||||||||
684 | advance(idx); executed 18460 times by 3 tests: advance(idx); Executed by:
| 18460 | ||||||||||||
685 | } executed 638 times by 3 tests: end of block Executed by:
| 638 | ||||||||||||
686 | - | |||||||||||||
687 | m_stateUpdates.remove(0, i); | - | ||||||||||||
688 | m_advanceTime.start(); | - | ||||||||||||
689 | m_addAdvance = true; | - | ||||||||||||
690 | if (m_stateUpdates.isEmpty())
| 357-2022 | ||||||||||||
691 | return uint(-1); executed 357 times by 5 tests: return uint(-1); Executed by:
| 357 | ||||||||||||
692 | return m_stateUpdates.constFirst().first; executed 2022 times by 5 tests: return m_stateUpdates.constFirst().first; Executed by:
| 2022 | ||||||||||||
693 | } | - | ||||||||||||
694 | - | |||||||||||||
695 | int QQuickStochasticEngine::goalSeek(int curIdx, int spriteIdx, int dist) | - | ||||||||||||
696 | { | - | ||||||||||||
697 | QString goalName; | - | ||||||||||||
698 | if (m_goals.at(spriteIdx) != -1)
| 0-18468 | ||||||||||||
699 | goalName = m_states.at(m_goals.at(spriteIdx))->name(); never executed: goalName = m_states.at(m_goals.at(spriteIdx))->name(); | 0 | ||||||||||||
700 | else | - | ||||||||||||
701 | goalName = m_globalGoal; executed 18468 times by 3 tests: goalName = m_globalGoal; Executed by:
| 18468 | ||||||||||||
702 | if (goalName.isEmpty())
| 0-18468 | ||||||||||||
703 | return -1; executed 18468 times by 3 tests: return -1; Executed by:
| 18468 | ||||||||||||
704 | //TODO: caching instead of excessively redoing iterative deepening (which was chosen arbitrarily anyways) | - | ||||||||||||
705 | // Paraphrased - implement in an *efficient* manner | - | ||||||||||||
706 | for (int i=0; i<m_states.count(); i++)
| 0 | ||||||||||||
707 | if (m_states.at(curIdx)->name() == goalName)
| 0 | ||||||||||||
708 | return curIdx; never executed: return curIdx; | 0 | ||||||||||||
709 | if (dist < 0)
| 0 | ||||||||||||
710 | dist = m_states.count(); never executed: dist = m_states.count(); | 0 | ||||||||||||
711 | QQuickStochasticState* curState = m_states.at(curIdx); | - | ||||||||||||
712 | for (QVariantMap::const_iterator iter = curState->m_to.constBegin(); | - | ||||||||||||
713 | iter!=curState->m_to.constEnd(); ++iter){
| 0 | ||||||||||||
714 | if (iter.key() == goalName)
| 0 | ||||||||||||
715 | for (int i=0; i<m_states.count(); i++)
| 0 | ||||||||||||
716 | if (m_states.at(i)->name() == goalName)
| 0 | ||||||||||||
717 | return i; never executed: return i; | 0 | ||||||||||||
718 | } never executed: end of block | 0 | ||||||||||||
719 | QSet<int> options; | - | ||||||||||||
720 | for (int i=1; i<dist; i++){
| 0 | ||||||||||||
721 | for (QVariantMap::const_iterator iter = curState->m_to.constBegin(); | - | ||||||||||||
722 | iter!=curState->m_to.constEnd(); ++iter){
| 0 | ||||||||||||
723 | int option = -1; | - | ||||||||||||
724 | for (int j=0; j<m_states.count(); j++)//One place that could be a lot more efficient...
| 0 | ||||||||||||
725 | if (m_states.at(j)->name() == iter.key())
| 0 | ||||||||||||
726 | if (goalSeek(j, spriteIdx, i) != -1)
| 0 | ||||||||||||
727 | option = j; never executed: option = j; | 0 | ||||||||||||
728 | if (option != -1)
| 0 | ||||||||||||
729 | options << option; never executed: options << option; | 0 | ||||||||||||
730 | } never executed: end of block | 0 | ||||||||||||
731 | if (!options.isEmpty()){
| 0 | ||||||||||||
732 | if (options.count()==1)
| 0 | ||||||||||||
733 | return *(options.begin()); never executed: return *(options.begin()); | 0 | ||||||||||||
734 | int option = -1; | - | ||||||||||||
735 | qreal r = QRandomGenerator::global()->generateDouble(); | - | ||||||||||||
736 | qreal total = 0; | - | ||||||||||||
737 | for (QSet<int>::const_iterator iter=options.constBegin(); | - | ||||||||||||
738 | iter!=options.constEnd(); ++iter)
| 0 | ||||||||||||
739 | total += curState->m_to.value(m_states.at((*iter))->name()).toReal(); never executed: total += curState->m_to.value(m_states.at((*iter))->name()).toReal(); | 0 | ||||||||||||
740 | r *= total; | - | ||||||||||||
741 | for (QVariantMap::const_iterator iter = curState->m_to.constBegin(); | - | ||||||||||||
742 | iter!=curState->m_to.constEnd(); ++iter){
| 0 | ||||||||||||
743 | bool superContinue = true; | - | ||||||||||||
744 | for (int j=0; j<m_states.count(); j++)
| 0 | ||||||||||||
745 | if (m_states.at(j)->name() == iter.key())
| 0 | ||||||||||||
746 | if (options.contains(j))
| 0 | ||||||||||||
747 | superContinue = false; never executed: superContinue = false; | 0 | ||||||||||||
748 | if (superContinue)
| 0 | ||||||||||||
749 | continue; never executed: continue; | 0 | ||||||||||||
750 | if (r < (*iter).toReal()){
| 0 | ||||||||||||
751 | bool superBreak = false; | - | ||||||||||||
752 | for (int j=0; j<m_states.count(); j++){
| 0 | ||||||||||||
753 | if (m_states.at(j)->name() == iter.key()){
| 0 | ||||||||||||
754 | option = j; | - | ||||||||||||
755 | superBreak = true; | - | ||||||||||||
756 | break; never executed: break; | 0 | ||||||||||||
757 | } | - | ||||||||||||
758 | } never executed: end of block | 0 | ||||||||||||
759 | if (superBreak)
| 0 | ||||||||||||
760 | break; never executed: break; | 0 | ||||||||||||
761 | } never executed: end of block | 0 | ||||||||||||
762 | r-=(*iter).toReal(); | - | ||||||||||||
763 | } never executed: end of block | 0 | ||||||||||||
764 | return option; never executed: return option; | 0 | ||||||||||||
765 | } | - | ||||||||||||
766 | } never executed: end of block | 0 | ||||||||||||
767 | return -1; never executed: return -1; | 0 | ||||||||||||
768 | } | - | ||||||||||||
769 | - | |||||||||||||
770 | void QQuickStochasticEngine::addToUpdateList(uint t, int idx) | - | ||||||||||||
771 | { | - | ||||||||||||
772 | for (int i=0; i<m_stateUpdates.count(); i++){
| 770-447545 | ||||||||||||
773 | if (m_stateUpdates.at(i).first == t){
| 23896-423649 | ||||||||||||
774 | m_stateUpdates[i].second << idx; | - | ||||||||||||
775 | return; executed 23896 times by 4 tests: return; Executed by:
| 23896 | ||||||||||||
776 | } else if (m_stateUpdates.at(i).first > t) {
| 162-423487 | ||||||||||||
777 | QVector<int> tmpList; | - | ||||||||||||
778 | tmpList << idx; | - | ||||||||||||
779 | m_stateUpdates.insert(i, qMakePair(t, tmpList)); | - | ||||||||||||
780 | return; executed 162 times by 1 test: return; Executed by:
| 162 | ||||||||||||
781 | } | - | ||||||||||||
782 | } executed 423487 times by 5 tests: end of block Executed by:
| 423487 | ||||||||||||
783 | QVector<int> tmpList; | - | ||||||||||||
784 | tmpList << idx; | - | ||||||||||||
785 | m_stateUpdates << qMakePair(t, tmpList); | - | ||||||||||||
786 | } executed 770 times by 5 tests: end of block Executed by:
| 770 | ||||||||||||
787 | - | |||||||||||||
788 | QT_END_NAMESPACE | - | ||||||||||||
789 | - | |||||||||||||
790 | #include "moc_qquickspriteengine_p.cpp" | - | ||||||||||||
Source code | Switch to Preprocessed file |