Project

General

Profile

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

library / src / main / java / org / distorted / library / effect / Effect.java @ b82a9ac9

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2017 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.effect;
21

    
22
import java.lang.reflect.Method;
23

    
24
///////////////////////////////////////////////////////////////////////////////////////////////////
25
/**
26
 * Abstract Effect of any type.
27
 */
28
public abstract class Effect
29
  {
30
  private final static int MAX_UNITY_DIM = 4;
31
  private final static int NUM_EFFECTS = EffectName.LENGTH;
32

    
33
  private final long mID;
34
  private final EffectType mType;
35
  private final EffectName mName;
36
  private final int mDimension;
37
  private final int mRegionDim;
38
  private final int mCenterDim;
39

    
40
  private static long mNextID = 0;
41

    
42
  private final static float[] mUnity= new float[MAX_UNITY_DIM*NUM_EFFECTS];
43
  private final static int[]   mUnityDim = new int[NUM_EFFECTS];
44

    
45
  static boolean[] mEnabled = new boolean[NUM_EFFECTS];
46

    
47
  static
48
    {
49
    for(int i=0; i<NUM_EFFECTS; i++) mEnabled[i] = false;
50
    }
51

    
52
///////////////////////////////////////////////////////////////////////////////////////////////////
53

    
54
  Effect(EffectName name)
55
    {
56
    mName      = name;
57
    mType      = name.getType();
58
    mDimension = name.getEffectDimension();
59
    mCenterDim = name.getCenterDimension();
60
    mRegionDim = name.getRegionDimension();
61

    
62
    int n = name.ordinal();
63
    float[] u = name.getUnity();
64
    int l = u.length;
65

    
66
    for(int i=0; i<l; i++)
67
      {
68
      mUnity[n*MAX_UNITY_DIM+i] = u[i];
69
      }
70

    
71
    mUnityDim[n] = l;
72

    
73
    mID = ((mNextID++)<<EffectType.LENGTH) + mType.ordinal();
74
    }
75

    
76
///////////////////////////////////////////////////////////////////////////////////////////////////
77
/**
78
 * Only for use by the library itself.
79
 *
80
 * @y.exclude
81
 */
82
  public static void onDestroy()
83
    {
84
    mNextID = 0;
85

    
86
    for(int i=0; i<NUM_EFFECTS; i++) mEnabled[i] = false;
87

    
88
    MatrixEffect.destroyStatics();
89
    VertexEffect.destroyStatics();
90
    FragmentEffect.destroyStatics();
91
    PostprocessEffect.destroyStatics();
92
    }
93

    
94
///////////////////////////////////////////////////////////////////////////////////////////////////
95
/**
96
 * Only for use by the library itself.
97
 *
98
 * @y.exclude
99
 */
100
  public abstract boolean compute(float[] uniforms, int index, long currentDuration, long step );
101

    
102
///////////////////////////////////////////////////////////////////////////////////////////////////
103
// PUBLIC API
104
///////////////////////////////////////////////////////////////////////////////////////////////////
105
/**
106
 * Do the set of Uniforms written in buffer[index], buffer[index+1], etc represent a Unity, i.e a
107
 * null Effect?
108
 */
109
  public boolean isUnity(float[] buffer, int index)
110
    {
111
    int name = mName.ordinal();
112

    
113
    switch(mUnityDim[name])
114
      {
115
      case 0: return true;
116
      case 1: return buffer[index  ]==mUnity[MAX_UNITY_DIM*name  ];
117
      case 2: return buffer[index  ]==mUnity[MAX_UNITY_DIM*name  ] &&
118
                     buffer[index+1]==mUnity[MAX_UNITY_DIM*name+1];
119
      case 3: return buffer[index  ]==mUnity[MAX_UNITY_DIM*name  ] &&
120
                     buffer[index+1]==mUnity[MAX_UNITY_DIM*name+1] &&
121
                     buffer[index+2]==mUnity[MAX_UNITY_DIM*name+2];
122
      case 4: return buffer[index  ]==mUnity[MAX_UNITY_DIM*name  ] &&
123
                     buffer[index+1]==mUnity[MAX_UNITY_DIM*name+1] &&
124
                     buffer[index+2]==mUnity[MAX_UNITY_DIM*name+2] &&
125
                     buffer[index+3]==mUnity[MAX_UNITY_DIM*name+3];
126
      }
127

    
128
    return false;
129
    }
130

    
131
///////////////////////////////////////////////////////////////////////////////////////////////////
132
// this will enable() all Fragment Effects twice (once for smooth variant, once for non-smooth)
133
// but this shouldn't matter.
134
/**
135
 * Enable all effects of a given type.
136
 *
137
 * @param type EffectType to enable.
138
 */
139
  public static void enableEffects(EffectType type)
140
    {
141
    Method method=null;
142

    
143
    for(EffectName name: EffectName.values())
144
      {
145
      if( name.getType() == type )
146
        {
147
        Class<? extends Effect> cls = name.getEffectClass();
148

    
149
        try
150
          {
151
          method = cls.getMethod("enable");
152
          }
153
        catch(NoSuchMethodException ex)
154
          {
155
          android.util.Log.e("Effect", "exception getting method: "+ex.getMessage());
156
          }
157

    
158
        try
159
          {
160
          method.invoke(null);
161
          }
162
        catch(Exception ex)
163
          {
164
          android.util.Log.e("Effect", "exception invoking method: "+ex.getMessage());
165
          }
166
        }
167
      }
168
    }
169

    
170
///////////////////////////////////////////////////////////////////////////////////////////////////
171
/**
172
 * Return the EffectType enum corresponding to this Effect.
173
 *
174
 * @see EffectType
175
 */
176
  public EffectType getType()
177
    {
178
    return mType;
179
    }
180

    
181
///////////////////////////////////////////////////////////////////////////////////////////////////
182
/**
183
 * Return the EffectName enum corresponding to this Effect.
184
 *
185
 * @see EffectName
186
 */
187
  public EffectName getName()
188
    {
189
    return mName;
190
    }
191

    
192
///////////////////////////////////////////////////////////////////////////////////////////////////
193
/**
194
 * Return the unique ID of this Effect.
195
 */
196
  public long getID()
197
    {
198
    return mID;
199
    }
200

    
201
///////////////////////////////////////////////////////////////////////////////////////////////////
202
/**
203
 * Return a printable name of this Effect.
204
 */
205
  public String getString()
206
    {
207
    return mName.name();
208
    }
209

    
210
///////////////////////////////////////////////////////////////////////////////////////////////////
211
/**
212
 * Return the dimension of the Center supported by this effect (0- no center supported at all).
213
 */
214
  public int getCenterDimension()
215
    {
216
    return mCenterDim;
217
    }
218

    
219
///////////////////////////////////////////////////////////////////////////////////////////////////
220
/**
221
 * Return the dimension of the Region supported by this effect (0- no region supported at all).
222
 */
223
  public int getRegionDimension()
224
    {
225
    return mRegionDim;
226
    }
227

    
228
///////////////////////////////////////////////////////////////////////////////////////////////////
229
/**
230
 * Return the number of Uniforms needed to describe this effect.
231
 */
232
  public int getEffectDimension()
233
    {
234
    return mDimension;
235
    }
236
  }
(1-1/26)