Project

General

Profile

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

library / src / main / java / org / distorted / library / main / Distorted.java @ 1dfc9074

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 android.app.ActivityManager;
23
import android.content.Context;
24
import android.content.pm.ConfigurationInfo;
25
import android.content.res.Resources;
26

    
27
import org.distorted.library.effect.Effect;
28
import org.distorted.library.effect.PostprocessEffectBlur;
29
import org.distorted.library.program.*;
30

    
31
///////////////////////////////////////////////////////////////////////////////////////////////////
32
/**
33
 * A singleton class used to control various global settings.
34
 */
35
public class Distorted 
36
  {
37
  static int GLSL;
38
  static String GLSL_VERSION;
39
  /**
40
   * When creating an instance of a DistortedTexture from another instance, clone the Bitmap that's
41
   * backing up our DistortedTexture.
42
   * <p>
43
   * This way we can have two DistortedTextures, both backed up by the same Bitmap, to which we can
44
   * apply different effects. Used in the copy constructor.
45
   */
46
  public static final int CLONE_SURFACE = 0x1;
47
  /**
48
   * When creating an instance of a DistortedEffects from another instance, clone the Matrix Effects.
49
   * <p>
50
   * This way we can have two different DistortedEffects sharing the MATRIX queue.
51
   */
52
  public static final int CLONE_MATRIX = 0x2;
53
  /**
54
   * When creating an instance of a DistortedEffects from another instance, clone the Vertex Effects.
55
   * <p>
56
   * This way we can have two different DistortedEffects sharing the VERTEX queue.
57
   */
58
  public static final int CLONE_VERTEX  = 0x4;
59
  /**
60
   * When creating an instance of a DistortedEffects from another instance, clone the Fragment Effects.
61
   * <p>
62
   * This way we can have two different DistortedEffects sharing the FRAGMENT queue.
63
   */
64
  public static final int CLONE_FRAGMENT= 0x8;
65
   /**
66
   * When creating an instance of a DistortedEffects from another instance, clone the PostProcess Effects.
67
   * <p>
68
   * This way we can have two different DistortedEffects sharing the POSTPROCESS queue.
69
   */
70
  public static final int CLONE_POSTPROCESS= 0x10;
71
  /**
72
   * When creating an instance of a DistortedNode from another instance, clone the children Nodes.
73
   * <p>
74
   * This is mainly useful for creating many similar sub-trees and rendering then at different places
75
   * on the screen with (optionally) different Effects.
76
   */
77
  public static final int CLONE_CHILDREN= 0x20;
78

    
79
  private static boolean mInitialized=false;
80

    
81
///////////////////////////////////////////////////////////////////////////////////////////////////
82
// private: hide this from Javadoc
83

    
84
  private Distorted()
85
    {
86

    
87
    }
88

    
89
///////////////////////////////////////////////////////////////////////////////////////////////////
90
/**
91
 * Have we called onCreate yet, ie have we initialized the library?
92
 * @return <code>true</code> if the library is initialized and ready for action.
93
 */
94
  public static boolean isInitialized()
95
    {
96
    return mInitialized;
97
    }
98

    
99
///////////////////////////////////////////////////////////////////////////////////////////////////
100
/**
101
 * When OpenGL context gets created, you need to call this method so that the library can initialise its internal data structures.
102
 * I.e. best called from GLSurfaceView.onCreate().
103
 * <p>
104
 * Needs to be called from a thread holding the OpenGL context.
105
 *   
106
 * @param context Context of the App using the library - used to open up Resources and read Shader code.
107
 * @throws FragmentCompilationException Fragment Shader failed to compile
108
 * @throws VertexCompilationException   Vertex Shader failed to compile
109
 * @throws VertexUniformsException      Too many uniforms in the Vertex Shader
110
 * @throws FragmentUniformsException    Too many uniforms in the Fragment Shader
111
 * @throws LinkingException             Shader failed to link
112
 */
113
  public static void onCreate(final Context context)
114
  throws FragmentCompilationException,VertexCompilationException,VertexUniformsException,FragmentUniformsException,LinkingException
115
    {
116
    final ActivityManager activityManager     = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
117
    final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
118
    android.util.Log.e("DISTORTED", "Using OpenGL ES "+configurationInfo.getGlEsVersion());
119

    
120
    GLSL = ( (configurationInfo.reqGlEsVersion>>16)>=3 ? 300 : 100 );
121
    GLSL_VERSION= (GLSL==100 ? "#version 100\n" : "#version 300 es\n");
122

    
123
    final Resources resources = context.getResources();
124
    DistortedEffects.createProgram(resources);
125
    PostprocessEffectBlur.createProgram(resources);
126
    EffectMessageSender.startSending();
127

    
128
    mInitialized = true;
129
    }
130

    
131
///////////////////////////////////////////////////////////////////////////////////////////////////
132
/**
133
 * Call this so that the Library can release its internal data structures.
134
 * Must be called from Activity.onPause().
135
 */
136
  public static void onPause()
137
    {
138
    DistortedObject.onPause();
139
    DistortedNode.onPause();
140
    }
141

    
142
///////////////////////////////////////////////////////////////////////////////////////////////////
143
/**
144
 * Call this so that the Library can release its internal data structures.
145
 * Must be called from Activity.onDestroy(). 
146
 */
147
  public static void onDestroy()
148
    {
149
    DistortedObject.onDestroy();
150
    DistortedNode.onDestroy();
151
    DistortedEffects.onDestroy();
152
    DistortedMaster.onDestroy();
153
    EffectQueue.onDestroy();
154
    Effect.onDestroy();
155
    EffectMessageSender.stopSending();
156

    
157
    mInitialized = false;
158
    }
159

    
160
///////////////////////////////////////////////////////////////////////////////////////////////////
161
/**
162
 * Return 2 or 3 depending if we have OpenGL Es 2.0 or 3.x context created.
163
 */
164
  public static int getGlVersion()
165
    {
166
    return GLSL == 300 ? 3:2;
167
    }
168
  }
(1-1/23)