Project

General

Profile

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

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

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.nio.ByteBuffer;
26
import java.nio.ByteOrder;
27

    
28
import javax.microedition.khronos.egl.EGLConfig;
29
import javax.microedition.khronos.opengles.GL10;
30

    
31
import org.distorted.examples.R;
32

    
33
import org.distorted.library.Distorted;
34
import org.distorted.library.DistortedBitmap;
35
import org.distorted.library.EffectTypes;
36
import org.distorted.library.type.Float1D;
37
import org.distorted.library.type.Float2D;
38
import org.distorted.library.type.Float4D;
39
import org.distorted.library.type.Interpolator1D;
40

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

    
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49

    
50
class SaveRenderer implements GLSurfaceView.Renderer
51
  {
52
  private GLSurfaceView mView;
53
  private static DistortedBitmap mGirl;
54
  private Float2D pLeft, pRight;
55
  private Float4D sinkRegion;
56
  private static Interpolator1D diSink;
57
  private static Float1D s0;
58

    
59
  private int bmpHeight, bmpWidth;
60
  private static int scrHeight, scrWidth;
61
  private static float boobsSink;
62
  private static boolean isSaving = false;
63
  private static String mPath;
64

    
65
  private static SaveWorkerThread mWorkerThread =null;
66

    
67
///////////////////////////////////////////////////////////////////////////////////////////////////
68

    
69
  public SaveRenderer(GLSurfaceView v)
70
    {
71
    mView = v;
72
      
73
    boobsSink  = 1.0f;
74
      
75
    pLeft = new Float2D(132, 264);
76
    pRight= new Float2D(247, 264);
77
      
78
    sinkRegion = new Float4D(0,0,60,60);
79
      
80
    s0 = new Float1D(boobsSink);
81
      
82
    diSink = new Interpolator1D();
83
    diSink.setCount(0.5f);
84
    diSink.setDuration(0);
85
    diSink.add(s0);
86
    }
87

    
88
///////////////////////////////////////////////////////////////////////////////////////////////////
89

    
90
  public static void startThread(Activity act)
91
    {
92
    if( mWorkerThread ==null ) mWorkerThread = new SaveWorkerThread();
93

    
94
    mWorkerThread.startSending(act);
95
    }
96

    
97
///////////////////////////////////////////////////////////////////////////////////////////////////
98

    
99
  public static void stopThread()
100
    {
101
    mWorkerThread.stopSending();
102
    mWorkerThread = null;
103
    }
104

    
105
///////////////////////////////////////////////////////////////////////////////////////////////////
106

    
107
  public static void setSize(float s)
108
    {
109
    boobsSink = s;
110
    s0.set(boobsSink);
111
    }
112

    
113
///////////////////////////////////////////////////////////////////////////////////////////////////
114

    
115
  public static void Save()
116
    {
117
    if( isSaving==false )
118
      {
119
      isSaving = true;
120

    
121
      File file;
122
      int lowestNotFound = 1;
123

    
124
      while(true)
125
        {
126
        mPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/girl" + lowestNotFound +".png";
127
        file = new File(mPath);
128

    
129
        if( !file.exists() ) break;
130

    
131
        lowestNotFound++;
132
        }
133
      }
134
    }
135

    
136
///////////////////////////////////////////////////////////////////////////////////////////////////
137
   
138
  public void onDrawFrame(GL10 glUnused)
139
    {
140
    GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
141
    GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
142
      
143
    mGirl.draw(System.currentTimeMillis());
144

    
145
    if( isSaving )  // we should save the buffer to location pointed at by mPath
146
      {
147
      ByteBuffer buf = ByteBuffer.allocateDirect( scrWidth*scrHeight*4 );
148
      buf.order(ByteOrder.LITTLE_ENDIAN);
149
      GLES20.glReadPixels( 0, 0, scrWidth, scrHeight , GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, buf);
150

    
151
      mWorkerThread.newBuffer(buf,scrWidth,scrHeight,mPath);
152

    
153
      isSaving = false;
154
      }
155
    }
156

    
157
///////////////////////////////////////////////////////////////////////////////////////////////////
158
    
159
  public void onSurfaceChanged(GL10 glUnused, int width, int height)
160
    {
161
    scrWidth = width;
162
    scrHeight= height;
163

    
164
    mGirl.abortEffects(EffectTypes.MATRIX);
165
      
166
    if( bmpHeight/bmpWidth > height/width )
167
      {
168
      int w = (height*bmpWidth)/bmpHeight;
169
      mGirl.move((width-w)/2 ,0, 0);
170
      mGirl.scale((float)height/bmpHeight);
171
      }
172
    else
173
      {
174
      int h = (width*bmpHeight)/bmpWidth;
175
      mGirl.move(0 ,(height-h)/2, 0);
176
      mGirl.scale((float)width/bmpWidth);
177
      }
178
      
179
    Distorted.onSurfaceChanged(width, height);
180
    }
181

    
182
///////////////////////////////////////////////////////////////////////////////////////////////////
183
    
184
  public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
185
    {
186
    InputStream is = mView.getContext().getResources().openRawResource(R.raw.girl);
187
    Bitmap bitmap;
188
        
189
    try
190
      {
191
      bitmap = BitmapFactory.decodeStream(is);
192
      }
193
    finally
194
      {
195
      try
196
        {
197
        is.close();
198
        }
199
      catch(IOException e) { }
200
      }
201
      
202
    bmpHeight = bitmap.getHeight();
203
    bmpWidth  = bitmap.getWidth();
204
      
205
    mGirl = new DistortedBitmap(bitmap, 30);
206

    
207
    mGirl.sink( diSink, sinkRegion, pLeft );
208
    mGirl.sink( diSink, sinkRegion, pRight);
209

    
210
    try
211
      {
212
      Distorted.onSurfaceCreated(mView.getContext());
213
      }
214
    catch(Exception ex)
215
      {
216
      android.util.Log.e("Renderer", ex.getMessage() );
217
      }
218
    }
219
  }
(2-2/4)