1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2016 Leszek Koltunski //
|
3
|
// //
|
4
|
// This file is part of Distorted. //
|
5
|
// //
|
6
|
// Distorted is free software: you can redistribute it and/or modify //
|
7
|
// it under the terms of the GNU General Public License as published by //
|
8
|
// the Free Software Foundation, either version 2 of the License, or //
|
9
|
// (at your option) any later version. //
|
10
|
// //
|
11
|
// Distorted is distributed in the hope that it will be useful, //
|
12
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
|
13
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
|
14
|
// GNU General Public License for more details. //
|
15
|
// //
|
16
|
// You should have received a copy of the GNU General Public License //
|
17
|
// along with Distorted. If not, see <http://www.gnu.org/licenses/>. //
|
18
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
19
|
|
20
|
package org.distorted.library.main;
|
21
|
|
22
|
import org.distorted.library.effect.Effect;
|
23
|
import org.distorted.library.effect.EffectName;
|
24
|
import org.distorted.library.effect.EffectType;
|
25
|
import org.distorted.library.message.EffectListener;
|
26
|
import org.distorted.library.message.EffectMessage;
|
27
|
|
28
|
import java.util.Vector;
|
29
|
|
30
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
31
|
|
32
|
abstract class EffectQueue
|
33
|
{
|
34
|
protected byte mNumEffects;
|
35
|
protected float[] mUniforms;
|
36
|
protected long[] mCurrentDuration;
|
37
|
protected Effect[] mEffects;
|
38
|
protected int[] mName;
|
39
|
protected long mTime=0;
|
40
|
protected static int[] mMax = new int[EffectType.LENGTH];
|
41
|
protected Vector<EffectListener> mListeners =null;
|
42
|
protected int mNumListeners=0; // ==mListeners.length(), but we only create mListeners if the first one gets added
|
43
|
protected long mDistortedEffectsID;
|
44
|
|
45
|
private static boolean mCreated;
|
46
|
private int mIndex;
|
47
|
private int mNumUniforms;
|
48
|
|
49
|
static
|
50
|
{
|
51
|
onDestroy();
|
52
|
}
|
53
|
|
54
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
55
|
|
56
|
EffectQueue(long id, int numUniforms, int index)
|
57
|
{
|
58
|
mNumEffects = 0;
|
59
|
mDistortedEffectsID = id;
|
60
|
mIndex = index;
|
61
|
mNumUniforms = numUniforms;
|
62
|
|
63
|
int max = mMax[mIndex];
|
64
|
|
65
|
if( max>0 )
|
66
|
{
|
67
|
mUniforms = new float[mNumUniforms*max];
|
68
|
mCurrentDuration = new long[max];
|
69
|
mEffects = new Effect[max];
|
70
|
mName = new int[max];
|
71
|
}
|
72
|
|
73
|
mCreated = true;
|
74
|
}
|
75
|
|
76
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
77
|
|
78
|
@SuppressWarnings("unused")
|
79
|
int getNumEffects()
|
80
|
{
|
81
|
return mNumEffects;
|
82
|
}
|
83
|
|
84
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
85
|
// Only max Byte.MAX_VALUE concurrent effects per DistortedEffects object.
|
86
|
// If you want more, change type of the mNumEffects, mIDIndex and mFreeIndexes variables to shorts.
|
87
|
// (although probably this many uniforms will not fit in the shaders anyway!)
|
88
|
|
89
|
static boolean setMax(int index, int m)
|
90
|
{
|
91
|
if( (!mCreated && !Distorted.isInitialized()) || m<=mMax[index] )
|
92
|
{
|
93
|
if( m<0 ) m = 0;
|
94
|
else if( m>Byte.MAX_VALUE ) m = Byte.MAX_VALUE;
|
95
|
|
96
|
mMax[index] = m;
|
97
|
return true;
|
98
|
}
|
99
|
|
100
|
return false;
|
101
|
}
|
102
|
|
103
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
104
|
|
105
|
static int getMax(int index)
|
106
|
{
|
107
|
return mMax[index];
|
108
|
}
|
109
|
|
110
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
111
|
|
112
|
void registerForMessages(EffectListener el)
|
113
|
{
|
114
|
if( mListeners==null ) mListeners = new Vector<>(2,2);
|
115
|
|
116
|
if( !mListeners.contains(el) )
|
117
|
{
|
118
|
mListeners.add(el);
|
119
|
mNumListeners++;
|
120
|
}
|
121
|
}
|
122
|
|
123
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
124
|
|
125
|
void deregisterForMessages(EffectListener el)
|
126
|
{
|
127
|
if( mListeners.remove(el) )
|
128
|
{
|
129
|
mNumListeners--;
|
130
|
}
|
131
|
}
|
132
|
|
133
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
134
|
|
135
|
static void onDestroy()
|
136
|
{
|
137
|
EffectType.reset(mMax);
|
138
|
mCreated = false;
|
139
|
}
|
140
|
|
141
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
142
|
|
143
|
synchronized int removeByName(EffectName name)
|
144
|
{
|
145
|
int ret = 0;
|
146
|
|
147
|
for(int i=0; i<mNumEffects; i++)
|
148
|
{
|
149
|
if( mEffects[i].getName() == name )
|
150
|
{
|
151
|
remove(i);
|
152
|
i--;
|
153
|
ret++;
|
154
|
}
|
155
|
}
|
156
|
|
157
|
return ret;
|
158
|
}
|
159
|
|
160
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
161
|
|
162
|
synchronized int removeById(long id)
|
163
|
{
|
164
|
int ret = 0;
|
165
|
|
166
|
for(int i=0; i<mNumEffects; i++)
|
167
|
{
|
168
|
if( mEffects[i].getID() == id )
|
169
|
{
|
170
|
remove(i);
|
171
|
i--;
|
172
|
ret++;
|
173
|
}
|
174
|
}
|
175
|
|
176
|
return ret;
|
177
|
}
|
178
|
|
179
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
180
|
|
181
|
synchronized int removeEffect(Effect effect)
|
182
|
{
|
183
|
int ret = 0;
|
184
|
|
185
|
for(int i=0; i<mNumEffects; i++)
|
186
|
{
|
187
|
if( mEffects[i]==effect )
|
188
|
{
|
189
|
remove(i);
|
190
|
i--;
|
191
|
ret++;
|
192
|
}
|
193
|
}
|
194
|
|
195
|
return ret;
|
196
|
}
|
197
|
|
198
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
199
|
// we do want to notify Listeners if they called 'abortAll' themselves but don't want to notify
|
200
|
// them if it is the library itself which is releasing resources.
|
201
|
|
202
|
synchronized int abortAll(boolean notify)
|
203
|
{
|
204
|
int ret = mNumEffects;
|
205
|
|
206
|
for(int i=0; i<ret; i++ )
|
207
|
{
|
208
|
if( notify )
|
209
|
{
|
210
|
for(int j=0; j<mNumListeners; j++)
|
211
|
EffectMessageSender.newMessage( mListeners.elementAt(j), EffectMessage.EFFECT_REMOVED, mEffects[i].getID(), mDistortedEffectsID);
|
212
|
}
|
213
|
|
214
|
mEffects[i] = null;
|
215
|
}
|
216
|
|
217
|
mNumEffects= 0;
|
218
|
|
219
|
return ret;
|
220
|
}
|
221
|
|
222
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
223
|
// this assumes 0<=effect<mNumEffects
|
224
|
|
225
|
protected void remove(int effect)
|
226
|
{
|
227
|
mNumEffects--;
|
228
|
|
229
|
long removedID = mEffects[effect].getID();
|
230
|
|
231
|
for(int j=effect; j<mNumEffects; j++ )
|
232
|
{
|
233
|
mEffects[j] = mEffects[j+1];
|
234
|
mCurrentDuration[j] = mCurrentDuration[j+1];
|
235
|
mName[j] = mName[j+1];
|
236
|
|
237
|
for(int k=0; k<mNumUniforms; k++)
|
238
|
mUniforms[mNumUniforms*j+k] = mUniforms[mNumUniforms*(j+1)+k];
|
239
|
}
|
240
|
|
241
|
mEffects[mNumEffects] = null;
|
242
|
|
243
|
for(int i=0; i<mNumListeners; i++)
|
244
|
EffectMessageSender.newMessage( mListeners.elementAt(i), EffectMessage.EFFECT_REMOVED, removedID, mDistortedEffectsID);
|
245
|
}
|
246
|
|
247
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
248
|
|
249
|
boolean add(Effect effect)
|
250
|
{
|
251
|
if( mMax[mIndex]>mNumEffects )
|
252
|
{
|
253
|
mCurrentDuration[mNumEffects] = 0;
|
254
|
mEffects[mNumEffects] = effect;
|
255
|
mName[mNumEffects] = effect.getName().ordinal();
|
256
|
mNumEffects++;
|
257
|
return true;
|
258
|
}
|
259
|
|
260
|
return false;
|
261
|
}
|
262
|
}
|