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.app.ActivityManager;
|
25
|
import android.content.Context;
|
26
|
import android.content.pm.ConfigurationInfo;
|
27
|
import android.graphics.Bitmap;
|
28
|
import android.graphics.Canvas;
|
29
|
import android.graphics.Paint;
|
30
|
import android.opengl.GLES30;
|
31
|
import android.opengl.GLSurfaceView;
|
32
|
|
33
|
import org.distorted.library.effect.MatrixEffectMove;
|
34
|
import org.distorted.library.type.Static3D;
|
35
|
|
36
|
/**
|
37
|
* Class which represents the Screen.
|
38
|
* <p>
|
39
|
* User is able to render to it just like to a DistortedFramebuffer.
|
40
|
*/
|
41
|
public class DistortedScreen extends DistortedOutputSurface
|
42
|
{
|
43
|
///// DEBUGGING ONLY /////////////////////////
|
44
|
private static final int NUM_FRAMES = 100;
|
45
|
|
46
|
private MeshObject fpsMesh;
|
47
|
private DistortedTexture fpsTexture;
|
48
|
private DistortedEffects fpsEffects;
|
49
|
private Canvas fpsCanvas;
|
50
|
private Bitmap fpsBitmap;
|
51
|
private Paint mPaint;
|
52
|
private int fpsH, fpsW;
|
53
|
private String fpsString = "";
|
54
|
private long lastTime=0;
|
55
|
private long[] durations;
|
56
|
private int currDuration;
|
57
|
private static MatrixEffectMove mMoveEffect = new MatrixEffectMove( new Static3D(5,5,0) );
|
58
|
private boolean mInitialized;
|
59
|
|
60
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
61
|
// here we don't manage underlying OpenGL assets ourselves
|
62
|
|
63
|
void create() {}
|
64
|
void delete() {}
|
65
|
void recreate() {}
|
66
|
|
67
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
68
|
|
69
|
private void initialize()
|
70
|
{
|
71
|
fpsW = 120;
|
72
|
fpsH = 70;
|
73
|
|
74
|
fpsBitmap = Bitmap.createBitmap(fpsW,fpsH, Bitmap.Config.ARGB_8888);
|
75
|
fpsMesh = new MeshFlat(1,1);
|
76
|
fpsTexture = new DistortedTexture(fpsW,fpsH);
|
77
|
fpsTexture.setTexture(fpsBitmap);
|
78
|
fpsCanvas = new Canvas(fpsBitmap);
|
79
|
fpsEffects = new DistortedEffects();
|
80
|
fpsEffects.apply(mMoveEffect);
|
81
|
|
82
|
mPaint = new Paint();
|
83
|
mPaint.setAntiAlias(true);
|
84
|
mPaint.setTextAlign(Paint.Align.CENTER);
|
85
|
mPaint.setTextSize(0.7f*fpsH);
|
86
|
|
87
|
durations = new long[NUM_FRAMES+1];
|
88
|
currDuration = 0;
|
89
|
|
90
|
for(int i=0; i<NUM_FRAMES+1; i++) durations[i]=16; // Assume FPS will be
|
91
|
durations[NUM_FRAMES] = NUM_FRAMES*16; // close to 1000/16 ~ 60
|
92
|
mInitialized=true;
|
93
|
}
|
94
|
|
95
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
96
|
|
97
|
void prepareDebug(long time)
|
98
|
{
|
99
|
if( !mInitialized ) initialize();
|
100
|
|
101
|
if( lastTime==0 ) lastTime = time;
|
102
|
|
103
|
currDuration++;
|
104
|
if (currDuration >= NUM_FRAMES) currDuration = 0;
|
105
|
durations[NUM_FRAMES] += ((time - lastTime) - durations[currDuration]);
|
106
|
durations[currDuration] = time - lastTime;
|
107
|
|
108
|
fpsString = "" + ((int)(10000.0f*NUM_FRAMES/durations[NUM_FRAMES]))/10.0f;
|
109
|
|
110
|
mPaint.setColor(0xffffffff);
|
111
|
fpsCanvas.drawRect(0, 0, fpsW, fpsH, mPaint);
|
112
|
mPaint.setColor(0xff000000);
|
113
|
fpsCanvas.drawText(fpsString, fpsW/2, 0.75f*fpsH, mPaint);
|
114
|
fpsTexture.setTexture(fpsBitmap);
|
115
|
|
116
|
lastTime = time;
|
117
|
}
|
118
|
|
119
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
120
|
|
121
|
void renderDebug(long time)
|
122
|
{
|
123
|
if (fpsTexture.setAsInput())
|
124
|
{
|
125
|
setAsOutput(time);
|
126
|
GLES30.glColorMask(true,true,true,true);
|
127
|
GLES30.glDepthMask(false);
|
128
|
GLES30.glDisable(GLES30.GL_STENCIL_TEST);
|
129
|
GLES30.glDisable(GLES30.GL_DEPTH_TEST);
|
130
|
fpsEffects.drawPriv(fpsW/2.0f, fpsH/2.0f, fpsMesh, this, time, 0);
|
131
|
}
|
132
|
}
|
133
|
|
134
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
135
|
// PUBLIC API
|
136
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
137
|
/**
|
138
|
* Create a new Screen.
|
139
|
* <p>
|
140
|
* Has to be followed by a 'resize()' to set the size.
|
141
|
*/
|
142
|
public DistortedScreen()
|
143
|
{
|
144
|
// set color to 'DONT_CREATE' so that Screens will not get added to the Surface lists
|
145
|
// set depth to 'CREATED' so that depth will be, by default, on.
|
146
|
super(0,0,DONT_CREATE,1,DEPTH_NO_STENCIL,0,TYPE_USER);
|
147
|
|
148
|
mInitialized = false;
|
149
|
}
|
150
|
}
|