Project

General

Profile

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

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

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 DistortedOutputSurface
38
  {
39
  ///// 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
  private static MatrixEffectMove mMoveEffect = new MatrixEffectMove( new Static3D(5,5,0) );
54
  private boolean mInitialized;
55

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

    
59
  void create()   {}
60
  void delete()   {}
61
  void recreate() {}
62

    
63
///////////////////////////////////////////////////////////////////////////////////////////////////
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
///////////////////////////////////////////////////////////////////////////////////////////////////
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
    // set color to 'DONT_CREATE' so that Screens will not get added to the Surface lists
141
    // set depth to 'CREATED' so that depth will be, by default, on.
142
    super(0,0,DONT_CREATE,1,DEPTH_NO_STENCIL,0,TYPE_USER);
143

    
144
    mInitialized = false;
145
    }
146
  }
(11-11/22)