Project

General

Profile

« Previous | Next » 

Revision e5424523

Added by Leszek Koltunski over 8 years ago

Upgrade what used to be the 'Scratchpad' app; step 1: rename.

View differences:

src/main/AndroidManifest.xml
27 27
        <activity android:name=".movingeffects.MovingEffectsActivity" />    
28 28
        <activity android:name=".differenteffects.DifferentEffectsActivity" />    
29 29
        <activity android:name=".differentbitmaps.DifferentBitmapsActivity" />    
30
        <activity android:name=".scratchpad.ScratchpadActivity" />    
30
        <activity android:name=".effects2d.Effects2DActivity" />
31 31
        <activity android:name=".check.CheckActivity" />    
32 32
        <activity android:name=".fbo.FBOActivity" />    
33 33
        <activity android:name=".starwars.StarWarsActivity" />      
src/main/java/org/distorted/examples/TableOfContents.java
46 46
import org.distorted.examples.olimpic.OlimpicActivity;
47 47
import org.distorted.examples.differenteffects.DifferentEffectsActivity;
48 48
import org.distorted.examples.differentbitmaps.DifferentBitmapsActivity;
49
import org.distorted.examples.scratchpad.ScratchpadActivity;
49
import org.distorted.examples.effects2d.Effects2DActivity;
50 50
import org.distorted.examples.check.CheckActivity;
51 51
import org.distorted.examples.bean.BeanActivity;
52 52
import org.distorted.examples.fbo.FBOActivity;
......
189 189
   
190 190
   {
191 191
      final Map<String, Object> item = new HashMap<>();
192
      item.put(ITEM_IMAGE, R.drawable.icon_example_scratchpad);
193
      item.put(ITEM_TITLE, (i+1)+". "+getText(R.string.example_scratchpad));
194
      item.put(ITEM_SUBTITLE, getText(R.string.example_scratchpad_subtitle));
192
      item.put(ITEM_IMAGE, R.drawable.icon_example_effects2d);
193
      item.put(ITEM_TITLE, (i+1)+". "+getText(R.string.example_effects2d));
194
      item.put(ITEM_SUBTITLE, getText(R.string.example_effects2d_subtitle));
195 195
      data.add(item);
196
      activityMapping.put(i++, ScratchpadActivity.class);
196
      activityMapping.put(i++, Effects2DActivity.class);
197 197
   }
198 198
   
