Project

General

Profile

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

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

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