Project

General

Profile

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

examples / src / main / java / org / distorted / examples / movingeffects / MovingEffectsRenderer.java @ 698ad0a8

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.movingeffects;
21

    
22
import javax.microedition.khronos.egl.EGLConfig;
23
import javax.microedition.khronos.opengles.GL10;
24

    
25
import android.graphics.Bitmap;
26
import android.graphics.Canvas;
27
import android.graphics.Paint;
28
import android.graphics.Paint.Style;
29
import android.opengl.GLSurfaceView;
30

    
31
import org.distorted.library.effect.FragmentEffectAlpha;
32
import org.distorted.library.effect.FragmentEffectChroma;
33
import org.distorted.library.effect.VertexEffectDeform;
34
import org.distorted.library.effect.VertexEffectSink;
35
import org.distorted.library.effect.VertexEffectSwirl;
36
import org.distorted.library.main.DistortedLibrary;
37
import org.distorted.library.main.DistortedScreen;
38
import org.distorted.library.main.DistortedTexture;
39
import org.distorted.library.main.DistortedEffects;
40
import org.distorted.library.mesh.MeshRectangles;
41

    
42
///////////////////////////////////////////////////////////////////////////////////////////////////
43

    
44
class MovingEffectsRenderer implements GLSurfaceView.Renderer
45
   {  
46
   private MovingEffectsSurfaceView mView;
47
   private Canvas mCanvas;
48
   private Bitmap mBitmap;
49
   private Paint mPaint;
50
   private int texW, texH;
51

    
52
   private DistortedEffects mEffects;
53
   private DistortedTexture mTexture;
54
   private DistortedScreen mScreen;
55
   private MeshRectangles mMesh;
56
   private boolean mRefresh;
57

    
58
///////////////////////////////////////////////////////////////////////////////////////////////////
59

    
60
   MovingEffectsRenderer(MovingEffectsSurfaceView v)
61
     {    
62
     mPaint = new Paint();
63
     mPaint.setAntiAlias(true);
64
     mPaint.setFakeBoldText(true);
65
     mPaint.setStyle(Style.FILL);
66

    
67
     mView   = v;
68
     mEffects= new DistortedEffects();
69
     mScreen = new DistortedScreen();
70
     mRefresh= true;
71
     }
72

    
73
///////////////////////////////////////////////////////////////////////////////////////////////////
74

    
75
   private void drawBackground()
76
     {
77
     int NW, NH;
78

    
79
     if( texH<texW )
80
       {
81
       NH = 10;
82
       NW = NH*texW/texH;
83
       }
84
     else
85
       {
86
       NW = 10;
87
       NH = NW*texH/texW;
88
       }
89

    
90
     mPaint.setColor(0xff008800);
91
     mCanvas.drawRect(0, 0, texW, texH, mPaint);
92
     mPaint.setColor(0xffffffff);
93
         
94
     for(int i=0; i<=NH ; i++ ) mCanvas.drawRect( 0, texH*i/NH -2,  texW,  texH*i/NH + 2, mPaint);
95
     for(int i=0; i<=NW ; i++ ) mCanvas.drawRect( texW*i/NW - 2, 0, texW*i/NW + 2,  texH, mPaint);
96
     }
97
   
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99

    
100
   DistortedEffects getEffects()
101
     {
102
     return mEffects;
103
     }
104

    
105
///////////////////////////////////////////////////////////////////////////////////////////////////
106

    
107
   void setRefresh()
108
     {
109
     mRefresh = true;
110
     }
111

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

    
114
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
115
     {
116
     VertexEffectSwirl.enable();
117
     VertexEffectDeform.enable();
118
     VertexEffectSink.enable();
119
     FragmentEffectChroma.enable();
120
     FragmentEffectAlpha.enable();
121

    
122
     try
123
       {
124
       DistortedLibrary.onCreate(mView.getContext());
125
       }
126
     catch(Exception ex)
127
       {
128
       android.util.Log.e("MovingEffects", ex.getMessage() );
129
       }
130
     }
131

    
132
///////////////////////////////////////////////////////////////////////////////////////////////////
133

    
134
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
135
     {
136
     texW = width;
137
     texH = height;
138

    
139
     if( mTexture!=null ) mTexture.markForDeletion();
140
     mTexture = new DistortedTexture();
141
     mBitmap  = Bitmap.createBitmap(texW,texH, Bitmap.Config.ARGB_8888);
142
     mCanvas  = new Canvas(mBitmap);
143

    
144
     if( mMesh!=null ) mMesh.markForDeletion();
145
     mMesh = new MeshRectangles(80,80*texH/texW);
146
     mMesh.setStretch(texW,texH,0);
147

    
148
     mScreen.detachAll();
149
     mScreen.attach(mTexture,mEffects,mMesh);
150
     mScreen.resize(texW, texH);
151
     mView.onSurfaceChanged(texW,texH);
152

    
153
     mRefresh = true;
154
     }
155
   
156
///////////////////////////////////////////////////////////////////////////////////////////////////
157
   
158
   public void onDrawFrame(GL10 glUnused)
159
     {   
160
     long time = System.currentTimeMillis();
161
      
162
     if (mView.getCurrentEffect() == MovingEffectsSurfaceView.EFFECT_POINTS && mRefresh )
163
       {
164
       drawBackground();
165
       mView.drawCurve(mCanvas,time);
166
       mTexture.setTexture(mBitmap);
167
       mRefresh = false;
168
       }
169
      
170
     mScreen.render(time);
171
     }
172

    
173
///////////////////////////////////////////////////////////////////////////////////////////////////
174
}
(2-2/3)