Project

General

Profile

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

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

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 10b7e588 Leszek Koltunski
import org.distorted.library.GridFlat;
33 f6d884d5 Leszek Koltunski
import org.distorted.library.DistortedTexture;
34
import org.distorted.library.DistortedEffectQueues;
35 5068fa06 Leszek Koltunski
import org.distorted.library.Distorted;
36 c5a28eb8 Leszek Koltunski
import org.distorted.library.EffectNames;
37 95593730 Leszek Koltunski
import org.distorted.library.EffectTypes;
38 af225332 Leszek Koltunski
import org.distorted.library.message.EffectListener;
39
import org.distorted.library.message.EffectMessage;
40 7589635e Leszek Koltunski
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
  static final int BWID = 300;
48
  static 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 3f07bedc Leszek Koltunski
54 f6d884d5 Leszek Koltunski
  private DistortedTexture mTexture;
55
  private GridFlat mGrid;
56
  private DistortedEffectQueues mQueues;
57 3f07bedc Leszek Koltunski
58 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
59
60 758303a3 Leszek Koltunski
  EffectQueueRenderer(EffectQueueSurfaceView v)
61 bc0a685b Leszek Koltunski
    {    
62
    mPaint = new Paint();
63
    mPaint.setAntiAlias(true);
64
    mPaint.setFakeBoldText(true);
65
    mPaint.setStyle(Style.FILL);
66 427ab7bf Leszek Koltunski
      
67 bc0a685b Leszek Koltunski
    mView = v;
68 427ab7bf Leszek Koltunski
      
69 bc0a685b Leszek Koltunski
    texWidth = BWID;
70
    texHeight= BHEI;
71 e8b6aa95 Leszek Koltunski
72 f6d884d5 Leszek Koltunski
    mGrid    = new GridFlat(80,80*texHeight/texWidth);
73
    mTexture = new DistortedTexture(texWidth,texHeight,0);
74
    mQueues  = new DistortedEffectQueues();
75 e8b6aa95 Leszek Koltunski
76 f6d884d5 Leszek Koltunski
    mQueues.addEventListener(this);
77 bc0a685b Leszek Koltunski
    }
78 427ab7bf Leszek Koltunski
79
///////////////////////////////////////////////////////////////////////////////////////////////////
80 f6d884d5 Leszek Koltunski
81
  DistortedEffectQueues getQueues()
82
    {
83
    return mQueues;
84
    }
85
86
///////////////////////////////////////////////////////////////////////////////////////////////////
87
88 bc0a685b Leszek Koltunski
  public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
89 e8b6aa95 Leszek Koltunski
    {
90 bc0a685b Leszek Koltunski
    Bitmap bitmap = Bitmap.createBitmap(texWidth,texHeight, Bitmap.Config.ARGB_8888);
91 e8b6aa95 Leszek Koltunski
    Canvas canvas = new Canvas(bitmap);
92
93 bc0a685b Leszek Koltunski
    mPaint.setColor(0xff008800);
94
    canvas.drawRect(0, 0, texWidth, texHeight, mPaint);
95
    mPaint.setColor(0xffffffff);
96 e8b6aa95 Leszek Koltunski
97 bc0a685b Leszek Koltunski
    for(int i=0; i<=NUMLINES ; i++ )
98
      {
99
      canvas.drawRect(texWidth*i/NUMLINES - 1,                       0,  texWidth*i/NUMLINES + 1,  texHeight               , mPaint);
100 e8b6aa95 Leszek Koltunski
      canvas.drawRect(                      0, texHeight*i/NUMLINES -1,  texWidth               ,  texHeight*i/NUMLINES + 1, mPaint);
101 bc0a685b Leszek Koltunski
      }
102 f6d884d5 Leszek Koltunski
    mTexture.setTexture(bitmap);
103 af225332 Leszek Koltunski
104 bc0a685b Leszek Koltunski
    try
105
      {
106
      Distorted.onSurfaceCreated(mView.getContext());
107
      }
108
    catch(Exception ex)
109
      {
110 e8b6aa95 Leszek Koltunski
      android.util.Log.e("EffectQueue", ex.getMessage() );
111 bc0a685b Leszek Koltunski
      }
112
    }
113 427ab7bf Leszek Koltunski
114
///////////////////////////////////////////////////////////////////////////////////////////////////
115
116 bc0a685b Leszek Koltunski
  public void onSurfaceChanged(GL10 glUnused, int width, int height)
117
    {
118 f6d884d5 Leszek Koltunski
    mQueues.abortEffects(EffectTypes.MATRIX);
119
    mQueues.scale( new Static3D((float)width/texWidth,(float)height/texHeight,1) );
120 bc0a685b Leszek Koltunski
    Distorted.onSurfaceChanged(width,height);
121 3f07bedc Leszek Koltunski
    mView.setScreenSize(width,height);
122 bc0a685b Leszek Koltunski
    }
123 427ab7bf Leszek Koltunski
   
124
///////////////////////////////////////////////////////////////////////////////////////////////////
125
   
126 bc0a685b Leszek Koltunski
  public void onDrawFrame(GL10 glUnused)
127
    {   
128
    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
129 f6d884d5 Leszek Koltunski
    mQueues.draw(System.currentTimeMillis(), mTexture,mGrid);
130 bc0a685b Leszek Koltunski
    }
131 af225332 Leszek Koltunski
132
///////////////////////////////////////////////////////////////////////////////////////////////////
133 c5a28eb8 Leszek Koltunski
// the library sending messages to us. This is running on a library 'MessageSender' thread.
134 af225332 Leszek Koltunski
135 c5a28eb8 Leszek Koltunski
  public void effectMessage(final EffectMessage em, final long effectID, final EffectNames effectName, final long objectID, final String message)
136 af225332 Leszek Koltunski
    {
137 758303a3 Leszek Koltunski
    EffectQueueActivity act = (EffectQueueActivity)mView.getContext();
138 af225332 Leszek Koltunski
139
    switch(em)
140
      {
141
      case EFFECT_REMOVED : act.effectRemoved(effectID) ; break;
142
      case EFFECT_FINISHED: act.effectFinished(effectID); break;
143
      default             : break;
144
      }
145
    }
146 bc0a685b Leszek Koltunski
  }