Project

General

Profile

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

examples / src / main / java / org / distorted / examples / save / SaveRenderer.java @ 13efa930

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