Project

General

Profile

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

library / src / main / java / org / distorted / library / Distorted.java @ 05ecc6fe

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;
21

    
22
import android.app.ActivityManager;
23
import android.content.Context;
24
import android.content.pm.ConfigurationInfo;
25
import android.content.res.Resources;
26
import org.distorted.library.program.*;
27

    
28
///////////////////////////////////////////////////////////////////////////////////////////////////
29
/**
30
 * A singleton class used to control various global settings.
31
 */
32
public class Distorted 
33
  {
34
  static int GLSL;
35
  static String GLSL_VERSION;
36

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

    
77
  private static boolean mInitialized=false;
78

    
79
///////////////////////////////////////////////////////////////////////////////////////////////////
80
// private: hide this from Javadoc
81

    
82
  private Distorted()
83
    {
84
    
85
    }
86

    
87
///////////////////////////////////////////////////////////////////////////////////////////////////
88

    
89
  static boolean isInitialized()
90
    {
91
    return mInitialized;
92
    }
93

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

    
115
    GLSL = ( (configurationInfo.reqGlEsVersion>>16)>=3 ? 300 : 100 );
116
    GLSL_VERSION= (GLSL==100 ? "#version 100\n" : "#version 300 es\n");
117

    
118
    final Resources resources = context.getResources();
119
    DistortedEffects.createProgram(resources);
120
    EffectQueuePostprocess.createProgram(resources);
121
    EffectMessageSender.startSending();
122

    
123
    mInitialized = true;
124
    }
125

    
126
///////////////////////////////////////////////////////////////////////////////////////////////////
127
/**
128
 * Call this so that the Library can release its internal data structures.
129
 * Must be called from Activity.onPause().
130
 */
131
  public static void onPause()
132
    {
133
    DistortedSurface.onPause();
134
    }
135

    
136
///////////////////////////////////////////////////////////////////////////////////////////////////
137
/**
138
 * Call this so that the Library can release its internal data structures.
139
 * Must be called from Activity.onDestroy(). 
140
 */
141
  public static void onDestroy()
142
    {
143
    DistortedSurface.onDestroy();
144
    DistortedNode.onDestroy();
145
    EffectQueue.onDestroy();
146
    DistortedEffects.onDestroy();
147
    DistortedEffectsPostprocess.onDestroy();
148
    DistortedMaster.onDestroy();
149
    EffectMessageSender.stopSending();
150

    
151
    mInitialized = false;
152
    }
153
  }
(1-1/24)