Project

General

Profile

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

library / src / main / java / org / distorted / library / Distorted.java @ 86e99907

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

    
76
  private static boolean mInitialized=false;
77

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

    
81
  private Distorted()
82
    {
83
    
84
    }
85

    
86
///////////////////////////////////////////////////////////////////////////////////////////////////
87

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

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

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

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

    
122
    mInitialized = true;
123
    }
124

    
125
///////////////////////////////////////////////////////////////////////////////////////////////////
126
/**
127
 * Call this so that the Library can release its internal data structures.
128
 * Must be called from Activity.onPause().
129
 */
130
  public static void onPause()
131
    {
132
    DistortedObject.onPause();
133
    DistortedNode.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
    DistortedObject.onDestroy();
144
    DistortedNode.onDestroy();
145
    DistortedEffects.onDestroy();
146
    DistortedEffectsPostprocess.onDestroy();
147
    DistortedMaster.onDestroy();
148
    EffectQueue.onDestroy();
149
    EffectMessageSender.stopSending();
150

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