Project

General

Profile

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

library / src / main / java / org / distorted / library / main / EffectQueue.java @ 15aa7d94

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 removeByType(Effect effect)
142
    {
143
    int ret = 0;
144

    
145
    for(int i=0; i<mNumEffects; i++)
146
      {
147
      if( mEffects[i]==effect )
148
        {
149
        remove(i);
150
        i--;
151
        ret++;
152
        }
153
      }
154
   
155
    return ret;
156
    }
157

    
158
///////////////////////////////////////////////////////////////////////////////////////////////////
159
// we do want to notify Listeners if they called 'abortAll' themselves but don't want to notify
160
// them if it is the library itself which is releasing resources.
161

    
162
  synchronized int abortAll(boolean notify)
163
    {
164
    int ret = mNumEffects;
165

    
166
    for(int i=0; i<ret; i++ )
167
      {
168
      if( notify )
169
        {
170
        for(int j=0; j<mNumListeners; j++)
171
          EffectMessageSender.newMessage( mListeners.elementAt(j), EffectMessage.EFFECT_REMOVED, mEffects[i].getID(), mID);
172
        }
173

    
174
      mEffects[i] = null;
175
      }
176

    
177
    mNumEffects= 0;
178

    
179
    return ret;
180
    }
181

    
182
///////////////////////////////////////////////////////////////////////////////////////////////////
183
// this assumes 0<=effect<mNumEffects
184
  
185
  protected void remove(int effect)
186
    {
187
    mNumEffects--;     
188

    
189
    long removedID = mEffects[effect].getID();
190

    
191
    for(int j=effect; j<mNumEffects; j++ ) 
192
      {
193
      mEffects[j]         = mEffects[j+1];
194
      mCurrentDuration[j] = mCurrentDuration[j+1];
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];
199
      }
200
   
201
    mEffects[mNumEffects] = null;
202

    
203
    for(int i=0; i<mNumListeners; i++) 
204
      EffectMessageSender.newMessage( mListeners.elementAt(i), EffectMessage.EFFECT_REMOVED, removedID, mID);
205
    }
206
  
207
///////////////////////////////////////////////////////////////////////////////////////////////////
208
  
209
  public void add(Effect effect)
210
    {
211
    if( mMax[mIndex]>mNumEffects )
212
      {
213
      mCurrentDuration[mNumEffects] = 0;
214
      mEffects[mNumEffects] = effect;
215
      mName[mNumEffects] = effect.getName();
216
      mNumEffects++;
217
      }
218
    }
219
  }
(17-17/24)