Project

General

Profile

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

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

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 7589635e Leszek Koltunski
    diSink = new Dynamic1D();
84 b9615af9 Leszek Koltunski
    diSink.setCount(0.5f);
85
    diSink.setDuration(0);
86
    diSink.add(s0);
87
    }
88 a2cb31e9 Leszek Koltunski
89
///////////////////////////////////////////////////////////////////////////////////////////////////
90
91 e4237c21 Leszek Koltunski
  public static void startThread(Activity act)
92 b9615af9 Leszek Koltunski
    {
93 e4237c21 Leszek Koltunski
    if( mWorkerThread ==null ) mWorkerThread = new SaveWorkerThread();
94
95
    mWorkerThread.startSending(act);
96 b9615af9 Leszek Koltunski
    }
97 a2cb31e9 Leszek Koltunski
98 7cfeeb63 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
99
100 e4237c21 Leszek Koltunski
  public static void stopThread()
101 b9615af9 Leszek Koltunski
    {
102 e4237c21 Leszek Koltunski
    mWorkerThread.stopSending();
103
    mWorkerThread = null;
104
    }
105 7cfeeb63 Leszek Koltunski
106 e4237c21 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
107 b9615af9 Leszek Koltunski
108 e4237c21 Leszek Koltunski
  public static void setSize(float s)
109
    {
110
    boobsSink = s;
111
    s0.set(boobsSink);
112 b9615af9 Leszek Koltunski
    }
113 a2cb31e9 Leszek Koltunski
114
///////////////////////////////////////////////////////////////////////////////////////////////////
115 b9615af9 Leszek Koltunski
116 e4237c21 Leszek Koltunski
  public static void Save()
117 b9615af9 Leszek Koltunski
    {
118 e4237c21 Leszek Koltunski
    if( isSaving==false )
119 b9615af9 Leszek Koltunski
      {
120 e4237c21 Leszek Koltunski
      isSaving = true;
121 b9615af9 Leszek Koltunski
122 e4237c21 Leszek Koltunski
      File file;
123
      int lowestNotFound = 1;
124
125
      while(true)
126 a2cb31e9 Leszek Koltunski
        {
127 e4237c21 Leszek Koltunski
        mPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/girl" + lowestNotFound +".png";
128
        file = new File(mPath);
129
130
        if( !file.exists() ) break;
131
132
        lowestNotFound++;
133 a2cb31e9 Leszek Koltunski
        }
134 b9615af9 Leszek Koltunski
      }
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 a2cb31e9 Leszek Koltunski
      
144 b9615af9 Leszek Koltunski
    mGirl.draw(System.currentTimeMillis());
145 e4237c21 Leszek Koltunski
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 b9615af9 Leszek Koltunski
    }
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 7589635e Leszek Koltunski
      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 a2cb31e9 Leszek Koltunski
      }
175 b9615af9 Leszek Koltunski
    else
176
      {
177
      int h = (width*bmpHeight)/bmpWidth;
178 7589635e Leszek Koltunski
      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 b9615af9 Leszek Koltunski
      }
183
      
184
    Distorted.onSurfaceChanged(width, height);
185
    }
186 a2cb31e9 Leszek Koltunski
187
///////////////////////////////////////////////////////////////////////////////////////////////////
188
    
189 b9615af9 Leszek Koltunski
  public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
190
    {
191
    InputStream is = mView.getContext().getResources().openRawResource(R.raw.girl);
192
    Bitmap bitmap;
193 a2cb31e9 Leszek Koltunski
        
194 b9615af9 Leszek Koltunski
    try
195
      {
196
      bitmap = BitmapFactory.decodeStream(is);
197
      }
198
    finally
199
      {
200
      try
201 a2cb31e9 Leszek Koltunski
        {
202 b9615af9 Leszek Koltunski
        is.close();
203
        }
204
      catch(IOException e) { }
205
      }
206 a2cb31e9 Leszek Koltunski
      
207 b9615af9 Leszek Koltunski
    bmpHeight = bitmap.getHeight();
208
    bmpWidth  = bitmap.getWidth();
209 a2cb31e9 Leszek Koltunski
      
210 b9615af9 Leszek Koltunski
    mGirl = new DistortedBitmap(bitmap, 30);
211 a2cb31e9 Leszek Koltunski
212 7bf107f7 Leszek Koltunski
    mGirl.sink( diSink, pLeft , sinkRegion);
213
    mGirl.sink( diSink, pRight, sinkRegion);
214 a2cb31e9 Leszek Koltunski
215 b9615af9 Leszek Koltunski
    try
216
      {
217
      Distorted.onSurfaceCreated(mView.getContext());
218
      }
219
    catch(Exception ex)
220
      {
221
      android.util.Log.e("Renderer", ex.getMessage() );
222 a2cb31e9 Leszek Koltunski
      }
223 b9615af9 Leszek Koltunski
    }
224
  }