Project

General

Profile

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

examples / src / main / java / org / distorted / examples / save / SaveRenderer.java @ b9615af9

1 a2cb31e9 Leszek Koltunski
2
package org.distorted.examples.save;
3
4 7cfeeb63 Leszek Koltunski
import java.io.File;
5 a2cb31e9 Leszek Koltunski
import java.io.IOException;
6
import java.io.InputStream;
7 b9615af9 Leszek Koltunski
import java.util.Vector;
8 a2cb31e9 Leszek Koltunski
9
import javax.microedition.khronos.egl.EGLConfig;
10
import javax.microedition.khronos.opengles.GL10;
11
12
import org.distorted.examples.R;
13
14
import org.distorted.library.Distorted;
15
import org.distorted.library.DistortedBitmap;
16 b9615af9 Leszek Koltunski
import org.distorted.library.EffectListener;
17
import org.distorted.library.EffectMessage;
18 a2cb31e9 Leszek Koltunski
import org.distorted.library.EffectTypes;
19
import org.distorted.library.Float1D;
20
import org.distorted.library.Float2D;
21
import org.distorted.library.Float4D;
22
import org.distorted.library.Interpolator1D;
23
24 b9615af9 Leszek Koltunski
import android.app.Activity;
25 a2cb31e9 Leszek Koltunski
import android.graphics.Bitmap;
26
import android.graphics.BitmapFactory;
27
import android.opengl.GLES20;
28
import android.opengl.GLSurfaceView;
29 7cfeeb63 Leszek Koltunski
import android.os.Environment;
30 b9615af9 Leszek Koltunski
import android.widget.Toast;
31 a2cb31e9 Leszek Koltunski
32
///////////////////////////////////////////////////////////////////////////////////////////////////
33
34 b9615af9 Leszek Koltunski
class SaveRenderer implements GLSurfaceView.Renderer ,EffectListener
35
  {
36
  private GLSurfaceView mView;
37
  private static DistortedBitmap mGirl;
38
  private Float2D pLeft, pRight;
39
  private Float4D sinkRegion;
40
  private static Interpolator1D diSink;
41
  private static Float1D s0;
42
43
  private int bmpHeight, bmpWidth;
44
  private static int scrHeight, scrWidth;
45
  private static float boobsSink;
46
47
  private static class SaveInfo
48
    {
49
    private long id;
50
    private String path;
51
52
    SaveInfo(long id, String path)
53
      {
54
      this.id   =   id;
55
      this.path = path;
56
      }
57
    }
58
59
  private static Vector<SaveInfo> mSaveInfo;  // Vector keeping all the information about the PATH
60
                                              // where current 'SAVE' effects are saving to and also
61
                                              // the IDs of the effects.
62
                                              // We need to have a whole Vector because there can be
63
                                              // many concurrent 'SAVE' operations (click 'SAVE',
64
                                              // don't wait for the Toast to appear, immediately
65
                                              // click again! )
66
67 a2cb31e9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
68
69 b9615af9 Leszek Koltunski
  public SaveRenderer(GLSurfaceView v)
70
    {
71
    mView = v;
72 a2cb31e9 Leszek Koltunski
      
73 b9615af9 Leszek Koltunski
    boobsSink  = 1.0f;
74 a2cb31e9 Leszek Koltunski
      
75 b9615af9 Leszek Koltunski
    pLeft = new Float2D(132, 264);
76
    pRight= new Float2D(247, 264);
77 a2cb31e9 Leszek Koltunski
      
78 b9615af9 Leszek Koltunski
    sinkRegion = new Float4D(0,0,60,60);
79 a2cb31e9 Leszek Koltunski
      
80 b9615af9 Leszek Koltunski
    s0 = new Float1D(boobsSink);
81 a2cb31e9 Leszek Koltunski
      
82 b9615af9 Leszek Koltunski
    diSink = new Interpolator1D();
83
    diSink.setCount(0.5f);
84
    diSink.setDuration(0);
85
    diSink.add(s0);
86
87
    mSaveInfo = new Vector<>();
88
    }
89 a2cb31e9 Leszek Koltunski
90
///////////////////////////////////////////////////////////////////////////////////////////////////
91
92 b9615af9 Leszek Koltunski
  public static void setSize(float s)
93
    {
94
    boobsSink = s;
95
    s0.set(boobsSink);
96
    }
97 a2cb31e9 Leszek Koltunski
98 7cfeeb63 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
99
100 b9615af9 Leszek Koltunski
  public static void Save()
101
    {
102
    File file;
103
    int lowestNotFound = 1;
104
    String path;
105 7cfeeb63 Leszek Koltunski
106 b9615af9 Leszek Koltunski
    while(true)
107 a2cb31e9 Leszek Koltunski
      {
108 b9615af9 Leszek Koltunski
      path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/girl" + lowestNotFound +".png";
109
      file = new File(path);
110
111
      if( !file.exists() )
112
        {
113
        long id= mGirl.savePNG(file.getAbsolutePath(),0,0,scrWidth,scrHeight);
114
        mSaveInfo.add(new SaveInfo(id,path));
115
        break;
116
        }
117
      lowestNotFound++;
118 a2cb31e9 Leszek Koltunski
      }
119 b9615af9 Leszek Koltunski
    }
120 a2cb31e9 Leszek Koltunski
121
///////////////////////////////////////////////////////////////////////////////////////////////////
122 b9615af9 Leszek Koltunski
// the library sending messages to us. This does not run on this UI thread!
123
124
  public void effectMessage(final EffectMessage em, final long effectID, final int effectName, final long bitmapID, final String message)
125
    {
126
    int numSaves = mSaveInfo.size();
127
    int found = -1;
128
129
    for(int i=0; i<numSaves; i++)           // doesn't need to be synchronized with Save()
130
      {                                     // because over there we only add() and here we only
131
      if( effectID == mSaveInfo.get(i).id ) // remove() - and even if we add() in between of
132
        {                                   // computing the 'numSaves' above and this loop,
133
        found = i;                          // it still doesn't matter :)
134
        break;
135 a2cb31e9 Leszek Koltunski
        }
136 b9615af9 Leszek Koltunski
      }
137
138
    if( found>=0 )  // the message really is about one of the 'SAVE' effects in execution now
139
      {
140
      Activity act = (Activity)mView.getContext();
141
      final String path = mSaveInfo.get(found).path;
142
      mSaveInfo.remove(found);
143
144
      switch(em)
145 a2cb31e9 Leszek Koltunski
        {
146 b9615af9 Leszek Koltunski
        case EFFECT_FINISHED: act.runOnUiThread(new Runnable()
147
                                {
148
                                public void run()
149
                                  {
150
                                  Toast.makeText(mView.getContext(),
151
                                      "Saving to \n\n"+path+"\n\nsuccessful" ,
152
                                      Toast.LENGTH_LONG).show();
153
                                  }
154
                                });
155
                              break;
156
157
        case EFFECT_FAILED  : act.runOnUiThread(new Runnable()
158
                                {
159
                                public void run()
160
                                  {
161
                                  Toast.makeText(mView.getContext(),
162
                                      "Saving to \n\n"+path+"\n\n failed: "+"\n\n"+message ,
163
                                      Toast.LENGTH_LONG).show();
164
                                  }
165
                                });
166
                              break;
167
168
        default             : break;
169 a2cb31e9 Leszek Koltunski
        }
170 b9615af9 Leszek Koltunski
      }
171
    }
172
173
///////////////////////////////////////////////////////////////////////////////////////////////////
174
   
175
  public void onDrawFrame(GL10 glUnused)
176
    {
177
    GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
178
    GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
179 a2cb31e9 Leszek Koltunski
      
180 b9615af9 Leszek Koltunski
    mGirl.draw(System.currentTimeMillis());
181
    }
182
183
///////////////////////////////////////////////////////////////////////////////////////////////////
184
    
185
  public void onSurfaceChanged(GL10 glUnused, int width, int height)
186
    {
187
    scrWidth = width;
188
    scrHeight= height;
189
190
    mGirl.abortEffects(EffectTypes.MATRIX);
191
      
192
    if( bmpHeight/bmpWidth > height/width )
193
      {
194
      int w = (height*bmpWidth)/bmpHeight;
195
      mGirl.move((width-w)/2 ,0, 0);
196
      mGirl.scale((float)height/bmpHeight);
197 a2cb31e9 Leszek Koltunski
      }
198 b9615af9 Leszek Koltunski
    else
199
      {
200
      int h = (width*bmpHeight)/bmpWidth;
201
      mGirl.move(0 ,(height-h)/2, 0);
202
      mGirl.scale((float)width/bmpWidth);
203
      }
204
      
205
    Distorted.onSurfaceChanged(width, height);
206
    }
207 a2cb31e9 Leszek Koltunski
208
///////////////////////////////////////////////////////////////////////////////////////////////////
209
    
210 b9615af9 Leszek Koltunski
  public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
211
    {
212
    InputStream is = mView.getContext().getResources().openRawResource(R.raw.girl);
213
    Bitmap bitmap;
214 a2cb31e9 Leszek Koltunski
        
215 b9615af9 Leszek Koltunski
    try
216
      {
217
      bitmap = BitmapFactory.decodeStream(is);
218
      }
219
    finally
220
      {
221
      try
222 a2cb31e9 Leszek Koltunski
        {
223 b9615af9 Leszek Koltunski
        is.close();
224
        }
225
      catch(IOException e) { }
226
      }
227 a2cb31e9 Leszek Koltunski
      
228 b9615af9 Leszek Koltunski
    bmpHeight = bitmap.getHeight();
229
    bmpWidth  = bitmap.getWidth();
230 a2cb31e9 Leszek Koltunski
      
231 b9615af9 Leszek Koltunski
    mGirl = new DistortedBitmap(bitmap, 30);
232
    mGirl.addEventListener(this);
233 a2cb31e9 Leszek Koltunski
234 b9615af9 Leszek Koltunski
    mGirl.sink( diSink, sinkRegion, pLeft );
235
    mGirl.sink( diSink, sinkRegion, pRight);
236 a2cb31e9 Leszek Koltunski
237 b9615af9 Leszek Koltunski
    try
238
      {
239
      Distorted.onSurfaceCreated(mView.getContext());
240
      }
241
    catch(Exception ex)
242
      {
243
      android.util.Log.e("Renderer", ex.getMessage() );
244 a2cb31e9 Leszek Koltunski
      }
245 b9615af9 Leszek Koltunski
    }
246
  }