Project

General

Profile

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

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

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.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
     try
112
       {
113
       Distorted.onCreate(mView.getContext());
114
       }
115
     catch(Exception ex)
116
       {
117
       android.util.Log.e("Renderer", ex.getMessage() );
118
       }
119
     }
120

    
121
///////////////////////////////////////////////////////////////////////////////////////////////////
122

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

    
128
     mTexture = new DistortedTexture(texW,texH);
129
     mBitmap  = Bitmap.createBitmap(texW,texH, Bitmap.Config.ARGB_8888);
130
     mCanvas  = new Canvas(mBitmap);
131

    
132
     mScreen.detachAll();
133
     mScreen.attach(mTexture,mEffects,new MeshFlat(80,80*texH/texW));
134
     mScreen.resize(texW, texH);
135
     mView.onSurfaceChanged(texW,texH);
136

    
137
     mRefresh = true;
138
     }
139
   
140
///////////////////////////////////////////////////////////////////////////////////////////////////
141
   
142
   public void onDrawFrame(GL10 glUnused)
143
     {   
144
     GLES30.glClear(GLES30.GL_COLOR_BUFFER_BIT | GLES30.GL_DEPTH_BUFFER_BIT);               
145
       
146
     long time = System.currentTimeMillis();
147
      
148
     if (mView.getCurrentEffect() == MovingEffectsSurfaceView.EFFECT_POINTS && mRefresh )
149
       {
150
       drawBackground();
151
       mView.drawCurve(mCanvas,time);
152
       mTexture.setTexture(mBitmap);
153
       mRefresh = false;
154
       }
155
      
156
     mScreen.render(time);
157
     }
158

    
159
///////////////////////////////////////////////////////////////////////////////////////////////////
160
}
(2-2/3)