Project

General

Profile

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

library / src / main / java / org / distorted / library / DistortedScreen.java @ 86e99907

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;
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.GLSurfaceView;
31

    
32
import org.distorted.library.type.Static3D;
33

    
34
/**
35
 * Class which represents the Screen.
36
 * <p>
37
 * User is able to render to it just like to a DistortedFramebuffer.
38
 */
39
public class DistortedScreen extends DistortedOutputSurface
40
  {
41
  ///// DEBUGGING ONLY /////////////////////////
42
  private static final int NUM_FRAMES  = 100;
43

    
44
  private MeshObject fpsMesh;
45
  private DistortedTexture fpsTexture;
46
  private DistortedEffects fpsEffects;
47
  private Canvas fpsCanvas;
48
  private Bitmap fpsBitmap;
49
  private Paint mPaint;
50
  private int fpsH, fpsW;
51
  private String fpsString = "";
52
  private long lastTime=0;
53
  private long[] durations;
54
  private int currDuration;
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
  void prepareDebug(long time)
66
    {
67
    if( lastTime==0 ) lastTime = time;
68

    
69
    currDuration++;
70
    if (currDuration >= NUM_FRAMES) currDuration = 0;
71
    durations[NUM_FRAMES] += ((time - lastTime) - durations[currDuration]);
72
    durations[currDuration] = time - lastTime;
73

    
74
    fpsString = "" + ((int)(10000.0f*NUM_FRAMES/durations[NUM_FRAMES]))/10.0f;
75

    
76
    mPaint.setColor(0xffffffff);
77
    fpsCanvas.drawRect(0, 0, fpsW, fpsH, mPaint);
78
    mPaint.setColor(0xff000000);
79
    fpsCanvas.drawText(fpsString, fpsW/2, 0.75f*fpsH, mPaint);
80
    fpsTexture.setTexture(fpsBitmap);
81

    
82
    lastTime = time;
83
    }
84

    
85
///////////////////////////////////////////////////////////////////////////////////////////////////
86

    
87
  void renderDebug(long time)
88
    {
89
    if (fpsTexture.setAsInput())
90
      {
91
      setAsOutput(time);
92
      fpsEffects.drawPriv(fpsW/2.0f, fpsH/2.0f, fpsMesh, this, time, 0);
93
      }
94
    }
95

    
96
///////////////////////////////////////////////////////////////////////////////////////////////////
97
// PUBLIC API
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99
/**
100
 * Create a new Screen.
101
 * <p>
102
 * Has to be followed by a 'resize()' to set the size.
103
 */
104
  public DistortedScreen(GLSurfaceView view)
105
    {
106
    // set color to 'DONT_CREATE' so that Screens will not get added to the Surface lists
107
    // set depth to 'CREATED' so that depth will be, by default, on.
108
    super(0,0,DONT_CREATE,1,DEPTH_NO_STENCIL,0,TYPE_USER);
109

    
110
    if( view!=null )
111
      {
112
      Context context = view.getContext();
113
      final ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
114
      final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
115
      view.setEGLContextClientVersion((configurationInfo.reqGlEsVersion >> 16) >= 3 ? 3 : 2);
116
      }
117

    
118
    /////// DEBUGGING ONLY //////////////
119
    fpsW = 120;
120
    fpsH =  70;
121

    
122
    fpsBitmap = Bitmap.createBitmap(fpsW,fpsH, Bitmap.Config.ARGB_8888);
123
    fpsMesh = new MeshFlat(1,1);
124
    fpsTexture = new DistortedTexture(fpsW,fpsH);
125
    fpsTexture.setTexture(fpsBitmap);
126
    fpsCanvas = new Canvas(fpsBitmap);
127
    fpsEffects = new DistortedEffects();
128
    fpsEffects.move( new Static3D(5,5,0) );
129

    
130
    mPaint = new Paint();
131
    mPaint.setAntiAlias(true);
132
    mPaint.setTextAlign(Paint.Align.CENTER);
133
    mPaint.setTextSize(0.7f*fpsH);
134

    
135
    durations = new long[NUM_FRAMES+1];
136
    currDuration = 0;
137

    
138
    for(int i=0; i<NUM_FRAMES+1; i++) durations[i]=0;
139
    }
140
  }
(11-11/26)