Project

General

Profile

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

examples / src / main / java / org / distorted / examples / movingeffects / MovingEffectsRenderer.java @ 10b7e588

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