Project

General

Profile

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

library / src / main / java / org / distorted / library / DistortedDebug.java @ b28c6c21

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
import android.graphics.Bitmap;
23
import android.graphics.Canvas;
24
import android.graphics.Paint;
25

    
26
import org.distorted.library.type.Static3D;
27

    
28
///////////////////////////////////////////////////////////////////////////////////////////////////
29
/**
30
 * Show various debugs.
31
 * <p>
32
 * Currently only showing the FPS in the upper-left corner of every DistortedScreen is supported.
33
 */
34
class DistortedDebug
35
{
36
   private static final int NUM_FRAMES = 100;
37

    
38
   private MeshObject fpsMesh;
39
   private DistortedTexture fpsTexture;
40
   private DistortedEffects fpsEffects;
41
   private Canvas fpsCanvas;
42
   private Bitmap fpsBitmap;
43
   private Paint mPaint;
44
   private int fpsH, fpsW;
45
   private String fpsString = "";
46
   private long lastTime=0;
47
   private long[] durations;
48
   private int currDuration;
49

    
50
///////////////////////////////////////////////////////////////////////////////////////////////////
51

    
52
  DistortedDebug()
53
    {
54
    fpsW = 120;
55
    fpsH =  70;
56

    
57
    fpsBitmap = Bitmap.createBitmap(fpsW,fpsH, Bitmap.Config.ARGB_8888);
58
    fpsMesh = new MeshFlat(1,1);
59
    fpsTexture = new DistortedTexture(fpsW,fpsH);
60
    fpsTexture.setTexture(fpsBitmap);
61
    fpsCanvas = new Canvas(fpsBitmap);
62
    fpsEffects = new DistortedEffects();
63
    fpsEffects.move( new Static3D(5,5,0) );
64

    
65
    mPaint = new Paint();
66
    mPaint.setAntiAlias(true);
67
    mPaint.setTextAlign(Paint.Align.CENTER);
68
    mPaint.setTextSize(0.7f*fpsH);
69

    
70
    durations = new long[NUM_FRAMES+1];
71
    currDuration = 0;
72
    for(int i=0; i<NUM_FRAMES+1; i++) durations[i]=0;
73
    }
74

    
75
///////////////////////////////////////////////////////////////////////////////////////////////////
76

    
77
  private void computeFPS(long currTime)
78
    {
79
    if( lastTime!=0 )
80
      {
81
      currDuration++;
82
      if( currDuration>=NUM_FRAMES ) currDuration = 0;
83
      durations[NUM_FRAMES] += ((currTime-lastTime)-durations[currDuration]);
84
      durations[currDuration] = currTime-lastTime;
85

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

    
88
      mPaint.setColor(0xffffffff);
89
      fpsCanvas.drawRect(0, 0, fpsW, fpsH, mPaint);
90
      mPaint.setColor(0xff000000);
91
      fpsCanvas.drawText(fpsString, fpsW/2, 0.75f*fpsH, mPaint);
92
      fpsTexture.setTexture(fpsBitmap);
93
      }
94

    
95
    lastTime = currTime;
96
    }
97

    
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99

    
100
  void renderFPS(long currTime, DistortedOutputSurface surface)
101
    {
102
    computeFPS(currTime);
103

    
104
    fpsTexture.setAsInput();
105
    surface.setAsOutput();
106
    fpsEffects.drawPriv( fpsW/2, fpsH/2, fpsMesh, surface, currTime, 0);
107
    }
108
}
(2-2/27)