Project

General

Profile

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

examples / src / main / java / org / distorted / examples / effectqueue / EffectQueueRenderer.java @ e4330c89

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 758303a3 Leszek Koltunski
package org.distorted.examples.effectqueue;
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 8dfa45c4 leszek
import org.distorted.library.effect.EffectName;
32
import org.distorted.library.effect.MatrixEffectScale;
33 01782e85 Leszek Koltunski
import org.distorted.library.main.DistortedEffects;
34
import org.distorted.library.main.DistortedScreen;
35
import org.distorted.library.main.MeshFlat;
36
import org.distorted.library.main.DistortedTexture;
37
import org.distorted.library.main.Distorted;
38 af225332 Leszek Koltunski
import org.distorted.library.message.EffectListener;
39
import org.distorted.library.message.EffectMessage;
40 630703d1 leszek
import org.distorted.library.type.Static3D;
41 427ab7bf Leszek Koltunski
42
///////////////////////////////////////////////////////////////////////////////////////////////////
43
44 f6d884d5 Leszek Koltunski
class EffectQueueRenderer implements GLSurfaceView.Renderer, EffectListener
45 bc0a685b Leszek Koltunski
  {  
46 3f07bedc Leszek Koltunski
  private static final int NUMLINES =  10;
47 6f3024ae Leszek Koltunski
  final int BWID = 300;
48
  final int BHEI = 400;
49 427ab7bf Leszek Koltunski
   
50 758303a3 Leszek Koltunski
  private EffectQueueSurfaceView mView;
51 bc0a685b Leszek Koltunski
  private Paint mPaint;
52
  private int texWidth, texHeight;
53 8dfa45c4 leszek
  private Static3D mScale;
54 3f07bedc Leszek Koltunski
55 f6d884d5 Leszek Koltunski
  private DistortedTexture mTexture;
56 b01acdaf Leszek Koltunski
  private MeshFlat mMesh;
57 d04a4886 Leszek Koltunski
  private DistortedEffects mEffects;
58 d218d64e leszek
  private DistortedScreen mScreen;
59 3f07bedc Leszek Koltunski
60 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
61
62 758303a3 Leszek Koltunski
  EffectQueueRenderer(EffectQueueSurfaceView v)
63 bc0a685b Leszek Koltunski
    {    
64
    mPaint = new Paint();
65
    mPaint.setAntiAlias(true);
66
    mPaint.setFakeBoldText(true);
67
    mPaint.setStyle(Style.FILL);
68 427ab7bf Leszek Koltunski
      
69 bc0a685b Leszek Koltunski
    mView = v;
70 427ab7bf Leszek Koltunski
      
71 bc0a685b Leszek Koltunski
    texWidth = BWID;
72
    texHeight= BHEI;
73 8dfa45c4 leszek
    mScale = new Static3D(1,1,1);
74
    MatrixEffectScale scaleEffect = new MatrixEffectScale(mScale);
75 e8b6aa95 Leszek Koltunski
76 b01acdaf Leszek Koltunski
    mMesh = new MeshFlat(80,80*texHeight/texWidth);
77 7451c98a Leszek Koltunski
    mTexture = new DistortedTexture(texWidth,texHeight);
78 d04a4886 Leszek Koltunski
    mEffects = new DistortedEffects();
79 8dfa45c4 leszek
    mEffects.apply(scaleEffect);
80 392e16fd Leszek Koltunski
    mEffects.registerForMessages(this);
81
82 e4330c89 Leszek Koltunski
    mScreen = new DistortedScreen();
83 bc0a685b Leszek Koltunski
    }
84 427ab7bf Leszek Koltunski
85
///////////////////////////////////////////////////////////////////////////////////////////////////
86 f6d884d5 Leszek Koltunski
87 d04a4886 Leszek Koltunski
  DistortedEffects getEffects()
88 f6d884d5 Leszek Koltunski
    {
89 392e16fd Leszek Koltunski
    return mEffects;
90 f6d884d5 Leszek Koltunski
    }
91
92
///////////////////////////////////////////////////////////////////////////////////////////////////
93
94 bc0a685b Leszek Koltunski
  public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
95 e8b6aa95 Leszek Koltunski
    {
96 bc0a685b Leszek Koltunski
    Bitmap bitmap = Bitmap.createBitmap(texWidth,texHeight, Bitmap.Config.ARGB_8888);
97 e8b6aa95 Leszek Koltunski
    Canvas canvas = new Canvas(bitmap);
98
99 bc0a685b Leszek Koltunski
    mPaint.setColor(0xff008800);
100
    canvas.drawRect(0, 0, texWidth, texHeight, mPaint);
101
    mPaint.setColor(0xffffffff);
102 e8b6aa95 Leszek Koltunski
103 bc0a685b Leszek Koltunski
    for(int i=0; i<=NUMLINES ; i++ )
104
      {
105
      canvas.drawRect(texWidth*i/NUMLINES - 1,                       0,  texWidth*i/NUMLINES + 1,  texHeight               , mPaint);
106 e8b6aa95 Leszek Koltunski
      canvas.drawRect(                      0, texHeight*i/NUMLINES -1,  texWidth               ,  texHeight*i/NUMLINES + 1, mPaint);
107 bc0a685b Leszek Koltunski
      }
108 f6d884d5 Leszek Koltunski
    mTexture.setTexture(bitmap);
109 af225332 Leszek Koltunski
110 fe59d375 Leszek Koltunski
    mScreen.detachAll();
111
    mScreen.attach(mTexture,mEffects,mMesh);
112
113 8dfa45c4 leszek
    DistortedEffects.enableEffect(EffectName.DISTORT);
114
    DistortedEffects.enableEffect(EffectName.SINK);
115
    DistortedEffects.enableEffect(EffectName.SMOOTH_ALPHA);
116
    DistortedEffects.enableEffect(EffectName.SATURATION);
117
    DistortedEffects.enableEffect(EffectName.SMOOTH_CHROMA);
118 6637d0f2 Leszek Koltunski
119 bc0a685b Leszek Koltunski
    try
120
      {
121 76f9798b Leszek Koltunski
      Distorted.onCreate(mView.getContext());
122 bc0a685b Leszek Koltunski
      }
123
    catch(Exception ex)
124
      {
125 e8b6aa95 Leszek Koltunski
      android.util.Log.e("EffectQueue", ex.getMessage() );
126 bc0a685b Leszek Koltunski
      }
127
    }
128 427ab7bf Leszek Koltunski
129
///////////////////////////////////////////////////////////////////////////////////////////////////
130
131 bc0a685b Leszek Koltunski
  public void onSurfaceChanged(GL10 glUnused, int width, int height)
132
    {
133 8dfa45c4 leszek
    mScale.set((float)width/texWidth,(float)height/texHeight,1);
134 392e16fd Leszek Koltunski
    mScreen.resize(width,height);
135 3f07bedc Leszek Koltunski
    mView.setScreenSize(width,height);
136 bc0a685b Leszek Koltunski
    }
137 427ab7bf Leszek Koltunski
   
138
///////////////////////////////////////////////////////////////////////////////////////////////////
139
   
140 bc0a685b Leszek Koltunski
  public void onDrawFrame(GL10 glUnused)
141
    {   
142 fe59d375 Leszek Koltunski
    mScreen.render( System.currentTimeMillis() );
143 bc0a685b Leszek Koltunski
    }
144 af225332 Leszek Koltunski
145
///////////////////////////////////////////////////////////////////////////////////////////////////
146 c5a28eb8 Leszek Koltunski
// the library sending messages to us. This is running on a library 'MessageSender' thread.
147 af225332 Leszek Koltunski
148 8dfa45c4 leszek
  public void effectMessage(final EffectMessage em, final long effectID, final long objectID)
149 af225332 Leszek Koltunski
    {
150 758303a3 Leszek Koltunski
    EffectQueueActivity act = (EffectQueueActivity)mView.getContext();
151 af225332 Leszek Koltunski
152
    switch(em)
153
      {
154
      case EFFECT_REMOVED : act.effectRemoved(effectID) ; break;
155
      case EFFECT_FINISHED: act.effectFinished(effectID); break;
156
      default             : break;
157
      }
158
    }
159 bc0a685b Leszek Koltunski
  }