Project

General

Profile

« Previous | Next » 

Revision 88c7b603

Added by Leszek Koltunski almost 6 years ago

Reengineer DistortedScreen

View differences:

src/main/java/org/distorted/library/main/DistortedScreen.java
34 34
 * <p>
35 35
 * User is able to render to it just like to a DistortedFramebuffer.
36 36
 */
37
public class DistortedScreen extends DistortedOutputSurface
37
public class DistortedScreen extends DistortedFramebuffer
38 38
  {
39 39
  ///// DEBUGGING ONLY /////////////////////////
40
  private boolean mShowFPS;
41

  
40 42
  private static final int NUM_FRAMES  = 100;
41 43

  
42 44
  private MeshObject fpsMesh;
......
46 48
  private Bitmap fpsBitmap;
47 49
  private Paint mPaint;
48 50
  private int fpsH, fpsW;
49
  private String fpsString = "";
51
  private String fpsString;
50 52
  private long lastTime=0;
51 53
  private long[] durations;
52 54
  private int currDuration;
53 55
  private static MatrixEffectMove mMoveEffect = new MatrixEffectMove( new Static3D(5,5,0) );
54
  private boolean mInitialized;
56
  ///// END DEBUGGING //////////////////////////
55 57

  
56 58
///////////////////////////////////////////////////////////////////////////////////////////////////
57
// here we don't manage underlying OpenGL assets ourselves
58

  
59
  void create()   {}
60
  void delete()   {}
61
  void recreate() {}
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
    }
62 71

  
63 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;
64 86

  
65
  private void initialize()
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()
66 128
    {
129
    mShowFPS = true;
130

  
67 131
    fpsW = 120;
68 132
    fpsH =  70;
69 133

  
134
    fpsString = "";
70 135
    fpsBitmap = Bitmap.createBitmap(fpsW,fpsH, Bitmap.Config.ARGB_8888);
71 136
    fpsMesh = new MeshFlat(1,1);
72 137
    fpsTexture = new DistortedTexture(fpsW,fpsH);
......
85 150

  
86 151
    for(int i=0; i<NUM_FRAMES+1; i++) durations[i]=16;  // Assume FPS will be
87 152
    durations[NUM_FRAMES] = NUM_FRAMES*16;              // close to 1000/16 ~ 60
88
    mInitialized=true;
89
    }
90

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

  
93
  void prepareDebug(long time)
94
    {
95
    if( !mInitialized ) initialize();
96

  
97
    if( lastTime==0 ) lastTime = time;
98

  
99
    currDuration++;
100
    if (currDuration >= NUM_FRAMES) currDuration = 0;
101
    durations[NUM_FRAMES] += ((time - lastTime) - durations[currDuration]);
102
    durations[currDuration] = time - lastTime;
103

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

  
106
    mPaint.setColor(0xffffffff);
107
    fpsCanvas.drawRect(0, 0, fpsW, fpsH, mPaint);
108
    mPaint.setColor(0xff000000);
109
    fpsCanvas.drawText(fpsString, fpsW/2, 0.75f*fpsH, mPaint);
110
    fpsTexture.setTexture(fpsBitmap);
111

  
112
    lastTime = time;
113
    }
114

  
115
///////////////////////////////////////////////////////////////////////////////////////////////////
116

  
117
  void renderDebug(long time)
118
    {
119
    if (fpsTexture.setAsInput())
120
      {
121
      setAsOutput(time);
122
      GLES31.glColorMask(true,true,true,true);
123
      GLES31.glDepthMask(false);
124
      GLES31.glDisable(GLES31.GL_STENCIL_TEST);
125
      GLES31.glDisable(GLES31.GL_DEPTH_TEST);
126
      fpsEffects.drawPriv(fpsW/2.0f, fpsH/2.0f, fpsMesh, this, time, 0);
127
      }
128
    }
129

  
130
///////////////////////////////////////////////////////////////////////////////////////////////////
131
// PUBLIC API
132
///////////////////////////////////////////////////////////////////////////////////////////////////
133
/**
134
 * Create a new Screen.
135
 * <p>
136
 * Has to be followed by a 'resize()' to set the size.
137
 */
138
  public DistortedScreen()
139
    {
140
    // Screen also has to be created (3rd arg 'NOT_CREATED_YET') because of the SSBO inside OutputSurface.
141
    super(0,0,NOT_CREATED_YET,1,DEPTH_NO_STENCIL,0,TYPE_USER);
142

  
143
    mInitialized = false;
144 153
    }
145 154
  }

Also available in: Unified diff