Project

General

Profile

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

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

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.Static3D;
39
import org.distorted.library.type.Static4D;
40

    
41
import android.graphics.Bitmap;
42
import android.graphics.BitmapFactory;
43
import android.opengl.GLES20;
44
import android.opengl.GLSurfaceView;
45
import android.os.Environment;
46

    
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48

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

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

    
64
///////////////////////////////////////////////////////////////////////////////////////////////////
65

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

    
83
///////////////////////////////////////////////////////////////////////////////////////////////////
84

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

    
91
///////////////////////////////////////////////////////////////////////////////////////////////////
92

    
93
  void Save()
94
    {
95
    if( !isSaving )
96
      {
97
      isSaving = true;
98

    
99
      File file;
100
      int lowestNotFound = 1;
101

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

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

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

    
114
///////////////////////////////////////////////////////////////////////////////////////////////////
115
   
116
  public void onDrawFrame(GL10 glUnused)
117
    {
118
    GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
119
      
120
    mGirl.draw(System.currentTimeMillis());
121

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

    
128
      SaveWorkerThread.newBuffer(buf,scrWidth,scrHeight,mPath);
129

    
130
      isSaving = false;
131
      }
132
    }
133

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

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

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

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

    
163
///////////////////////////////////////////////////////////////////////////////////////////////////
164
    
165
  public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
166
    {
167
    GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
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)