Project

General

Profile

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

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

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.GLSurfaceView;
30

    
31
import org.distorted.library.DistortedEffects;
32
import org.distorted.library.DistortedScreen;
33
import org.distorted.library.MeshFlat;
34
import org.distorted.library.DistortedTexture;
35
import org.distorted.library.Distorted;
36
import org.distorted.library.EffectNames;
37
import org.distorted.library.message.EffectListener;
38
import org.distorted.library.message.EffectMessage;
39

    
40
///////////////////////////////////////////////////////////////////////////////////////////////////
41

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

    
52
  private DistortedTexture mTexture;
53
  private MeshFlat mMesh;
54
  private DistortedEffects mEffects;
55
  private DistortedScreen mScreen;
56

    
57
///////////////////////////////////////////////////////////////////////////////////////////////////
58

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

    
71
    mMesh = new MeshFlat(80,80*texHeight/texWidth);
72
    mTexture = new DistortedTexture(texWidth,texHeight);
73
    mEffects = new DistortedEffects();
74

    
75
    mEffects.registerForMessages(this);
76

    
77
    mScreen = new DistortedScreen();
78
    }
79

    
80
///////////////////////////////////////////////////////////////////////////////////////////////////
81

    
82
  DistortedEffects getEffects()
83
    {
84
    return mEffects;
85
    }
86

    
87
///////////////////////////////////////////////////////////////////////////////////////////////////
88

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

    
94
    mPaint.setColor(0xff008800);
95
    canvas.drawRect(0, 0, texWidth, texHeight, mPaint);
96
    mPaint.setColor(0xffffffff);
97

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

    
105
    mScreen.detachAll();
106
    mScreen.attach(mTexture,mEffects,mMesh);
107

    
108
    DistortedEffects.enableEffect(EffectNames.DISTORT);
109
    DistortedEffects.enableEffect(EffectNames.SINK);
110
    DistortedEffects.enableEffect(EffectNames.SMOOTH_ALPHA);
111
    DistortedEffects.enableEffect(EffectNames.SATURATION);
112
    DistortedEffects.enableEffect(EffectNames.SMOOTH_CHROMA);
113

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

    
124
///////////////////////////////////////////////////////////////////////////////////////////////////
125

    
126
  public void onSurfaceChanged(GL10 glUnused, int width, int height)
127
    {
128
    mScreen.resize(width,height);
129
    mView.setScreenSize(width,height);
130
    }
131
   
132
///////////////////////////////////////////////////////////////////////////////////////////////////
133
   
134
  public void onDrawFrame(GL10 glUnused)
135
    {   
136
    mScreen.render( System.currentTimeMillis() );
137
    }
138

    
139
///////////////////////////////////////////////////////////////////////////////////////////////////
140
// the library sending messages to us. This is running on a library 'MessageSender' thread.
141

    
142
  public void effectMessage(final EffectMessage em, final long effectID, final EffectNames effectName, final long objectID)
143
    {
144
    EffectQueueActivity act = (EffectQueueActivity)mView.getContext();
145

    
146
    switch(em)
147
      {
148
      case EFFECT_REMOVED : act.effectRemoved(effectID) ; break;
149
      case EFFECT_FINISHED: act.effectFinished(effectID); break;
150
      default             : break;
151
      }
152
    }
153
  }
(2-2/3)