Project

General

Profile

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

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

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
   static boolean mStartDebug = false;
49

    
50
   private static class Fps
51
     {
52
     private static final int NUM_FRAMES  = 100;
53

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

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

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

    
68
     void onRender(long time)
69
       {
70
       if( lastTime==0 ) lastTime = time;
71

    
72
       currDuration++;
73
       if (currDuration >= NUM_FRAMES) currDuration = 0;
74
       durations[NUM_FRAMES] += ((time - lastTime) - durations[currDuration]);
75
       durations[currDuration] = time - lastTime;
76

    
77
       currFPS = ((int)(10000.0f*NUM_FRAMES/durations[NUM_FRAMES]))/10.0f;
78

    
79
       lastTime = time;
80
       }
81

    
82
     float getFPS()
83
       {
84
       return currFPS;
85
       }
86
     }
87

    
88
///////////////////////////////////////////////////////////////////////////////////////////////////
89

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

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

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

    
111
     if( mStartDebug )
112
       {
113
       mStartDebug = false;
114
       RubikDebug debug = RubikDebug.getInstance();
115
       debug.onFirstDraw();
116
       }
117
     }
118

    
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120

    
121
   @Override
122
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
123
      {
124
      mScreen.resize(width,height);
125
      mView.setScreenSize(width,height);
126
      mView.getPreRender().setScreenSize(width);
127
      }
128

    
129
///////////////////////////////////////////////////////////////////////////////////////////////////
130

    
131
   @Override
132
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
133
      {
134
      VertexEffectRotate.enable();
135
      VertexEffectQuaternion.enable();
136
      BaseEffect.Type.enableEffects();
137

    
138
      DistortedLibrary.onSurfaceCreated(mView.getContext(),this,1);
139
      }
140

    
141
///////////////////////////////////////////////////////////////////////////////////////////////////
142

    
143
   public void distortedException(Exception ex)
144
     {
145
     String message = ex.getMessage();
146
     String shading = GLES30.glGetString(GLES30.GL_SHADING_LANGUAGE_VERSION);
147
     String version = GLES30.glGetString(GLES30.GL_VERSION);
148
     String vendor  = GLES30.glGetString(GLES30.GL_VENDOR);
149
     String renderer= GLES30.glGetString(GLES30.GL_RENDERER);
150

    
151
     if( message==null ) message = "exception NULL";
152

    
153
     if( BuildConfig.DEBUG )
154
       {
155
       android.util.Log.e("DISTORTED", message );
156
       android.util.Log.e("DISTORTED", "GLSL Version "+shading);
157
       android.util.Log.e("DISTORTED", "GL Version "  +version);
158
       android.util.Log.e("DISTORTED", "GL Vendor "   +vendor);
159
       android.util.Log.e("DISTORTED", "GL Renderer " +renderer);
160
       }
161
     else
162
       {
163
       FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
164
       crashlytics.setCustomKey("DistortedError", message );
165
       crashlytics.setCustomKey("GLSL Version"  , shading );
166
       crashlytics.setCustomKey("GLversion"     , version );
167
       crashlytics.setCustomKey("GL Vendor "    , vendor  );
168
       crashlytics.setCustomKey("GLSLrenderer"  , renderer);
169
       crashlytics.recordException(ex);
170
       }
171

    
172
     int glsl = DistortedLibrary.getGLSL();
173

    
174
     if( glsl< 300 && !mErrorShown )
175
       {
176
       mErrorShown = true;
177
       RubikActivity act = (RubikActivity)mView.getContext();
178
       act.OpenGLError();
179
       }
180
     }
181

    
182
///////////////////////////////////////////////////////////////////////////////////////////////////
183

    
184
   float getFPS()
185
     {
186
     return mFPS.getFPS();
187
     }
188

    
189
///////////////////////////////////////////////////////////////////////////////////////////////////
190

    
191
   DistortedScreen getScreen()
192
     {
193
     return mScreen;
194
     }
195
}
(4-4/5)