Project

General

Profile

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

magiccube / src / main / java / org / distorted / magic / RubikRenderer.java @ 53f23b64

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
import android.os.Bundle;
25

    
26
import org.distorted.dialog.RubikDialogNewRecord;
27
import org.distorted.dialog.RubikDialogSolved;
28
import org.distorted.effect.BaseEffect;
29
import org.distorted.library.effect.VertexEffectSink;
30
import org.distorted.library.main.DistortedLibrary;
31
import org.distorted.library.main.DistortedScreen;
32
import org.distorted.library.message.EffectListener;
33
import org.distorted.object.RubikObject;
34
import org.distorted.object.RubikObjectList;
35
import org.distorted.scores.RubikScores;
36
import org.distorted.uistate.RubikState;
37
import org.distorted.uistate.RubikStateSolving;
38

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

    
42
///////////////////////////////////////////////////////////////////////////////////////////////////
43

    
44
public class RubikRenderer implements GLSurfaceView.Renderer, EffectListener
45
{
46
    public static final int NODE_FBO_SIZE = 600;
47

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

    
65
///////////////////////////////////////////////////////////////////////////////////////////////////
66

    
67
    RubikRenderer(RubikSurfaceView v)
68
      {
69
      mView = v;
70
      mScreen = new DistortedScreen();
71

    
72
      mOldObject = null;
73
      mNewObject = null;
74

    
75
      mScreenWidth = mScreenHeight = 0;
76
      mScrambleObjectNum = 0;
77

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

    
86
      mCanRotate   = true;
87
      mCanDrag     = true;
88
      mCanUI       = true;
89

    
90
      mEffectID = new long[BaseEffect.Type.LENGTH];
91
      }
92

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

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

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

    
102
     mNewObject = object.create(size, mView.getQuatCurrent(), mView.getQuatAccumulated());
103
     mNewObject.createTexture();
104
     mView.setMovement(object.getObjectMovementClass());
105

    
106
     if( firstTime ) mNewObject.restorePreferences(mPreferences);
107

    
108
     if( mScreenWidth!=0 )
109
       {
110
       mNewObject.recomputeScaleFactor(mScreenWidth, mScreenHeight);
111
       }
112

    
113
     mIsSolved = true;
114
     }
115

    
116
///////////////////////////////////////////////////////////////////////////////////////////////////
117
// do all 'adjustable' effects (SizeChange, Solve, Scramble)
118

    
119
   private void doEffectNow(BaseEffect.Type type)
120
     {
121
     int index = type.ordinal();
122

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

    
131
       mCanUI     = true;
132
       mCanRotate = true;
133
       mCanDrag   = true;
134
       }
135
     }
136

    
137
///////////////////////////////////////////////////////////////////////////////////////////////////
138

    
139
   void savePreferences(SharedPreferences.Editor editor)
