Project

General

Profile

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

library / src / main / java / org / distorted / library / main / DistortedScreen.java @ 586b5fa1

1 c5369f1b leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
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 fe82a979 Leszek Koltunski
package org.distorted.library.main;
21 c5369f1b leszek
22
///////////////////////////////////////////////////////////////////////////////////////////////////
23 806ca386 leszek
24 86e99907 leszek
import android.graphics.Bitmap;
25
import android.graphics.Canvas;
26
import android.graphics.Paint;
27 e6519ac8 Leszek Koltunski
import android.opengl.GLES31;
28 806ca386 leszek
29 81b1577b Leszek Koltunski
import org.distorted.library.effect.MatrixEffectMove;
30 86e99907 leszek
import org.distorted.library.type.Static3D;
31
32 c5369f1b leszek
/**
33
 * Class which represents the Screen.
34
 * <p>
35
 * User is able to render to it just like to a DistortedFramebuffer.
36
 */
37 88c7b603 Leszek Koltunski
public class DistortedScreen extends DistortedFramebuffer
38 c5369f1b leszek
  {
39 86e99907 leszek
  ///// DEBUGGING ONLY /////////////////////////
40 88c7b603 Leszek Koltunski
  private boolean mShowFPS;
41
42 86e99907 leszek
  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 88c7b603 Leszek Koltunski
  private String fpsString;
52 86e99907 leszek
  private long lastTime=0;
53
  private long[] durations;
54
  private int currDuration;
55 81b1577b Leszek Koltunski
  private static MatrixEffectMove mMoveEffect = new MatrixEffectMove( new Static3D(5,5,0) );
56 88c7b603 Leszek Koltunski
  ///// END DEBUGGING //////////////////////////
57 86e99907 leszek
58 8ebbc730 Leszek Koltunski
  private int mCurrFBO;
59
60 c5369f1b leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
61 88c7b603 Leszek Koltunski
// PUBLIC API
62
///////////////////////////////////////////////////////////////////////////////////////////////////
63
/**
64
 * Create a new Screen. Initially 1x1 in size.
65
 * <p>
66
 * Has to be followed by a 'resize()' to set the size.
67
 */
68
  public DistortedScreen()
69
    {
70 586b5fa1 Leszek Koltunski
    super(Distorted.FBO_QUEUE_SIZE,1,BOTH_DEPTH_STENCIL, TYPE_SYST, 1,1);
71 88c7b603 Leszek Koltunski
    mShowFPS = false;
72 8ebbc730 Leszek Koltunski
    mCurrFBO = 0;
73 88c7b603 Leszek Koltunski
    }
74 c5369f1b leszek
75 26a4e5f6 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
76 88c7b603 Leszek Koltunski
/**
77
 * Draws all the attached children to this OutputSurface.
78
 * <p>
79
 * Must be called from a thread holding OpenGL Context.
80
 *
81
 * @param time Current time, in milliseconds. This will be passed to all the Effects stored in the children Nodes.
82
 * @return Number of objects rendered.
83
 */
84
  public int render(long time)
85
    {
86
    if( mShowFPS )
87
      {
88
      if( lastTime==0 ) lastTime = time;
89 26a4e5f6 leszek
90 88c7b603 Leszek Koltunski
      currDuration++;
91
      if (currDuration >= NUM_FRAMES) currDuration = 0;
92
      durations[NUM_FRAMES] += ((time - lastTime) - durations[currDuration]);
93
      durations[currDuration] = time - lastTime;
94
95
      fpsString = "" + ((int)(10000.0f*NUM_FRAMES/durations[NUM_FRAMES]))/10.0f;
96
97
      mPaint.setColor(0xffffffff);
98
      fpsCanvas.drawRect(0, 0, fpsW, fpsH, mPaint);
99
      mPaint.setColor(0xff000000);
100
      fpsCanvas.drawText(fpsString, fpsW/2, 0.75f*fpsH, mPaint);
101
      fpsTexture.setTexture(fpsBitmap);
102
103
      lastTime = time;
104
      }
105
106 8ebbc730 Leszek Koltunski
    int numrender = super.render(time,mCurrFBO);
107 88c7b603 Leszek Koltunski
108
    GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, 0);
109 8ebbc730 Leszek Koltunski
    setAsInput(mCurrFBO,0);
110 88c7b603 Leszek Koltunski
    GLES31.glColorMask(true,true,true,true);
111
    GLES31.glDepthMask(false);
112
    GLES31.glDisable(GLES31.GL_STENCIL_TEST);
113
    GLES31.glDisable(GLES31.GL_DEPTH_TEST);
114 e92f191a Leszek Koltunski
    GLES31.glDisable(GLES31.GL_BLEND);
115
116 88c7b603 Leszek Koltunski
    DistortedEffects.blitPriv(this);
117
118
    if( mShowFPS && fpsTexture.setAsInput())
119
      {
120
      fpsEffects.drawPriv(fpsW / 2.0f, fpsH / 2.0f, fpsMesh, this, time, 0);
121
      }
122
123 8ebbc730 Leszek Koltunski
    mCurrFBO++;
124 586b5fa1 Leszek Koltunski
    if( mCurrFBO>=Distorted.FBO_QUEUE_SIZE ) mCurrFBO=0;
125 8ebbc730 Leszek Koltunski
126 88c7b603 Leszek Koltunski
    return numrender+1;
127
    }
128
129
///////////////////////////////////////////////////////////////////////////////////////////////////
130
/**
131
 * Make the library show Frames Per Second in the upper-left corner.
132
 * <p>
133
 */
134
  public void showFPS()
135 26a4e5f6 leszek
    {
136 88c7b603 Leszek Koltunski
    mShowFPS = true;
137
138 26a4e5f6 leszek
    fpsW = 120;
139
    fpsH =  70;
140
141 88c7b603 Leszek Koltunski
    fpsString = "";
142 26a4e5f6 leszek
    fpsBitmap = Bitmap.createBitmap(fpsW,fpsH, Bitmap.Config.ARGB_8888);
143
    fpsMesh = new MeshFlat(1,1);
144
    fpsTexture = new DistortedTexture(fpsW,fpsH);
145
    fpsTexture.setTexture(fpsBitmap);
146
    fpsCanvas = new Canvas(fpsBitmap);
147
    fpsEffects = new DistortedEffects();
148
    fpsEffects.apply(mMoveEffect);
149
150
    mPaint = new Paint();
151
    mPaint.setAntiAlias(true);
152
    mPaint.setTextAlign(Paint.Align.CENTER);
153
    mPaint.setTextSize(0.7f*fpsH);
154
155
    durations = new long[NUM_FRAMES+1];
156
    currDuration = 0;
157
158
    for(int i=0; i<NUM_FRAMES+1; i++) durations[i]=16;  // Assume FPS will be
159
    durations[NUM_FRAMES] = NUM_FRAMES*16;              // close to 1000/16 ~ 60
160 c5369f1b leszek
    }
161
  }