Project

General

Profile

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

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

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.GLES20;
30
import android.opengl.GLSurfaceView;
31
32 d04a4886 Leszek Koltunski
import org.distorted.library.DistortedEffects;
33 392e16fd Leszek Koltunski
import org.distorted.library.DistortedFramebuffer;
34 10b7e588 Leszek Koltunski
import org.distorted.library.GridFlat;
35 f6d884d5 Leszek Koltunski
import org.distorted.library.DistortedTexture;
36 5068fa06 Leszek Koltunski
import org.distorted.library.Distorted;
37 c5a28eb8 Leszek Koltunski
import org.distorted.library.EffectNames;
38 95593730 Leszek Koltunski
import org.distorted.library.EffectTypes;
39 af225332 Leszek Koltunski
import org.distorted.library.message.EffectListener;
40
import org.distorted.library.message.EffectMessage;
41 7589635e Leszek Koltunski
import org.distorted.library.type.Static3D;
42 427ab7bf Leszek Koltunski
43
///////////////////////////////////////////////////////////////////////////////////////////////////
44
45 f6d884d5 Leszek Koltunski
class EffectQueueRenderer implements GLSurfaceView.Renderer, EffectListener
46 bc0a685b Leszek Koltunski
  {  
47 3f07bedc Leszek Koltunski
  private static final int NUMLINES =  10;
48
  static final int BWID = 300;
49
  static final int BHEI = 400;
50 427ab7bf Leszek Koltunski
   
51 758303a3 Leszek Koltunski
  private EffectQueueSurfaceView mView;
52 bc0a685b Leszek Koltunski
  private Paint mPaint;
53
  private int texWidth, texHeight;
54 3f07bedc Leszek Koltunski
55 f6d884d5 Leszek Koltunski
  private DistortedTexture mTexture;
56
  private GridFlat mGrid;
57 d04a4886 Leszek Koltunski
  private DistortedEffects mEffects;
58 392e16fd Leszek Koltunski
  private DistortedFramebuffer 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 e8b6aa95 Leszek Koltunski
74 f6d884d5 Leszek Koltunski
    mGrid    = new GridFlat(80,80*texHeight/texWidth);
75 7451c98a Leszek Koltunski
    mTexture = new DistortedTexture(texWidth,texHeight);
76 d04a4886 Leszek Koltunski
    mEffects = new DistortedEffects();
77 e8b6aa95 Leszek Koltunski
78 392e16fd Leszek Koltunski
    mEffects.registerForMessages(this);
79
80
    mScreen = new DistortedFramebuffer(0);
81 bc0a685b Leszek Koltunski
    }
82 427ab7bf Leszek Koltunski
83
///////////////////////////////////////////////////////////////////////////////////////////////////
84 f6d884d5 Leszek Koltunski
85 d04a4886 Leszek Koltunski
  DistortedEffects getEffects()
86 f6d884d5 Leszek Koltunski
    {
87 392e16fd Leszek Koltunski
    return mEffects;
88 f6d884d5 Leszek Koltunski
    }
89
90
///////////////////////////////////////////////////////////////////////////////////////////////////
91
92 bc0a685b Leszek Koltunski
  public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
93 e8b6aa95 Leszek Koltunski
    {
94 bc0a685b Leszek Koltunski
    Bitmap bitmap = Bitmap.createBitmap(texWidth,texHeight, Bitmap.Config.ARGB_8888);
95 e8b6aa95 Leszek Koltunski
    Canvas canvas = new Canvas(bitmap);
96
97 bc0a685b Leszek Koltunski
    mPaint.setColor(0xff008800);
98
    canvas.drawRect(0, 0, texWidth, texHeight, mPaint);
99
    mPaint.setColor(0xffffffff);
100 e8b6aa95 Leszek Koltunski
101 bc0a685b Leszek Koltunski
    for(int i=0; i<=NUMLINES ; i++ )
102
      {
103
      canvas.drawRect(texWidth*i/NUMLINES - 1,                       0,  texWidth*i/NUMLINES + 1,  texHeight               , mPaint);
104 e8b6aa95 Leszek Koltunski
      canvas.drawRect(                      0, texHeight*i/NUMLINES -1,  texWidth               ,  texHeight*i/NUMLINES + 1, mPaint);
105 bc0a685b Leszek Koltunski
      }
106 f6d884d5 Leszek Koltunski
    mTexture.setTexture(bitmap);
107 af225332 Leszek Koltunski
108 bc0a685b Leszek Koltunski
    try
109
      {
110
      Distorted.onSurfaceCreated(mView.getContext());
111
      }
112
    catch(Exception ex)
113
      {
114 e8b6aa95 Leszek Koltunski
      android.util.Log.e("EffectQueue", ex.getMessage() );
115 bc0a685b Leszek Koltunski
      }
116
    }
117 427ab7bf Leszek Koltunski
118
///////////////////////////////////////////////////////////////////////////////////////////////////
119
120 bc0a685b Leszek Koltunski
  public void onSurfaceChanged(GL10 glUnused, int width, int height)
121
    {
122 392e16fd Leszek Koltunski
    mEffects.abortEffects(EffectTypes.MATRIX);
123
    mEffects.scale( new Static3D((float)width/texWidth,(float)height/texHeight,1) );
124
    mScreen.resize(width,height);
125 3f07bedc Leszek Koltunski
    mView.setScreenSize(width,height);
126 bc0a685b Leszek Koltunski
    }
127 427ab7bf Leszek Koltunski
   
128
///////////////////////////////////////////////////////////////////////////////////////////////////
129
   
130 bc0a685b Leszek Koltunski
  public void onDrawFrame(GL10 glUnused)
131
    {   
132
    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
133 bc29e409 Leszek Koltunski
    mScreen.renderTo(mTexture, mGrid, mEffects, System.currentTimeMillis() );
134 bc0a685b Leszek Koltunski
    }
135 af225332 Leszek Koltunski
136
///////////////////////////////////////////////////////////////////////////////////////////////////
137 c5a28eb8 Leszek Koltunski
// the library sending messages to us. This is running on a library 'MessageSender' thread.
138 af225332 Leszek Koltunski
139 c5a28eb8 Leszek Koltunski
  public void effectMessage(final EffectMessage em, final long effectID, final EffectNames effectName, final long objectID, final String message)
140 af225332 Leszek Koltunski
    {
141 758303a3 Leszek Koltunski
    EffectQueueActivity act = (EffectQueueActivity)mView.getContext();
142 af225332 Leszek Koltunski
143
    switch(em)
144
      {
145
      case EFFECT_REMOVED : act.effectRemoved(effectID) ; break;
146
      case EFFECT_FINISHED: act.effectFinished(effectID); break;
147
      default             : break;
148
      }
149
    }
150 bc0a685b Leszek Koltunski
  }