Revision 15aa7d94
Added by Leszek Koltunski almost 7 years ago
src/main/java/org/distorted/library/main/EffectQueue.java | ||
---|---|---|
22 | 22 |
import org.distorted.library.effect.Effect; |
23 | 23 |
import org.distorted.library.message.EffectListener; |
24 | 24 |
import org.distorted.library.message.EffectMessage; |
25 |
import org.distorted.library.type.Dynamic; |
|
26 | 25 |
|
27 | 26 |
import java.util.Vector; |
28 | 27 |
|
... | ... | |
30 | 29 |
|
31 | 30 |
abstract class EffectQueue |
32 | 31 |
{ |
33 |
protected byte mNumEffects; // number of effects at the moment |
|
34 |
protected long mTotalEffects; // total number of effects ever created |
|
35 |
protected int[] mName; |
|
36 |
protected int[] mType; |
|
32 |
protected byte mNumEffects; |
|
37 | 33 |
protected float[] mUniforms; |
38 |
protected float[] mCache; |
|
39 |
protected Dynamic[][] mInter; |
|
40 | 34 |
protected long[] mCurrentDuration; |
41 |
protected byte[] mFreeIndexes; |
|
42 |
protected byte[] mIDIndex; |
|
43 |
protected long[] mID; |
|
35 |
protected Effect[] mEffects; |
|
36 |
protected int[] mName; |
|
44 | 37 |
protected long mTime=0; |
45 | 38 |
protected static int[] mMax = new int[Effect.LENGTH]; |
46 |
protected int mMaxIndex; |
|
47 | 39 |
protected Vector<EffectListener> mListeners =null; |
48 | 40 |
protected int mNumListeners=0; // ==mListeners.length(), but we only create mListeners if the first one gets added |
49 |
protected long mObjectID;
|
|
41 |
protected long mID; |
|
50 | 42 |
|
51 | 43 |
private static boolean mCreated; |
44 |
private int mIndex; |
|
45 |
private int mNumUniforms; |
|
52 | 46 |
|
53 | 47 |
static |
54 | 48 |
{ |
... | ... | |
57 | 51 |
|
58 | 52 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
59 | 53 |
|
60 |
EffectQueue(long id, int numUniforms, int numCache, int index)
|
|
54 |
EffectQueue(long id, int numUniforms, int index) |
|
61 | 55 |
{ |
62 |
mNumEffects = 0;
|
|
63 |
mTotalEffects = 0;
|
|
64 |
mMaxIndex = index;
|
|
65 |
mObjectID = id;
|
|
56 |
mNumEffects = 0; |
|
57 |
mID = id;
|
|
58 |
mIndex = index;
|
|
59 |
mNumUniforms = numUniforms;
|
|
66 | 60 |
|
67 |
int max = mMax[mMaxIndex];
|
|
61 |
int max = mMax[mIndex]; |
|
68 | 62 |
|
69 | 63 |
if( max>0 ) |
70 | 64 |
{ |
71 |
mName = new int[max]; |
|
72 |
mType = new int[max]; |
|
73 |
mUniforms = new float[numUniforms*max]; |
|
74 |
mInter = new Dynamic[3][max]; |
|
65 |
mUniforms = new float[mNumUniforms*max]; |
|
75 | 66 |
mCurrentDuration = new long[max]; |
76 |
mID = new long[max]; |
|
77 |
mIDIndex = new byte[max]; |
|
78 |
mFreeIndexes = new byte[max]; |
|
79 |
|
|
80 |
for(byte i=0; i<max; i++) mFreeIndexes[i] = i; |
|
81 |
|
|
82 |
if( numCache>0 ) |
|
83 |
{ |
|
84 |
mCache = new float[numCache*max]; |
|
85 |
} |
|
67 |
mEffects = new Effect[max]; |
|
68 |
mName = new int[max]; |
|
86 | 69 |
} |
87 | 70 |
|
88 | 71 |
mCreated = true; |
... | ... | |
152 | 135 |
Effect.reset(mMax); |
153 | 136 |
mCreated = false; |
154 | 137 |
} |
155 |
|
|
156 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
157 |
|
|
158 |
synchronized int removeByID(long id) |
|
159 |
{ |
|
160 |
int i = getEffectIndex(id); |
|
161 |
|
|
162 |
if( i>=0 ) |
|
163 |
{ |
|
164 |
remove(i); |
|
165 |
return 1; |
|
166 |
} |
|
167 |
|
|
168 |
return 0; |
|
169 |
} |
|
170 | 138 |
|
171 | 139 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
172 | 140 |
|
173 | 141 |
synchronized int removeByType(Effect effect) |
174 | 142 |
{ |
175 |
int ret = 0; |
|
176 |
int name = effect.getName(); |
|
177 |
int type = effect.getType(); |
|
143 |
int ret = 0; |
|
178 | 144 |
|
179 | 145 |
for(int i=0; i<mNumEffects; i++) |
180 | 146 |
{ |
181 |
if( mName[i]==name && mType[i]==type )
|
|
147 |
if( mEffects[i]==effect )
|
|
182 | 148 |
{ |
183 | 149 |
remove(i); |
184 | 150 |
i--; |
... | ... | |
188 | 154 |
|
189 | 155 |
return ret; |
190 | 156 |
} |
191 |
|
|
192 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
193 |
|
|
194 |
private synchronized int getEffectIndex(long id) |
|
195 |
{ |
|
196 |
int index = mIDIndex[(int)(id%mMax[mMaxIndex])]; |
|
197 |
return (index<mNumEffects && mID[index]==id ? index : -1); |
|
198 |
} |
|
199 | 157 |
|
200 | 158 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
201 | 159 |
// we do want to notify Listeners if they called 'abortAll' themselves but don't want to notify |
... | ... | |
204 | 162 |
synchronized int abortAll(boolean notify) |
205 | 163 |
{ |
206 | 164 |
int ret = mNumEffects; |
207 |
long removedID; |
|
208 |
int removedName, removedType; |
|
209 | 165 |
|
210 | 166 |
for(int i=0; i<ret; i++ ) |
211 | 167 |
{ |
212 |
mInter[0][i] = null; |
|
213 |
mInter[1][i] = null; |
|
214 |
mInter[2][i] = null; |
|
215 |
|
|
216 | 168 |
if( notify ) |
217 | 169 |
{ |
218 |
removedID = mID[i]; |
|
219 |
removedName= mName[i]; |
|
220 |
removedType= mType[i]; |
|
221 |
|
|
222 | 170 |
for(int j=0; j<mNumListeners; j++) |
223 |
EffectMessageSender.newMessage( mListeners.elementAt(j), |
|
224 |
EffectMessage.EFFECT_REMOVED, |
|
225 |
(removedID<<Effect.LENGTH)+removedType, |
|
226 |
removedName, |
|
227 |
mObjectID); |
|
171 |
EffectMessageSender.newMessage( mListeners.elementAt(j), EffectMessage.EFFECT_REMOVED, mEffects[i].getID(), mID); |
|
228 | 172 |
} |
173 |
|
|
174 |
mEffects[i] = null; |
|
229 | 175 |
} |
230 | 176 |
|
231 | 177 |
mNumEffects= 0; |
... | ... | |
239 | 185 |
protected void remove(int effect) |
240 | 186 |
{ |
241 | 187 |
mNumEffects--; |
242 |
|
|
243 |
byte removedIndex = (byte)(mID[effect]%mMax[mMaxIndex]); |
|
244 |
byte removedPosition = mIDIndex[removedIndex]; |
|
245 |
mFreeIndexes[mNumEffects] = removedIndex; |
|
246 |
|
|
247 |
long removedID = mID[effect]; |
|
248 |
int removedName= mName[effect]; |
|
249 |
int removedType= mType[effect]; |
|
250 |
|
|
251 |
for(int j=0; j<mMax[mMaxIndex]; j++) |
|
252 |
{ |
|
253 |
if( mIDIndex[j] > removedPosition ) mIDIndex[j]--; |
|
254 |
} |
|
255 |
|
|
188 |
|
|
189 |
long removedID = mEffects[effect].getID(); |
|
190 |
|
|
256 | 191 |
for(int j=effect; j<mNumEffects; j++ ) |
257 | 192 |
{ |
258 |
mName[j] = mName[j+1]; |
|
259 |
mType[j] = mType[j+1]; |
|
260 |
mInter[0][j] = mInter[0][j+1]; |
|
261 |
mInter[1][j] = mInter[1][j+1]; |
|
262 |
mInter[2][j] = mInter[2][j+1]; |
|
193 |
mEffects[j] = mEffects[j+1]; |
|
263 | 194 |
mCurrentDuration[j] = mCurrentDuration[j+1]; |
264 |
mID[j] = mID[j+1]; |
|
265 |
|
|
266 |
moveEffect(j); |
|
195 |
mName[j] = mName[j+1]; |
|
196 |
|
|
197 |
for(int k=0; k<mNumUniforms; k++) |
|
198 |
mUniforms[mNumUniforms*j+k] = mUniforms[mNumUniforms*(j+1)+k]; |
|
267 | 199 |
} |
268 | 200 |
|
269 |
mInter[0][mNumEffects] = null; |
|
270 |
mInter[1][mNumEffects] = null; |
|
271 |
mInter[2][mNumEffects] = null; |
|
201 |
mEffects[mNumEffects] = null; |
|
272 | 202 |
|
273 | 203 |
for(int i=0; i<mNumListeners; i++) |
274 |
EffectMessageSender.newMessage( mListeners.elementAt(i), |
|
275 |
EffectMessage.EFFECT_REMOVED, |
|
276 |
(removedID<<Effect.LENGTH)+removedType, |
|
277 |
removedName, |
|
278 |
mObjectID); |
|
204 |
EffectMessageSender.newMessage( mListeners.elementAt(i), EffectMessage.EFFECT_REMOVED, removedID, mID); |
|
279 | 205 |
} |
280 | 206 |
|
281 | 207 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
282 | 208 |
|
283 |
protected long addBase(Effect effect)
|
|
209 |
public void add(Effect effect)
|
|
284 | 210 |
{ |
285 |
int type = effect.getType(); |
|
286 |
|
|
287 |
mName[mNumEffects] = effect.getName(); |
|
288 |
mType[mNumEffects] = type; |
|
289 |
mCurrentDuration[mNumEffects] = 0; |
|
290 |
|
|
291 |
int index = mFreeIndexes[mNumEffects]; |
|
292 |
long id = mTotalEffects*mMax[mMaxIndex] + index; |
|
293 |
mID[mNumEffects] = id; |
|
294 |
mIDIndex[index] = mNumEffects; |
|
295 |
|
|
296 |
mNumEffects++; |
|
297 |
mTotalEffects++; |
|
298 |
|
|
299 |
return (id<<Effect.LENGTH)+type; |
|
300 |
} |
|
301 |
|
|
302 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
303 |
// used only for debugging |
|
304 |
|
|
305 |
@SuppressWarnings("unused") |
|
306 |
protected String printEffects(int max) |
|
307 |
{ |
|
308 |
long[] indexes = new long[mMax[mMaxIndex]]; |
|
309 |
|
|
310 |
for(int g=0; g<mMax[mMaxIndex]; g++) |
|
311 |
{ |
|
312 |
indexes[g] = -1; |
|
313 |
} |
|
314 |
|
|
315 |
String ret="("; |
|
316 |
int f; |
|
317 |
|
|
318 |
for(int g=0; g<max; g++) |
|
211 |
if( mMax[mIndex]>mNumEffects ) |
|
319 | 212 |
{ |
320 |
f = getEffectIndex(g); |
|
321 |
if( f>=0 ) indexes[f] = g; |
|
213 |
mCurrentDuration[mNumEffects] = 0; |
|
214 |
mEffects[mNumEffects] = effect; |
|
215 |
mName[mNumEffects] = effect.getName(); |
|
216 |
mNumEffects++; |
|
322 | 217 |
} |
323 |
|
|
324 |
for(int g=0; g<mMax[mMaxIndex]; g++) |
|
325 |
{ |
|
326 |
ret += (g>0 ? ",":"")+(indexes[g]>=0 ? indexes[g] : " "); |
|
327 |
} |
|
328 |
|
|
329 |
ret += ")"; |
|
330 |
|
|
331 |
return ret; |
|
332 | 218 |
} |
333 |
|
|
334 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
335 |
// Only used for debugging |
|
336 |
|
|
337 |
protected boolean printByID(long id) |
|
338 |
{ |
|
339 |
int index = getEffectIndex(id); |
|
340 |
|
|
341 |
if( index>=0 ) |
|
342 |
{ |
|
343 |
boolean inter0 = mInter[0][index]==null; |
|
344 |
boolean inter1 = mInter[1][index]==null; |
|
345 |
boolean inter2 = mInter[2][index]==null; |
|
346 |
|
|
347 |
android.util.Log.e("EffectQueue", "numEffects="+mNumEffects+" effect id="+id+" index="+index+ |
|
348 |
" duration="+mCurrentDuration[index]+" inter[0] null="+inter0+" inter[1] null="+inter1+" inter[2] null="+inter2); |
|
349 |
|
|
350 |
if( !inter0 ) android.util.Log.e("EffectQueue","inter[0]: "+mInter[0][index].print()); |
|
351 |
if( !inter1 ) android.util.Log.e("EffectQueue","inter[1]: "+mInter[1][index].print()); |
|
352 |
if( !inter2 ) android.util.Log.e("EffectQueue","inter[2]: "+mInter[2][index].print()); |
|
353 |
|
|
354 |
return true; |
|
355 |
} |
|
356 |
|
|
357 |
android.util.Log.e("EffectQueue", "effect id="+id+" not found"); |
|
358 |
|
|
359 |
return false; |
|
360 |
} |
|
361 |
|
|
362 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
363 |
|
|
364 |
abstract void moveEffect(int index); |
|
365 | 219 |
} |
366 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
Also available in: Unified diff
Progress with support for Effect classes.