Project

General

Profile

Download (6.89 KB) Statistics
| Branch: | Tag: | Revision:

magiccube / src / main / java / org / distorted / main / RubikRenderer.java @ c0460c5c

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2019 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube 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
// Magic Cube 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 Magic Cube.  If not, see <http://www.gnu.org/licenses/>.                           //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

    
20
package org.distorted.main;
21

    
22
import android.opengl.GLES30;
23
import android.opengl.GLSurfaceView;
24

    
25
import org.distorted.effects.BaseEffect;
26
import org.distorted.library.effect.EffectType;
27
import org.distorted.library.effect.VertexEffectQuaternion;
28
import org.distorted.library.effect.VertexEffectRotate;
29
import org.distorted.library.main.DistortedLibrary;
30
import org.distorted.library.main.DistortedScreen;
31

    
32
import javax.microedition.khronos.egl.EGLConfig;
33
import javax.microedition.khronos.opengles.GL10;
34

    
35
import com.google.firebase.crashlytics.FirebaseCrashlytics;
36

    
37
///////////////////////////////////////////////////////////////////////////////////////////////////
38

    
39
public class RubikRenderer implements GLSurfaceView.Renderer, DistortedLibrary.ExceptionListener
40
{
41
   public static final float BRIGHTNESS = 0.30f;
42

    
43
   private final RubikSurfaceView mView;
44
   private final DistortedScreen mScreen;
45
   private final Fps mFPS;
46
   private boolean mErrorShown;
47

    
48
   private static class Fps
49
     {
50
     private static final int NUM_FRAMES  = 100;
51

    
52
     private long lastTime=0;
53
     private long[] durations;
54
     private int currDuration;
55
     private float currFPS;
56

    
57
     Fps()
58
       {
59
       durations = new long[NUM_FRAMES+1];
60
       currDuration = 0;
61

    
62
       for (int i=0; i<NUM_FRAMES+1; i++) durations[i] = 16;
63
       durations[NUM_FRAMES] = NUM_FRAMES * 16;
64
       }
65

    
66
     void onRender(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
       currFPS = ((int)(10000.0f*NUM_FRAMES/durations[NUM_FRAMES]))/10.0f;
76

    
77
       lastTime = time;
78
       }
79

    
80
     float getFPS()
81
       {
82
       return currFPS;
83
       }
84
     }
85

    
86
///////////////////////////////////////////////////////////////////////////////////////////////////
87

    
88
   RubikRenderer(RubikSurfaceView v)
89
     {
90
     mErrorShown = false;
91
     mView = v;
92
     mFPS = new Fps();
93
     mScreen = new DistortedScreen();
94
     mScreen.glClearColor(BRIGHTNESS, BRIGHTNESS, BRIGHTNESS, 1.0f);
95
     }
96

    
97
///////////////////////////////////////////////////////////////////////////////////////////////////
98
// various things are done here delayed, 'after the next render' as not to be done mid-render and
99
// cause artifacts.
100

    
101
   @Override
102
   public void onDrawFrame(GL10 glUnused)
103
     {
104
     long time = System.currentTimeMillis();
105
     mFPS.onRender(time);
106
     mView.getPreRender().preRender();
107
     mScreen.render(time);
108
     }
109

    
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111

    
112
   @Override
113
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
114
      {
115
      mScreen.resize(width,height);
116
      mView.setScreenSize(width,height);
117
      mView.getPreRender().setScreenSize(width);
118
      }
119

    
120
///////////////////////////////////////////////////////////////////////////////////////////////////
121

    
122
   @Override
123
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
124
      {
125
      DistortedLibrary.setMax(EffectType.VERTEX,61);    // 60 Minx quaternions + rotate
126
      VertexEffectRotate.enable();
127
      VertexEffectQuaternion.enable();
128
      BaseEffect.Type.enableEffects();
129

    
130
      DistortedLibrary.onSurfaceCreated(mView.getContext(),this,1);
131
      }
132

    
133
///////////////////////////////////////////////////////////////////////////////////////////////////
134

    
135
   public void distortedException(Exception ex)
136
     {
137
     String message = ex.getMessage();
138
     String shading = GLES30.glGetString(GLES30.GL_SHADING_LANGUAGE_VERSION);
139
     String version = GLES30.glGetString(GLES30.GL_VERSION);
140
     String vendor  = GLES30.glGetString(GLES30.GL_VENDOR);
141
     String renderer= GLES30.glGetString(GLES30.GL_RENDERER);
142

    
143
     if( message==null ) message = "exception NULL";
144

    
145
     if( BuildConfig.DEBUG )
146
       {
147
       android.util.Log.e("DISTORTED", message );
148
       android.util.Log.e("DISTORTED", "GLSL Version "+shading);
149
       android.util.Log.e("DISTORTED", "GL Version "  +version);
150
       android.util.Log.e("DISTORTED", "GL Vendor "   +vendor);
151
       android.util.Log.e("DISTORTED", "GL Renderer " +renderer);
152
       }
153
     else
154
       {
155
       FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
156
       crashlytics.setCustomKey("DistortedError", message );
157
       crashlytics.setCustomKey("GLSL Version"  , shading );
158
       crashlytics.setCustomKey("GLversion"     , version );
159
       crashlytics.setCustomKey("GL Vendor "    , vendor  );
160
       crashlytics.setCustomKey("GLSLrenderer"  , renderer);
161
       crashlytics.recordException(ex);
162
       }
163

    
164
     int glsl = DistortedLibrary.getGLSL();
165

    
166
     if( glsl< 300 && !mErrorShown )
167
       {
168
       mErrorShown = true;
169
       RubikActivity act = (RubikActivity)mView.getContext();
170
       act.OpenGLError();
171
       }
172
     }
173

    
174
///////////////////////////////////////////////////////////////////////////////////////////////////
175

    
176
   float getFPS()
177
     {
178
     return mFPS.getFPS();
179
     }
180

    
181
///////////////////////////////////////////////////////////////////////////////////////////////////
182

    
183
   DistortedScreen getScreen()
184
     {
185
     return mScreen;
186
     }
187
}
(3-3/4)