Project

General

Profile

Download (11.3 KB) Statistics
| Branch: | Revision:

library / src / main / java / org / distorted / library / EffectQueue.java @ 0df17fad

1 d333eb6b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 6a06a912 Leszek Koltunski
package org.distorted.library;
21
22 e458a4ba Leszek Koltunski
import org.distorted.library.message.EffectListener;
23
import org.distorted.library.message.EffectMessage;
24 568b29d8 Leszek Koltunski
import org.distorted.library.type.Dynamic;
25 a4835695 Leszek Koltunski
26 6a06a912 Leszek Koltunski
import java.util.Vector;
27
28
///////////////////////////////////////////////////////////////////////////////////////////////////
29
30 d07f2950 Leszek Koltunski
abstract class EffectQueue
31 6a06a912 Leszek Koltunski
  {
32
  protected byte mNumEffects;   // number of effects at the moment
33
  protected long mTotalEffects; // total number of effects ever created
34
  
35
  protected int[] mType;
36
  protected float[] mUniforms;
37 d425545a Leszek Koltunski
  protected Dynamic[][] mInter;  // center of the effect
38 6a06a912 Leszek Koltunski
  protected long[] mCurrentDuration;
39
  protected byte[] mFreeIndexes;
40
  protected byte[] mIDIndex;
41
  protected long[] mID;
42
  
43
  protected long mTime=0;
44
  protected float mObjHalfX, mObjHalfY, mObjHalfZ;
45
  
46 1e438fc7 Leszek Koltunski
  protected static int[] mMax = new int[EffectTypes.LENGTH];
47 6a06a912 Leszek Koltunski
  protected int mMaxIndex;
48 71887484 Leszek Koltunski
49 6a06a912 Leszek Koltunski
  protected Vector<EffectListener> mListeners =null;
50
  protected int mNumListeners=0;  // ==mListeners.length(), but we only create mListeners if the first one gets added
51
  protected long mBitmapID;
52 71887484 Leszek Koltunski
53
  private static boolean mCreated;
54
55 6a06a912 Leszek Koltunski
  static
56
    {
57 1e438fc7 Leszek Koltunski
    reset();
58 6a06a912 Leszek Koltunski
    }
59
  
60
///////////////////////////////////////////////////////////////////////////////////////////////////
61
   
62 d07f2950 Leszek Koltunski
  public EffectQueue(DistortedObject obj, int numUniforms, int index)
63 6a06a912 Leszek Koltunski
    {
64
    mNumEffects   = 0;
65
    mTotalEffects = 0;
66
    mMaxIndex     = index;
67
68 1e438fc7 Leszek Koltunski
    if( obj!=null )
69
      {
70
      mObjHalfX = obj.getWidth() / 2.0f;
71
      mObjHalfY = obj.getHeight() / 2.0f;
72
      mObjHalfZ = obj.getDepth() / 2.0f;
73
74
      mBitmapID = obj.getID();
75
      }
76
77 6a06a912 Leszek Koltunski
    if( mMax[mMaxIndex]>0 )
78
      {
79
      mType            = new int[mMax[mMaxIndex]];
80
      mUniforms        = new float[numUniforms*mMax[mMaxIndex]];
81 d425545a Leszek Koltunski
      mInter           = new Dynamic[3][mMax[mMaxIndex]];
82 6a06a912 Leszek Koltunski
      mCurrentDuration = new long[mMax[mMaxIndex]];
83
      mID              = new long[mMax[mMaxIndex]];
84
      mIDIndex         = new byte[mMax[mMaxIndex]];
85
      mFreeIndexes     = new byte[mMax[mMaxIndex]];
86
     
87
      for(byte i=0; i<mMax[mMaxIndex]; i++) mFreeIndexes[i] = i;
88
      }
89
   
90
    mCreated = true;  
91
    }
92
93
///////////////////////////////////////////////////////////////////////////////////////////////////
94
95
  int getNumEffects()
96
    {
97
    return mNumEffects;  
98
    }
99
100 71887484 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
101
// Only max Byte.MAX_VALUE concurrent effects per DistortedObject.
102
// If you want more, change type of the mNumEffects, mIDIndex and mFreeIndexes variables to shorts.
103
104
  static boolean setMax(int index, int m)
105
    {
106
    if( (mCreated==false && !Distorted.isInitialized()) || m<=mMax[index] )
107
      {
108
      if( m<0              ) m = 0;
109
      else if( m>Byte.MAX_VALUE ) m = Byte.MAX_VALUE;
110
111
      mMax[index] = m;
112
      return true;
113
      }
114
115
    return false;
116
    }
117
118
///////////////////////////////////////////////////////////////////////////////////////////////////
119
120
  static int getMax(int index)
121
    {
122
    return mMax[index];
123
    }
124
125 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
126
127
  void addListener(EffectListener el)
128
    {
129 b3618cb5 Leszek Koltunski
    if( mListeners==null ) mListeners = new Vector<>(2,2);
130 6a06a912 Leszek Koltunski
   
131
    mListeners.add(el);
132
    mNumListeners++;
133
    }
134
 
135
///////////////////////////////////////////////////////////////////////////////////////////////////
136
137
  void removeListener(EffectListener el)
138
    {
139
    if( mNumListeners>0 )  
140
      {
141
      mListeners.remove(el);
142
      mNumListeners--;
143
      }
144
    }
145
146
///////////////////////////////////////////////////////////////////////////////////////////////////
147
148
  static void reset()
149
    {
150 1e438fc7 Leszek Koltunski
    EffectTypes.reset(mMax);
151 6a06a912 Leszek Koltunski
    mCreated = false;  
152
    }
153
 
154
///////////////////////////////////////////////////////////////////////////////////////////////////
155
156 476bbc81 Leszek Koltunski
  synchronized int removeByID(long id)
157 6a06a912 Leszek Koltunski
    {
158
    int i = getEffectIndex(id);
159
   
160
    if( i>=0 ) 
161
      {
162
      remove(i);
163 476bbc81 Leszek Koltunski
      return 1;
164 6a06a912 Leszek Koltunski
      }
165
   
166 476bbc81 Leszek Koltunski
    return 0;
167 6a06a912 Leszek Koltunski
    }
168
169
///////////////////////////////////////////////////////////////////////////////////////////////////
170
171 476bbc81 Leszek Koltunski
  synchronized int removeByType(EffectNames effect)
172 6a06a912 Leszek Koltunski
    {
173 476bbc81 Leszek Koltunski
    int ret = 0;
174 6a06a912 Leszek Koltunski
    int ord = effect.ordinal();  
175
     
176
    for(int i=0; i<mNumEffects; i++)
177
      {
178
      if( mType[i]==ord )
179
        {
180
        remove(i);
181 476bbc81 Leszek Koltunski
        i--;
182
        ret++;
183 6a06a912 Leszek Koltunski
        }
184
      }
185
   
186
    return ret;
187
    }
188
  
189
///////////////////////////////////////////////////////////////////////////////////////////////////
190
  
191 2e18813f Leszek Koltunski
  private synchronized int getEffectIndex(long id)
192 6a06a912 Leszek Koltunski
    {
193
    int index = mIDIndex[(int)(id%mMax[mMaxIndex])];
194
    return (index<mNumEffects && mID[index]==id ? index : -1);
195
    }
196
197
///////////////////////////////////////////////////////////////////////////////////////////////////
198 0df17fad Leszek Koltunski
// we do want to notify Listeners if they called 'abortAll' themselves but don't want to notify
199
// them if it is the library itself which is releasing resources.
200
201
  synchronized int abortAll(boolean notify)
202 6a06a912 Leszek Koltunski
    {
203 d07f2950 Leszek Koltunski
    int ret = mNumEffects;
204 3a35681a Leszek Koltunski
    long removedID;
205
    int removedType;
206 d07f2950 Leszek Koltunski
207
    for(int i=0; i<ret; i++ )
208 6a06a912 Leszek Koltunski
      {
209 d425545a Leszek Koltunski
      mInter[0][i] = null;
210
      mInter[1][i] = null;
211
      mInter[2][i] = null;
212 3a35681a Leszek Koltunski
213 0df17fad Leszek Koltunski
      if( notify )
214
        {
215
        removedID = mID[i];
216
        removedType= mType[i];
217
218
        for(int j=0; j<mNumListeners; j++)
219
          EffectMessageSender.newMessage( mListeners.elementAt(j),
220
                                          EffectMessage.EFFECT_REMOVED,
221
                                          (removedID<<EffectTypes.LENGTH)+EffectNames.getType(removedType).type,
222
                                          removedType,
223
                                          mBitmapID,
224
                                          null);
225
        }
226 d07f2950 Leszek Koltunski
      }
227
228
    mNumEffects= 0;
229
230
    return ret;
231 6a06a912 Leszek Koltunski
    }
232
233
///////////////////////////////////////////////////////////////////////////////////////////////////
234
// this assumes 0<=effect<mNumEffects
235
  
236
  protected void remove(int effect)
237
    {
238
    mNumEffects--;     
239
    
240
    byte removedIndex = (byte)(mID[effect]%mMax[mMaxIndex]);
241
    byte removedPosition = mIDIndex[removedIndex];
242
    mFreeIndexes[mNumEffects] = removedIndex;
243
    
244
    long removedID = mID[effect];
245
    int removedType= mType[effect];
246
    
247
    for(int j=0; j<mMax[mMaxIndex]; j++)
248
      {
249
      if( mIDIndex[j] > removedPosition ) mIDIndex[j]--; 
250
      }
251
         
252
    for(int j=effect; j<mNumEffects; j++ ) 
253
      {
254
      mType[j]            = mType[j+1];
255 a595ee16 Leszek Koltunski
      mInter[0][j]        = mInter[0][j+1];
256
      mInter[1][j]        = mInter[1][j+1];
257
      mInter[2][j]        = mInter[2][j+1];
258 6a06a912 Leszek Koltunski
      mCurrentDuration[j] = mCurrentDuration[j+1];
259
      mID[j]              = mID[j+1];
260
    
261
      moveEffect(j);
262
      }
263
   
264 d425545a Leszek Koltunski
    mInter[0][mNumEffects] = null;
265
    mInter[1][mNumEffects] = null;
266
    mInter[2][mNumEffects] = null;
267
268 6a06a912 Leszek Koltunski
    for(int i=0; i<mNumListeners; i++) 
269
      EffectMessageSender.newMessage( mListeners.elementAt(i),
270 e458a4ba Leszek Koltunski
                                      EffectMessage.EFFECT_REMOVED,
271 1e438fc7 Leszek Koltunski
                                      (removedID<<EffectTypes.LENGTH)+EffectNames.getType(removedType).type,
272 6a06a912 Leszek Koltunski
                                      removedType,
273 c6e1c219 Leszek Koltunski
                                      mBitmapID,
274
                                      null);
275 6a06a912 Leszek Koltunski
    }
276
  
277
///////////////////////////////////////////////////////////////////////////////////////////////////
278
  
279
  protected long addBase(EffectNames eln)
280
    {    
281
    mType[mNumEffects]  = eln.ordinal();  
282
    mCurrentDuration[mNumEffects] = 0;
283
    
284
    int index = mFreeIndexes[mNumEffects];
285
    long id = mTotalEffects*mMax[mMaxIndex] + index;
286
    mID[mNumEffects] = id;
287
    mIDIndex[index] = mNumEffects;  
288
   
289
    mNumEffects++; 
290
    mTotalEffects++;
291
   
292 1e438fc7 Leszek Koltunski
    return (id<<EffectTypes.LENGTH)+eln.getType().type;
293 6a06a912 Leszek Koltunski
    }
294
    
295
///////////////////////////////////////////////////////////////////////////////////////////////////
296
// used only for debugging
297
  
298
  protected String printEffects(int max)
299
    {
300
    long[] indexes = new long[mMax[mMaxIndex]];
301
   
302
    for(int g=0; g<mMax[mMaxIndex]; g++)
303
      {
304
      indexes[g] = -1;  
305
      }
306
   
307 b329f352 Leszek Koltunski
    String ret="(";
308 6a06a912 Leszek Koltunski
    int f;
309
   
310
    for(int g=0; g<max; g++) 
311
      {
312
      f = getEffectIndex(g);
313
      if( f>=0 ) indexes[f] = g;
314
      }
315
   
316
    for(int g=0; g<mMax[mMaxIndex]; g++)
317
      {
318
      ret += (g>0 ? ",":"")+(indexes[g]>=0 ? indexes[g] : " ");   
319
      }
320
   
321
    ret += ")";
322
   
323
    return ret;
324
    }
325
326
///////////////////////////////////////////////////////////////////////////////////////////////////
327
// Only used for debugging
328
  
329
  protected boolean printByID(long id)
330
    {
331
    int index = getEffectIndex(id);
332
   
333
    if( index>=0 ) 
334
      {
335 d425545a Leszek Koltunski
      boolean inter0 = mInter[0][index]==null;
336
      boolean inter1 = mInter[1][index]==null;
337
      boolean inter2 = mInter[2][index]==null;
338
339
      android.util.Log.e("EffectQueue", "numEffects="+mNumEffects+" effect id="+id+" index="+index+
340
                         " duration="+mCurrentDuration[index]+" inter[0] null="+inter0+" inter[1] null="+inter1+" inter[2] null="+inter2);
341 6a06a912 Leszek Koltunski
      
342 d425545a Leszek Koltunski
      if( inter0==false )
343 6a06a912 Leszek Koltunski
        {
344 d425545a Leszek Koltunski
        android.util.Log.e("EffectQueue","inter[0]: "+mInter[0][index].print());
345 6a06a912 Leszek Koltunski
        }
346 d425545a Leszek Koltunski
      if( inter1==false )
347 6a06a912 Leszek Koltunski
        {
348 d425545a Leszek Koltunski
        android.util.Log.e("EffectQueue","inter[1]: "+mInter[1][index].print());
349 6a06a912 Leszek Koltunski
        }
350 d425545a Leszek Koltunski
      if( inter2==false )
351
        {
352
        android.util.Log.e("EffectQueue","inter[2]: "+mInter[2][index].print());
353
        }
354
355 6a06a912 Leszek Koltunski
      return true;
356
      }
357
   
358 d07f2950 Leszek Koltunski
    android.util.Log.e("EffectQueue", "effect id="+id+" not found");
359 6a06a912 Leszek Koltunski
360
    return false;  
361
    }
362
 
363
///////////////////////////////////////////////////////////////////////////////////////////////////
364
365
  abstract void moveEffect(int index);
366
  }
367
///////////////////////////////////////////////////////////////////////////////////////////////////