Project

General

Profile

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

magiccube / src / main / java / org / distorted / magic / RubikRenderer.java @ 27a70eae

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.magic;
21

    
22
import android.content.SharedPreferences;
23
import android.opengl.GLSurfaceView;
24

    
25
import org.distorted.effect.BaseEffect;
26
import org.distorted.library.effect.VertexEffectSink;
27
import org.distorted.library.main.DistortedEffects;
28
import org.distorted.library.main.DistortedLibrary;
29
import org.distorted.library.main.DistortedScreen;
30
import org.distorted.library.main.DistortedTexture;
31
import org.distorted.library.mesh.MeshFlat;
32
import org.distorted.library.message.EffectListener;
33
import org.distorted.object.RubikObject;
34
import org.distorted.object.RubikObjectList;
35
import org.distorted.uistate.RubikState;
36
import org.distorted.uistate.RubikStateSolving;
37

    
38
import javax.microedition.khronos.egl.EGLConfig;
39
import javax.microedition.khronos.opengles.GL10;
40

    
41
///////////////////////////////////////////////////////////////////////////////////////////////////
42

    
43
public class RubikRenderer implements GLSurfaceView.Renderer, EffectListener
44
{
45
    static final float CAMERA_DISTANCE = 0.6f;  // 0.6 of the length of min(scrHeight,scrWidth)
46
    public static final int TEXTURE_SIZE = 600;
47

    
48
    private RubikSurfaceView mView;
49
    private DistortedScreen mScreen;
50
    private RubikObjectList mNextObject;
51
    private int mScrambleObjectNum;
52
    private long mRotationFinishedID;
53
    private long[] mEffectID;
54
    private boolean mFinishRotation, mRemoveRotation, mSetQuatCurrent, mSetQuatAccumulated;
55
    private boolean mChangeObject, mSolveObject, mScrambleObject;
56
    private boolean mCanRotate, mCanDrag, mCanUI;
57
    private boolean mIsSolved;
58
    private RubikObject mOldObject, mNewObject;
59
    private int mScreenWidth, mScreenHeight;
60
    private MeshFlat mMesh;
61
    private SharedPreferences mPreferences;
62

    
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64

    
65
    RubikRenderer(RubikSurfaceView v)
66
      {
67
      mView = v;
68
      mScreen = new DistortedScreen();
69

    
70
      mOldObject = null;
71
      mNewObject = null;
72

    
73
      mScreenWidth = mScreenHeight = 0;
74
      mScrambleObjectNum = 0;
75

    
76
      mFinishRotation     = false;
77
      mRemoveRotation     = false;
78
      mSetQuatCurrent     = false;
79
      mSetQuatAccumulated = false;
80
      mChangeObject       = false;
81
      mSolveObject        = false;
82
      mScrambleObject     = false;
83

    
84
      mCanRotate   = true;
85
      mCanDrag     = true;
86
      mCanUI       = true;
87

    
88
      mEffectID = new long[BaseEffect.Type.LENGTH];
89

    
90
      mMesh= new MeshFlat(20,20);
91
      }
92

    
93
///////////////////////////////////////////////////////////////////////////////////////////////////
94

    
95
   private void createObjectNow(RubikObjectList object)
96
     {
97
     boolean firstTime = (mNewObject==null);
98

    
99
     if( mOldObject!=null ) mOldObject.releaseResources();
100
     mOldObject = mNewObject;
101

    
102
     DistortedTexture texture = new DistortedTexture(TEXTURE_SIZE,TEXTURE_SIZE);
103
     DistortedEffects effects = new DistortedEffects();
104

    
105
     mNewObject = object.create(mView.getQuatCurrent(), mView.getQuatAccumulated(), texture, mMesh, effects);
106
     mNewObject.createTexture();
107
     mView.setMovement(object.getObjectMovementClass());
108

    
109
     if( firstTime ) mNewObject.restorePreferences(mPreferences);
110

    
111
     if( mScreenWidth!=0 )
112
       {
113
       mNewObject.recomputeScaleFactor(mScreenWidth, mScreenHeight);
114
       }
115

    
116
     mIsSolved = true;
117
     }
118

    
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120
// do all 'adjustable' effects (SizeChange, Solve, Scramble)
121

    
122
   private void doEffectNow(BaseEffect.Type type)
123
     {
124
     int index = type.ordinal();
125

    
126
     try
127
       {
128
       mEffectID[index] = type.startEffect(this);
129
       }
130
     catch( Exception ex )
131
       {
132
       android.util.Log.e("renderer", "exception starting effect: "+ex.getMessage());
133

    
134
       mCanUI     = true;
135
       mCanRotate = true;
136
       mCanDrag   = true;
137
       }
138
     }
139

    
140
///////////////////////////////////////////////////////////////////////////////////////////////////
141

    
142
   void savePreferences(SharedPreferences.Editor editor)
143
     {
144
     mNewObject.savePreferences(editor);
145
     }
146

    
147
///////////////////////////////////////////////////////////////////////////////////////////////////
148

    
149
   void restorePreferences(SharedPreferences preferences)
150
     {
151
     mPreferences = preferences;
152
     }
153

    
154
///////////////////////////////////////////////////////////////////////////////////////////////////
155
// no this will not race with onDrawFrame
156

    
157
   void finishRotation()
158
     {
159
     mFinishRotation = true;
160
     }
161

    
162
///////////////////////////////////////////////////////////////////////////////////////////////////
163

    
164
   boolean createObject(RubikObjectList object)
165
     {
166
     if( mCanDrag && mCanRotate && object!=mNextObject )
167
       {
168
       mChangeObject = true;
169
       mNextObject = object;
170
       return true;
171
       }
172

    
173
     return false;
174
     }
175

    
176
///////////////////////////////////////////////////////////////////////////////////////////////////
177

    
178
   void scrambleObject(int num)
179
     {
180
     if( mCanUI )
181
       {
182
       mScrambleObject = true;
183
       mScrambleObjectNum = num;
184
       }
185
     }
186

    
187
///////////////////////////////////////////////////////////////////////////////////////////////////
188

    
189
   void solveObject()
190
     {
191
     if( mCanUI )
192
       {
193
       mSolveObject = true;
194
       }
195
     }
196

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

    
199
   boolean canRotate()
200
     {
201
     return mCanRotate;
202
     }
203

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

    
206
   boolean canDrag()
207
     {
208
     return mCanDrag;
209
     }
210

    
211
///////////////////////////////////////////////////////////////////////////////////////////////////
212

    
213
   void setQuatCurrentOnNextRender()
214
     {
215
     mSetQuatCurrent = true;
216
     }
217

    
218
///////////////////////////////////////////////////////////////////////////////////////////////////
219

    
220
   void setQuatAccumulatedOnNextRender()
221
     {
222
     mSetQuatAccumulated = true;
223
     }
224

    
225
///////////////////////////////////////////////////////////////////////////////////////////////////
226
// various things are done here delayed, 'after the next render' as not to be done mid-render and
227
// cause artifacts.
228

    
229
   @Override
230
   public void onDrawFrame(GL10 glUnused)
231
     {
232
     mScreen.render( System.currentTimeMillis() );
233

    
234
     if( mSetQuatCurrent )
235
       {
236
       mSetQuatCurrent = false;
237
       mView.setQuatCurrent();
238
       }
239

    
240
     if( mSetQuatAccumulated )
241
       {
242
       mSetQuatAccumulated = false;
243
       mView.setQuatAccumulated();
244
       }
245

    
246
     if( mFinishRotation )
247
       {
248
       mFinishRotation = false;
249
       mCanRotate      = false;
250
       mCanUI          = false;
251
       mRotationFinishedID = mNewObject.finishRotationNow(this);
252
       }
253

    
254
     if( mRemoveRotation )
255
       {
256
       mRemoveRotation=false;
257
       mNewObject.removeRotationNow();
258

    
259
       boolean solved = mNewObject.isSolved();
260

    
261
       if( solved && !mIsSolved )
262
         {
263
         if( RubikState.getCurrentState()==RubikState.SOLV )
264
           {
265
           RubikStateSolving solving = (RubikStateSolving)RubikState.SOLV.getStateClass();
266
           long time = solving.stopCounting();
267
           android.util.Log.e("renderer", "solving time: "+time+" milliseconds");
268
           }
269

    
270
         mCanDrag   = true;
271
         mCanRotate = false;
272
         mCanUI     = false;
273
         doEffectNow( BaseEffect.Type.WIN );
274
         }
275
       else
276
         {
277
         mCanRotate = true;
278
         mCanUI     = true;
279
         }
280

    
281
       mIsSolved = solved;
282
       }
283

    
284
     if( mChangeObject )
285
       {
286
       mChangeObject = false;
287
       mCanDrag      = false;
288
       mCanRotate    = false;
289
       mCanUI        = false;
290
       createObjectNow(mNextObject);
291
       doEffectNow( BaseEffect.Type.SIZECHANGE );
292
       }
293

    
294
     if( mSolveObject )
295
       {
296
       mSolveObject    = false;
297
       mCanDrag        = false;
298
       mCanRotate      = false;
299
       mCanUI          = false;
300
       doEffectNow( BaseEffect.Type.SOLVE );
301
       }
302

    
303
     if( mScrambleObject )
304
       {
305
       mScrambleObject = false;
306
       mCanDrag        = false;
307
       mCanRotate      = false;
308
       mCanUI          = false;
309
       doEffectNow( BaseEffect.Type.SCRAMBLE );
310
       }
311
     }
312

    
313
///////////////////////////////////////////////////////////////////////////////////////////////////
314

    
315
   @Override
316
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
317
      {
318
      if( mNewObject!=null ) mNewObject.createTexture();
319

    
320
      double halfFOVInRadians = Math.atan( 1.0f/(2*CAMERA_DISTANCE) );
321
      float fovInDegrees = (float)(2*halfFOVInRadians*(180/Math.PI));
322

    
323
      mScreen.setProjection( fovInDegrees, 0.1f);
324
      mScreen.resize(width, height);
325
      mView.setScreenSize(width,height);
326

    
327
      if( mNewObject!=null )
328
        {
329
        mNewObject.recomputeScaleFactor(width,height);
330
        }
331

    
332
      mScreenHeight = height;
333
      mScreenWidth  = width;
334
      }
335

    
336
///////////////////////////////////////////////////////////////////////////////////////////////////
337

    
338
   @Override
339
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
340
      {
341
      VertexEffectSink.enable();
342
      BaseEffect.Type.enableEffects();
343

    
344
      try
345
        {
346
        DistortedLibrary.onCreate(mView.getContext());
347
        }
348
      catch(Exception ex)
349
        {
350
        android.util.Log.e("Rubik", ex.getMessage() );
351
        }
352
      }
353

    
354
///////////////////////////////////////////////////////////////////////////////////////////////////
355
// PUBLIC API
356
///////////////////////////////////////////////////////////////////////////////////////////////////
357

    
358
   public void effectFinished(final long effectID)
359
     {
360
     if( effectID == mRotationFinishedID )
361
       {
362
       mRemoveRotation = true;
363
       }
364
     else
365
       {
366
       for(int i=0; i<BaseEffect.Type.LENGTH; i++)
367
         {
368
         if( effectID == mEffectID[i] )
369
           {
370
           mCanRotate   = true;
371
           mCanDrag     = true;
372
           mCanUI       = true;
373

    
374
           if( i==BaseEffect.Type.SCRAMBLE.ordinal() )
375
             {
376
             final RubikActivity act = (RubikActivity)mView.getContext();
377

    
378
             act.runOnUiThread(new Runnable()
379
               {
380
               @Override
381
               public void run()
382
                 {
383
                 RubikState.switchState( act, RubikState.SOLV);
384
                 }
385
               });
386
             }
387
           break;
388
           }
389
         }
390
       }
391
     }
392

    
393
///////////////////////////////////////////////////////////////////////////////////////////////////
394

    
395
   public RubikObject getObject()
396
     {
397
     return mNewObject;
398
     }
399

    
400
///////////////////////////////////////////////////////////////////////////////////////////////////
401

    
402
   public RubikObject getOldObject()
403
     {
404
     return mOldObject;
405
     }
406

    
407
///////////////////////////////////////////////////////////////////////////////////////////////////
408

    
409
   public DistortedScreen getScreen()
410
     {
411
     return mScreen;
412
     }
413

    
414
///////////////////////////////////////////////////////////////////////////////////////////////////
415

    
416
   public int getNumScrambles()
417
     {
418
     return mScrambleObjectNum;
419
     }
420
}
(2-2/3)