Project

General

Profile

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

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

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

    
44
///////////////////////////////////////////////////////////////////////////////////////////////////
45

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

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

    
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62

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

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

    
79
    mEffects.registerForMessages(this);
80

    
81
    mScreen = new DistortedScreen();
82
    }
83

    
84
///////////////////////////////////////////////////////////////////////////////////////////////////
85

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

    
91
///////////////////////////////////////////////////////////////////////////////////////////////////
92

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

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

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

    
109
    try
110
      {
111
      Distorted.onCreate(mView.getContext());
112
      }
113
    catch(Exception ex)
114
      {
115
      android.util.Log.e("EffectQueue", ex.getMessage() );
116
      }
117
    }
118

    
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120

    
121
  public void onSurfaceChanged(GL10 glUnused, int width, int height)
122
    {
123
    mEffects.abortEffects(EffectTypes.MATRIX);
124
    mEffects.scale( new Static3D((float)width/texWidth,(float)height/texHeight,1) );
125
    mScreen.resize(width,height);
126
    mView.setScreenSize(width,height);
127
    }
128
   
129
///////////////////////////////////////////////////////////////////////////////////////////////////
130
   
131
  public void onDrawFrame(GL10 glUnused)
132
    {   
133
    GLES30.glClear(GLES30.GL_COLOR_BUFFER_BIT | GLES30.GL_DEPTH_BUFFER_BIT);
134
    mScreen.renderTo(mTexture, mMesh, mEffects, System.currentTimeMillis() );
135
    }
136

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

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

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