Project

General

Profile

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

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

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.GLES20;
30
import android.opengl.GLSurfaceView;
31
32 f6d884d5 Leszek Koltunski
import org.distorted.library.DistortedTexture;
33
import org.distorted.library.DistortedEffectQueues;
34 10b7e588 Leszek Koltunski
import org.distorted.library.GridFlat;
35 5068fa06 Leszek Koltunski
import org.distorted.library.Distorted;
36 427ab7bf Leszek Koltunski
37
///////////////////////////////////////////////////////////////////////////////////////////////////
38
39 97dadfe5 Leszek Koltunski
class MovingEffectsRenderer implements GLSurfaceView.Renderer
40 427ab7bf Leszek Koltunski
   {  
41 4562f295 Leszek Koltunski
   private MovingEffectsSurfaceView mView;
42 427ab7bf Leszek Koltunski
   private Canvas mCanvas;
43
   private Bitmap mBitmap;
44
   private Paint mPaint;
45 97dadfe5 Leszek Koltunski
   private int texW, texH;
46
47 10b7e588 Leszek Koltunski
   private GridFlat mGrid;
48 f6d884d5 Leszek Koltunski
   private DistortedEffectQueues mQueues;
49
   private DistortedTexture mTexture;
50 3c8b1903 Leszek Koltunski
   private boolean mRefresh;
51 97dadfe5 Leszek Koltunski
52 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
53
54 4562f295 Leszek Koltunski
   MovingEffectsRenderer(MovingEffectsSurfaceView v)
55 427ab7bf Leszek Koltunski
     {    
56
     mPaint = new Paint();
57
     mPaint.setAntiAlias(true);
58
     mPaint.setFakeBoldText(true);
59
     mPaint.setStyle(Style.FILL);
60
      
61
     mView = v;
62 f6d884d5 Leszek Koltunski
63
     mQueues = new DistortedEffectQueues();
64 3c8b1903 Leszek Koltunski
     mRefresh = true;
65 427ab7bf Leszek Koltunski
     }
66
67
///////////////////////////////////////////////////////////////////////////////////////////////////
68
69
   private void drawBackground()
70
     {
71 97dadfe5 Leszek Koltunski
     int NW, NH;
72
73
     if( texH<texW )
74
       {
75
       NH = 10;
76
       NW = NH*texW/texH;
77
       }
78
     else
79
       {
80
       NW = 10;
81
       NH = NW*texH/texW;
82
       }
83
84 427ab7bf Leszek Koltunski
     mPaint.setColor(0xff008800);
85 97dadfe5 Leszek Koltunski
     mCanvas.drawRect(0, 0, texW, texH, mPaint);
86 427ab7bf Leszek Koltunski
     mPaint.setColor(0xffffffff);
87
         
88 97dadfe5 Leszek Koltunski
     for(int i=0; i<=NH ; i++ ) mCanvas.drawRect( 0, texH*i/NH -2,  texW,  texH*i/NH + 2, mPaint);
89
     for(int i=0; i<=NW ; i++ ) mCanvas.drawRect( texW*i/NW - 2, 0, texW*i/NW + 2,  texH, mPaint);
90 427ab7bf Leszek Koltunski
     }
91
   
92
///////////////////////////////////////////////////////////////////////////////////////////////////
93 f6d884d5 Leszek Koltunski
94
   DistortedEffectQueues getQueues()
95
     {
96
     return mQueues;
97
     }
98
99 3c8b1903 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
100
101
   void setRefresh()
102
     {
103
     mRefresh = true;
104
     }
105
106 f6d884d5 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
107
108 427ab7bf Leszek Koltunski
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
109 97dadfe5 Leszek Koltunski
     {
110 427ab7bf Leszek Koltunski
     try
111 97dadfe5 Leszek Koltunski
       {
112
       Distorted.onSurfaceCreated(mView.getContext());
113
       }
114
     catch(Exception ex)
115
       {
116
       android.util.Log.e("Renderer", ex.getMessage() );
117
       }
118 427ab7bf Leszek Koltunski
     }
119
120
///////////////////////////////////////////////////////////////////////////////////////////////////
121
122
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
123
     {
124 97dadfe5 Leszek Koltunski
     texW = width;
125
     texH = height;
126
127 f6d884d5 Leszek Koltunski
     mGrid    = new GridFlat(80,80*texH/texW);
128 7451c98a Leszek Koltunski
     mTexture = new DistortedTexture(texW,texH);
129 f6d884d5 Leszek Koltunski
     mBitmap  = Bitmap.createBitmap(texW,texH, Bitmap.Config.ARGB_8888);
130
     mCanvas  = new Canvas(mBitmap);
131 97dadfe5 Leszek Koltunski
132
     Distorted.onSurfaceChanged(texW, texH);
133 4562f295 Leszek Koltunski
     mView.onSurfaceChanged(texW,texH);
134 fd89ecb6 Leszek Koltunski
135
     mRefresh = true;
136 427ab7bf Leszek Koltunski
     }
137
   
138
///////////////////////////////////////////////////////////////////////////////////////////////////
139
   
140
   public void onDrawFrame(GL10 glUnused)
141
     {   
142
     GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);               
143
       
144
     long time = System.currentTimeMillis();
145
      
146 3c8b1903 Leszek Koltunski
     if (mView.getCurrentEffect() == MovingEffectsSurfaceView.EFFECT_POINTS && mRefresh )
147 427ab7bf Leszek Koltunski
       {
148 fd89ecb6 Leszek Koltunski
       drawBackground();
149 4562f295 Leszek Koltunski
       mView.drawCurve(mCanvas,time);
150 f6d884d5 Leszek Koltunski
       mTexture.setTexture(mBitmap);
151 3c8b1903 Leszek Koltunski
       mRefresh = false;
152 427ab7bf Leszek Koltunski
       }
153
      
154 f6d884d5 Leszek Koltunski
     mQueues.draw(time,mTexture,mGrid);
155 427ab7bf Leszek Koltunski
     }
156
157
///////////////////////////////////////////////////////////////////////////////////////////////////
158
}