Project

General

Profile

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

library / src / main / java / org / distorted / library / main / DistortedScreen.java @ 2ab60f72

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 af4cc5db Leszek Koltunski
public class DistortedScreen extends DistortedOutputSurface
38 c5369f1b leszek
  {
39 86e99907 leszek
  ///// DEBUGGING ONLY /////////////////////////
40
  private static final int NUM_FRAMES  = 100;
41
42
  private MeshObject fpsMesh;
43
  private DistortedTexture fpsTexture;
44
  private DistortedEffects fpsEffects;
45
  private Canvas fpsCanvas;
46
  private Bitmap fpsBitmap;
47
  private Paint mPaint;
48
  private int fpsH, fpsW;
49
  private String fpsString = "";
50
  private long lastTime=0;
51
  private long[] durations;
52
  private int currDuration;
53 81b1577b Leszek Koltunski
  private static MatrixEffectMove mMoveEffect = new MatrixEffectMove( new Static3D(5,5,0) );
54 26a4e5f6 leszek
  private boolean mInitialized;
55 86e99907 leszek
56 c5369f1b leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
57 af4cc5db Leszek Koltunski
// here we don't manage underlying OpenGL assets ourselves
58 c5369f1b leszek
59 12f45260 Leszek Koltunski
  void createSurface()   {}
60
  void deleteSurface()   {}
61
  void recreateSurface() {}
62 c5369f1b leszek
63 26a4e5f6 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
64
65
  private void initialize()
66
    {
67
    fpsW = 120;
68
    fpsH =  70;
69
70
    fpsBitmap = Bitmap.createBitmap(fpsW,fpsH, Bitmap.Config.ARGB_8888);
71
    fpsMesh = new MeshFlat(1,1);
72
    fpsTexture = new DistortedTexture(fpsW,fpsH);
73
    fpsTexture.setTexture(fpsBitmap);
74
    fpsCanvas = new Canvas(fpsBitmap);
75
    fpsEffects = new DistortedEffects();
76
    fpsEffects.apply(mMoveEffect);
77
78
    mPaint = new Paint();
79
    mPaint.setAntiAlias(true);
80
    mPaint.setTextAlign(Paint.Align.CENTER);
81
    mPaint.setTextSize(0.7f*fpsH);
82
83
    durations = new long[NUM_FRAMES+1];
84
    currDuration = 0;
85
86
    for(int i=0; i<NUM_FRAMES+1; i++) durations[i]=16;  // Assume FPS will be
87
    durations[NUM_FRAMES] = NUM_FRAMES*16;              // close to 1000/16 ~ 60
88
    mInitialized=true;
89
    }
90
91 86e99907 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
92
93
  void prepareDebug(long time)
94
    {
95 26a4e5f6 leszek
    if( !mInitialized ) initialize();
96
97 86e99907 leszek
    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 e6519ac8 Leszek Koltunski
      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 86e99907 leszek
      fpsEffects.drawPriv(fpsW/2.0f, fpsH/2.0f, fpsMesh, this, time, 0);
127
      }
128
    }
129
130 c5369f1b leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
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 a2d56cfd Leszek Koltunski
  public DistortedScreen()
139 c5369f1b leszek
    {
140 2ab60f72 Leszek Koltunski
    // 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 806ca386 leszek
143 26a4e5f6 leszek
    mInitialized = false;
144 c5369f1b leszek
    }
145
  }