199 199
   {
src/main/java/org/distorted/examples/effects2d/Effects2DActivity.java
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.examples.effects2d;
21

  
22
import org.distorted.library.Distorted;
23
import org.distorted.examples.R;
24

  
25
import android.app.Activity;
26
import android.opengl.GLSurfaceView;
27
import android.os.Bundle;
28
import android.view.View;
29
import android.widget.SeekBar.OnSeekBarChangeListener;
30
import android.widget.SeekBar;
31
import android.widget.TextView;
32

  
33
///////////////////////////////////////////////////////////////////////////////////////////////////
34

  
35
public class Effects2DActivity extends Activity implements OnSeekBarChangeListener
36
  {
37
  private static final float D_MULT=200.0f;
38
  private static final float C_MULT=  0.1f;
39
   
40
  private long effectID;
41
   
42
  private SeekBar barD, barC, barI;
43
  private TextView textD, textC, textI;
44
   
45
///////////////////////////////////////////////////////////////////////////////////////////////////
46

  
47
  @Override
48
  protected void onCreate(Bundle savedInstanceState) 
49
    {
50
    super.onCreate(savedInstanceState);
51
 
52
    setContentView(R.layout.scratchpadlayout);
53
      
54
    barD = (SeekBar)findViewById(R.id.scratchpadSeekDuration);
55
    barD.setOnSeekBarChangeListener(this); 
56
    barC = (SeekBar)findViewById(R.id.scratchpadSeekCount);
57
    barC.setOnSeekBarChangeListener(this); 
58
    barI = (SeekBar)findViewById(R.id.scratchpadSeekID);
59
    barI.setOnSeekBarChangeListener(this); 
60
        
61
    textD = (TextView)findViewById(R.id.scratchpadTextDuration);
62
    textC = (TextView)findViewById(R.id.scratchpadTextCount);
63
    textI = (TextView)findViewById(R.id.scratchpadTextID);
64
      
65
    barD.setProgress(100);
66
    barC.setProgress(10);
67
    barI.setProgress(0);
68
      
69
    textD.setText("Dur: 20 s");
70
    textC.setText("Cou: 1.0");
71
    textI.setText("ID: 0");
72
    }
73

  
74
///////////////////////////////////////////////////////////////////////////////////////////////////
75

  
76
  @Override
77
  protected void onResume() 
78
    {
79
    super.onResume();
80
      
81
    GLSurfaceView mView = (GLSurfaceView) this.findViewById(R.id.scratchpadSurfaceView);
82
    mView.onResume();
83
    }
84

  
85
///////////////////////////////////////////////////////////////////////////////////////////////////
86

  
87
  @Override
88
  protected void onPause() 
89
    {
90
    GLSurfaceView mView = (GLSurfaceView) this.findViewById(R.id.scratchpadSurfaceView);
91
    mView.onPause();
92
      
93
    super.onPause();
94
    }
95
    
96
///////////////////////////////////////////////////////////////////////////////////////////////////
97

  
98
  @Override
99
  public void onStop()
100
    {
101
    super.onStop();
102
    }
103

  
104
///////////////////////////////////////////////////////////////////////////////////////////////////
105

  
106
  @Override
107
  public void onDestroy()
108
    {
109
    Distorted.onDestroy();
110
    super.onDestroy();
111
    }     
112
 
113
///////////////////////////////////////////////////////////////////////////////////////////////////
114
    
115
  public void Distort(View v)
116
    {
117
    Effects2DSurfaceView.setEffect(0);
118
    }     
119
    
120
///////////////////////////////////////////////////////////////////////////////////////////////////
121

  
122
  public void Sink(View v)
123
    {
124
    Effects2DSurfaceView.setEffect(1);
125
    }       
126

  
127
///////////////////////////////////////////////////////////////////////////////////////////////////
128
    
129
  public void Transparency(View v)
130
    {
131
    Effects2DSurfaceView.setEffect(2);
132
    }     
133
    
134
///////////////////////////////////////////////////////////////////////////////////////////////////
135

  
136
  public void Macroblock(View v)
137
    {
138
    Effects2DSurfaceView.setEffect(3);
139
    }       
140
 
141
///////////////////////////////////////////////////////////////////////////////////////////////////
142

  
143
  public void Brightness(View v)
144
    {
145
    Effects2DSurfaceView.setEffect(4);
146
    }       
147
     
148
///////////////////////////////////////////////////////////////////////////////////////////////////
149

  
150
  public void Print(View v)
151
    {
152
    Effects2DRenderer.mBackground.printEffect(effectID);
153
    }
154

  
155
///////////////////////////////////////////////////////////////////////////////////////////////////
156

  
157
  public void Abort(View v)
158
    {
159
    Effects2DRenderer.mBackground.abortEffect(effectID);
160
    }
161

  
162
///////////////////////////////////////////////////////////////////////////////////////////////////
163
    
164
  public void onProgressChanged(SeekBar bar, int progress, boolean fromUser) 
165
    {
166
    float v, t; 
167
    int i;
168
      
169
    switch (bar.getId()) 
170
      {
171
      case R.id.scratchpadSeekDuration: v = progress*D_MULT;
172
                                        i = (int)(v/100);
173
                                        t = i/10.0f;
174
                                        Effects2DSurfaceView.setDuration((int)v);
175
                                        textD.setText("Dur: "+(int)t+" s");
176
                                        break;
177
      case R.id.scratchpadSeekCount   : v = progress*C_MULT;
178
                                        i = (int)(v*10);
179
                                        t = i/10.0f;
180
                                        Effects2DSurfaceView.setCount(v);
181
                                        textC.setText("Cou: "+t);
182
                                        break;
183
      case R.id.scratchpadSeekID      : effectID = progress;
184
                                        textI.setText("ID: "+progress);
185
                                        break;                        
186
      }
187
    }
188

  
189
///////////////////////////////////////////////////////////////////////////////////////////////////
190

  
191
  public void onStartTrackingTouch(SeekBar bar) { }
192
    
193
///////////////////////////////////////////////////////////////////////////////////////////////////
194

  
195
  public void onStopTrackingTouch(SeekBar bar)  { }
196
  }
src/main/java/org/distorted/examples/effects2d/Effects2DRenderer.java
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.examples.effects2d;
21

  
22
import javax.microedition.khronos.egl.EGLConfig;
23
import javax.microedition.khronos.opengles.GL10;
24

  
25
import android.graphics.Bitmap;
26
import android.graphics.Canvas;
27
import android.graphics.Paint;
28
import android.graphics.Paint.Style;
29
import android.opengl.GLES20;
30
import android.opengl.GLSurfaceView;
31

  
32
import org.distorted.library.DistortedBitmap;
33
import org.distorted.library.Distorted;
34
import org.distorted.library.EffectTypes;
35
import org.distorted.library.type.Static3D;
36

  
37
///////////////////////////////////////////////////////////////////////////////////////////////////
38

  
39
public class Effects2DRenderer implements GLSurfaceView.Renderer
40
  {  
41
  public static final int NUMLINES = 10;
42
  public static final int BWID = 300;
43
  public static final int BHEI = 400;
44
   
45
  private GLSurfaceView mView;
46
  public static DistortedBitmap mBackground;
47
  private Paint mPaint;
48
  private int texWidth, texHeight;
49
    
50
///////////////////////////////////////////////////////////////////////////////////////////////////
51

  
52
  public Effects2DRenderer(GLSurfaceView v)
53
    {    
54
    mPaint = new Paint();
55
    mPaint.setAntiAlias(true);
56
    mPaint.setFakeBoldText(true);
57
    mPaint.setStyle(Style.FILL);
58
      
59
    mView = v;
60
      
61
    texWidth = BWID;
62
    texHeight= BHEI;
63
    }
64

  
65
///////////////////////////////////////////////////////////////////////////////////////////////////
66
   
67
  public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
68
    {      
69
    mBackground = new DistortedBitmap(texWidth,texHeight, 80);
70
    Bitmap bitmap = Bitmap.createBitmap(texWidth,texHeight, Bitmap.Config.ARGB_8888);
71
    Canvas canvas = new Canvas(bitmap);  
72
      
73
    mPaint.setColor(0xff008800);
74
    canvas.drawRect(0, 0, texWidth, texHeight, mPaint);
75
    mPaint.setColor(0xffffffff);
76
         
77
    for(int i=0; i<=NUMLINES ; i++ )
78
      {
79
      canvas.drawRect(texWidth*i/NUMLINES - 1,                       0,  texWidth*i/NUMLINES + 1,  texHeight               , mPaint);
80
      canvas.drawRect(                      0, texHeight*i/NUMLINES -1,  texWidth               ,  texHeight*i/NUMLINES + 1, mPaint);  
81
      }
82
          
83
    mBackground.setBitmap(bitmap);
84
          
85
    try
86
      {
87
      Distorted.onSurfaceCreated(mView.getContext());
88
      }
89
    catch(Exception ex)
90
      {
91
      android.util.Log.e("Scratchpad", ex.getMessage() );
92
      }
93
    }
94

  
95
///////////////////////////////////////////////////////////////////////////////////////////////////
96

  
97
  public void onSurfaceChanged(GL10 glUnused, int width, int height)
98
    {
99
    mBackground.abortEffects(EffectTypes.MATRIX);
100
    mBackground.scale( new Static3D((float)width/texWidth,(float)height/texHeight,1) );
101
    Distorted.onSurfaceChanged(width,height);
102
    Effects2DSurfaceView.setScreenSize(width,height);
103
    }
104
   
105
///////////////////////////////////////////////////////////////////////////////////////////////////
106
   
107
  public void onDrawFrame(GL10 glUnused)
108
    {   
109
    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
110
    mBackground.draw(System.currentTimeMillis());
111
    }
112
  }
src/main/java/org/distorted/examples/effects2d/Effects2DSurfaceView.java
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.examples.effects2d;
21

  
22
import android.content.Context;
23
import android.opengl.GLSurfaceView;
24
import android.os.Build;
25
import android.view.MotionEvent;
26
import android.util.AttributeSet;
27

  
28
import org.distorted.library.type.Dynamic1D;
29
import org.distorted.library.type.Static1D;
30
import org.distorted.library.type.Static2D;
31
import org.distorted.library.type.Static3D;
32
import org.distorted.library.type.Static4D;
33
import org.distorted.library.type.Dynamic3D;
34

  
35
///////////////////////////////////////////////////////////////////////////////////////////////////
36

  
37
public class Effects2DSurfaceView extends GLSurfaceView
38
  {
39
  private Effects2DRenderer mRenderer;
40
  private static int mCurrentEffect;
41
  private static int mDuration;
42
  private static float mCount;
43
  private static int mScrW, mScrH;
44
    
45
  private static Static4D region;
46
  private static Static2D point;
47

  
48
  private static Static4D mRegion;
49
  private static Dynamic1D mInterA, mInterM, mInterB, mInterS;
50

  
51
  private static Dynamic3D mInterD;
52
  private static Static3D v0, v1, v2, v3;
53
     
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55
    
56
  public Effects2DSurfaceView(Context c, AttributeSet attrs)
57
    {
58
    super(c, attrs);
59
      
60
    mDuration = 10000;
61
    mCount    = 1.0f;
62
      
63
    mInterD = new Dynamic3D(mDuration,mCount);
64

  
65
    int h = 30;
66
    int r = 20;
67

  
68
    v0 = new Static3D( 0, r, h );
69
    v1 = new Static3D(-r, 0, h );
70
    v2 = new Static3D( 0,-r, h );
71
    v3 = new Static3D( r, 0, h );
72

  
73
    mInterD.add(v0);
74
    mInterD.add(v1);
75
    mInterD.add(v2);
76
    mInterD.add(v3);
77

  
78
    mInterA = new Dynamic1D(mDuration,mCount);
79
    mInterA.add(new Static1D(1));
80
    mInterA.add(new Static1D(0));
81

  
82
    mInterS = new Dynamic1D(mDuration,mCount);
83
    mInterS.add(new Static1D(1.0f));
84
    mInterS.add(new Static1D(0.3f));
85

  
86
    mInterB = new Dynamic1D(mDuration,mCount);
87
    mInterB.add(new Static1D(1));
88
    mInterB.add(new Static1D(0));
89

  
90
    mInterM = new Dynamic1D(mDuration,mCount);
91
    mInterM.add(new Static1D(1));
92
    mInterM.add(new Static1D(10));
93

  
94
    if(!isInEditMode())
95
      {
96
      setFocusable(true);
97
      setFocusableInTouchMode(true);
98
       
99
      setEGLContextClientVersion(2);
100
        
101
      if( Build.FINGERPRINT.startsWith("generic") ) // when running on the emulator, insert a magic line that is
102
        {                                           // supposed to cure the 'no config chosen' crash on emulator startup
103
        setEGLConfigChooser(8, 8, 8, 8, 16, 0);   
104
        }
105
        
106
      mRenderer = new Effects2DRenderer(this);
107
      setRenderer(mRenderer);
108
        
109
      point = new Static2D(0,0);
110
      region= new Static4D(0,0,60,60);
111
      mRegion = new Static4D(0,0,60,60);
112
        
113
      setEffect(0);  
114
      }
115
    }
116

  
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118

  
119
  public static void setScreenSize(int width, int height)
120
    {
121
    mScrW = width;
122
    mScrH = height;
123
    }
124

  
125
///////////////////////////////////////////////////////////////////////////////////////////////////
126

  
127
  public static int getEffect()
128
    {
129
    return mCurrentEffect;
130
    }
131
   
132
///////////////////////////////////////////////////////////////////////////////////////////////////
133

  
134
  public static void setEffect(int effect)
135
    {
136
    mCurrentEffect = effect;
137
    }
138

  
139
///////////////////////////////////////////////////////////////////////////////////////////////////
140

  
141
  public static void setDuration(int duration)
142
    {
143
    mDuration = duration;
144
    mInterD.setDuration(duration);
145
    mInterA.setDuration(duration);
146
    mInterB.setDuration(duration);
147
    mInterM.setDuration(duration);
148
    mInterS.setDuration(duration);
149
    }
150

  
151
///////////////////////////////////////////////////////////////////////////////////////////////////
152

  
153
  public static void setCount(float count)
154
    {
155
    mCount = count;
156
    mInterD.setCount(count);
157
    mInterA.setCount(count);
158
    mInterB.setCount(count);
159
    mInterM.setCount(count);
160
    mInterS.setCount(count);
161
    }
162
  
163
///////////////////////////////////////////////////////////////////////////////////////////////////
164
    
165
  @Override public boolean onTouchEvent(MotionEvent event) 
166
    {
167
    int action = event.getAction();
168
    int x, y;
169
      
170
    switch(action)
171
      {
172
      case MotionEvent.ACTION_DOWN: x = (int)event.getX()* Effects2DRenderer.BWID/mScrW;
173
                                    y = (int)event.getY()* Effects2DRenderer.BHEI/mScrH;
174
                                    point.set(x,y);
175
                                    mRegion.set(x,y,60,60);
176

  
177
                                    switch(mCurrentEffect)
178
                                      {
179
                                      case 0: Effects2DRenderer.mBackground.distort(mInterD, point, region);
180
                                           break;
181
                                      case 1: Effects2DRenderer.mBackground.sink(mInterS, point, region);
182
                                           break;
183
                                      case 2: Effects2DRenderer.mBackground.alpha(mInterA, mRegion, false);
184
                                           break;  
185
                                      case 3: Effects2DRenderer.mBackground.macroblock(mInterM, mRegion);
186
                                           break;
187
                                      case 4: Effects2DRenderer.mBackground.brightness(mInterB, mRegion, false);
188
                                           break;      
189
                                      }
190
                                    break;
191
      }
192
            
193
    return true;
194
    }
195
  }
src/main/java/org/distorted/examples/scratchpad/ScratchpadActivity.java
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.examples.scratchpad;
21

  
22
import org.distorted.library.Distorted;
23
import org.distorted.examples.R;
24

  
25
import android.app.Activity;
26
import android.opengl.GLSurfaceView;
27
import android.os.Bundle;
28
import android.view.View;
29
import android.widget.SeekBar.OnSeekBarChangeListener;
30
import android.widget.SeekBar;
31
import android.widget.TextView;
32

  
33
///////////////////////////////////////////////////////////////////////////////////////////////////
34

  
35
public class ScratchpadActivity extends Activity implements OnSeekBarChangeListener
36
  {
37
  private static final float D_MULT=200.0f;
38
  private static final float C_MULT=  0.1f;
39
   
40
  private long effectID;
41
   
42
  private SeekBar barD, barC, barI;
43
  private TextView textD, textC, textI;
44
   
45
///////////////////////////////////////////////////////////////////////////////////////////////////
46

  
47
  @Override
48
  protected void onCreate(Bundle savedInstanceState) 
49
    {
50
    super.onCreate(savedInstanceState);
51
 
52
    setContentView(R.layout.scratchpadlayout);
53
      
54
    barD = (SeekBar)findViewById(R.id.scratchpadSeekDuration);
55
    barD.setOnSeekBarChangeListener(this); 
56
    barC = (SeekBar)findViewById(R.id.scratchpadSeekCount);
57
    barC.setOnSeekBarChangeListener(this); 
58
    barI = (SeekBar)findViewById(R.id.scratchpadSeekID);
59
    barI.setOnSeekBarChangeListener(this); 
60
        
61
    textD = (TextView)findViewById(R.id.scratchpadTextDuration);
62
    textC = (TextView)findViewById(R.id.scratchpadTextCount);
63
    textI = (TextView)findViewById(R.id.scratchpadTextID);
64
      
65
    barD.setProgress(100);
66
    barC.setProgress(10);
67
    barI.setProgress(0);
68
      
69
    textD.setText("Dur: 20 s");
70
    textC.setText("Cou: 1.0");
71
    textI.setText("ID: 0");
72
    }
73

  
74
///////////////////////////////////////////////////////////////////////////////////////////////////
75

  
76
  @Override
77
  protected void onResume() 
78
    {
79
    super.onResume();
80
      
81
    GLSurfaceView mView = (GLSurfaceView) this.findViewById(R.id.scratchpadSurfaceView);
82
    mView.onResume();
83
    }
84

  
85
///////////////////////////////////////////////////////////////////////////////////////////////////
86

  
87
  @Override
88
  protected void onPause() 
89
    {
90
    GLSurfaceView mView = (GLSurfaceView) this.findViewById(R.id.scratchpadSurfaceView);
91
    mView.onPause();
92
      
93
    super.onPause();
94
    }
95
    
96
///////////////////////////////////////////////////////////////////////////////////////////////////
97

  
98
  @Override
99
  public void onStop()
100
    {
101
    super.onStop();
102
    }
103

  
104
///////////////////////////////////////////////////////////////////////////////////////////////////
105

  
106
  @Override
107
  public void onDestroy()
108
    {
109
    Distorted.onDestroy();
110
    super.onDestroy();
111
    }     
112
 
113
///////////////////////////////////////////////////////////////////////////////////////////////////
114
    
115
  public void Distort(View v)
116
    {
117
    ScratchpadSurfaceView.setEffect(0);
118
    }     
119
    
120
///////////////////////////////////////////////////////////////////////////////////////////////////
121

  
122
  public void Sink(View v)
123
    {
124
    ScratchpadSurfaceView.setEffect(1);
125
    }       
126

  
127
///////////////////////////////////////////////////////////////////////////////////////////////////
128
    
129
  public void Transparency(View v)
130
    {
131
    ScratchpadSurfaceView.setEffect(2);
132
    }     
133
    
134
///////////////////////////////////////////////////////////////////////////////////////////////////
135

  
136
  public void Macroblock(View v)
137
    {
138
    ScratchpadSurfaceView.setEffect(3);
139
    }       
140
 
141
///////////////////////////////////////////////////////////////////////////////////////////////////
142

  
143
  public void Brightness(View v)
144
    {
145
    ScratchpadSurfaceView.setEffect(4);
146
    }       
147
     
148
///////////////////////////////////////////////////////////////////////////////////////////////////
149

  
150
  public void Print(View v)
151
    {
152
    ScratchpadRenderer.mBackground.printEffect(effectID);
153
    }
154

  
155
///////////////////////////////////////////////////////////////////////////////////////////////////
156

  
157
  public void Abort(View v)
158
    {
159
    ScratchpadRenderer.mBackground.abortEffect(effectID);
160
    }
161

  
162
///////////////////////////////////////////////////////////////////////////////////////////////////
163
    
164
  public void onProgressChanged(SeekBar bar, int progress, boolean fromUser) 
165
    {
166
    float v, t; 
167
    int i;
168
      
169
    switch (bar.getId()) 
170
      {
171
      case R.id.scratchpadSeekDuration: v = progress*D_MULT;
172
                                        i = (int)(v/100);
173
                                        t = i/10.0f;
174
                                        ScratchpadSurfaceView.setDuration((int)v);
175
                                        textD.setText("Dur: "+(int)t+" s");
176
                                        break;
177
      case R.id.scratchpadSeekCount   : v = progress*C_MULT;
178
                                        i = (int)(v*10);
179
                                        t = i/10.0f;
180
                                        ScratchpadSurfaceView.setCount(v);
181
                                        textC.setText("Cou: "+t);
182
                                        break;
183
      case R.id.scratchpadSeekID      : effectID = progress;
184
                                        textI.setText("ID: "+progress);
185
                                        break;                        
186
      }
187
    }
188

  
189
///////////////////////////////////////////////////////////////////////////////////////////////////
190

  
191
  public void onStartTrackingTouch(SeekBar bar) { }
192
    
193
///////////////////////////////////////////////////////////////////////////////////////////////////
194

  
195
  public void onStopTrackingTouch(SeekBar bar)  { }
196
  }
src/main/java/org/distorted/examples/scratchpad/ScratchpadRenderer.java
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.examples.scratchpad;
21

  
22
import javax.microedition.khronos.egl.EGLConfig;
23
import javax.microedition.khronos.opengles.GL10;
24

  
25
import android.graphics.Bitmap;
26
import android.graphics.Canvas;
27
import android.graphics.Paint;
28
import android.graphics.Paint.Style;
29
import android.opengl.GLES20;
30
import android.opengl.GLSurfaceView;
31

  
32
import org.distorted.library.DistortedBitmap;
33
import org.distorted.library.Distorted;
34
import org.distorted.library.EffectTypes;
35
import org.distorted.library.type.Static3D;
36

  
37
///////////////////////////////////////////////////////////////////////////////////////////////////
38

  
39
public class ScratchpadRenderer implements GLSurfaceView.Renderer 
40
  {  
41
  public static final int NUMLINES = 10;
42
  public static final int BWID = 300;
43
  public static final int BHEI = 400;
44
   
45
  private GLSurfaceView mView;
46
  public static DistortedBitmap mBackground;
47
  private Paint mPaint;
48
  private int texWidth, texHeight;
49
    
50
///////////////////////////////////////////////////////////////////////////////////////////////////
51

  
52
  public ScratchpadRenderer(GLSurfaceView v)
53
    {    
54
    mPaint = new Paint();
55
    mPaint.setAntiAlias(true);
56
    mPaint.setFakeBoldText(true);
57
    mPaint.setStyle(Style.FILL);
58
      
59
    mView = v;
60
      
61
    texWidth = BWID;
62
    texHeight= BHEI;
63
    }
64

  
65
///////////////////////////////////////////////////////////////////////////////////////////////////
66
   
67
  public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
68
    {      
69
    mBackground = new DistortedBitmap(texWidth,texHeight, 80);
70
    Bitmap bitmap = Bitmap.createBitmap(texWidth,texHeight, Bitmap.Config.ARGB_8888);
71
    Canvas canvas = new Canvas(bitmap);  
72
      
73
    mPaint.setColor(0xff008800);
74
    canvas.drawRect(0, 0, texWidth, texHeight, mPaint);
75
    mPaint.setColor(0xffffffff);
76
         
77
    for(int i=0; i<=NUMLINES ; i++ )
78
      {
79
      canvas.drawRect(texWidth*i/NUMLINES - 1,                       0,  texWidth*i/NUMLINES + 1,  texHeight               , mPaint);
80
      canvas.drawRect(                      0, texHeight*i/NUMLINES -1,  texWidth               ,  texHeight*i/NUMLINES + 1, mPaint);  
81
      }
82
          
83
    mBackground.setBitmap(bitmap);
84
          
85
    try
86
      {
87
      Distorted.onSurfaceCreated(mView.getContext());
88
      }
89
    catch(Exception ex)
90
      {
91
      android.util.Log.e("Scratchpad", ex.getMessage() );
92
      }
93
    }
94

  
95
///////////////////////////////////////////////////////////////////////////////////////////////////
96

  
97
  public void onSurfaceChanged(GL10 glUnused, int width, int height)
98
    {
99
    mBackground.abortEffects(EffectTypes.MATRIX);
100
    mBackground.scale( new Static3D((float)width/texWidth,(float)height/texHeight,1) );
101
    Distorted.onSurfaceChanged(width,height);
102
    ScratchpadSurfaceView.setScreenSize(width,height);     
103
    }
104
   
105
///////////////////////////////////////////////////////////////////////////////////////////////////
106
   
107
  public void onDrawFrame(GL10 glUnused)
108
    {   
109
    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
110
    mBackground.draw(System.currentTimeMillis());
111
    }
112
  }
src/main/java/org/distorted/examples/scratchpad/ScratchpadSurfaceView.java
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.examples.scratchpad;
21

  
22
import android.content.Context;
23
import android.opengl.GLSurfaceView;
24
import android.os.Build;
25
import android.view.MotionEvent;
26
import android.util.AttributeSet;
27

  
28
import org.distorted.library.type.Dynamic1D;
29
import org.distorted.library.type.Static1D;
30
import org.distorted.library.type.Static2D;
31
import org.distorted.library.type.Static3D;
32
import org.distorted.library.type.Static4D;
33
import org.distorted.library.type.Dynamic3D;
34

  
35
///////////////////////////////////////////////////////////////////////////////////////////////////
36

  
37
public class ScratchpadSurfaceView extends GLSurfaceView
38
  {
39
  private ScratchpadRenderer mRenderer;
40
  private static int mCurrentEffect;
41
  private static int mDuration;
42
  private static float mCount;
43
  private static int mScrW, mScrH;
44
    
45
  private static Static4D region;
46
  private static Static2D point;
47

  
48
  private static Static4D mRegion;
49
  private static Dynamic1D mInterA, mInterM, mInterB, mInterS;
50

  
51
  private static Dynamic3D mInterD;
52
  private static Static3D v0, v1, v2, v3;
53
     
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55
    
56
  public ScratchpadSurfaceView(Context c, AttributeSet attrs) 
57
    {
58
    super(c, attrs);
59
      
60
    mDuration = 10000;
61
    mCount    = 1.0f;
62
      
63
    mInterD = new Dynamic3D(mDuration,mCount);
64

  
65
    int h = 30;
66
    int r = 20;
67

  
68
    v0 = new Static3D( 0, r, h );
69
    v1 = new Static3D(-r, 0, h );
70
    v2 = new Static3D( 0,-r, h );
71
    v3 = new Static3D( r, 0, h );
72

  
73
    mInterD.add(v0);
74
    mInterD.add(v1);
75
    mInterD.add(v2);
76
    mInterD.add(v3);
77

  
78
    mInterA = new Dynamic1D(mDuration,mCount);
79
    mInterA.add(new Static1D(1));
80
    mInterA.add(new Static1D(0));
81

  
82
    mInterS = new Dynamic1D(mDuration,mCount);
83
    mInterS.add(new Static1D(1.0f));
84
    mInterS.add(new Static1D(0.3f));
85

  
86
    mInterB = new Dynamic1D(mDuration,mCount);
87
    mInterB.add(new Static1D(1));
88
    mInterB.add(new Static1D(0));
89

  
90
    mInterM = new Dynamic1D(mDuration,mCount);
91
    mInterM.add(new Static1D(1));
92
    mInterM.add(new Static1D(10));
93

  
94
    if(!isInEditMode())
95
      {
96
      setFocusable(true);
97
      setFocusableInTouchMode(true);
98
       
99
      setEGLContextClientVersion(2);
100
        
101
      if( Build.FINGERPRINT.startsWith("generic") ) // when running on the emulator, insert a magic line that is
102
        {                                           // supposed to cure the 'no config chosen' crash on emulator startup
103
        setEGLConfigChooser(8, 8, 8, 8, 16, 0);   
104
        }
105
        
106
      mRenderer = new ScratchpadRenderer(this);
107
      setRenderer(mRenderer);
108
        
109
      point = new Static2D(0,0);
110
      region= new Static4D(0,0,60,60);
111
      mRegion = new Static4D(0,0,60,60);
112
        
113
      setEffect(0);  
114
      }
115
    }
116

  
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118

  
119
  public static void setScreenSize(int width, int height)
120
    {
121
    mScrW = width;
122
    mScrH = height;
123
    }
124

  
125
///////////////////////////////////////////////////////////////////////////////////////////////////
126

  
127
  public static int getEffect()
128
    {
129
    return mCurrentEffect;
130
    }
131
   
132
///////////////////////////////////////////////////////////////////////////////////////////////////
133

  
134
  public static void setEffect(int effect)
135
    {
136
    mCurrentEffect = effect;
137
    }
138

  
139
///////////////////////////////////////////////////////////////////////////////////////////////////
140

  
141
  public static void setDuration(int duration)
142
    {
143
    mDuration = duration;
144
    mInterD.setDuration(duration);
145
    mInterA.setDuration(duration);
146
    mInterB.setDuration(duration);
147
    mInterM.setDuration(duration);
148
    mInterS.setDuration(duration);
149
    }
150

  
151
///////////////////////////////////////////////////////////////////////////////////////////////////
152

  
153
  public static void setCount(float count)
154
    {
155
    mCount = count;
156
    mInterD.setCount(count);
157
    mInterA.setCount(count);
158
    mInterB.setCount(count);
159
    mInterM.setCount(count);
160
    mInterS.setCount(count);
161
    }
162
  
163
///////////////////////////////////////////////////////////////////////////////////////////////////
164
    
165
  @Override public boolean onTouchEvent(MotionEvent event) 
166
    {
167
    int action = event.getAction();
168
    int x, y;
169
      
170
    switch(action)
171
      {
172
      case MotionEvent.ACTION_DOWN: x = (int)event.getX()*ScratchpadRenderer.BWID/mScrW;
173
                                    y = (int)event.getY()*ScratchpadRenderer.BHEI/mScrH;
174
                                    point.set(x,y);
175
                                    mRegion.set(x,y,60,60);
176

  
177
                                    switch(mCurrentEffect)
178
                                      {
179
                                      case 0: ScratchpadRenderer.mBackground.distort(mInterD, point, region);
180
                                           break;
181
                                      case 1: ScratchpadRenderer.mBackground.sink(mInterS, point, region);
182
                                           break;
183
                                      case 2: ScratchpadRenderer.mBackground.alpha(mInterA, mRegion, false);
184
                                           break;  
185
                                      case 3: ScratchpadRenderer.mBackground.macroblock(mInterM, mRegion);
186
                                           break;
187
                                      case 4: ScratchpadRenderer.mBackground.brightness(mInterB, mRegion, false);
188
                                           break;      
189
                                      }
190
                                    break;
191
      }
192
            
193
    return true;
194
    }
195
  }
src/main/res/layout/scratchpadlayout.xml
5 5
    android:onClick="Brightness"
6 6
    android:orientation="vertical" >
7 7

  
8
    <org.distorted.examples.scratchpad.ScratchpadSurfaceView
8
    <org.distorted.examples.effects2d.Effects2DSurfaceView
9 9
        android:id="@+id/scratchpadSurfaceView"
10 10
        android:layout_width="fill_parent"
11 11
        android:layout_height="0dp"
src/main/res/values/strings.xml
65 65
    <string name="example_differenteffects_subtitle">See how to draw the same bitmap multiple times, each time with different set of effects applied.</string>
66 66
    <string name="example_differentbitmaps">Different Bitmaps</string>  
67 67
    <string name="example_differentbitmaps_subtitle">Draw 3 different bitmaps, apply the same set of effects to each one in one go.</string>
68
    <string name="example_scratchpad">Scratchpad</string>  
69
    <string name="example_scratchpad_subtitle">Application used as a scratchpad during development of the library. </string>
68
    <string name="example_effects2d">Effects 2D</string>
69
    <string name="example_effects2d_subtitle">Add, remove and list all effects currently acting on a Bitmap.</string>
70 70
    <string name="example_check">Check</string>  
71 71
    <string name="example_check_subtitle">Check the maximum number of effects (separately for vertex and fragment shaders) we can apply to a single bitmap.</string>
72 72
    <string name="example_fbo">Bitmap Tree</string>  

Also available in: Unified diff