Project

General

Profile

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

library / src / main / java / org / distorted / library / Distorted.java @ b28c6c21

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
  //////////// DEBUG FLAGS /////////////////////////////////////////////
38
  /**
39
   * When rendering a Screen, show FPS in the upper-left corner?
40
   */
41
  public static final int DEBUG_FPS = 1;
42

    
43
  //////////// END DEBUG FLAGS /////////////////////////////////////////
44

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

    
85
  private static boolean mInitialized=false;
86

    
87
  static int mDebugLevel = 0;
88
  static DistortedDebug mDebug;
89

    
90
///////////////////////////////////////////////////////////////////////////////////////////////////
91
// private: hide this from Javadoc
92

    
93
  private Distorted()
94
    {
95
    
96
    }
97

    
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99

    
100
  static boolean isInitialized()
101
    {
102
    return mInitialized;
103
    }
104

    
105
///////////////////////////////////////////////////////////////////////////////////////////////////
106
/**
107
 * Make the library show various debugging information.
108
 * <p>
109
 * Currently only DEBUG_FPS - show FPS in the upper-left corner of every Screen - is defined.
110
 *
111
 * @param bitmask 0, or a bitmask of DEBUG_** flags to enable (currently only DEBUG_FPS defined)
112
 */
113
  public static void setDebug(int bitmask)
114
    {
115
    mDebugLevel = bitmask;
116

    
117
    if( mDebugLevel!=0 && mDebug==null ) mDebug = new DistortedDebug();
118
    }
119

    
120
///////////////////////////////////////////////////////////////////////////////////////////////////
121
/**
122
 * When OpenGL context gets created, you need to call this method so that the library can initialise its internal data structures.
123
 * I.e. best called from GLSurfaceView.onCreate().
124
 * <p>
125
 * Needs to be called from a thread holding the OpenGL context.
126
 *   
127
 * @param context Context of the App using the library - used to open up Resources and read Shader code.
128
 * @throws FragmentCompilationException
129
 * @throws VertexCompilationException
130
 * @throws VertexUniformsException
131
 * @throws FragmentUniformsException
132
 * @throws LinkingException
133
 */
134
  public static void onCreate(final Context context)
135
  throws FragmentCompilationException,VertexCompilationException,VertexUniformsException,FragmentUniformsException,LinkingException
136
    {
137
    final ActivityManager activityManager     = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
138
    final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
139
    android.util.Log.e("DISTORTED", "Using OpenGL ES "+configurationInfo.getGlEsVersion());
140

    
141
    GLSL = ( (configurationInfo.reqGlEsVersion>>16)>=3 ? 300 : 100 );
142
    GLSL_VERSION= (GLSL==100 ? "#version 100\n" : "#version 300 es\n");
143

    
144
    final Resources resources = context.getResources();
145
    DistortedEffects.createProgram(resources);
146
    EffectQueuePostprocess.createProgram(resources);
147
    EffectMessageSender.startSending();
148

    
149
    mInitialized = true;
150
    }
151

    
152
///////////////////////////////////////////////////////////////////////////////////////////////////
153
/**
154
 * Call this so that the Library can release its internal data structures.
155
 * Must be called from Activity.onPause().
156
 */
157
  public static void onPause()
158
    {
159
    DistortedObject.onPause();
160
    DistortedNode.onPause();
161
    }
162

    
163
///////////////////////////////////////////////////////////////////////////////////////////////////
164
/**
165
 * Call this so that the Library can release its internal data structures.
166
 * Must be called from Activity.onDestroy(). 
167
 */
168
  public static void onDestroy()
169
    {
170
    DistortedObject.onDestroy();
171
    DistortedNode.onDestroy();
172
    DistortedEffects.onDestroy();
173
    DistortedEffectsPostprocess.onDestroy();
174
    DistortedMaster.onDestroy();
175
    EffectQueue.onDestroy();
176
    EffectMessageSender.stopSending();
177

    
178
    mInitialized = false;
179
    }
180
  }
(1-1/27)