Project

General

Profile

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

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

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.control.RubikControl;
26
import org.distorted.objectlib.effects.BaseEffect;
27
import org.distorted.library.effect.EffectType;
28
import org.distorted.library.effect.VertexEffectQuaternion;
29
import org.distorted.library.effect.VertexEffectRotate;
30
import org.distorted.library.main.DistortedLibrary;
31
import org.distorted.library.main.DistortedScreen;
32
import org.distorted.library.mesh.MeshBase;
33
import org.distorted.network.RubikNetwork;
34

    
35
import javax.microedition.khronos.egl.EGLConfig;
36
import javax.microedition.khronos.opengles.GL10;
37

    
38
import com.google.firebase.crashlytics.FirebaseCrashlytics;
39

    
40
///////////////////////////////////////////////////////////////////////////////////////////////////
41

    
42
public class RubikRenderer implements GLSurfaceView.Renderer, DistortedLibrary.ExceptionListener
43
{
44
   public static final float BRIGHTNESS = 0.30f;
45

    
46
   private final RubikSurfaceView mView;
47
   private final DistortedScreen mScreen;
48
   private final Fps mFPS;
49
   private boolean mErrorShown;
50
   private boolean mDebugSent;
51
   private RubikControl mControl;
52

    
53
   private static class Fps
54
     {
55
     private static final int NUM_FRAMES  = 100;
56

    
57
     private long lastTime=0;
58
     private final long[] durations;
59
     private int currDuration;
60
     private float currFPS;
61

    
62
     Fps()
63
       {
64
       durations = new long[NUM_FRAMES+1];
65
       currDuration = 0;
66

    
67
       for (int i=0; i<NUM_FRAMES+1; i++) durations[i] = 16;
68
       durations[NUM_FRAMES] = NUM_FRAMES * 16;
69
       }
70

    
71
     void onRender(long time)
72
       {
73
       if( lastTime==0 ) lastTime = time;
74

    
75
       currDuration++;
76
       if (currDuration >= NUM_FRAMES) currDuration = 0;
77
       durations[NUM_FRAMES] += ((time - lastTime) - durations[currDuration]);
78
       durations[currDuration] = time - lastTime;
79

    
80
       currFPS = ((int)(10000.0f*NUM_FRAMES/durations[NUM_FRAMES]))/10.0f;
81

    
82
       lastTime = time;
83
       }
84

    
85
     float getFPS()
86
       {
87
       return currFPS;
88
       }
89
     }
90

    
91
///////////////////////////////////////////////////////////////////////////////////////////////////
92

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

    
102
///////////////////////////////////////////////////////////////////////////////////////////////////
103

    
104
   public void setControlState(boolean on)
105
     {
106
     mControl = on ? RubikControl.getInstance() : null;
107
     }
108

    
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110
// various things are done here delayed, 'after the next render' as not to be done mid-render and
111
// cause artifacts.
112

    
113
   @Override
114
   public void onDrawFrame(GL10 glUnused)
115
     {
116
     long time = System.currentTimeMillis();
117
     mFPS.onRender(time);
118
     mView.getPreRender().preRender();
119
     mScreen.render(time);
120

    
121
     if( mControl!=null ) mControl.postDrawFrame(time);
122
     }
123

    
124
///////////////////////////////////////////////////////////////////////////////////////////////////
125

    
126
   @Override
127
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
128
      {
129
      mScreen.resize(width,height);
130
      mView.setScreenSize(width,height);
131
      mView.getPreRender().setScreenSize(width);
132
      }
133

    
134
///////////////////////////////////////////////////////////////////////////////////////////////////
135

    
136
   @Override
137
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
138
      {
139
      DistortedLibrary.setMax(EffectType.VERTEX,61);    // 60 Minx quaternions + rotate
140
      MeshBase.setMaxEffComponents(242);                // 242 moving parts (Gigaminx)
141

    
142
      VertexEffectRotate.enable();
143
      VertexEffectQuaternion.enable();
144
      BaseEffect.Type.enableEffects();
145

    
146
      DistortedLibrary.onSurfaceCreated(mView.getContext(),this,1);
147

    
148
      if( !BuildConfig.DEBUG && !mDebugSent )
149
        {
150
        mDebugSent= true;
151
        RubikNetwork network = RubikNetwork.getInstance();
152
        network.debug( (RubikActivity)mView.getContext());
153
        }
154
      }
155

    
156
///////////////////////////////////////////////////////////////////////////////////////////////////
157

    
158
   public void distortedException(Exception ex)
159
     {
160
     String message = ex.getMessage();
161
     String shading = GLES30.glGetString(GLES30.GL_SHADING_LANGUAGE_VERSION);
162
     String version = GLES30.glGetString(GLES30.GL_VERSION);
163
     String vendor  = GLES30.glGetString(GLES30.GL_VENDOR);
164
     String renderer= GLES30.glGetString(GLES30.GL_RENDERER);
165

    
166
     if( message==null ) message = "exception NULL";
167

    
168
     if( BuildConfig.DEBUG )
169
       {
170
       android.util.Log.e("DISTORTED", message );
171
       android.util.Log.e("DISTORTED", "GLSL Version "+shading);
172
       android.util.Log.e("DISTORTED", "GL Version "  +version);
173
       android.util.Log.e("DISTORTED", "GL Vendor "   +vendor);
174
       android.util.Log.e("DISTORTED", "GL Renderer " +renderer);
175
       }
176
     else
177
       {
178
       FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
179
       crashlytics.setCustomKey("DistortedError", message );
180
       crashlytics.setCustomKey("GLSL Version"  , shading );
181
       crashlytics.setCustomKey("GLversion"     , version );
182
       crashlytics.setCustomKey("GL Vendor "    , vendor  );
183
       crashlytics.setCustomKey("GLSLrenderer"  , renderer);
184
       crashlytics.recordException(ex);
185
       }
186

    
187
     int glsl = DistortedLibrary.getGLSL();
188

    
189
     if( glsl< 300 && !mErrorShown )
190
       {
191
       mErrorShown = true;
192
       RubikActivity act = (RubikActivity)mView.getContext();
193
       act.OpenGLError();
194
       }
195
     }
196

    
197
///////////////////////////////////////////////////////////////////////////////////////////////////
198

    
199
   float getFPS()
200
     {
201
     return mFPS.getFPS();
202
     }
203

    
204
///////////////////////////////////////////////////////////////////////////////////////////////////
205

    
206
   DistortedScreen getScreen()
207
     {
208
     return mScreen;
209
     }
210
}
(3-3/4)