Project

General

Profile

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

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

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 e4237c21 Leszek Koltunski
import java.nio.ByteBuffer;
26
import java.nio.ByteOrder;
27 a2cb31e9 Leszek Koltunski
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 08eabc44 Leszek Koltunski
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 a2cb31e9 Leszek Koltunski
41 b9615af9 Leszek Koltunski
import android.app.Activity;
42 a2cb31e9 Leszek Koltunski
import android.graphics.Bitmap;
43
import android.graphics.BitmapFactory;
44
import android.opengl.GLES20;
45
import android.opengl.GLSurfaceView;
46 7cfeeb63 Leszek Koltunski
import android.os.Environment;
47 a2cb31e9 Leszek Koltunski
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49
50 e4237c21 Leszek Koltunski
class SaveRenderer implements GLSurfaceView.Renderer
51 b9615af9 Leszek Koltunski
  {
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 e4237c21 Leszek Koltunski
  private static boolean isSaving = false;
63
  private static String mPath;
64 b9615af9 Leszek Koltunski
65 e4237c21 Leszek Koltunski
  private static SaveWorkerThread mWorkerThread =null;
66 b9615af9 Leszek Koltunski
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 a2cb31e9 Leszek Koltunski
88
///////////////////////////////////////////////////////////////////////////////////////////////////
89
90 e4237c21 Leszek Koltunski
  public static void startThread(Activity act)
91 b9615af9 Leszek Koltunski
    {
92 e4237c21 Leszek Koltunski
    if( mWorkerThread ==null ) mWorkerThread = new SaveWorkerThread();
93
94
    mWorkerThread.startSending(act);
95 b9615af9 Leszek Koltunski
    }
96 a2cb31e9 Leszek Koltunski
97 7cfeeb63 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
98
99 e4237c21 Leszek Koltunski
  public static void stopThread()
100 b9615af9 Leszek Koltunski
    {
101 e4237c21 Leszek Koltunski
    mWorkerThread.stopSending();
102
    mWorkerThread = null;
103
    }
104 7cfeeb63 Leszek Koltunski
105 e4237c21 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
106 b9615af9 Leszek Koltunski
107 e4237c21 Leszek Koltunski
  public static void setSize(float s)
108
    {
109
    boobsSink = s;
110
    s0.set(boobsSink);
111 b9615af9 Leszek Koltunski
    }
112 a2cb31e9 Leszek Koltunski
113
///////////////////////////////////////////////////////////////////////////////////////////////////
114 b9615af9 Leszek Koltunski
115 e4237c21 Leszek Koltunski
  public static void Save()
116 b9615af9 Leszek Koltunski
    {
117 e4237c21 Leszek Koltunski
    if( isSaving==false )
118 b9615af9 Leszek Koltunski
      {
119 e4237c21 Leszek Koltunski
      isSaving = true;
120 b9615af9 Leszek Koltunski
121 e4237c21 Leszek Koltunski
      File file;
122
      int lowestNotFound = 1;
123
124
      while(true)
125 a2cb31e9 Leszek Koltunski
        {
126 e4237c21 Leszek Koltunski
        mPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/girl" + lowestNotFound +".png";
127
        file = new File(mPath);
128
129
        if( !file.exists() ) break;
130
131
        lowestNotFound++;
132 a2cb31e9 Leszek Koltunski
        }
133 b9615af9 Leszek Koltunski
      }
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 a2cb31e9 Leszek Koltunski
      
143 b9615af9 Leszek Koltunski
    mGirl.draw(System.currentTimeMillis());
144 e4237c21 Leszek Koltunski
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 b9615af9 Leszek Koltunski
    }
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 a2cb31e9 Leszek Koltunski
      }
172 b9615af9 Leszek Koltunski
    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 a2cb31e9 Leszek Koltunski
182
///////////////////////////////////////////////////////////////////////////////////////////////////
183
    
184 b9615af9 Leszek Koltunski
  public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
185
    {
186
    InputStream is = mView.getContext().getResources().openRawResource(R.raw.girl);
187
    Bitmap bitmap;
188 a2cb31e9 Leszek Koltunski
        
189 b9615af9 Leszek Koltunski
    try
190
      {
191
      bitmap = BitmapFactory.decodeStream(is);
192
      }
193
    finally
194
      {
195
      try
196 a2cb31e9 Leszek Koltunski
        {
197 b9615af9 Leszek Koltunski
        is.close();
198
        }
199
      catch(IOException e) { }
200
      }
201 a2cb31e9 Leszek Koltunski
      
202 b9615af9 Leszek Koltunski
    bmpHeight = bitmap.getHeight();
203
    bmpWidth  = bitmap.getWidth();
204 a2cb31e9 Leszek Koltunski
      
205 b9615af9 Leszek Koltunski
    mGirl = new DistortedBitmap(bitmap, 30);
206 a2cb31e9 Leszek Koltunski
207 b9615af9 Leszek Koltunski
    mGirl.sink( diSink, sinkRegion, pLeft );
208
    mGirl.sink( diSink, sinkRegion, pRight);
209 a2cb31e9 Leszek Koltunski
210 b9615af9 Leszek Koltunski
    try
211
      {
212
      Distorted.onSurfaceCreated(mView.getContext());
213
      }
214
    catch(Exception ex)
215
      {
216
      android.util.Log.e("Renderer", ex.getMessage() );
217 a2cb31e9 Leszek Koltunski
      }
218 b9615af9 Leszek Koltunski
    }
219
  }