Project

General

Profile

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

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

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.type.Static3D;
34

    
35
/**
36
 * Class which represents the Screen.
37
 * <p>
38
 * User is able to render to it just like to a DistortedFramebuffer.
39
 */
40
public class DistortedScreen extends DistortedOutputSurface
41
  {
42
  ///// 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
///////////////////////////////////////////////////////////////////////////////////////////////////
58
// here we don't manage underlying OpenGL assets ourselves
59

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

    
64
///////////////////////////////////////////////////////////////////////////////////////////////////
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
      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
      fpsEffects.drawPriv(fpsW/2.0f, fpsH/2.0f, fpsMesh, this, time, 0);
98
      }
99
    }
100

    
101
///////////////////////////////////////////////////////////////////////////////////////////////////
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
  public DistortedScreen(GLSurfaceView view)
110
    {
111
    // 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
    super(0,0,DONT_CREATE,1,DEPTH_NO_STENCIL,0,TYPE_USER);
114

    
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

    
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
    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
    }
146
  }
(11-11/24)