Project

General

Profile

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

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

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(0,0.5f);
84
    diSink.add(s0);
85
    }
86

    
87
///////////////////////////////////////////////////////////////////////////////////////////////////
88

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

    
93
    mWorkerThread.startSending(act);
94
    }
95

    
96
///////////////////////////////////////////////////////////////////////////////////////////////////
97

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

    
104
///////////////////////////////////////////////////////////////////////////////////////////////////
105

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

    
112
///////////////////////////////////////////////////////////////////////////////////////////////////
113

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

    
120
      File file;
121
      int lowestNotFound = 1;
122

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

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

    
130
        lowestNotFound++;
131
        }
132
      }
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
      
142
    mGirl.draw(System.currentTimeMillis());
143

    
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
    }
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
      float factor = (float)height/bmpHeight;
169

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

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

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

    
210
    mGirl.sink( diSink, pLeft , sinkRegion);
211
    mGirl.sink( diSink, pRight, sinkRegion);
212

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