Project

General

Profile

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

magiccube / src / main / java / org / distorted / main / RubikRenderer.java @ 65bc1da3

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 296219b4 Leszek Koltunski
// 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 0c52af30 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
9
10 1f9772f3 Leszek Koltunski
package org.distorted.main;
11 0c52af30 Leszek Koltunski
12 e4854e54 Leszek Koltunski
import android.app.Activity;
13 d0ad3964 Leszek Koltunski
import android.app.ActivityManager;
14
import android.content.Context;
15
import android.content.pm.ConfigurationInfo;
16
import android.content.res.Resources;
17 31911113 Leszek Koltunski
import android.opengl.GLES30;
18 0c52af30 Leszek Koltunski
import android.opengl.GLSurfaceView;
19 31911113 Leszek Koltunski
20 b4cbe056 Leszek Koltunski
import org.distorted.library.main.InternalOutputSurface;
21 d2556e79 Leszek Koltunski
import org.distorted.objectlib.effects.BaseEffect;
22 40ab026e Leszek Koltunski
import org.distorted.library.effect.EffectType;
23 98904e45 Leszek Koltunski
import org.distorted.library.effect.VertexEffectQuaternion;
24 27e6c301 Leszek Koltunski
import org.distorted.library.effect.VertexEffectRotate;
25 e1111500 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
26 0c52af30 Leszek Koltunski
import org.distorted.library.main.DistortedScreen;
27 42661133 Leszek Koltunski
import org.distorted.library.mesh.MeshBase;
28 acabdd83 Leszek Koltunski
import org.distorted.external.RubikNetwork;
29 2afc6754 Leszek Koltunski
import org.distorted.objectlib.main.ObjectControl;
30 b4cbe056 Leszek Koltunski
import org.distorted.overlays.OverlayGeneric;
31 0c52af30 Leszek Koltunski
32
import javax.microedition.khronos.egl.EGLConfig;
33
import javax.microedition.khronos.opengles.GL10;
34
35 31911113 Leszek Koltunski
import com.google.firebase.crashlytics.FirebaseCrashlytics;
36
37 d0ad3964 Leszek Koltunski
import java.io.InputStream;
38
39 0c52af30 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
40
41 d0ad3964 Leszek Koltunski
public class RubikRenderer implements GLSurfaceView.Renderer, DistortedLibrary.LibraryUser
42 0c52af30 Leszek Koltunski
{
43 2e0258fe Leszek Koltunski
   public static final float BRIGHTNESS = 0.30f;
44
45 4b4c217e Leszek Koltunski
   private final RubikSurfaceView mView;
46 d0ad3964 Leszek Koltunski
   private final Resources mResources;
47 4b4c217e Leszek Koltunski
   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 8becce57 Leszek Koltunski
     mView = v;
96 d0ad3964 Leszek Koltunski
     mResources = v.getResources();
97
98
     mErrorShown = false;
99 2afc6754 Leszek Koltunski
     mControl = v.getObjectControl();
100 7eae2d49 Leszek Koltunski
     mFPS = new Fps();
101 8becce57 Leszek Koltunski
     mScreen = new DistortedScreen();
102 14bd7976 Leszek Koltunski
     mScreen.glClearColor(BRIGHTNESS, BRIGHTNESS, BRIGHTNESS, 1.0f);
103 b4cbe056 Leszek Koltunski
     mScreen.enableDepthStencil(InternalOutputSurface.DEPTH_NO_STENCIL);
104 47ba5ddc Leszek Koltunski
     }
105
106 d0ad3964 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
107
108
   float getFPS()
109
     {
110
     return mFPS.getFPS();
111
     }
112
113
///////////////////////////////////////////////////////////////////////////////////////////////////
114
115
   DistortedScreen getScreen()
116
     {
117
     return mScreen;
118
     }
119
120 0c52af30 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
121
// various things are done here delayed, 'after the next render' as not to be done mid-render and
122
// cause artifacts.
123
124 a7a7cc9c Leszek Koltunski
   @Override
125
   public void onDrawFrame(GL10 glUnused)
126
     {
127 7eae2d49 Leszek Koltunski
     long time = System.currentTimeMillis();
128
     mFPS.onRender(time);
129 2afc6754 Leszek Koltunski
     mControl.preRender();
130 7eae2d49 Leszek Koltunski
     mScreen.render(time);
131 a7a7cc9c Leszek Koltunski
     }
132 0c52af30 Leszek Koltunski
133
///////////////////////////////////////////////////////////////////////////////////////////////////
134 aa8b36aa Leszek Koltunski
135
   @Override
