Project

General

Profile

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

magiccube / src / main / java / org / distorted / main / RubikRenderer.java @ 63dd19c4

1 0c52af30 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2019 Leszek Koltunski                                                               //
3
//                                                                                               //
4 fdec60a3 Leszek Koltunski
// This file is part of Magic Cube.                                                              //
5 0c52af30 Leszek Koltunski
//                                                                                               //
6 fdec60a3 Leszek Koltunski
// Magic Cube is free software: you can redistribute it and/or modify                            //
7 0c52af30 Leszek Koltunski
// 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 fdec60a3 Leszek Koltunski
// Magic Cube is distributed in the hope that it will be useful,                                 //
12 0c52af30 Leszek Koltunski
// 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 fdec60a3 Leszek Koltunski
// along with Magic Cube.  If not, see <http://www.gnu.org/licenses/>.                           //
18 0c52af30 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
19
20 1f9772f3 Leszek Koltunski
package org.distorted.main;
21 0c52af30 Leszek Koltunski
22 31911113 Leszek Koltunski
import android.opengl.GLES30;
23 0c52af30 Leszek Koltunski
import android.opengl.GLSurfaceView;
24 31911113 Leszek Koltunski
25 d2556e79 Leszek Koltunski
import org.distorted.objectlib.effects.BaseEffect;
26 40ab026e Leszek Koltunski
import org.distorted.library.effect.EffectType;
27 98904e45 Leszek Koltunski
import org.distorted.library.effect.VertexEffectQuaternion;
28 27e6c301 Leszek Koltunski
import org.distorted.library.effect.VertexEffectRotate;
29 e1111500 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
30 0c52af30 Leszek Koltunski
import org.distorted.library.main.DistortedScreen;
31 42661133 Leszek Koltunski
import org.distorted.library.mesh.MeshBase;
32 6a083c6a Leszek Koltunski
import org.distorted.network.RubikNetwork;
33 2afc6754 Leszek Koltunski
import org.distorted.objectlib.main.ObjectControl;
34 0c52af30 Leszek Koltunski
35
import javax.microedition.khronos.egl.EGLConfig;
36
import javax.microedition.khronos.opengles.GL10;
37
38 31911113 Leszek Koltunski
import com.google.firebase.crashlytics.FirebaseCrashlytics;
39
40 0c52af30 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
41
42 c1df2105 Leszek Koltunski
public class RubikRenderer implements GLSurfaceView.Renderer, DistortedLibrary.ExceptionListener
43 0c52af30 Leszek Koltunski
{
44 2e0258fe Leszek Koltunski
   public static final float BRIGHTNESS = 0.30f;
45
46 4b4c217e Leszek Koltunski
   private final RubikSurfaceView mView;
47
   private final DistortedScreen mScreen;
48 2afc6754 Leszek Koltunski
   private final ObjectControl mControl;
49 4b4c217e Leszek Koltunski
   private final Fps mFPS;
50 e7e0a94d Leszek Koltunski
   private boolean mErrorShown;
51 6a083c6a Leszek Koltunski
   private boolean mDebugSent;
52 7eae2d49 Leszek Koltunski
53
   private static class Fps
54
     {
55
     private static final int NUM_FRAMES  = 100;
56
57
     private long lastTime=0;
58 42661133 Leszek Koltunski
     private final long[] durations;
59 7eae2d49 Leszek Koltunski
     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 47ba5ddc Leszek Koltunski
91
///////////////////////////////////////////////////////////////////////////////////////////////////
92
93 8becce57 Leszek Koltunski
   RubikRenderer(RubikSurfaceView v)
94 47ba5ddc Leszek Koltunski
     {
95 e7e0a94d Leszek Koltunski
     mErrorShown = false;
96 8becce57 Leszek Koltunski
     mView = v;
97 2afc6754 Leszek Koltunski
     mControl = v.getObjectControl();
98 7eae2d49 Leszek Koltunski
     mFPS = new Fps();
99 8becce57 Leszek Koltunski
     mScreen = new DistortedScreen();
100 14bd7976 Leszek Koltunski
     mScreen.glClearColor(BRIGHTNESS, BRIGHTNESS, BRIGHTNESS, 1.0f);
101 47ba5ddc Leszek Koltunski
     }
102
103 0c52af30 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
104
// various things are done here delayed, 'after the next render' as not to be done mid-render and
105
// cause artifacts.
106
107 a7a7cc9c Leszek Koltunski
   @Override
108
   public void onDrawFrame(GL10 glUnused)
109
     {
110 7eae2d49 Leszek Koltunski
     long time = System.currentTimeMillis();
111
     mFPS.onRender(time);
112 2afc6754 Leszek Koltunski
     mControl.preRender();
113 7eae2d49 Leszek Koltunski
     mScreen.render(time);
114 a7a7cc9c Leszek Koltunski
     }
115 0c52af30 Leszek Koltunski
116
///////////////////////////////////////////////////////////////////////////////////////////////////
117 aa8b36aa Leszek Koltunski
118
   @Override
119
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
120
      {
121 8becce57 Leszek Koltunski
      mScreen.resize(width,height);
122 123d6172 Leszek Koltunski
      mView.setScreenSize(width,height);
123 aa8b36aa Leszek Koltunski
      }
124
125
///////////////////////////////////////////////////////////////////////////////////////////////////
126
127
   @Override
128
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
129
      {
130 c0460c5c Leszek Koltunski
      DistortedLibrary.setMax(EffectType.VERTEX,61);    // 60 Minx quaternions + rotate
131 b4a9a34f Leszek Koltunski
      MeshBase.setMaxEffComponents(242);                // 242 moving parts (Gigaminx)
132
133 27e6c301 Leszek Koltunski
      VertexEffectRotate.enable();
134 98904e45 Leszek Koltunski
      VertexEffectQuaternion.enable();
135 64975793 Leszek Koltunski
      BaseEffect.Type.enableEffects();
136 aa8b36aa Leszek Koltunski
137 d7de3072 Leszek Koltunski
      DistortedLibrary.onSurfaceCreated(mView.getContext(),this,1);
138 6a083c6a Leszek Koltunski
139 63dd19c4 Leszek Koltunski
      if( /*!BuildConfig.DEBUG &&*/ !mDebugSent )
140 6a083c6a Leszek Koltunski
        {
141
        mDebugSent= true;
142
        RubikNetwork network = RubikNetwork.getInstance();
143
        network.debug( (RubikActivity)mView.getContext());
144
        }
145 aa8b36aa Leszek Koltunski
      }
146
147 c1df2105 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
148
149
   public void distortedException(Exception ex)
150
     {
151
     String message = ex.getMessage();
152
     String shading = GLES30.glGetString(GLES30.GL_SHADING_LANGUAGE_VERSION);
153
     String version = GLES30.glGetString(GLES30.GL_VERSION);
154
     String vendor  = GLES30.glGetString(GLES30.GL_VENDOR);
155
     String renderer= GLES30.glGetString(GLES30.GL_RENDERER);
156
157
     if( message==null ) message = "exception NULL";
158
159
     if( BuildConfig.DEBUG )
160
       {
161
       android.util.Log.e("DISTORTED", message );
162
       android.util.Log.e("DISTORTED", "GLSL Version "+shading);
163
       android.util.Log.e("DISTORTED", "GL Version "  +version);
164
       android.util.Log.e("DISTORTED", "GL Vendor "   +vendor);
165
       android.util.Log.e("DISTORTED", "GL Renderer " +renderer);
166
       }
167
     else
168
       {
169
       FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
170
       crashlytics.setCustomKey("DistortedError", message );
171
       crashlytics.setCustomKey("GLSL Version"  , shading );
172
       crashlytics.setCustomKey("GLversion"     , version );
173
       crashlytics.setCustomKey("GL Vendor "    , vendor  );
174
       crashlytics.setCustomKey("GLSLrenderer"  , renderer);
175
       crashlytics.recordException(ex);
176
       }
177
178
     int glsl = DistortedLibrary.getGLSL();
179
180 e7e0a94d Leszek Koltunski
     if( glsl< 300 && !mErrorShown )
181 c1df2105 Leszek Koltunski
       {
182 e7e0a94d Leszek Koltunski
       mErrorShown = true;
183 c1df2105 Leszek Koltunski
       RubikActivity act = (RubikActivity)mView.getContext();
184 e7e0a94d Leszek Koltunski
       act.OpenGLError();
185 c1df2105 Leszek Koltunski
       }
186
     }
187
188 7eae2d49 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
189
190
   float getFPS()
191
     {
192
     return mFPS.getFPS();
193
     }
194
195 aa8b36aa Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
196 0c52af30 Leszek Koltunski
197 8becce57 Leszek Koltunski
   DistortedScreen getScreen()
198 434f2f5a Leszek Koltunski
     {
199 64975793 Leszek Koltunski
     return mScreen;
200 434f2f5a Leszek Koltunski
     }
201 0c52af30 Leszek Koltunski
}