Project

General

Profile

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

examples / src / main / java / org / distorted / examples / movingeffects / MovingEffectsRenderer.java @ b0ebdf5e

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.DistortedScreen;
32
import org.distorted.library.DistortedTexture;
33
import org.distorted.library.DistortedEffects;
34
import org.distorted.library.EffectNames;
35
import org.distorted.library.MeshFlat;
36
import org.distorted.library.Distorted;
37

    
38
///////////////////////////////////////////////////////////////////////////////////////////////////
39

    
40
class MovingEffectsRenderer implements GLSurfaceView.Renderer
41
   {  
42
   private MovingEffectsSurfaceView mView;
43
   private Canvas mCanvas;
44
   private Bitmap mBitmap;
45
   private Paint mPaint;
46
   private int texW, texH;
47

    
48
   private DistortedEffects mEffects;
49
   private DistortedTexture mTexture;
50
   private DistortedScreen mScreen;
51
   private boolean mRefresh;
52

    
53
///////////////////////////////////////////////////////////////////////////////////////////////////
54

    
55
   MovingEffectsRenderer(MovingEffectsSurfaceView v)
56
     {    
57
     mPaint = new Paint();
58
     mPaint.setAntiAlias(true);
59
     mPaint.setFakeBoldText(true);
60
     mPaint.setStyle(Style.FILL);
61

    
62
     mView   = v;
63
     mEffects= new DistortedEffects();
64
     mScreen = new DistortedScreen();
65
     mRefresh= true;
66
     }
67

    
68
///////////////////////////////////////////////////////////////////////////////////////////////////
69

    
70
   private void drawBackground()
71
     {
72
     int NW, NH;
73

    
74
     if( texH<texW )
75
       {
76
       NH = 10;
77
       NW = NH*texW/texH;
78
       }
79
     else
80
       {
81
       NW = 10;
82
       NH = NW*texH/texW;
83
       }
84

    
85
     mPaint.setColor(0xff008800);
86
     mCanvas.drawRect(0, 0, texW, texH, mPaint);
87
     mPaint.setColor(0xffffffff);
88
         
89
     for(int i=0; i<=NH ; i++ ) mCanvas.drawRect( 0, texH*i/NH -2,  texW,  texH*i/NH + 2, mPaint);
90
     for(int i=0; i<=NW ; i++ ) mCanvas.drawRect( texW*i/NW - 2, 0, texW*i/NW + 2,  texH, mPaint);
91
     }
92
   
93
///////////////////////////////////////////////////////////////////////////////////////////////////
94

    
95
   DistortedEffects getEffects()
96
     {
97
     return mEffects;
98
     }
99

    
100
///////////////////////////////////////////////////////////////////////////////////////////////////
101

    
102
   void setRefresh()
103
     {
104
     mRefresh = true;
105
     }
106

    
107
///////////////////////////////////////////////////////////////////////////////////////////////////
108

    
109
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
110
     {
111
     DistortedEffects.enableEffect(EffectNames.SWIRL);
112
     DistortedEffects.enableEffect(EffectNames.SMOOTH_ALPHA);
113
     DistortedEffects.enableEffect(EffectNames.SMOOTH_CHROMA);
114
     DistortedEffects.enableEffect(EffectNames.DISTORT);
115
     DistortedEffects.enableEffect(EffectNames.SINK);
116

    
117
     try
118
       {
119
       Distorted.onCreate(mView.getContext());
120
       }
121
     catch(Exception ex)
122
       {
123
       android.util.Log.e("Renderer", ex.getMessage() );
124
       }
125
     }
126

    
127
///////////////////////////////////////////////////////////////////////////////////////////////////
128

    
129
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
130
     {
131
     texW = width;
132
     texH = height;
133

    
134
     if( mTexture!=null ) mTexture.markForDeletion();
135
     mTexture = new DistortedTexture(texW,texH);
136
     mBitmap  = Bitmap.createBitmap(texW,texH, Bitmap.Config.ARGB_8888);
137
     mCanvas  = new Canvas(mBitmap);
138

    
139
     mScreen.detachAll();
140
     mScreen.attach(mTexture,mEffects,new MeshFlat(80,80*texH/texW));
141
     mScreen.resize(texW, texH);
142
     mView.onSurfaceChanged(texW,texH);
143

    
144
     mRefresh = true;
145
     }
146
   
147
///////////////////////////////////////////////////////////////////////////////////////////////////
148
   
149
   public void onDrawFrame(GL10 glUnused)
150
     {   
151
     long time = System.currentTimeMillis();
152
      
153
     if (mView.getCurrentEffect() == MovingEffectsSurfaceView.EFFECT_POINTS && mRefresh )
154
       {
155
       drawBackground();
156
       mView.drawCurve(mCanvas,time);
157
       mTexture.setTexture(mBitmap);
158
       mRefresh = false;
159
       }
160
      
161
     mScreen.render(time);
162
     }
163

    
164
///////////////////////////////////////////////////////////////////////////////////////////////////
165
}
(2-2/3)