Project

General

Profile

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

examples / src / main / java / org / distorted / examples / starwars / StarWarsRenderer.java @ 59bbb86a

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// Distorted 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
// Distorted 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 Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

    
20
package org.distorted.examples.starwars;
21

    
22
import java.io.IOException;
23
import java.io.InputStream;
24
import java.util.Random;
25

    
26
import javax.microedition.khronos.egl.EGLConfig;
27
import javax.microedition.khronos.opengles.GL10;
28

    
29
import org.distorted.examples.R;
30

    
31
import org.distorted.library.type.Dynamic1D;
32
import org.distorted.library.type.Dynamic3D;
33
import org.distorted.library.DistortedNode;
34
import org.distorted.library.type.Static1D;
35
import org.distorted.library.type.Static3D;
36
import org.distorted.library.type.Static4D;
37
import org.distorted.library.message.EffectListener;
38
import org.distorted.library.message.EffectMessage;
39
import org.distorted.library.Distorted;
40
import org.distorted.library.DistortedBitmap;
41

    
42
import android.graphics.Bitmap;
43
import android.graphics.BitmapFactory;
44
import android.graphics.Canvas;
45
import android.graphics.Paint;
46
import android.graphics.Typeface;
47
import android.opengl.GLES20;
48
import android.opengl.GLSurfaceView;
49

    
50
///////////////////////////////////////////////////////////////////////////////////////////////////
51

    
52
class StarWarsRenderer implements GLSurfaceView.Renderer, EffectListener
53
  {
54
  private final String[] mGFFAString = 
55
         
56
            { "A long time ago, in a galaxy far,",
57
              "far away...." };                    // yes, there really was a four-dot ellipsis
58

    
59
  private final String[] mCRAWLString = 
60
      
61
            { "It is a period of civil war.",      // in the original 1977 version, there was no 
62
              "Rebel spaceships, striking",        // 'Episode IV New Hope' subtitle line before
63
              "from a hidden base, have",          // the crawl. Lets keep to the classic.
64
              "won their first victory",
65
              "against the evil Galactic",
66
              "Empire.",
67
              "",
68
              "During the battle, rebel",
69
              "spies managed to steal",
70
              "secret plans to the Empire's",
71
              "ultimate weapon, the",
72
              "DEATH STAR, an armored",
73
              "space station with enough",
74
              "power to destroy an entire",
75
              "planet.",
76
              "",
77
              "Pursued by the Empire's",
78
              "sinister agents, Princess",
79
              "Leia races home aboard her",
80
              "starship, custodian of the",
81
              "stolen plans that can save",
82
              "her people and restore",
83
              "freedom to the galaxy...." };      // four-dot.
84
   
85
  private final int NUM_STARS = 40;
86
   
87
  private final int CRAWL_COLOR = 0xffffe81f;
88
  private final int GFFA_COLOR  = 0xff0000ff;
89
  private final int FONT_HEIGHT      = 40;
90
  private final int VERTICAL_SPACING = 10;
91
  private final String CRAWL_TYPEFACE = "News Gothic";
92
   
93
  private final int CRAWL_WIDTH = 500;
94
  private final int CRAWL_HEIGHT= (FONT_HEIGHT+VERTICAL_SPACING)*(mCRAWLString.length+1);
95
   
96
  private final int GFFA_WIDTH = 600;
97
  private final int GFFA_HEIGHT= (FONT_HEIGHT+VERTICAL_SPACING)*(mGFFAString.length+1);
98
   
99
  private final float CRAWL_ANGLE = -30.0f;
100
   
101
  private GLSurfaceView mView;
102
  private DistortedBitmap mScreen, mGFFA, mLogo, mCrawl, mCrawlBackground;
103
  private DistortedBitmap[] mStars;
104
  private long gffaID, logoID, crawlID;
105
    
106
  private Random mRnd = new Random(0);
107
  private DistortedNode mRoot, mBackground;
108
    
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110

    
111
  public StarWarsRenderer(GLSurfaceView v) 
112
    {
113
    mView = v;
114
     
115
    Distorted.setFov(60.0f);
116
    }
117

    
118
///////////////////////////////////////////////////////////////////////////////////////////////////
119
   
120
  public void onDrawFrame(GL10 glUnused) 
121
    {
122
    GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
123
    GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
124
     
125
    mRoot.draw(System.currentTimeMillis());
126
    }
127

    
128
///////////////////////////////////////////////////////////////////////////////////////////////////
129
    
130
  public void onSurfaceChanged(GL10 glUnused, int width, int height) 
131
    { 
132
    Distorted.onSurfaceChanged(width, height); 
133
    setupScreen(width,height);
134
    }
135

    
136
///////////////////////////////////////////////////////////////////////////////////////////////////
137
    
138
  public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
139
    {
140
    setupBitmaps();
141
         
142
    try
143
      {
144
      Distorted.onSurfaceCreated(mView.getContext());
145
      }
146
    catch(Exception ex)
147
      {
148
      android.util.Log.e("Star Wars", ex.getMessage() );
149
      }
150
    }
151
    
152
///////////////////////////////////////////////////////////////////////////////////////////////////
153

    
154
  private void setupScreen(int w, int h)
155
    {
156
    mScreen = new DistortedBitmap(w,h,1);
157
    mRoot = new DistortedNode(mScreen);
158
      
159
    mCrawlBackground = new DistortedBitmap(w,(int)(Math.sqrt(3.0)*h),1);
160
       
161
    int randomA, randomX, randomY, randomTime;
162
    float randomS, randomAlpha1, randomAlpha2;
163
       
164
    Static3D center = new Static3D(0,0,0);
165
    Static3D axis   = new Static3D(0,0,1);
166

    
167
    for(int i=0; i<NUM_STARS; i++)
168
      {
169
      randomX = mRnd.nextInt(w);
170
      randomY = mRnd.nextInt(h);
171
      randomS = 0.2f+ mRnd.nextFloat();
172
      randomA = (int)(180*mRnd.nextFloat());
173
      randomAlpha1 = 0.2f + 0.8f*mRnd.nextFloat();
174
      randomAlpha2 = 0.8f + 0.2f*mRnd.nextFloat();
175
      randomTime = 500+mRnd.nextInt(2000);
176
      
177
      mStars[i].move( new Static3D(randomX,randomY,0) );
178
      mStars[i].scale( new Static3D(randomS,randomS,randomS) );
179
      mStars[i].rotate( new Static1D(randomA), axis, center );
180
      
181
      Dynamic1D di = new Dynamic1D(randomTime,0.0f);
182
      di.setNoise(0.5f);
183
      di.add(new Static1D(randomAlpha1));
184
      di.add(new Static1D(randomAlpha2));
185
      
186
      mStars[i].alpha(di);
187
      
188
      mRoot.attach(mStars[i]);
189
      }
190
      
191
    float scale = (0.5f*w/mGFFA.getWidth());
192
    
193
    Dynamic1D di = new Dynamic1D(6000,0.5f);
194
    di.add(new Static1D(1.0f));
195
    di.add(new Static1D(1.0f));
196
    di.add(new Static1D(0.0f));
197
    
198
    mGFFA.move( new Static3D(w/5,h/3,0) );
199
    mGFFA.scale( new Static3D(scale,scale,scale) );
200
    mGFFA.alpha(di);
201
      
202
    mRoot.attach(mGFFA);
203
    mGFFA.addEventListener(this); 
204
    }
205
    
206
///////////////////////////////////////////////////////////////////////////////////////////////////
207

    
208
  private void setupBitmaps()
209
    {
210
    InputStream is1 = mView.getContext().getResources().openRawResource(R.raw.starwars);
211
    InputStream is2 = mView.getContext().getResources().openRawResource(R.raw.star);
212
    
213
    Bitmap bitmapStar, bitmapGFFA, bitmapLogo, bitmapText;
214
      
215
    try 
216
      {
217
      bitmapLogo = BitmapFactory.decodeStream(is1);
218
      bitmapStar = BitmapFactory.decodeStream(is2);
219
      } 
220
    finally 
221
      {
222
      try 
223
        {
224
        is1.close();
225
        is2.close();
226
        } 
227
      catch(IOException e) { }
228
      } 
229
      
230
    Paint paint = new Paint();
231
    paint.setAntiAlias(true);
232
    paint.setTextAlign(Paint.Align.LEFT);
233
    paint.setTextSize(FONT_HEIGHT);
234
    paint.setColor(GFFA_COLOR);
235
      
236
    Typeface tf = Typeface.create(CRAWL_TYPEFACE, Typeface.BOLD);
237
    paint.setTypeface(tf);     
238
 
239
    ///// create GFFA ///////////////////
240
    mGFFA  = new DistortedBitmap(GFFA_WIDTH, GFFA_HEIGHT, 1);
241
    bitmapGFFA = Bitmap.createBitmap(GFFA_WIDTH,GFFA_HEIGHT,Bitmap.Config.ARGB_8888);
242
    bitmapGFFA.eraseColor(0x00000000);
243
    Canvas gffaCanvas = new Canvas(bitmapGFFA);
244
      
245
    for(int i=0; i<mGFFAString.length; i++)
246
      {
247
      gffaCanvas.drawText(mGFFAString[i], 0, (i+1)*(FONT_HEIGHT+VERTICAL_SPACING), paint);  
248
      }
249
  
250
    mGFFA.setBitmap(bitmapGFFA);
251
      
252
    ///// create Logo ///////////////////
253
    mLogo  = new DistortedBitmap(bitmapLogo, 1);
254
      
255
    ///// create CRAWL //////////////////
256
    mCrawl = new DistortedBitmap(CRAWL_WIDTH, CRAWL_HEIGHT, 1);
257
    bitmapText = Bitmap.createBitmap(CRAWL_WIDTH,CRAWL_HEIGHT,Bitmap.Config.ARGB_8888);
258
    bitmapText.eraseColor(0x00000000);
259
    Canvas textCanvas = new Canvas(bitmapText);
260
    paint.setColor(CRAWL_COLOR);
261
  
262
    for(int i=0; i<mCRAWLString.length; i++)
263
      {
264
      displayJustified(mCRAWLString[i], 0, (i+1)*(FONT_HEIGHT+VERTICAL_SPACING), CRAWL_WIDTH, textCanvas, paint);  
265
      }
266
      
267
    mCrawl.setBitmap(bitmapText);
268
      
269
    ///// create Stars ///////////////////
270
      
271
    mStars = new DistortedBitmap[NUM_STARS];
272
      
273
    mStars[0] = new DistortedBitmap(bitmapStar,1);
274
      
275
    for(int i=1; i<NUM_STARS; i++)
276
      {
277
      mStars[i] = new DistortedBitmap(mStars[0], Distorted.CLONE_BITMAP|Distorted.CLONE_VERTEX);  
278
      }
279
      
280
    gffaID = mGFFA.getID();
281
    logoID = mLogo.getID();
282
    crawlID= mCrawl.getID();
283
    }
284
 
285
///////////////////////////////////////////////////////////////////////////////////////////////////
286

    
287
  private void displayJustified(String str, int x, int y, int length, Canvas c, Paint paint)
288
    { 
289
    int len       = str.length();
290
    int numspaces = retNumSpaces(str);
291
    
292
    if( len>0 && str.charAt(len-1) == ' ' ) numspaces--;
293

    
294
    float left=x,w = (numspaces>0 ? (length-paint.measureText(str))/numspaces : 0);
295
    String tmp;
296
    int begin,end=0;
297

    
298
    while( end<len )
299
      {
300
      begin=end;
301
      end = str.indexOf(' ', begin)+1;
302
      if( end<=0 ) end = len;
303

    
304
      tmp = str.substring(begin,end);
305
      c.drawText( tmp, left, y, paint);
306
      left+= (paint.measureText(tmp)+w);
307
      }  
308
    }
309

    
310
///////////////////////////////////////////////////////////////////////////////////////////////////
311
   
312
  private int retNumSpaces(String str)
313
    {
314
    int begin=0, ret=0;
315

    
316
    while( true )
317
      {
318
      begin = str.indexOf(' ', begin) +1;
319

    
320
      if( begin>0 ) ret++;
321
      else break;
322
      }
323

    
324
    return ret;
325
    }
326
    
327
///////////////////////////////////////////////////////////////////////////////////////////////////
328

    
329
  public void effectMessage(final EffectMessage em, final long effectID, final int effectName, final long bitmapID, final String message)
330
    {
331
    if( em==EffectMessage.EFFECT_FINISHED )
332
      {
333
      if( bitmapID == gffaID )
334
        {
335
        mRoot.detach(mGFFA);   
336
        mGFFA.removeEventListener(this);
337
       
338
        int screenW=mScreen.getWidth();
339
        int screenH=mScreen.getHeight();
340
        
341
        int logoW = mLogo.getWidth();
342
        int logoH = mLogo.getHeight();
343
      
344
        int initSize= (int)(3.0f*screenW/logoW);
345
        int finaSize= (int)(0.1f*screenW/logoW);
346
      
347
        Dynamic3D di = new Dynamic3D(10000,0.5f);
348
        di.add(new Static3D(initSize,initSize,1));
349
        di.add(new Static3D(finaSize,finaSize,1));
350

    
351
        mLogo.move( new Static3D(screenW/2,screenH/2,0) );
352
        mLogo.scale(di);
353
        mLogo.move( new Static3D(-logoW/2,-logoH/2,0) );
354
      
355
        mRoot.attach(mLogo);
356
        mLogo.addEventListener(this);
357
        }
358
      else if( bitmapID==logoID )
359
        {
360
        mRoot.detach(mLogo);   
361
        mLogo.removeEventListener(this);
362
        
363
        int crawlW = mCrawl.getWidth();
364
        int crawlH = mCrawl.getHeight();
365
        int screenW= mScreen.getWidth();
366
        int screenH= mScreen.getHeight();
367
        int backH  = mCrawlBackground.getHeight();
368
        float scale= (float)screenW/crawlW;
369
      
370
        Dynamic3D di = new Dynamic3D(60000,0.5f);
371
        di.add(new Static3D(screenW/2,+backH       , 0));
372
        di.add(new Static3D(screenW/2,-scale*crawlH, 0));
373
        
374
        mCrawlBackground.move( new Static3D(0,screenH-backH,0) );
375
        mCrawlBackground.rotate( new Static1D(CRAWL_ANGLE), new Static3D(1,0,0), new Static3D(screenW/2,backH,0) );
376
        
377
        final int transpDist = 5;
378
        Static4D region = new Static4D(screenW/2,(1-transpDist)*backH,transpDist*backH,transpDist*backH);
379
        mCrawlBackground.alpha(new Static1D(1-transpDist/2), region, true);
380
        
381
        mCrawl.move(di);
382
        mCrawl.scale( new Static3D(scale,scale,scale) );
383
        mCrawl.move( new Static3D(-crawlW/2,0,0) );
384
        
385
        mBackground = mRoot.attach(mCrawlBackground);
386
        mBackground.attach(mCrawl);
387
        mCrawl.addEventListener(this);
388
        }
389
      else if( bitmapID==crawlID )
390
        {
391
        mRoot.detach(mBackground);
392
        mBackground.detach(mCrawl);
393
        mCrawl.removeEventListener(this);
394
        }
395
      }
396
    }
397
  }
(2-2/3)