Project

General

Profile

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

examples / src / main / java / org / distorted / examples / movingeffects / MovingEffectsRenderer.java @ 01782e85

1 bc0a685b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 5068fa06 Leszek Koltunski
package org.distorted.examples.movingeffects;
21 427ab7bf Leszek Koltunski
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 01782e85 Leszek Koltunski
import org.distorted.library.main.DistortedScreen;
32
import org.distorted.library.main.DistortedTexture;
33
import org.distorted.library.main.DistortedEffects;
34 6637d0f2 Leszek Koltunski
import org.distorted.library.EffectNames;
35 01782e85 Leszek Koltunski
import org.distorted.library.main.MeshFlat;
36
import org.distorted.library.main.Distorted;
37 427ab7bf Leszek Koltunski
38
///////////////////////////////////////////////////////////////////////////////////////////////////
39
40 97dadfe5 Leszek Koltunski
class MovingEffectsRenderer implements GLSurfaceView.Renderer
41 427ab7bf Leszek Koltunski
   {  
42 4562f295 Leszek Koltunski
   private MovingEffectsSurfaceView mView;
43 427ab7bf Leszek Koltunski
   private Canvas mCanvas;
44
   private Bitmap mBitmap;
45
   private Paint mPaint;
46 97dadfe5 Leszek Koltunski
   private int texW, texH;
47
48 d04a4886 Leszek Koltunski
   private DistortedEffects mEffects;
49 f6d884d5 Leszek Koltunski
   private DistortedTexture mTexture;
50 d218d64e leszek
   private DistortedScreen mScreen;
51 5f2a53b6 leszek
   private MeshFlat mMesh;
52 3c8b1903 Leszek Koltunski
   private boolean mRefresh;
53 97dadfe5 Leszek Koltunski
54 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
55
56 4562f295 Leszek Koltunski
   MovingEffectsRenderer(MovingEffectsSurfaceView v)
57 427ab7bf Leszek Koltunski
     {    
58
     mPaint = new Paint();
59
     mPaint.setAntiAlias(true);
60
     mPaint.setFakeBoldText(true);
61
     mPaint.setStyle(Style.FILL);
62 f6d884d5 Leszek Koltunski
63 392e16fd Leszek Koltunski
     mView   = v;
64 d04a4886 Leszek Koltunski
     mEffects= new DistortedEffects();
65 1f9a52f1 leszek
     mScreen = new DistortedScreen(mView);
66 392e16fd Leszek Koltunski
     mRefresh= true;
67 427ab7bf Leszek Koltunski
     }
68
69
///////////////////////////////////////////////////////////////////////////////////////////////////
70
71
   private void drawBackground()
72
     {
73 97dadfe5 Leszek Koltunski
     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 427ab7bf Leszek Koltunski
     mPaint.setColor(0xff008800);
87 97dadfe5 Leszek Koltunski
     mCanvas.drawRect(0, 0, texW, texH, mPaint);
88 427ab7bf Leszek Koltunski
     mPaint.setColor(0xffffffff);
89
         
90 97dadfe5 Leszek Koltunski
     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 427ab7bf Leszek Koltunski
     }
93
   
94
///////////////////////////////////////////////////////////////////////////////////////////////////
95 f6d884d5 Leszek Koltunski
96 d04a4886 Leszek Koltunski
   DistortedEffects getEffects()
97 f6d884d5 Leszek Koltunski
     {
98 392e16fd Leszek Koltunski
     return mEffects;
99 f6d884d5 Leszek Koltunski
     }
100
101 3c8b1903 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
102
103
   void setRefresh()
104
     {
105
     mRefresh = true;
106
     }
107
108 f6d884d5 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
109
110 427ab7bf Leszek Koltunski
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
111 97dadfe5 Leszek Koltunski
     {
112 6637d0f2 Leszek Koltunski
     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 427ab7bf Leszek Koltunski
     try
119 97dadfe5 Leszek Koltunski
       {
120 76f9798b Leszek Koltunski
       Distorted.onCreate(mView.getContext());
121 97dadfe5 Leszek Koltunski
       }
122
     catch(Exception ex)
123
       {
124
       android.util.Log.e("Renderer", ex.getMessage() );
125
       }
126 427ab7bf Leszek Koltunski
     }
127
128
///////////////////////////////////////////////////////////////////////////////////////////////////
129
130
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
131
     {
132 97dadfe5 Leszek Koltunski
     texW = width;
133
     texH = height;
134
135 b0ebdf5e Leszek Koltunski
     if( mTexture!=null ) mTexture.markForDeletion();
136 7451c98a Leszek Koltunski
     mTexture = new DistortedTexture(texW,texH);
137 f6d884d5 Leszek Koltunski
     mBitmap  = Bitmap.createBitmap(texW,texH, Bitmap.Config.ARGB_8888);
138
     mCanvas  = new Canvas(mBitmap);
139 97dadfe5 Leszek Koltunski
140 5f2a53b6 leszek
     if( mMesh!=null ) mMesh.markForDeletion();
141
     mMesh = new MeshFlat(80,80*texH/texW);
142
143 fe59d375 Leszek Koltunski
     mScreen.detachAll();
144 5f2a53b6 leszek
     mScreen.attach(mTexture,mEffects,mMesh);
145 392e16fd Leszek Koltunski
     mScreen.resize(texW, texH);
146 4562f295 Leszek Koltunski
     mView.onSurfaceChanged(texW,texH);
147 fd89ecb6 Leszek Koltunski
148
     mRefresh = true;
149 427ab7bf Leszek Koltunski
     }
150
   
151
///////////////////////////////////////////////////////////////////////////////////////////////////
152
   
153
   public void onDrawFrame(GL10 glUnused)
154
     {   
155
     long time = System.currentTimeMillis();
156
      
157 3c8b1903 Leszek Koltunski
     if (mView.getCurrentEffect() == MovingEffectsSurfaceView.EFFECT_POINTS && mRefresh )
158 427ab7bf Leszek Koltunski
       {
159 fd89ecb6 Leszek Koltunski
       drawBackground();
160 4562f295 Leszek Koltunski
       mView.drawCurve(mCanvas,time);
161 f6d884d5 Leszek Koltunski
       mTexture.setTexture(mBitmap);
162 3c8b1903 Leszek Koltunski
       mRefresh = false;
163 427ab7bf Leszek Koltunski
       }
164
      
165 fe59d375 Leszek Koltunski
     mScreen.render(time);
166 427ab7bf Leszek Koltunski
     }
167
168
///////////////////////////////////////////////////////////////////////////////////////////////////
169
}