Project

General

Profile

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

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

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 7589635e Leszek Koltunski
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 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 a2cb31e9 Leszek Koltunski
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50
51 e4237c21 Leszek Koltunski
class SaveRenderer implements GLSurfaceView.Renderer
52 b9615af9 Leszek Koltunski
  {
53
  private GLSurfaceView mView;
54
  private static DistortedBitmap mGirl;
55 7589635e Leszek Koltunski
  private Static2D pLeft, pRight;
56
  private Static4D sinkRegion;
57
  private static Dynamic1D diSink;
58
  private static Static1D s0;
59 b9615af9 Leszek Koltunski
60
  private int bmpHeight, bmpWidth;
61
  private static int scrHeight, scrWidth;
62
  private static float boobsSink;
63 e4237c21 Leszek Koltunski
  private static boolean isSaving = false;
64
  private static String mPath;
65 b9615af9 Leszek Koltunski
66 e4237c21 Leszek Koltunski
  private static SaveWorkerThread mWorkerThread =null;
67 b9615af9 Leszek Koltunski
68 a2cb31e9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
69
70 b9615af9 Leszek Koltunski
  public SaveRenderer(GLSurfaceView v)
71
    {
72
    mView = v;
73 a2cb31e9 Leszek Koltunski
      
74 b9615af9 Leszek Koltunski
    boobsSink  = 1.0f;
75 a2cb31e9 Leszek Koltunski
      
76 7589635e Leszek Koltunski
    pLeft = new Static2D(132, 264);
77
    pRight= new Static2D(247, 264);
78 a2cb31e9 Leszek Koltunski
      
79 7589635e Leszek Koltunski
    sinkRegion = new Static4D(0,0,60,60);
80 a2cb31e9 Leszek Koltunski
      
81 7589635e Leszek Koltunski
    s0 = new Static1D(boobsSink);
82 a2cb31e9 Leszek Koltunski
      
83 f988589e Leszek Koltunski
    diSink = new Dynamic1D(0,0.5f);
84 b9615af9 Leszek Koltunski
    diSink.add(s0);
85
    }
86 a2cb31e9 Leszek Koltunski
87
///////////////////////////////////////////////////////////////////////////////////////////////////
88
89 e4237c21 Leszek Koltunski
  public static void startThread(Activity act)
90 b9615af9 Leszek Koltunski
    {
91 e4237c21 Leszek Koltunski
    if( mWorkerThread ==null ) mWorkerThread = new SaveWorkerThread();
92
93
    mWorkerThread.startSending(act);
94 b9615af9 Leszek Koltunski
    }
95 a2cb31e9 Leszek Koltunski
96 7cfeeb63 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
97
98 e4237c21 Leszek Koltunski
  public static void stopThread()
99 b9615af9 Leszek Koltunski
    {
100 e4237c21 Leszek Koltunski
    mWorkerThread.stopSending();
101
    mWorkerThread = null;
102
    }
103 7cfeeb63 Leszek Koltunski
104 e4237c21 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
105 b9615af9 Leszek Koltunski
106 e4237c21 Leszek Koltunski
  public static void setSize(float s)
107
    {
108
    boobsSink = s;
109
    s0.set(boobsSink);
110 b9615af9 Leszek Koltunski
    }
111 a2cb31e9 Leszek Koltunski
112
///////////////////////////////////////////////////////////////////////////////////////////////////
113 b9615af9 Leszek Koltunski
114 e4237c21 Leszek Koltunski
  public static void Save()
115 b9615af9 Leszek Koltunski
    {
116 e4237c21 Leszek Koltunski
    if( isSaving==false )
117 b9615af9 Leszek Koltunski
      {
118 e4237c21 Leszek Koltunski
      isSaving = true;
119 b9615af9 Leszek Koltunski
120 e4237c21 Leszek Koltunski
      File file;
121
      int lowestNotFound = 1;
122
123
      while(true)
124 a2cb31e9 Leszek Koltunski
        {
125 e4237c21 Leszek Koltunski
        mPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/girl" + lowestNotFound +".png";
126
        file = new File(mPath);
127
128
        if( !file.exists() ) break;
129
130
        lowestNotFound++;
131 a2cb31e9 Leszek Koltunski
        }
132 b9615af9 Leszek Koltunski
      }
133
    }
134
135
///////////////////////////////////////////////////////////////////////////////////////////////////
136
   
137
  public void onDrawFrame(GL10 glUnused)
138
    {
139
    GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
140
    GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
141 a2cb31e9 Leszek Koltunski
      
142 b9615af9 Leszek Koltunski
    mGirl.draw(System.currentTimeMillis());
143 e4237c21 Leszek Koltunski
144
    if( isSaving )  // we should save the buffer to location pointed at by mPath
145
      {
146
      ByteBuffer buf = ByteBuffer.allocateDirect( scrWidth*scrHeight*4 );
147
      buf.order(ByteOrder.LITTLE_ENDIAN);
148
      GLES20.glReadPixels( 0, 0, scrWidth, scrHeight , GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, buf);
149
150
      mWorkerThread.newBuffer(buf,scrWidth,scrHeight,mPath);
151
152
      isSaving = false;
153
      }
154 b9615af9 Leszek Koltunski
    }
155
156
///////////////////////////////////////////////////////////////////////////////////////////////////
157
    
158
  public void onSurfaceChanged(GL10 glUnused, int width, int height)
159
    {
160
    scrWidth = width;
161
    scrHeight= height;
162
163
    mGirl.abortEffects(EffectTypes.MATRIX);
164
      
165
    if( bmpHeight/bmpWidth > height/width )
166
      {
167
      int w = (height*bmpWidth)/bmpHeight;
168 7589635e Leszek Koltunski
      float factor = (float)height/bmpHeight;
169
170
      mGirl.move( new Static3D((width-w)/2,0,0) );
171 f988589e Leszek Koltunski
      mGirl.scale(factor);
172 a2cb31e9 Leszek Koltunski
      }
173 b9615af9 Leszek Koltunski
    else
174
      {
175
      int h = (width*bmpHeight)/bmpWidth;
176 7589635e Leszek Koltunski
      float factor = (float)width/bmpWidth;
177
178
      mGirl.move( new Static3D(0,(height-h)/2,0) );
179 f988589e Leszek Koltunski
      mGirl.scale(factor);
180 b9615af9 Leszek Koltunski
      }
181
      
182
    Distorted.onSurfaceChanged(width, height);
183
    }
184 a2cb31e9 Leszek Koltunski
185
///////////////////////////////////////////////////////////////////////////////////////////////////
186
    
187 b9615af9 Leszek Koltunski
  public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
188
    {
189
    InputStream is = mView.getContext().getResources().openRawResource(R.raw.girl);
190
    Bitmap bitmap;
191 a2cb31e9 Leszek Koltunski
        
192 b9615af9 Leszek Koltunski
    try
193
      {
194
      bitmap = BitmapFactory.decodeStream(is);
195
      }
196
    finally
197
      {
198
      try
199 a2cb31e9 Leszek Koltunski
        {
200 b9615af9 Leszek Koltunski
        is.close();
201
        }
202
      catch(IOException e) { }
203
      }
204 a2cb31e9 Leszek Koltunski
      
205 b9615af9 Leszek Koltunski
    bmpHeight = bitmap.getHeight();
206
    bmpWidth  = bitmap.getWidth();
207 a2cb31e9 Leszek Koltunski
      
208 b9615af9 Leszek Koltunski
    mGirl = new DistortedBitmap(bitmap, 30);
209 a2cb31e9 Leszek Koltunski
210 7bf107f7 Leszek Koltunski
    mGirl.sink( diSink, pLeft , sinkRegion);
211
    mGirl.sink( diSink, pRight, sinkRegion);
212 a2cb31e9 Leszek Koltunski
213 b9615af9 Leszek Koltunski
    try
214
      {
215
      Distorted.onSurfaceCreated(mView.getContext());
216
      }
217
    catch(Exception ex)
218
      {
219
      android.util.Log.e("Renderer", ex.getMessage() );
220 a2cb31e9 Leszek Koltunski
      }
221 b9615af9 Leszek Koltunski
    }
222
  }