Project

General

Profile

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

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

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

    
32
import org.distorted.library.DistortedTexture;
33
import org.distorted.library.DistortedEffectQueues;
34
import org.distorted.library.GridFlat;
35
import org.distorted.library.Distorted;
36

    
37
///////////////////////////////////////////////////////////////////////////////////////////////////
38

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

    
47
   private GridFlat mGrid;
48
   private DistortedEffectQueues mQueues;
49
   private DistortedTexture mTexture;
50
   private boolean mRefresh;
51

    
52
///////////////////////////////////////////////////////////////////////////////////////////////////
53

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

    
63
     mQueues = new DistortedEffectQueues();
64
     mRefresh = true;
65
     }
66

    
67
///////////////////////////////////////////////////////////////////////////////////////////////////
68

    
69
   private void drawBackground()
70
     {
71
     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
     mPaint.setColor(0xff008800);
85
     mCanvas.drawRect(0, 0, texW, texH, mPaint);
86
     mPaint.setColor(0xffffffff);
87
         
88
     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
     }
91
   
92
///////////////////////////////////////////////////////////////////////////////////////////////////
93

    
94
   DistortedEffectQueues getQueues()
95
     {
96
     return mQueues;
97
     }
98

    
99
///////////////////////////////////////////////////////////////////////////////////////////////////
100

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

    
106
///////////////////////////////////////////////////////////////////////////////////////////////////
107

    
108
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
109
     {
110
     try
111
       {
112
       Distorted.onSurfaceCreated(mView.getContext());
113
       }
114
     catch(Exception ex)
115
       {
116
       android.util.Log.e("Renderer", ex.getMessage() );
117
       }
118
     }
119

    
120
///////////////////////////////////////////////////////////////////////////////////////////////////
121

    
122
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
123
     {
124
     texW = width;
125
     texH = height;
126

    
127
     mGrid    = new GridFlat(80,80*texH/texW);
128
     mTexture = new DistortedTexture(texW,texH);
129
     mBitmap  = Bitmap.createBitmap(texW,texH, Bitmap.Config.ARGB_8888);
130
     mCanvas  = new Canvas(mBitmap);
131

    
132
     Distorted.onSurfaceChanged(texW, texH);
133
     mView.onSurfaceChanged(texW,texH);
134

    
135
     mRefresh = true;
136
     }
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
     if (mView.getCurrentEffect() == MovingEffectsSurfaceView.EFFECT_POINTS && mRefresh )
147
       {
148
       drawBackground();
149
       mView.drawCurve(mCanvas,time);
150
       mTexture.setTexture(mBitmap);
151
       mRefresh = false;
152
       }
153
      
154
     mQueues.draw(time,mTexture,mGrid);
155
     }
156

    
157
///////////////////////////////////////////////////////////////////////////////////////////////////
158
}
(2-2/3)