Project

General

Profile

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

library / src / main / java / org / distorted / library / main / Distorted.java @ 040cd18c

1 d333eb6b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 fe82a979 Leszek Koltunski
package org.distorted.library.main;
21 6a06a912 Leszek Koltunski
22 30beb34f Leszek Koltunski
import android.app.ActivityManager;
23 7845dc66 Leszek Koltunski
import android.content.Context;
24 30beb34f Leszek Koltunski
import android.content.pm.ConfigurationInfo;
25 d6e94c84 Leszek Koltunski
import android.content.res.Resources;
26 310e14fb leszek
27 26a4e5f6 leszek
import org.distorted.library.effect.Effect;
28 7cd24173 leszek
import org.distorted.library.effect.FragmentEffect;
29 aa2f0486 Leszek Koltunski
import org.distorted.library.effect.PostprocessEffect;
30 7cd24173 leszek
import org.distorted.library.effect.VertexEffect;
31 6a06a912 Leszek Koltunski
32
///////////////////////////////////////////////////////////////////////////////////////////////////
33
/**
34
 * A singleton class used to control various global settings.
35
 */
36
public class Distorted 
37 39cbf9dc Leszek Koltunski
  {
38 f8f6d457 leszek
  static int GLSL;
39
  static String GLSL_VERSION;
40 6a06a912 Leszek Koltunski
  /**
41 cacc63de Leszek Koltunski
   * When creating an instance of a DistortedTexture from another instance, clone the Bitmap that's
42
   * backing up our DistortedTexture.
43 6a06a912 Leszek Koltunski
   * <p>
44 cacc63de Leszek Koltunski
   * This way we can have two DistortedTextures, both backed up by the same Bitmap, to which we can
45 6a06a912 Leszek Koltunski
   * apply different effects. Used in the copy constructor.
46
   */
47 29a06526 Leszek Koltunski
  public static final int CLONE_SURFACE = 0x1;
48 6a06a912 Leszek Koltunski
  /**
49 cacc63de Leszek Koltunski
   * When creating an instance of a DistortedEffects from another instance, clone the Matrix Effects.
50 6a06a912 Leszek Koltunski
   * <p>
51 cacc63de Leszek Koltunski
   * This way we can have two different DistortedEffects sharing the MATRIX queue.
52 6a06a912 Leszek Koltunski
   */
53 015642fb Leszek Koltunski
  public static final int CLONE_MATRIX = 0x2;
54 6a06a912 Leszek Koltunski
  /**
55 cacc63de Leszek Koltunski
   * When creating an instance of a DistortedEffects from another instance, clone the Vertex Effects.
56 6a06a912 Leszek Koltunski
   * <p>
57 cacc63de Leszek Koltunski
   * This way we can have two different DistortedEffects sharing the VERTEX queue.
58 6a06a912 Leszek Koltunski
   */
59
  public static final int CLONE_VERTEX  = 0x4;
60
  /**
61 cacc63de Leszek Koltunski
   * When creating an instance of a DistortedEffects from another instance, clone the Fragment Effects.
62 6a06a912 Leszek Koltunski
   * <p>
63 cacc63de Leszek Koltunski
   * This way we can have two different DistortedEffects sharing the FRAGMENT queue.
64 6a06a912 Leszek Koltunski
   */
65
  public static final int CLONE_FRAGMENT= 0x8;
66 d6e94c84 Leszek Koltunski
   /**
67
   * When creating an instance of a DistortedEffects from another instance, clone the PostProcess Effects.
68
   * <p>
69
   * This way we can have two different DistortedEffects sharing the POSTPROCESS queue.
70
   */
71
  public static final int CLONE_POSTPROCESS= 0x10;
72 6a06a912 Leszek Koltunski
  /**
73 a09ada4c Leszek Koltunski
   * When creating an instance of a DistortedNode from another instance, clone the children Nodes.
74 6a06a912 Leszek Koltunski
   * <p>
75 cacc63de Leszek Koltunski
   * This is mainly useful for creating many similar sub-trees and rendering then at different places
76
   * on the screen with (optionally) different Effects.
77 6a06a912 Leszek Koltunski
   */
78 d6e94c84 Leszek Koltunski
  public static final int CLONE_CHILDREN= 0x20;
79 b3618cb5 Leszek Koltunski
80 55c14a19 Leszek Koltunski
  private static boolean mInitialized=false;
81 6a06a912 Leszek Koltunski
82
///////////////////////////////////////////////////////////////////////////////////////////////////
83 c638c1b0 Leszek Koltunski
// private: hide this from Javadoc
84 6a06a912 Leszek Koltunski
85
  private Distorted()
86
    {
87 8eccf334 Leszek Koltunski
88 6a06a912 Leszek Koltunski
    }
89
90 421c2728 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
91 310e14fb leszek
/**
92
 * Have we called onCreate yet, ie have we initialized the library?
93 1dfc9074 leszek
 * @return <code>true</code> if the library is initialized and ready for action.
94 310e14fb leszek
 */
95
  public static boolean isInitialized()
96 421c2728 Leszek Koltunski
    {
97 55c14a19 Leszek Koltunski
    return mInitialized;
98 421c2728 Leszek Koltunski
    }
99
100 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
101
/**
102
 * When OpenGL context gets created, you need to call this method so that the library can initialise its internal data structures.
103 432442f9 Leszek Koltunski
 * I.e. best called from GLSurfaceView.onCreate().
104 6a06a912 Leszek Koltunski
 * <p>
105 432442f9 Leszek Koltunski
 * Needs to be called from a thread holding the OpenGL context.
106 6a06a912 Leszek Koltunski
 *   
107 015642fb Leszek Koltunski
 * @param context Context of the App using the library - used to open up Resources and read Shader code.
108 6a06a912 Leszek Koltunski
 */
109 432442f9 Leszek Koltunski
  public static void onCreate(final Context context)
110 d6e94c84 Leszek Koltunski
    {
111 30beb34f Leszek Koltunski
    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 040cd18c Leszek Koltunski
    GLSL = ( (configurationInfo.reqGlEsVersion>>16)>=3 ? 310 : 100 );
116
    GLSL_VERSION= (GLSL==100 ? "#version 100\n" : "#version 310 es\n");
117 94f6d472 Leszek Koltunski
118 0e924ba5 Leszek Koltunski
    EffectMessageSender.startSending();
119
120 d6e94c84 Leszek Koltunski
    final Resources resources = context.getResources();
121 c90b9e01 Leszek Koltunski
    DistortedEffects.createProgram(resources);
122 aa2f0486 Leszek Koltunski
    PostprocessEffect.createPrograms();
123 c90b9e01 Leszek Koltunski
124
    mInitialized = true;
125 6a06a912 Leszek Koltunski
    }
126
127 05ecc6fe Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
128
/**
129 040cd18c Leszek Koltunski
 * Call this so that the Library can mark OpenGL objects that would need to be recreated when we
130
 * get resumed.
131 05ecc6fe Leszek Koltunski
 * Must be called from Activity.onPause().
132
 */
133
  public static void onPause()
134
    {
135 226144d0 leszek
    DistortedObject.onPause();
136 0c303a2c Leszek Koltunski
    DistortedNode.onPause();
137 05ecc6fe Leszek Koltunski
    }
138
139 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
140
/**
141
 * Call this so that the Library can release its internal data structures.
142
 * Must be called from Activity.onDestroy(). 
143
 */
144
  public static void onDestroy()
145
    {
146 226144d0 leszek
    DistortedObject.onDestroy();
147 a09ada4c Leszek Koltunski
    DistortedNode.onDestroy();
148 421c2728 Leszek Koltunski
    DistortedEffects.onDestroy();
149 efe3d8fe leszek
    DistortedMaster.onDestroy();
150 ce154014 Leszek Koltunski
    DistortedOutputSurface.onDestroy();
151 42571056 Leszek Koltunski
    EffectQueue.onDestroy();
152 26a4e5f6 leszek
    Effect.onDestroy();
153 7cd24173 leszek
    VertexEffect.onDestroy();
154
    FragmentEffect.onDestroy();
155 6a06a912 Leszek Koltunski
    EffectMessageSender.stopSending();
156 f8686932 Leszek Koltunski
157 55c14a19 Leszek Koltunski
    mInitialized = false;
158 6a06a912 Leszek Koltunski
    }
159 f8f6d457 leszek
  }