Project

General

Profile

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

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

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.save;
21

    
22
import java.io.File;
23
import java.io.IOException;
24
import java.io.InputStream;
25
import java.util.Vector;
26

    
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
import org.distorted.library.message.EffectListener;
35
import org.distorted.library.message.EffectMessage;
36
import org.distorted.library.EffectTypes;
37
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

    
42
import android.app.Activity;
43
import android.graphics.Bitmap;
44
import android.graphics.BitmapFactory;
45
import android.opengl.GLES20;
46
import android.opengl.GLSurfaceView;
47
import android.os.Environment;
48
import android.widget.Toast;
49

    
50
///////////////////////////////////////////////////////////////////////////////////////////////////
51

    
52
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
///////////////////////////////////////////////////////////////////////////////////////////////////
86

    
87
  public SaveRenderer(GLSurfaceView v)
88
    {
89
    mView = v;
90
      
91
    boobsSink  = 1.0f;
92
      
93
    pLeft = new Float2D(132, 264);
94
    pRight= new Float2D(247, 264);
95
      
96
    sinkRegion = new Float4D(0,0,60,60);
97
      
98
    s0 = new Float1D(boobsSink);
99
      
100
    diSink = new Interpolator1D();
101
    diSink.setCount(0.5f);
102
    diSink.setDuration(0);
103
    diSink.add(s0);
104

    
105
    mSaveInfo = new Vector<>();
106
    }
107

    
108
///////////////////////////////////////////////////////////////////////////////////////////////////
109

    
110
  public static void setSize(float s)
111
    {
112
    boobsSink = s;
113
    s0.set(boobsSink);
114
    }
115

    
116
///////////////////////////////////////////////////////////////////////////////////////////////////
117

    
118
  public static void Save()
119
    {
120
    File file;
121
    int lowestNotFound = 1;
122
    String path;
123

    
124
    while(true)
125
      {
126
      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
      }
137
    }
138

    
139
///////////////////////////////////////////////////////////////////////////////////////////////////
140
// 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
        }
154
      }
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
        {
164
        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
        }
188
      }
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
      
198
    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
      }
216
    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

    
226
///////////////////////////////////////////////////////////////////////////////////////////////////
227
    
228
  public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
229
    {
230
    InputStream is = mView.getContext().getResources().openRawResource(R.raw.girl);
231
    Bitmap bitmap;
232
        
233
    try
234
      {
235
      bitmap = BitmapFactory.decodeStream(is);
236
      }
237
    finally
238
      {
239
      try
240
        {
241
        is.close();
242
        }
243
      catch(IOException e) { }
244
      }
245
      
246
    bmpHeight = bitmap.getHeight();
247
    bmpWidth  = bitmap.getWidth();
248
      
249
    mGirl = new DistortedBitmap(bitmap, 30);
250
    mGirl.addEventListener(this);
251

    
252
    mGirl.sink( diSink, sinkRegion, pLeft );
253
    mGirl.sink( diSink, sinkRegion, pRight);
254

    
255
    try
256
      {
257
      Distorted.onSurfaceCreated(mView.getContext());
258
      }
259
    catch(Exception ex)
260
      {
261
      android.util.Log.e("Renderer", ex.getMessage() );
262
      }
263
    }
264
  }
(2-2/3)