Project

General

Profile

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

examples / src / main / java / org / distorted / examples / effects2d / Effects2DRenderer.java @ c5a28eb8

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.effects2d;
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.GLES20;
30
import android.opengl.GLSurfaceView;
31

    
32
import org.distorted.library.DistortedBitmap;
33
import org.distorted.library.Distorted;
34
import org.distorted.library.EffectNames;
35
import org.distorted.library.EffectTypes;
36
import org.distorted.library.message.EffectListener;
37
import org.distorted.library.message.EffectMessage;
38
import org.distorted.library.type.Static3D;
39

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

    
42
public class Effects2DRenderer implements GLSurfaceView.Renderer, EffectListener
43
  {  
44
  public static final int NUMLINES = 10;
45
  public static final int BWID = 300;
46
  public static final int BHEI = 400;
47
   
48
  private GLSurfaceView mView;
49
  public static DistortedBitmap mBackground;
50
  private Paint mPaint;
51
  private int texWidth, texHeight;
52
    
53
///////////////////////////////////////////////////////////////////////////////////////////////////
54

    
55
  public Effects2DRenderer(GLSurfaceView v)
56
    {    
57
    mPaint = new Paint();
58
    mPaint.setAntiAlias(true);
59
    mPaint.setFakeBoldText(true);
60
    mPaint.setStyle(Style.FILL);
61
      
62
    mView = v;
63
      
64
    texWidth = BWID;
65
    texHeight= BHEI;
66
    }
67

    
68
///////////////////////////////////////////////////////////////////////////////////////////////////
69
   
70
  public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
71
    {      
72
    mBackground = new DistortedBitmap(texWidth,texHeight, 80);
73
    Bitmap bitmap = Bitmap.createBitmap(texWidth,texHeight, Bitmap.Config.ARGB_8888);
74
    Canvas canvas = new Canvas(bitmap);  
75
      
76
    mPaint.setColor(0xff008800);
77
    canvas.drawRect(0, 0, texWidth, texHeight, mPaint);
78
    mPaint.setColor(0xffffffff);
79
         
80
    for(int i=0; i<=NUMLINES ; i++ )
81
      {
82
      canvas.drawRect(texWidth*i/NUMLINES - 1,                       0,  texWidth*i/NUMLINES + 1,  texHeight               , mPaint);
83
      canvas.drawRect(                      0, texHeight*i/NUMLINES -1,  texWidth               ,  texHeight*i/NUMLINES + 1, mPaint);  
84
      }
85
          
86
    mBackground.setBitmap(bitmap);
87
    mBackground.addEventListener(this);
88

    
89
    try
90
      {
91
      Distorted.onSurfaceCreated(mView.getContext());
92
      }
93
    catch(Exception ex)
94
      {
95
      android.util.Log.e("Scratchpad", ex.getMessage() );
96
      }
97
    }
98

    
99
///////////////////////////////////////////////////////////////////////////////////////////////////
100

    
101
  public void onSurfaceChanged(GL10 glUnused, int width, int height)
102
    {
103
    mBackground.abortEffects(EffectTypes.MATRIX);
104
    mBackground.scale( new Static3D((float)width/texWidth,(float)height/texHeight,1) );
105
    Distorted.onSurfaceChanged(width,height);
106
    Effects2DSurfaceView.setScreenSize(width,height);
107
    }
108
   
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110
   
111
  public void onDrawFrame(GL10 glUnused)
112
    {   
113
    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
114
    mBackground.draw(System.currentTimeMillis());
115
    }
116

    
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118
// the library sending messages to us. This is running on a library 'MessageSender' thread.
119

    
120
  public void effectMessage(final EffectMessage em, final long effectID, final EffectNames effectName, final long objectID, final String message)
121
    {
122
    Effects2DActivity act = (Effects2DActivity)mView.getContext();
123

    
124
    switch(em)
125
      {
126
      case EFFECT_REMOVED : act.effectRemoved(effectID) ; break;
127
      case EFFECT_FINISHED: act.effectFinished(effectID); break;
128
      default             : break;
129
      }
130
    }
131
  }
(2-2/3)