140
     {
141
     if( mNewObject!=null )
142
       {
143
       mNewObject.savePreferences(editor);
144
       }
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
   void createObject(RubikObjectList object, int size)
165
     {
166
     if( (object!=mNextObject || mNextSize!=size) && size>0 )
167
       {
168
       mChangeObject = true;
169
       mNextObject = object;
170
       mNextSize   = size;
171
       }
172
     }
173

    
174
///////////////////////////////////////////////////////////////////////////////////////////////////
175

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

    
185
///////////////////////////////////////////////////////////////////////////////////////////////////
186

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

    
195
///////////////////////////////////////////////////////////////////////////////////////////////////
196

    
197
   void setCanRotate(boolean can)
198
     {
199
     mCanRotate = can;
200
     }
201

    
202
///////////////////////////////////////////////////////////////////////////////////////////////////
203

    
204
   boolean canRotate()
205
     {
206
     return mCanRotate;
207
     }
208

    
209
///////////////////////////////////////////////////////////////////////////////////////////////////
210

    
211
   boolean canDrag()
212
     {
213
     return mCanDrag;
214
     }
215

    
216
///////////////////////////////////////////////////////////////////////////////////////////////////
217

    
218
   void setQuatCurrentOnNextRender()
219
     {
220
     mSetQuatCurrent = true;
221
     }
222

    
223
///////////////////////////////////////////////////////////////////////////////////////////////////
224

    
225
   void setQuatAccumulatedOnNextRender()
226
     {
227
     mSetQuatAccumulated = true;
228
     }
229

    
230
///////////////////////////////////////////////////////////////////////////////////////////////////
231
// various things are done here delayed, 'after the next render' as not to be done mid-render and
232
// cause artifacts.
233

    
234
   @Override
235
   public void onDrawFrame(GL10 glUnused)
236
     {
237
     mScreen.render( System.currentTimeMillis() );
238

    
239
     if( mSetQuatCurrent )
240
       {
241
       mSetQuatCurrent = false;
242
       mView.setQuatCurrent();
243
       }
244

    
245
     if( mSetQuatAccumulated )
246
       {
247
       mSetQuatAccumulated = false;
248
       mView.setQuatAccumulated();
249
       }
250

    
251
     if( mFinishRotation )
252
       {
253
       mFinishRotation = false;
254
       mCanRotate      = false;
255
       mCanUI          = false;
256
       mRotationFinishedID = mNewObject.finishRotationNow(this);
257

    
258
       if( mRotationFinishedID==0 ) // failed to add effect - should never happen
259
         {
260
         mCanRotate = true;
261
         mCanUI     = true;
262
         }
263
       }
264

    
265
     if( mRemoveRotation )
266
       {
267
       mRemoveRotation=false;
268
       mNewObject.removeRotationNow();
269

    
270
       boolean solved = mNewObject.isSolved();
271

    
272
       if( solved && !mIsSolved )
273
         {
274
         if( RubikState.getCurrentState()==RubikState.SOLV )
275
           {
276
           RubikStateSolving solving = (RubikStateSolving)RubikState.SOLV.getStateClass();
277
           mNewRecord = solving.stopCounting((RubikActivity)mView.getContext());
278

    
279
           if( mNewRecord< 0 )
280
             {
281
             mNewRecord = -mNewRecord;
282
             mIsNewRecord = false;
283
             }
284
           else
285
             {
286
             mIsNewRecord = true;
287
             }
288
           }
289

    
290
         mCanDrag   = true;
291
         mCanRotate = false;
292
         mCanUI     = false;
293
         doEffectNow( BaseEffect.Type.WIN );
294
         }
295
       else
296
         {
297
         mCanRotate = true;
298
         mCanUI     = true;
299
         }
300

    
301
       mIsSolved = solved;
302
       }
303

    
304
     if( mChangeObject )
305
       {
306
       mChangeObject = false;
307
       mCanDrag      = false;
308
       mCanRotate    = false;
309
       mCanUI        = false;
310
       createObjectNow(mNextObject, mNextSize);
311
       doEffectNow( BaseEffect.Type.SIZECHANGE );
312
       }
313

    
314
     if( mSolveObject )
315
       {
316
       mSolveObject    = false;
317
       mCanDrag        = false;
318
       mCanRotate      = false;
319
       mCanUI          = false;
320
       doEffectNow( BaseEffect.Type.SOLVE );
321
       }
322

    
323
     if( mScrambleObject )
324
       {
325
       mScrambleObject = false;
326
       mCanDrag        = false;
327
       mCanRotate      = false;
328
       mCanUI          = false;
329
       mIsSolved       = false;
330
       RubikScores.getInstance().incrementNumPlays();
331
       doEffectNow( BaseEffect.Type.SCRAMBLE );
332
       }
333
     }
334

    
335
///////////////////////////////////////////////////////////////////////////////////////////////////
336

    
337
   @Override
338
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
339
      {
340
      if( mNewObject!=null ) mNewObject.createTexture();
341

    
342
      mScreen.resize(width, height);
343
      mView.setScreenSize(width,height);
344

    
345
      if( mNewObject!=null )
346
        {
347
        mNewObject.recomputeScaleFactor(width,height);
348
        }
349

    
350
      mScreenHeight = height;
351
      mScreenWidth  = width;
352
      }
353

    
354
///////////////////////////////////////////////////////////////////////////////////////////////////
355

    
356
   @Override
357
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
358
      {
359
      VertexEffectSink.enable();
360
      BaseEffect.Type.enableEffects();
361

    
362
      try
363
        {
364
        DistortedLibrary.onCreate(mView.getContext());
365
        }
366
      catch(Exception ex)
367
        {
368
        android.util.Log.e("Rubik", ex.getMessage() );
369
        }
370
      }
371

    
372
///////////////////////////////////////////////////////////////////////////////////////////////////
373
// PUBLIC API
374
///////////////////////////////////////////////////////////////////////////////////////////////////
375

    
376
   public void effectFinished(final long effectID)
377
     {
378
     if( effectID == mRotationFinishedID )
379
       {
380
       mRemoveRotation = true;
381
       }
382
     else
383
       {
384
       for(int i=0; i<BaseEffect.Type.LENGTH; i++)
385
         {
386
         if( effectID == mEffectID[i] )
387
           {
388
           mCanRotate   = true;
389
           mCanDrag     = true;
390
           mCanUI       = true;
391

    
392
           if( i==BaseEffect.Type.SCRAMBLE.ordinal() )
393
             {
394
             final RubikActivity act = (RubikActivity)mView.getContext();
395

    
396
             act.runOnUiThread(new Runnable()
397
               {
398
               @Override
399
               public void run()
400
                 {
401
                 RubikState.switchState( act, RubikState.SOLV);
402
                 }
403
               });
404
             }
405

    
406
           if( i==BaseEffect.Type.WIN.ordinal() )
407
             {
408
             if( RubikState.getCurrentState()==RubikState.SOLV )
409
               {
410
               final RubikActivity act = (RubikActivity)mView.getContext();
411
               Bundle bundle = new Bundle();
412
               bundle.putLong("time", mNewRecord );
413

    
414
               if( mIsNewRecord )
415
                 {
416
                 RubikDialogNewRecord dialog = new RubikDialogNewRecord();
417
                 dialog.setArguments(bundle);
418
                 dialog.show( act.getSupportFragmentManager(), null);
419
                 }
420
               else
421
                 {
422
                 RubikDialogSolved dialog = new RubikDialogSolved();
423
                 dialog.setArguments(bundle);
424
                 dialog.show( act.getSupportFragmentManager(), null);
425
                 }
426
               }
427
             }
428

    
429
           break;
430
           }
431
         }
432
       }
433
     }
434

    
435
///////////////////////////////////////////////////////////////////////////////////////////////////
436

    
437
   public RubikObject getObject()
438
     {
439
     return mNewObject;
440
     }
441

    
442
///////////////////////////////////////////////////////////////////////////////////////////////////
443

    
444
   public RubikObject getOldObject()
445
     {
446
     return mOldObject;
447
     }
448

    
449
///////////////////////////////////////////////////////////////////////////////////////////////////
450

    
451
   public DistortedScreen getScreen()
452
     {
453
     return mScreen;
454
     }
455

    
456
///////////////////////////////////////////////////////////////////////////////////////////////////
457

    
458
   public int getNumScrambles()
459
     {
460
     return mScrambleObjectNum;
461
     }
462
}
(2-2/3)