Project

General

Profile

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

magiccube / src / main / java / org / distorted / main / RubikRenderer.java @ 6142069a

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2019 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is proprietary software licensed under an EULA which you should have received      //
7
// along with the code. If not, check https://distorted.org/magic/License-Magic-Cube.html        //
8
///////////////////////////////////////////////////////////////////////////////////////////////////
9

    
10
package org.distorted.main;
11

    
12
import android.app.Activity;
13
import android.opengl.GLES30;
14
import android.opengl.GLSurfaceView;
15

    
16
import org.distorted.objectlib.effects.BaseEffect;
17
import org.distorted.library.effect.EffectType;
18
import org.distorted.library.effect.VertexEffectQuaternion;
19
import org.distorted.library.effect.VertexEffectRotate;
20
import org.distorted.library.main.DistortedLibrary;
21
import org.distorted.library.main.DistortedScreen;
22
import org.distorted.library.mesh.MeshBase;
23
import org.distorted.external.RubikNetwork;
24
import org.distorted.objectlib.main.ObjectControl;
25

    
26
import javax.microedition.khronos.egl.EGLConfig;
27
import javax.microedition.khronos.opengles.GL10;
28

    
29
import com.google.firebase.crashlytics.FirebaseCrashlytics;
30

    
31
///////////////////////////////////////////////////////////////////////////////////////////////////
32

    
33
public class RubikRenderer implements GLSurfaceView.Renderer, DistortedLibrary.ExceptionListener
34
{
35
   public static final float BRIGHTNESS = 0.30f;
36

    
37
   private final RubikSurfaceView mView;
38
   private final DistortedScreen mScreen;
39
   private final ObjectControl mControl;
40
   private final Fps mFPS;
41
   private boolean mErrorShown;
42
   private boolean mDebugSent;
43

    
44
   private static class Fps
45
     {
46
     private static final int NUM_FRAMES  = 100;
47

    
48
     private long lastTime=0;
49
     private final long[] durations;
50
     private int currDuration;
51
     private float currFPS;
52

    
53
     Fps()
54
       {
55
       durations = new long[NUM_FRAMES+1];
56
       currDuration = 0;
57

    
58
       for (int i=0; i<NUM_FRAMES+1; i++) durations[i] = 16;
59
       durations[NUM_FRAMES] = NUM_FRAMES * 16;
60
       }
61

    
62
     void onRender(long time)
63
       {
64
       if( lastTime==0 ) lastTime = time;
65

    
66
       currDuration++;
67
       if (currDuration >= NUM_FRAMES) currDuration = 0;
68
       durations[NUM_FRAMES] += ((time - lastTime) - durations[currDuration]);
69
       durations[currDuration] = time - lastTime;
70

    
71
       currFPS = ((int)(10000.0f*NUM_FRAMES/durations[NUM_FRAMES]))/10.0f;
72

    
73
       lastTime = time;
74
       }
75

    
76
     float getFPS()
77
       {
78
       return currFPS;
79
       }
80
     }
81

    
82
///////////////////////////////////////////////////////////////////////////////////////////////////
83

    
84
   RubikRenderer(RubikSurfaceView v)
85
     {
86
     mErrorShown = false;
87
     mView = v;
88
     mControl = v.getObjectControl();
89
     mFPS = new Fps();
90
     mScreen = new DistortedScreen();
91
     mScreen.glClearColor(BRIGHTNESS, BRIGHTNESS, BRIGHTNESS, 1.0f);
92
     }
93

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

    
98
   @Override
99
   public void onDrawFrame(GL10 glUnused)
100
     {
101
     long time = System.currentTimeMillis();
102
     mFPS.onRender(time);
103
     mControl.preRender();
104
     mScreen.render(time);
105
     }
106

    
107
///////////////////////////////////////////////////////////////////////////////////////////////////
108

    
109
   @Override
110
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
111
      {
112
      mScreen.resize(width,height);
113
      mView.setScreenSize(width,height);
114
      }
115

    
116
///////////////////////////////////////////////////////////////////////////////////////////////////
117

    
118
   @Override
119
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
120
      {
121
      DistortedLibrary.setMax(EffectType.VERTEX,ObjectControl.MAX_QUATS+1);
122
      MeshBase.setMaxEffComponents(ObjectControl.MAX_MOVING_PARTS);
123

    
124
      VertexEffectRotate.enable();
125
      VertexEffectQuaternion.enable();
126
      BaseEffect.Type.enableEffects();
127

    
128
      DistortedLibrary.onSurfaceCreated(mView.getContext(),this,1);
129
      DistortedLibrary.setCull(true);
130

    
131
      if( !mDebugSent )
132
        {
133
        mDebugSent= true;
134
        Activity act = (Activity)mView.getContext();
135
        RubikNetwork network = RubikNetwork.getInstance();
136
        network.debug(act);
137
        }
138
      }
139

    
140
///////////////////////////////////////////////////////////////////////////////////////////////////
141

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

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

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

    
171
     int glsl = DistortedLibrary.getGLSL();
172

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

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

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

    
188
///////////////////////////////////////////////////////////////////////////////////////////////////
189

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