Project

General

Profile

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

magiccube / src / main / java / org / distorted / main_old / RubikRenderer.java @ 1c04d054

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_old;
11

    
12
import android.app.Activity;
13
import android.content.res.Resources;
14
import android.opengl.GLES30;
15
import android.opengl.GLSurfaceView;
16

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

    
29
import javax.microedition.khronos.egl.EGLConfig;
30
import javax.microedition.khronos.opengles.GL10;
31

    
32
import com.google.firebase.crashlytics.FirebaseCrashlytics;
33

    
34
import java.io.InputStream;
35

    
36
///////////////////////////////////////////////////////////////////////////////////////////////////
37

    
38
public class RubikRenderer implements GLSurfaceView.Renderer, DistortedLibrary.LibraryUser
39
{
40
   public static final float BRIGHTNESS = 0.30f;
41

    
42
   private final RubikSurfaceView mView;
43
   private final Resources mResources;
44
   private final DistortedScreen mScreen;
45
   private final ObjectControl mControl;
46
   private final Fps mFPS;
47
   private boolean mErrorShown;
48
   private boolean mDebugSent;
49

    
50
   private static class Fps
51
     {
52
     private static final int NUM_FRAMES  = 100;
53

    
54
     private long lastTime=0;
55
     private final long[] durations;
56
     private int currDuration;
57
     private float currFPS;
58

    
59
     Fps()
60
       {
61
       durations = new long[NUM_FRAMES+1];
62
       currDuration = 0;
63

    
64
       for (int i=0; i<NUM_FRAMES+1; i++) durations[i] = 16;
65
       durations[NUM_FRAMES] = NUM_FRAMES * 16;
66
       }
67

    
68
     void onRender(long time)
69
       {
70
       if( lastTime==0 ) lastTime = time;
71

    
72
       currDuration++;
73
       if (currDuration >= NUM_FRAMES) currDuration = 0;
74
       durations[NUM_FRAMES] += ((time - lastTime) - durations[currDuration]);
75
       durations[currDuration] = time - lastTime;
76

    
77
       currFPS = ((int)(10000.0f*NUM_FRAMES/durations[NUM_FRAMES]))/10.0f;
78

    
79
       lastTime = time;
80
       }
81

    
82
     float getFPS()
83
       {
84
       return currFPS;
85
       }
86
     }
87

    
88
///////////////////////////////////////////////////////////////////////////////////////////////////
89

    
90
   RubikRenderer(RubikSurfaceView v)
91
     {
92
     mView = v;
93
     mResources = v.getResources();
94

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

    
103
///////////////////////////////////////////////////////////////////////////////////////////////////
104

    
105
   float getFPS()
106
     {
107
     return mFPS.getFPS();
108
     }
109

    
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111

    
112
   DistortedScreen getScreen()
113
     {
114
     return mScreen;
115
     }
116

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

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

    
130
///////////////////////////////////////////////////////////////////////////////////////////////////
131

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

    
139
///////////////////////////////////////////////////////////////////////////////////////////////////
140

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

    
147
      VertexEffectRotate.enable();
148
      VertexEffectQuaternion.enable();
149
      BaseEffect.Type.enableEffects();
150
      //OverlayGeneric.enableEffects();
151

    
152
      DistortedLibrary.onSurfaceCreated(this,1);
153
      DistortedLibrary.setCull(true);
154

    
155
      if( !mDebugSent )
156
        {
157
        mDebugSent= true;
158
        Activity act = (Activity)mView.getContext();
159
        RubikNetwork network = RubikNetwork.getInstance();
160
        network.downloadUpdates(act);
161
        }
162
      }
163

    
164
///////////////////////////////////////////////////////////////////////////////////////////////////
165

    
166
   public void distortedException(Exception ex)
167
     {
168
     String message = ex.getMessage();
169
     String shading = GLES30.glGetString(GLES30.GL_SHADING_LANGUAGE_VERSION);
170
     String version = GLES30.glGetString(GLES30.GL_VERSION);
171
     String vendor  = GLES30.glGetString(GLES30.GL_VENDOR);
172
     String renderer= GLES30.glGetString(GLES30.GL_RENDERER);
173

    
174
     if( message==null ) message = "exception NULL";
175

    
176
     if( BuildConfig.DEBUG )
177
       {
178
       android.util.Log.e("DISTORTED", message );
179
       android.util.Log.e("DISTORTED", "GLSL Version "+shading);
180
       android.util.Log.e("DISTORTED", "GL Version "  +version);
181
       android.util.Log.e("DISTORTED", "GL Vendor "   +vendor);
182
       android.util.Log.e("DISTORTED", "GL Renderer " +renderer);
183
       }
184
     else
185
       {
186
       FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
187
       crashlytics.setCustomKey("DistortedError", message );
188
       crashlytics.setCustomKey("GLSL Version"  , shading );
189
       crashlytics.setCustomKey("GLversion"     , version );
190
       crashlytics.setCustomKey("GL Vendor "    , vendor  );
191
       crashlytics.setCustomKey("GLSLrenderer"  , renderer);
192
       crashlytics.recordException(ex);
193
       }
194

    
195
     int glsl = DistortedLibrary.getGLSL();
196

    
197
     if( glsl< 300 && !mErrorShown )
198
       {
199
       mErrorShown = true;
200
       RubikActivity act = (RubikActivity)mView.getContext();
201
       act.OpenGLError();
202
       }
203
     }
204

    
205
///////////////////////////////////////////////////////////////////////////////////////////////////
206

    
207
   public InputStream localFile(int fileID)
208
     {
209
     return mResources.openRawResource(fileID);
210
     }
211

    
212
///////////////////////////////////////////////////////////////////////////////////////////////////
213

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