Project

General

Profile

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

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

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