Project

General

Profile

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

examples / src / main / java / org / distorted / examples / save / SaveRenderer.java @ 7bf107f7

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.Dynamic1D;
37
import org.distorted.library.type.Static1D;
38
import org.distorted.library.type.Static2D;
39
import org.distorted.library.type.Static3D;
40
import org.distorted.library.type.Static4D;
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

    
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50

    
51
class SaveRenderer implements GLSurfaceView.Renderer
52
  {
53
  private GLSurfaceView mView;
54
  private static DistortedBitmap mGirl;
55
  private Static2D pLeft, pRight;
56
  private Static4D sinkRegion;
57
  private static Dynamic1D diSink;
58
  private static Static1D s0;
59

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

    
66
  private static SaveWorkerThread mWorkerThread =null;
67

    
68
///////////////////////////////////////////////////////////////////////////////////////////////////
69

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

    
89
///////////////////////////////////////////////////////////////////////////////////////////////////
90

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

    
95
    mWorkerThread.startSending(act);
96
    }
97

    
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99

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

    
106
///////////////////////////////////////////////////////////////////////////////////////////////////
107

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

    
114
///////////////////////////////////////////////////////////////////////////////////////////////////
115

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

    
122
      File file;
123
      int lowestNotFound = 1;
124

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

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

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

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

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

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

    
154
      isSaving = false;
155
      }
156
    }
157

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

    
165
    mGirl.abortEffects(EffectTypes.MATRIX);
166
      
167
    if( bmpHeight/bmpWidth > height/width )
168
      {
169
      int w = (height*bmpWidth)/bmpHeight;
170
      float factor = (float)height/bmpHeight;
171

    
172
      mGirl.move( new Static3D((width-w)/2,0,0) );
173
      mGirl.scale( new Static3D(factor,factor,factor) );
174
      }
175
    else
176
      {
177
      int h = (width*bmpHeight)/bmpWidth;
178
      float factor = (float)width/bmpWidth;
179

    
180
      mGirl.move( new Static3D(0,(height-h)/2,0) );
181
      mGirl.scale(new Static3D(factor,factor,factor));
182
      }
183
      
184
    Distorted.onSurfaceChanged(width, height);
185
    }
186

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

    
212
    mGirl.sink( diSink, pLeft , sinkRegion);
213
    mGirl.sink( diSink, pRight, sinkRegion);
214

    
215
    try
216
      {
217
      Distorted.onSurfaceCreated(mView.getContext());
218
      }
219
    catch(Exception ex)
220
      {
221
      android.util.Log.e("Renderer", ex.getMessage() );
222
      }
223
    }
224
  }
(2-2/4)