Project

General

Profile

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

library / src / main / java / org / distorted / library / main / DistortedScreen.java @ c41d046c

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.library.main;
21

    
22
///////////////////////////////////////////////////////////////////////////////////////////////////
23

    
24
import android.graphics.Bitmap;
25
import android.graphics.Canvas;
26
import android.graphics.Paint;
27
import android.opengl.GLES31;
28

    
29
import org.distorted.library.effect.MatrixEffectMove;
30
import org.distorted.library.type.Static3D;
31

    
32
/**
33
 * Class which represents the Screen.
34
 * <p>
35
 * User is able to render to it just like to a DistortedFramebuffer.
36
 */
37
public class DistortedScreen extends DistortedFramebuffer
38
  {
39
  ///// DEBUGGING ONLY /////////////////////////
40
  private boolean mShowFPS;
41

    
42
  private static final int NUM_FRAMES  = 100;
43

    
44
  private MeshObject fpsMesh;
45
  private DistortedTexture fpsTexture;
46
  private DistortedEffects fpsEffects;
47
  private Canvas fpsCanvas;
48
  private Bitmap fpsBitmap;
49
  private Paint mPaint;
50
  private int fpsH, fpsW;
51
  private String fpsString;
52
  private long lastTime=0;
53
  private long[] durations;
54
  private int currDuration;
55
  private static MatrixEffectMove mMoveEffect = new MatrixEffectMove( new Static3D(5,5,0) );
56
  ///// END DEBUGGING //////////////////////////
57

    
58
///////////////////////////////////////////////////////////////////////////////////////////////////
59
// PUBLIC API
60
///////////////////////////////////////////////////////////////////////////////////////////////////
61
/**
62
 * Create a new Screen. Initially 1x1 in size.
63
 * <p>
64
 * Has to be followed by a 'resize()' to set the size.
65
 */
66
  public DistortedScreen()
67
    {
68
    super(1,1,1,BOTH_DEPTH_STENCIL);
69
    mShowFPS = false;
70
    }
71

    
72
///////////////////////////////////////////////////////////////////////////////////////////////////
73
/**
74
 * Draws all the attached children to this OutputSurface.
75
 * <p>
76
 * Must be called from a thread holding OpenGL Context.
77
 *
78
 * @param time Current time, in milliseconds. This will be passed to all the Effects stored in the children Nodes.
79
 * @return Number of objects rendered.
80
 */
81
  public int render(long time)
82
    {
83
    if( mShowFPS )
84
      {
85
      if( lastTime==0 ) lastTime = time;
86

    
87
      currDuration++;
88
      if (currDuration >= NUM_FRAMES) currDuration = 0;
89
      durations[NUM_FRAMES] += ((time - lastTime) - durations[currDuration]);
90
      durations[currDuration] = time - lastTime;
91

    
92
      fpsString = "" + ((int)(10000.0f*NUM_FRAMES/durations[NUM_FRAMES]))/10.0f;
93

    
94
      mPaint.setColor(0xffffffff);
95
      fpsCanvas.drawRect(0, 0, fpsW, fpsH, mPaint);
96
      mPaint.setColor(0xff000000);
97
      fpsCanvas.drawText(fpsString, fpsW/2, 0.75f*fpsH, mPaint);
98
      fpsTexture.setTexture(fpsBitmap);
99

    
100
      lastTime = time;
101
      }
102

    
103
    int numrender = super.render(time);
104

    
105
    GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, 0);
106
    clear();
107
    setAsInput();
108
    GLES31.glColorMask(true,true,true,true);
109
    GLES31.glDepthMask(false);
110
    GLES31.glDisable(GLES31.GL_STENCIL_TEST);
111
    GLES31.glDisable(GLES31.GL_DEPTH_TEST);
112
    DistortedEffects.blitPriv(this);
113

    
114
    if( mShowFPS && fpsTexture.setAsInput())
115
      {
116
      fpsEffects.drawPriv(fpsW / 2.0f, fpsH / 2.0f, fpsMesh, this, time, 0);
117
      }
118

    
119
    return numrender+1;
120
    }
121

    
122
///////////////////////////////////////////////////////////////////////////////////////////////////
123
/**
124
 * Make the library show Frames Per Second in the upper-left corner.
125
 * <p>
126
 */
127
  public void showFPS()
128
    {
129
    mShowFPS = true;
130

    
131
    fpsW = 120;
132
    fpsH =  70;
133

    
134
    fpsString = "";
135
    fpsBitmap = Bitmap.createBitmap(fpsW,fpsH, Bitmap.Config.ARGB_8888);
136
    fpsMesh = new MeshFlat(1,1);
137
    fpsTexture = new DistortedTexture(fpsW,fpsH);
138
    fpsTexture.setTexture(fpsBitmap);
139
    fpsCanvas = new Canvas(fpsBitmap);
140
    fpsEffects = new DistortedEffects();
141
    fpsEffects.apply(mMoveEffect);
142

    
143
    mPaint = new Paint();
144
    mPaint.setAntiAlias(true);
145
    mPaint.setTextAlign(Paint.Align.CENTER);
146
    mPaint.setTextSize(0.7f*fpsH);
147

    
148
    durations = new long[NUM_FRAMES+1];
149
    currDuration = 0;
150

    
151
    for(int i=0; i<NUM_FRAMES+1; i++) durations[i]=16;  // Assume FPS will be
152
    durations[NUM_FRAMES] = NUM_FRAMES*16;              // close to 1000/16 ~ 60
153
    }
154
  }
(10-10/21)