Project

General

Profile

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

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

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.app.ActivityManager;
14
import android.content.Context;
15
import android.content.pm.ConfigurationInfo;
16
import android.content.res.Resources;
17
import android.opengl.GLES30;
18
import android.opengl.GLSurfaceView;
19

    
20
import org.distorted.library.main.InternalOutputSurface;
21
import org.distorted.objectlib.effects.BaseEffect;
22
import org.distorted.library.effect.EffectType;
23
import org.distorted.library.effect.VertexEffectQuaternion;
24
import org.distorted.library.effect.VertexEffectRotate;
25
import org.distorted.library.main.DistortedLibrary;
26
import org.distorted.library.main.DistortedScreen;
27
import org.distorted.library.mesh.MeshBase;
28
import org.distorted.external.RubikNetwork;
29
import org.distorted.objectlib.main.ObjectControl;
30
import org.distorted.overlays.OverlayGeneric;
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
import java.io.InputStream;
38

    
39
///////////////////////////////////////////////////////////////////////////////////////////////////
40

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

    
45
   private final RubikSurfaceView mView;
46
   private final Resources mResources;
47
   private final DistortedScreen mScreen;
48
   private final ObjectControl mControl;
49
   private final Fps mFPS;
50
   private boolean mErrorShown;
51
   private boolean mDebugSent;
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
     mView = v;
96
     mResources = v.getResources();
97

    
98
     mErrorShown = false;
99
     mControl = v.getObjectControl();
100
     mFPS = new Fps();
101
     mScreen = new DistortedScreen();
102
     mScreen.glClearColor(BRIGHTNESS, BRIGHTNESS, BRIGHTNESS, 1.0f);
103
     mScreen.enableDepthStencil(InternalOutputSurface.DEPTH_NO_STENCIL);
104
     }
105

    
106
///////////////////////////////////////////////////////////////////////////////////////////////////
107

    
108
   float getFPS()
109
     {
110
     return mFPS.getFPS();
111
     }
112

    
113
///////////////////////////////////////////////////////////////////////////////////////////////////
114

    
115
   DistortedScreen getScreen()
116
     {
117
     return mScreen;
118
     }
119

    
120
///////////////////////////////////////////////////////////////////////////////////////////////////
121
// various things are done here delayed, 'after the next render' as not to be done mid-render and
122
// cause artifacts.
123

    
124
   @Override
125
   public void onDrawFrame(GL10 glUnused)
126
     {
127
     long time = System.currentTimeMillis();
128
     mFPS.onRender(time);
129
     mControl.preRender();
130
     mScreen.render(time);
131
     }
132

    
133
///////////////////////////////////////////////////////////////////////////////////////////////////
134

    
135
   @Override
136
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
137
      {
138
      mScreen.resize(width,height);
139
      mView.setScreenSize(width,height);
140
      }
141

    
142
///////////////////////////////////////////////////////////////////////////////////////////////////
143

    
144
   @Override
145
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
146
      {
147
      DistortedLibrary.setMax(EffectType.VERTEX,ObjectControl.MAX_QUATS+1);
148
      MeshBase.setMaxEffComponents(ObjectControl.MAX_MOVING_PARTS);
149

    
150
      VertexEffectRotate.enable();
151
      VertexEffectQuaternion.enable();
152
      BaseEffect.Type.enableEffects();
153
      OverlayGeneric.enableEffects();
154

    
155
      DistortedLibrary.onSurfaceCreated(this,1);
156
      DistortedLibrary.setCull(true);
157

    
158
      if( !mDebugSent )
159
        {
160
        mDebugSent= true;
161
        Activity act = (Activity)mView.getContext();
162
        RubikNetwork network = RubikNetwork.getInstance();
163
        network.debug(act);
164
        }
165
      }
166

    
167
///////////////////////////////////////////////////////////////////////////////////////////////////
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
     if( glsl< 300 && !mErrorShown )
201
       {
202
       mErrorShown = true;
203
       RubikActivity act = (RubikActivity)mView.getContext();
204
       act.OpenGLError();
205
       }
206
     }
207

    
208
///////////////////////////////////////////////////////////////////////////////////////////////////
209

    
210
   public InputStream localFile(int fileID)
211
     {
212
     return mResources.openRawResource(fileID);
213
     }
214

    
215
///////////////////////////////////////////////////////////////////////////////////////////////////
216

    
217
   public void logMessage(String message)
218
     {
219
     android.util.Log.e("Rubik", message );
220
     }
221
}
(3-3/4)