Project

General

Profile

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

magiccube / src / main / java / org / distorted / magic / RubikRenderer.java @ c3ffcf58

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.DistortedLibrary;
28
import org.distorted.library.main.DistortedScreen;
29
import org.distorted.library.message.EffectListener;
30
import org.distorted.object.RubikObject;
31
import org.distorted.object.RubikObjectList;
32
import org.distorted.scores.RubikScores;
33
import org.distorted.uistate.RubikState;
34
import org.distorted.uistate.RubikStateSolving;
35

    
36
import javax.microedition.khronos.egl.EGLConfig;
37
import javax.microedition.khronos.opengles.GL10;
38

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

    
41
public class RubikRenderer implements GLSurfaceView.Renderer, EffectListener
42
{
43
    public static final int NODE_FBO_SIZE = 600;
44

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

    
60
///////////////////////////////////////////////////////////////////////////////////////////////////
61

    
62
    RubikRenderer(RubikSurfaceView v)
63
      {
64
      mView = v;
65
      mScreen = new DistortedScreen();
66

    
67
      mOldObject = null;
68
      mNewObject = null;
69

    
70
      mScreenWidth = mScreenHeight = 0;
71
      mScrambleObjectNum = 0;
72

    
73
      mFinishRotation     = false;
74
      mRemoveRotation     = false;
75
      mSetQuatCurrent     = false;
76
      mSetQuatAccumulated = false;
77
      mChangeObject       = false;
78
      mSolveObject        = false;
79
      mScrambleObject     = false;
80

    
81
      mCanRotate   = true;
82
      mCanDrag     = true;
83
      mCanUI       = true;
84

    
85
      mEffectID = new long[BaseEffect.Type.LENGTH];
86
      }
87

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

    
90
   private void createObjectNow(RubikObjectList object, int size)
91
     {
92
     boolean firstTime = (mNewObject==null);
93

    
94
     if( mOldObject!=null ) mOldObject.releaseResources();
95
     mOldObject = mNewObject;
96

    
97
     mNewObject = object.create(size, mView.getQuatCurrent(), mView.getQuatAccumulated());
98
     mNewObject.createTexture();
99
     mView.setMovement(object.getObjectMovementClass());
100

    
101
     if( firstTime ) mNewObject.restorePreferences(mPreferences);
102

    
103
     if( mScreenWidth!=0 )
104
       {
105
       mNewObject.recomputeScaleFactor(mScreenWidth, mScreenHeight);
106
       }
107

    
108
     mIsSolved = true;
109
     }
110

    
111
///////////////////////////////////////////////////////////////////////////////////////////////////
112
// do all 'adjustable' effects (SizeChange, Solve, Scramble)
113

    
114
   private void doEffectNow(BaseEffect.Type type)
115
     {
116
     int index = type.ordinal();
117

    
118
     try
119
       {
120
       mEffectID[index] = type.startEffect(this);
121
       }
122
     catch( Exception ex )
123
       {
124
       android.util.Log.e("renderer", "exception starting effect: "+ex.getMessage());
125

    
126
       mCanUI     = true;
127
       mCanRotate = true;
128
       mCanDrag   = true;
129
       }
130
     }
131

    
132
///////////////////////////////////////////////////////////////////////////////////////////////////
133

    
134
   void savePreferences(SharedPreferences.Editor editor)
135
     {
136
     if( mNewObject!=null )
137
       {
138
       mNewObject.savePreferences(editor);
139
       }
140
     }
141

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

    
144
   void restorePreferences(SharedPreferences preferences)
145
     {
146
     mPreferences = preferences;
147
     }
148

    
149
///////////////////////////////////////////////////////////////////////////////////////////////////
150
// no this will not race with onDrawFrame
151

    
152
   void finishRotation()
153
     {
154
     mFinishRotation = true;
155
     }
156

    
157
///////////////////////////////////////////////////////////////////////////////////////////////////
158

    
159
   boolean createObject(RubikObjectList object, int size)
160
     {
161
     if( mCanDrag && mCanRotate && (object!=mNextObject || mNextSize!=size) && size>0 )
162
       {
163
       mChangeObject = true;
164
       mNextObject = object;
165
       mNextSize   = size;
166
       return true;
167
       }
168

    
169
     return false;
170
     }
171

    
172
///////////////////////////////////////////////////////////////////////////////////////////////////
173

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

    
183
///////////////////////////////////////////////////////////////////////////////////////////////////
184

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

    
193
///////////////////////////////////////////////////////////////////////////////////////////////////
194

    
195
   boolean canRotate()
196
     {
197
     return mCanRotate;
198
     }
199

    
200
///////////////////////////////////////////////////////////////////////////////////////////////////
201

    
202
   boolean canDrag()
203
     {
204
     return mCanDrag;
205
     }
206

    
207
///////////////////////////////////////////////////////////////////////////////////////////////////
208

    
209
   void setQuatCurrentOnNextRender()
210
     {
211
     mSetQuatCurrent = true;
212
     }
213

    
214
///////////////////////////////////////////////////////////////////////////////////////////////////
215

    
216
   void setQuatAccumulatedOnNextRender()
217
     {
218
     mSetQuatAccumulated = true;
219
     }
220

    
221
///////////////////////////////////////////////////////////////////////////////////////////////////
222
// various things are done here delayed, 'after the next render' as not to be done mid-render and
223
// cause artifacts.
224

    
225
   @Override
226
   public void onDrawFrame(GL10 glUnused)
227
     {
228
     mScreen.render( System.currentTimeMillis() );
229

    
230
     if( mSetQuatCurrent )
231
       {
232
       mSetQuatCurrent = false;
233
       mView.setQuatCurrent();
234
       }
235

    
236
     if( mSetQuatAccumulated )
237
       {
238
       mSetQuatAccumulated = false;
239
       mView.setQuatAccumulated();
240
       }
241

    
242
     if( mFinishRotation )
243
       {
244
       mFinishRotation = false;
245
       mCanRotate      = false;
246
       mCanUI          = false;
247
       mRotationFinishedID = mNewObject.finishRotationNow(this);
248

    
249
       if( mRotationFinishedID==0 ) // failed to add effect - should never happen
250
         {
251
         mCanRotate = true;
252
         mCanUI     = true;
253
         }
254
       }
255

    
256
     if( mRemoveRotation )
257
       {
258
       mRemoveRotation=false;
259
       mNewObject.removeRotationNow();
260

    
261
       boolean solved = mNewObject.isSolved();
262

    
263
       if( solved && !mIsSolved )
264
         {
265
         if( RubikState.getCurrentState()==RubikState.SOLV )
266
           {
267
           RubikStateSolving solving = (RubikStateSolving)RubikState.SOLV.getStateClass();
268
           solving.stopCounting();
269
           }
270

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

    
282
       mIsSolved = solved;
283
       }
284

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

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

    
304
     if( mScrambleObject )
305
       {
306
       mScrambleObject = false;
307
       mCanDrag        = false;
308
       mCanRotate      = false;
309
       mCanUI          = false;
310
       mIsSolved       = false;
311
       RubikScores.getInstance().incrementNumPlays();
312
       doEffectNow( BaseEffect.Type.SCRAMBLE );
313
       }
314
     }
315

    
316
///////////////////////////////////////////////////////////////////////////////////////////////////
317

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

    
323
      mScreen.resize(width, height);
324
      mView.setScreenSize(width,height);
325

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

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

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

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

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

    
353
///////////////////////////////////////////////////////////////////////////////////////////////////
354
// PUBLIC API
355
///////////////////////////////////////////////////////////////////////////////////////////////////
356

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

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

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

    
392
///////////////////////////////////////////////////////////////////////////////////////////////////
393

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

    
399
///////////////////////////////////////////////////////////////////////////////////////////////////
400

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

    
406
///////////////////////////////////////////////////////////////////////////////////////////////////
407

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

    
413
///////////////////////////////////////////////////////////////////////////////////////////////////
414

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