136
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
137
      {
138 8becce57 Leszek Koltunski
      mScreen.resize(width,height);
139 123d6172 Leszek Koltunski
      mView.setScreenSize(width,height);
140 aa8b36aa Leszek Koltunski
      }
141
142
///////////////////////////////////////////////////////////////////////////////////////////////////
143
144
   @Override
145
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
146
      {
147 34bc9f31 Leszek Koltunski
      DistortedLibrary.setMax(EffectType.VERTEX,ObjectControl.MAX_QUATS+1);
148
      MeshBase.setMaxEffComponents(ObjectControl.MAX_MOVING_PARTS);
149 b4a9a34f Leszek Koltunski
150 27e6c301 Leszek Koltunski
      VertexEffectRotate.enable();
151 98904e45 Leszek Koltunski
      VertexEffectQuaternion.enable();
152 64975793 Leszek Koltunski
      BaseEffect.Type.enableEffects();
153 b4cbe056 Leszek Koltunski
      OverlayGeneric.enableEffects();
154 aa8b36aa Leszek Koltunski
155 d0ad3964 Leszek Koltunski
      DistortedLibrary.onSurfaceCreated(this,1);
156 6b1998e0 Leszek Koltunski
      DistortedLibrary.setCull(true);
157 6a083c6a Leszek Koltunski
158 e4854e54 Leszek Koltunski
      if( !mDebugSent )
159 6a083c6a Leszek Koltunski
        {
160
        mDebugSent= true;
161 e4854e54 Leszek Koltunski
        Activity act = (Activity)mView.getContext();
162 6a083c6a Leszek Koltunski
        RubikNetwork network = RubikNetwork.getInstance();
163 e4854e54 Leszek Koltunski
        network.debug(act);
164 6a083c6a Leszek Koltunski
        }
165 aa8b36aa Leszek Koltunski
      }
166
167 c1df2105 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
168
169
   public void distortedException(Exception ex)
170
     {
171
     String message = ex.getMessage();
172
     String shading = GLES30.glGetString(GLES30.GL_SHADING_LANGUAGE_VERSION);
173
     String version = GLES30.glGetString(GLES30.GL_VERSION);
174
     String vendor  = GLES30.glGetString(GLES30.GL_VENDOR);
175
     String renderer= GLES30.glGetString(GLES30.GL_RENDERER);
176
177
     if( message==null ) message = "exception NULL";
178
179
     if( BuildConfig.DEBUG )
180
       {
181
       android.util.Log.e("DISTORTED", message );
182
       android.util.Log.e("DISTORTED", "GLSL Version "+shading);
183
       android.util.Log.e("DISTORTED", "GL Version "  +version);
184
       android.util.Log.e("DISTORTED", "GL Vendor "   +vendor);
185
       android.util.Log.e("DISTORTED", "GL Renderer " +renderer);
186
       }
187
     else
188
       {
189
       FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
190
       crashlytics.setCustomKey("DistortedError", message );
191
       crashlytics.setCustomKey("GLSL Version"  , shading );
192
       crashlytics.setCustomKey("GLversion"     , version );
193
       crashlytics.setCustomKey("GL Vendor "    , vendor  );
194
       crashlytics.setCustomKey("GLSLrenderer"  , renderer);
195
       crashlytics.recordException(ex);
196
       }
197
198
     int glsl = DistortedLibrary.getGLSL();
199
200 e7e0a94d Leszek Koltunski
     if( glsl< 300 && !mErrorShown )
201 c1df2105 Leszek Koltunski
       {
202 e7e0a94d Leszek Koltunski
       mErrorShown = true;
203 c1df2105 Leszek Koltunski
       RubikActivity act = (RubikActivity)mView.getContext();
204 e7e0a94d Leszek Koltunski
       act.OpenGLError();
205 c1df2105 Leszek Koltunski
       }
206
     }
207
208 aa8b36aa Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
209 0c52af30 Leszek Koltunski
210 d0ad3964 Leszek Koltunski
   public InputStream localFile(int fileID)
211 434f2f5a Leszek Koltunski
     {
212 d0ad3964 Leszek Koltunski
     return mResources.openRawResource(fileID);
213
     }
214
215
///////////////////////////////////////////////////////////////////////////////////////////////////
216
217
   public void logMessage(String message)
218
     {
219
     android.util.Log.e("Rubik", message );
220 434f2f5a Leszek Koltunski
     }
221 0c52af30 Leszek Koltunski
}