Project

General

Profile

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

examples / src / main / java / org / distorted / examples / effectqueue / EffectQueueRenderer.java @ 6637d0f2

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.effectqueue;
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.DistortedEffects;
33
import org.distorted.library.DistortedScreen;
34
import org.distorted.library.MeshFlat;
35
import org.distorted.library.DistortedTexture;
36
import org.distorted.library.Distorted;
37
import org.distorted.library.EffectNames;
38
import org.distorted.library.EffectTypes;
39
import org.distorted.library.message.EffectListener;
40
import org.distorted.library.message.EffectMessage;
41
import org.distorted.library.type.Static3D;
42

    
43
///////////////////////////////////////////////////////////////////////////////////////////////////
44

    
45
class EffectQueueRenderer implements GLSurfaceView.Renderer, EffectListener
46
  {  
47
  private static final int NUMLINES =  10;
48
  final int BWID = 300;
49
  final int BHEI = 400;
50
   
51
  private EffectQueueSurfaceView mView;
52
  private Paint mPaint;
53
  private int texWidth, texHeight;
54

    
55
  private DistortedTexture mTexture;
56
  private MeshFlat mMesh;
57
  private DistortedEffects mEffects;
58
  private DistortedScreen mScreen;
59

    
60
///////////////////////////////////////////////////////////////////////////////////////////////////
61

    
62
  EffectQueueRenderer(EffectQueueSurfaceView v)
63
    {    
64
    mPaint = new Paint();
65
    mPaint.setAntiAlias(true);
66
    mPaint.setFakeBoldText(true);
67
    mPaint.setStyle(Style.FILL);
68
      
69
    mView = v;
70
      
71
    texWidth = BWID;
72
    texHeight= BHEI;
73

    
74
    mMesh = new MeshFlat(80,80*texHeight/texWidth);
75
    mTexture = new DistortedTexture(texWidth,texHeight);
76
    mEffects = new DistortedEffects();
77

    
78
    mEffects.registerForMessages(this);
79

    
80
    mScreen = new DistortedScreen();
81
    }
82

    
83
///////////////////////////////////////////////////////////////////////////////////////////////////
84

    
85
  DistortedEffects getEffects()
86
    {
87
    return mEffects;
88
    }
89

    
90
///////////////////////////////////////////////////////////////////////////////////////////////////
91

    
92
  public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
93
    {
94
    Bitmap bitmap = Bitmap.createBitmap(texWidth,texHeight, Bitmap.Config.ARGB_8888);
95
    Canvas canvas = new Canvas(bitmap);
96

    
97
    mPaint.setColor(0xff008800);
98
    canvas.drawRect(0, 0, texWidth, texHeight, mPaint);
99
    mPaint.setColor(0xffffffff);
100

    
101
    for(int i=0; i<=NUMLINES ; i++ )
102
      {
103
      canvas.drawRect(texWidth*i/NUMLINES - 1,                       0,  texWidth*i/NUMLINES + 1,  texHeight               , mPaint);
104
      canvas.drawRect(                      0, texHeight*i/NUMLINES -1,  texWidth               ,  texHeight*i/NUMLINES + 1, mPaint);
105
      }
106
    mTexture.setTexture(bitmap);
107

    
108
    mScreen.detachAll();
109
    mScreen.attach(mTexture,mEffects,mMesh);
110

    
111
    DistortedEffects.enableEffect(EffectNames.DISTORT);
112
    DistortedEffects.enableEffect(EffectNames.SINK);
113
    DistortedEffects.enableEffect(EffectNames.SMOOTH_ALPHA);
114
    DistortedEffects.enableEffect(EffectNames.SATURATION);
115
    DistortedEffects.enableEffect(EffectNames.SMOOTH_CHROMA);
116

    
117
    try
118
      {
119
      Distorted.onCreate(mView.getContext());
120
      }
121
    catch(Exception ex)
122
      {
123
      android.util.Log.e("EffectQueue", ex.getMessage() );
124
      }
125
    }
126

    
127
///////////////////////////////////////////////////////////////////////////////////////////////////
128

    
129
  public void onSurfaceChanged(GL10 glUnused, int width, int height)
130
    {
131
    mEffects.abortEffects(EffectTypes.MATRIX);
132
    mEffects.scale( new Static3D((float)width/texWidth,(float)height/texHeight,1) );
133
    mScreen.resize(width,height);
134
    mView.setScreenSize(width,height);
135
    }
136
   
137
///////////////////////////////////////////////////////////////////////////////////////////////////
138
   
139
  public void onDrawFrame(GL10 glUnused)
140
    {   
141
    GLES30.glClear(GLES30.GL_COLOR_BUFFER_BIT | GLES30.GL_DEPTH_BUFFER_BIT);
142
    mScreen.render( System.currentTimeMillis() );
143
    }
144

    
145
///////////////////////////////////////////////////////////////////////////////////////////////////
146
// the library sending messages to us. This is running on a library 'MessageSender' thread.
147

    
148
  public void effectMessage(final EffectMessage em, final long effectID, final EffectNames effectName, final long objectID)
149
    {
150
    EffectQueueActivity act = (EffectQueueActivity)mView.getContext();
151

    
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
  }
(2-2/3)