Project

General

Profile

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

examples / src / main / java / org / distorted / examples / movingeffects / MovingEffectsRenderer.java @ 6637d0f2

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.GLES30;
30
import android.opengl.GLSurfaceView;
31

    
32
import org.distorted.library.DistortedScreen;
33
import org.distorted.library.DistortedTexture;
34
import org.distorted.library.DistortedEffects;
35
import org.distorted.library.EffectNames;
36
import org.distorted.library.MeshFlat;
37
import org.distorted.library.Distorted;
38

    
39
///////////////////////////////////////////////////////////////////////////////////////////////////
40

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

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

    
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55

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

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

    
69
///////////////////////////////////////////////////////////////////////////////////////////////////
70

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

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

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

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

    
101
///////////////////////////////////////////////////////////////////////////////////////////////////
102

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

    
108
///////////////////////////////////////////////////////////////////////////////////////////////////
109

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

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

    
128
///////////////////////////////////////////////////////////////////////////////////////////////////
129

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

    
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
     GLES30.glClear(GLES30.GL_COLOR_BUFFER_BIT | GLES30.GL_DEPTH_BUFFER_BIT);               
152
       
153
     long time = System.currentTimeMillis();
154
      
155
     if (mView.getCurrentEffect() == MovingEffectsSurfaceView.EFFECT_POINTS && mRefresh )
156
       {
157
       drawBackground();
158
       mView.drawCurve(mCanvas,time);
159
       mTexture.setTexture(mBitmap);
160
       mRefresh = false;
161
       }
162
      
163
     mScreen.render(time);
164
     }
165

    
166
///////////////////////////////////////////////////////////////////////////////////////////////////
167
}
(2-2/3)