Project

General

Profile

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

library / src / main / java / org / distorted / library / main / EffectQueue.java @ 6bb59aad

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.message.EffectListener;
24
import org.distorted.library.message.EffectMessage;
25

    
26
import java.util.Vector;
27

    
28
///////////////////////////////////////////////////////////////////////////////////////////////////
29

    
30
abstract class EffectQueue
31
  {
32
  protected byte mNumEffects;
33
  protected float[] mUniforms;
34
  protected long[] mCurrentDuration;
35
  protected Effect[] mEffects;
36
  protected int[] mName;
37
  protected long mTime=0;
38
  protected static int[] mMax = new int[Effect.LENGTH];
39
  protected Vector<EffectListener> mListeners =null;
40
  protected int mNumListeners=0;  // ==mListeners.length(), but we only create mListeners if the first one gets added
41
  protected long mID;
42

    
43
  private static boolean mCreated;
44
  private int mIndex;
45
  private int mNumUniforms;
46

    
47
  static
48
    {
49
    onDestroy();
50
    }
51
  
52
///////////////////////////////////////////////////////////////////////////////////////////////////
53
   
54
  EffectQueue(long id, int numUniforms, int index)
55
    {
56
    mNumEffects  = 0;
57
    mID          = id;
58
    mIndex       = index;
59
    mNumUniforms = numUniforms;
60

    
61
    int max = mMax[mIndex];
62

    
63
    if( max>0 )
64
      {
65
      mUniforms        = new float[mNumUniforms*max];
66
      mCurrentDuration = new long[max];
67
      mEffects         = new Effect[max];
68
      mName            = new int[max];
69
      }
70
   
71
    mCreated = true;  
72
    }
73

    
74
///////////////////////////////////////////////////////////////////////////////////////////////////
75

    
76
  @SuppressWarnings("unused")
77
  int getNumEffects()
78
    {
79
    return mNumEffects;  
80
    }
81

    
82
///////////////////////////////////////////////////////////////////////////////////////////////////
83
// Only max Byte.MAX_VALUE concurrent effects per DistortedEffects object.
84
// If you want more, change type of the mNumEffects, mIDIndex and mFreeIndexes variables to shorts.
85
// (although probably this many uniforms will not fit in the shaders anyway!)
86

    
87
  static boolean setMax(int index, int m)
88
    {
89
    if( (!mCreated && !Distorted.isInitialized()) || m<=mMax[index] )
90
      {
91
      if( m<0              ) m = 0;
92
      else if( m>Byte.MAX_VALUE ) m = Byte.MAX_VALUE;
93

    
94
      mMax[index] = m;
95
      return true;
96
      }
97

    
98
    return false;
99
    }
100

    
101
///////////////////////////////////////////////////////////////////////////////////////////////////
102

    
103
  static int getMax(int index)
104
    {
105
    return mMax[index];
106
    }
107

    
108
///////////////////////////////////////////////////////////////////////////////////////////////////
109

    
110
  void registerForMessages(EffectListener el)
111
    {
112
    if( mListeners==null ) mListeners = new Vector<>(2,2);
113

    
114
    if( !mListeners.contains(el) )
115
      {
116
      mListeners.add(el);
117
      mNumListeners++;
118
      }
119
    }
120
 
121
///////////////////////////////////////////////////////////////////////////////////////////////////
122

    
123
  void deregisterForMessages(EffectListener el)
124
    {
125
    if( mListeners.remove(el) )
126
      {
127
      mNumListeners--;
128
      }
129
    }
130

    
131
///////////////////////////////////////////////////////////////////////////////////////////////////
132

    
133
  static void onDestroy()
134
    {
135
    Effect.reset(mMax);
136
    mCreated = false;  
137
    }
138

    
139
///////////////////////////////////////////////////////////////////////////////////////////////////
140

    
141
  synchronized int removeByName(int name)
142
    {
143
    int ret = 0;
144

    
145
    for(int i=0; i<mNumEffects; i++)
146
      {
147
      if( mEffects[i].getName() == name )
148
        {
149
        remove(i);
150
        i--;
151
        ret++;
152
        }
153
      }
154

    
155
    return ret;
156
    }
157

    
158
///////////////////////////////////////////////////////////////////////////////////////////////////
159

    
160
  synchronized int removeEffect(Effect effect)
161
    {
162
    int ret = 0;
163

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

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

    
181
  synchronized int abortAll(boolean notify)
182
    {
183
    int ret = mNumEffects;
184

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

    
193
      mEffects[i] = null;
194
      }
195

    
196
    mNumEffects= 0;
197

    
198
    return ret;
199
    }
200

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

    
208
    long removedID = mEffects[effect].getID();
209

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

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

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