Project

General

Profile

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

library / src / main / java / org / distorted / library / main / EffectQueue.java @ da9b3f07

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 mID;
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
    mID          = 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 removeEffect(Effect effect)
163
    {
164
    int ret = 0;
165

    
166
    for(int i=0; i<mNumEffects; i++)
167
      {
168
      if( mEffects[i]==effect )
169
        {
170
        remove(i);
171
        i--;
172
        ret++;
173
        }
174
      }
175
   
176
    return ret;
177
    }
178

    
179
///////////////////////////////////////////////////////////////////////////////////////////////////
180
// we do want to notify Listeners if they called 'abortAll' themselves but don't want to notify
181
// them if it is the library itself which is releasing resources.
182

    
183
  synchronized int abortAll(boolean notify)
184
    {
185
    int ret = mNumEffects;
186

    
187
    for(int i=0; i<ret; i++ )
188
      {
189
      if( notify )
190
        {
191
        for(int j=0; j<mNumListeners; j++)
192
          EffectMessageSender.newMessage( mListeners.elementAt(j), EffectMessage.EFFECT_REMOVED, mEffects[i].getID(), mID);
193
        }
194

    
195
      mEffects[i] = null;
196
      }
197

    
198
    mNumEffects= 0;
199

    
200
    return ret;
201
    }
202

    
203
///////////////////////////////////////////////////////////////////////////////////////////////////
204
// this assumes 0<=effect<mNumEffects
205
  
206
  protected void remove(int effect)
207
    {
208
    mNumEffects--;     
209

    
210
    long removedID = mEffects[effect].getID();
211

    
212
    for(int j=effect; j<mNumEffects; j++ ) 
213
      {
214
      mEffects[j]         = mEffects[j+1];
215
      mCurrentDuration[j] = mCurrentDuration[j+1];
216
      mName[j]            = mName[j+1];
217

    
218
      for(int k=0; k<mNumUniforms; k++)
219
        mUniforms[mNumUniforms*j+k] = mUniforms[mNumUniforms*(j+1)+k];
220
      }
221
   
222
    mEffects[mNumEffects] = null;
223

    
224
    for(int i=0; i<mNumListeners; i++) 
225
      EffectMessageSender.newMessage( mListeners.elementAt(i), EffectMessage.EFFECT_REMOVED, removedID, mID);
226
    }
227
  
228
///////////////////////////////////////////////////////////////////////////////////////////////////
229
  
230
  public void add(Effect effect)
231
    {
232
    if( mMax[mIndex]>mNumEffects )
233
      {
234
      mCurrentDuration[mNumEffects] = 0;
235
      mEffects[mNumEffects] = effect;
236
      mName[mNumEffects] = effect.getName().ordinal();
237
      mNumEffects++;
238
      }
239
    }
240
  }
(17-17/24)