Project

General

Profile

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

examples / src / main / java / org / distorted / examples / save / SaveRenderer.java @ 13a5c5cd

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.graphics.Bitmap;
43
import android.graphics.BitmapFactory;
44
import android.opengl.GLES20;
45
import android.opengl.GLSurfaceView;
46
import android.os.Environment;
47

    
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49

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

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

    
65
///////////////////////////////////////////////////////////////////////////////////////////////////
66

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

    
84
///////////////////////////////////////////////////////////////////////////////////////////////////
85

    
86
  void setSize(float s)
87
    {
88
    boobsSink = s;
89
    s0.set(boobsSink);
90
    }
91

    
92
///////////////////////////////////////////////////////////////////////////////////////////////////
93

    
94
  void Save()
95
    {
96
    if( isSaving==false )
97
      {
98
      isSaving = true;
99

    
100
      File file;
101
      int lowestNotFound = 1;
102

    
103
      while(true)
104
        {
105
        mPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/girl" + lowestNotFound +".png";
106
        file = new File(mPath);
107

    
108
        if( !file.exists() ) break;
109

    
110
        lowestNotFound++;
111
        }
112
      }
113
    }
114

    
115
///////////////////////////////////////////////////////////////////////////////////////////////////
116
   
117
  public void onDrawFrame(GL10 glUnused)
118
    {
119
    GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
120
    GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
121
      
122
    mGirl.draw(System.currentTimeMillis());
123

    
124
    if( isSaving )  // we should save the buffer to location pointed at by mPath
125
      {
126
      ByteBuffer buf = ByteBuffer.allocateDirect( scrWidth*scrHeight*4 );
127
      buf.order(ByteOrder.LITTLE_ENDIAN);
128
      GLES20.glReadPixels( 0, 0, scrWidth, scrHeight , GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, buf);
129

    
130
      SaveWorkerThread.newBuffer(buf,scrWidth,scrHeight,mPath);
131

    
132
      isSaving = false;
133
      }
134
    }
135

    
136
///////////////////////////////////////////////////////////////////////////////////////////////////
137
    
138
  public void onSurfaceChanged(GL10 glUnused, int width, int height)
139
    {
140
    scrWidth = width;
141
    scrHeight= height;
142

    
143
    mGirl.abortEffects(EffectTypes.MATRIX);
144
      
145
    if( bmpHeight/bmpWidth > height/width )
146
      {
147
      int w = (height*bmpWidth)/bmpHeight;
148
      float factor = (float)height/bmpHeight;
149

    
150
      mGirl.move( new Static3D((width-w)/2,0,0) );
151
      mGirl.scale(factor);
152
      }
153
    else
154
      {
155
      int h = (width*bmpHeight)/bmpWidth;
156
      float factor = (float)width/bmpWidth;
157

    
158
      mGirl.move( new Static3D(0,(height-h)/2,0) );
159
      mGirl.scale(factor);
160
      }
161
      
162
    Distorted.onSurfaceChanged(width, height);
163
    }
164

    
165
///////////////////////////////////////////////////////////////////////////////////////////////////
166
    
167
  public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
168
    {
169
    InputStream is = mView.getContext().getResources().openRawResource(R.raw.girl);
170
    Bitmap bitmap;
171
        
172
    try
173
      {
174
      bitmap = BitmapFactory.decodeStream(is);
175
      }
176
    finally
177
      {
178
      try
179
        {
180
        is.close();
181
        }
182
      catch(IOException e) { }
183
      }
184
      
185
    bmpHeight = bitmap.getHeight();
186
    bmpWidth  = bitmap.getWidth();
187
      
188
    mGirl = new DistortedBitmap(bitmap, 30);
189

    
190
    mGirl.sink( diSink, pLeft , sinkRegion);
191
    mGirl.sink( diSink, pRight, sinkRegion);
192

    
193
    try
194
      {
195
      Distorted.onSurfaceCreated(mView.getContext());
196
      }
197
    catch(Exception ex)
198
      {
199
      android.util.Log.e("Renderer", ex.getMessage() );
200
      }
201
    }
202
  }
(2-2/4)