Project

General

Profile

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

library / src / main / java / org / distorted / library / main / Distorted.java @ 26a4e5f6

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.program.*;
29

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

    
78
  private static boolean mInitialized=false;
79

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

    
83
  private Distorted()
84
    {
85

    
86
    }
87

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

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

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

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

    
127
    mInitialized = true;
128
    }
129

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

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

    
156
    mInitialized = false;
157
    }
158
  }
(1-1/23)