Project

General

Profile

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

examples / src / main / java / org / distorted / examples / starwars / StarWarsRenderer.java @ bc0a685b

1 bc0a685b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 427ab7bf Leszek Koltunski
20 5068fa06 Leszek Koltunski
package org.distorted.examples.starwars;
21 427ab7bf Leszek Koltunski
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 5068fa06 Leszek Koltunski
import org.distorted.examples.R;
30
31
import org.distorted.library.Interpolator1D;
32
import org.distorted.library.Interpolator3D;
33
import org.distorted.library.DistortedNode;
34
import org.distorted.library.Float1D;
35
import org.distorted.library.Float2D;
36
import org.distorted.library.Float3D;
37
import org.distorted.library.Float4D;
38
import org.distorted.library.EffectListener;
39
import org.distorted.library.EffectMessage;
40
import org.distorted.library.Distorted;
41
import org.distorted.library.DistortedBitmap;
42 427ab7bf Leszek Koltunski
43
import android.graphics.Bitmap;
44
import android.graphics.BitmapFactory;
45
import android.graphics.Canvas;
46
import android.graphics.Paint;
47
import android.graphics.Typeface;
48
import android.opengl.GLES20;
49
import android.opengl.GLSurfaceView;
50
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52
53
class StarWarsRenderer implements GLSurfaceView.Renderer, EffectListener
54 bc0a685b Leszek Koltunski
  {
55
  private final String[] mGFFAString = 
56 427ab7bf Leszek Koltunski
         
57
            { "A long time ago, in a galaxy far,",
58
              "far away...." };                    // yes, there really was a four-dot ellipsis
59
60 bc0a685b Leszek Koltunski
  private final String[] mCRAWLString = 
61 427ab7bf Leszek Koltunski
      
62
            { "It is a period of civil war.",      // in the original 1977 version, there was no 
63
              "Rebel spaceships, striking",        // 'Episode IV New Hope' subtitle line before
64
              "from a hidden base, have",          // the crawl. Lets keep to the classic.
65
              "won their first victory",
66
              "against the evil Galactic",
67
              "Empire.",
68
              "",
69
              "During the battle, rebel",
70
              "spies managed to steal",
71
              "secret plans to the Empire's",
72
              "ultimate weapon, the",
73
              "DEATH STAR, an armored",
74
              "space station with enough",
75
              "power to destroy an entire",
76
              "planet.",
77
              "",
78
              "Pursued by the Empire's",
79
              "sinister agents, Princess",
80
              "Leia races home aboard her",
81
              "starship, custodian of the",
82
              "stolen plans that can save",
83
              "her people and restore",
84
              "freedom to the galaxy...." };      // four-dot.
85
   
86 bc0a685b Leszek Koltunski
  private final int NUM_STARS = 40;
87 427ab7bf Leszek Koltunski
   
88 bc0a685b Leszek Koltunski
  private final int CRAWL_COLOR = 0xffffe81f;
89
  private final int GFFA_COLOR  = 0xff0000ff;
90
  private final int FONT_HEIGHT      = 40;
91
  private final int VERTICAL_SPACING = 10;
92
  private final String CRAWL_TYPEFACE = "News Gothic";
93 427ab7bf Leszek Koltunski
   
94 bc0a685b Leszek Koltunski
  private final int CRAWL_WIDTH = 500;
95
  private final int CRAWL_HEIGHT= (FONT_HEIGHT+VERTICAL_SPACING)*(mCRAWLString.length+1);
96 427ab7bf Leszek Koltunski
   
97 bc0a685b Leszek Koltunski
  private final int GFFA_WIDTH = 600;
98
  private final int GFFA_HEIGHT= (FONT_HEIGHT+VERTICAL_SPACING)*(mGFFAString.length+1);
99 427ab7bf Leszek Koltunski
   
100 bc0a685b Leszek Koltunski
  private final float CRAWL_ANGLE = -30.0f;
101 427ab7bf Leszek Koltunski
   
102 bc0a685b Leszek Koltunski
  private GLSurfaceView mView;
103
  private DistortedBitmap mScreen, mGFFA, mLogo, mCrawl, mCrawlBackground;
104
  private DistortedBitmap[] mStars;
105
  private long gffaID, logoID, crawlID;
106 427ab7bf Leszek Koltunski
    
107 bc0a685b Leszek Koltunski
  private Random mRnd = new Random(0);
108
  private DistortedNode mRoot, mBackground;
109 427ab7bf Leszek Koltunski
    
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111
112 bc0a685b Leszek Koltunski
  public StarWarsRenderer(GLSurfaceView v) 
113
    {
114
    mView = v;
115 427ab7bf Leszek Koltunski
     
116 bc0a685b Leszek Koltunski
    Distorted.setFov(60.0f);
117
    }
118 427ab7bf Leszek Koltunski
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120
   
121 bc0a685b Leszek Koltunski
  public void onDrawFrame(GL10 glUnused) 
122
    {
123
    GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
124
    GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
125 427ab7bf Leszek Koltunski
     
126 bc0a685b Leszek Koltunski
    mRoot.draw(System.currentTimeMillis());
127
    }
128 427ab7bf Leszek Koltunski
129
///////////////////////////////////////////////////////////////////////////////////////////////////
130
    
131 bc0a685b Leszek Koltunski
  public void onSurfaceChanged(GL10 glUnused, int width, int height) 
132
    { 
133
    Distorted.onSurfaceChanged(width, height); 
134
    setupScreen(width,height);
135
    }
136 427ab7bf Leszek Koltunski
137
///////////////////////////////////////////////////////////////////////////////////////////////////
138
    
139 bc0a685b Leszek Koltunski
  public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
140
    {
141
    setupBitmaps();
142 427ab7bf Leszek Koltunski
         
143 bc0a685b Leszek Koltunski
    try
144
      {
145
      Distorted.onSurfaceCreated(mView.getContext());
146
      }
147
    catch(Exception ex)
148
      {
149
      android.util.Log.e("Star Wars", ex.getMessage() );
150 427ab7bf Leszek Koltunski
      }
151 bc0a685b Leszek Koltunski
    }
152 427ab7bf Leszek Koltunski
    
153
///////////////////////////////////////////////////////////////////////////////////////////////////
154
155 bc0a685b Leszek Koltunski
  private void setupScreen(int w, int h)
156
    {
157
    mScreen = new DistortedBitmap(w,h,1);
158
    mRoot = new DistortedNode(mScreen);
159 427ab7bf Leszek Koltunski
      
160 bc0a685b Leszek Koltunski
    mCrawlBackground = new DistortedBitmap(w,(int)(Math.sqrt(3.0)*h),1);
161 427ab7bf Leszek Koltunski
       
162 bc0a685b Leszek Koltunski
    int randomA, randomX, randomY, randomTime;
163
    float randomS, randomAlpha1, randomAlpha2;
164 427ab7bf Leszek Koltunski
       
165 bc0a685b Leszek Koltunski
    Float3D rot = new Float3D(0,0,0);
166 427ab7bf Leszek Koltunski
      
167 bc0a685b Leszek Koltunski
    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 427ab7bf Leszek Koltunski
      
177 bc0a685b Leszek Koltunski
      mStars[i].move( randomX,randomY,0);
178
      mStars[i].scale(randomS);
179
      mStars[i].rotate( rot, randomA);
180 427ab7bf Leszek Koltunski
      
181
      Interpolator1D di = new Interpolator1D();
182 bc0a685b Leszek Koltunski
      di.setDuration(randomTime);
183
      di.setCount(0.0f);
184
      di.setNoise(0.3f);
185
      di.add(new Float1D(randomAlpha1));
186
      di.add(new Float1D(randomAlpha2));
187 427ab7bf Leszek Koltunski
      
188 bc0a685b Leszek Koltunski
      mStars[i].alpha(di, null, new Float2D(0,0));
189 427ab7bf Leszek Koltunski
      
190 bc0a685b Leszek Koltunski
      mRoot.attach(mStars[i]);
191 427ab7bf Leszek Koltunski
      }
192 bc0a685b Leszek Koltunski
      
193
    float scale = (0.5f*w/mGFFA.getWidth());
194
    
195
    Interpolator1D di = new Interpolator1D();
196
    di.setDuration(6000);
197
    di.setCount(0.5f);
198
    di.add(new Float1D(1.0f));
199
    di.add(new Float1D(1.0f));
200
    di.add(new Float1D(0.0f));
201
    
202
    mGFFA.move( w/5, h/3, 0);
203
    mGFFA.scale(scale);
204
    mGFFA.alpha(di, null, new Float2D(0,0));
205
      
206
    mRoot.attach(mGFFA);
207
    mGFFA.addEventListener(this); 
208
    }
209 427ab7bf Leszek Koltunski
    
210
///////////////////////////////////////////////////////////////////////////////////////////////////
211
212 bc0a685b Leszek Koltunski
  private void setupBitmaps()
213
    {
214
    InputStream is1 = mView.getContext().getResources().openRawResource(R.raw.starwars);
215
    InputStream is2 = mView.getContext().getResources().openRawResource(R.raw.star);
216 427ab7bf Leszek Koltunski
    
217 bc0a685b Leszek Koltunski
    Bitmap bitmapStar, bitmapGFFA, bitmapLogo, bitmapText;
218
      
219
    try 
220
      {
221
      bitmapLogo = BitmapFactory.decodeStream(is1);
222
      bitmapStar = BitmapFactory.decodeStream(is2);
223
      } 
224
    finally 
225
      {
226 427ab7bf Leszek Koltunski
      try 
227
        {
228 bc0a685b Leszek Koltunski
        is1.close();
229
        is2.close();
230 427ab7bf Leszek Koltunski
        } 
231 bc0a685b Leszek Koltunski
      catch(IOException e) { }
232
      } 
233 427ab7bf Leszek Koltunski
      
234 bc0a685b Leszek Koltunski
    Paint paint = new Paint();
235
    paint.setAntiAlias(true);
236
    paint.setTextAlign(Paint.Align.LEFT);
237
    paint.setTextSize(FONT_HEIGHT);
238
    paint.setColor(GFFA_COLOR);
239 427ab7bf Leszek Koltunski
      
240 bc0a685b Leszek Koltunski
    Typeface tf = Typeface.create(CRAWL_TYPEFACE, Typeface.BOLD);
241
    paint.setTypeface(tf);     
242 427ab7bf Leszek Koltunski
 
243 bc0a685b Leszek Koltunski
    ///// create GFFA ///////////////////
244
    mGFFA  = new DistortedBitmap(GFFA_WIDTH, GFFA_HEIGHT, 1);
245
    bitmapGFFA = Bitmap.createBitmap(GFFA_WIDTH,GFFA_HEIGHT,Bitmap.Config.ARGB_8888);
246
    bitmapGFFA.eraseColor(0x00000000);
247
    Canvas gffaCanvas = new Canvas(bitmapGFFA);
248 427ab7bf Leszek Koltunski
      
249 bc0a685b Leszek Koltunski
    for(int i=0; i<mGFFAString.length; i++)
250
      {
251
      gffaCanvas.drawText(mGFFAString[i], 0, (i+1)*(FONT_HEIGHT+VERTICAL_SPACING), paint);  
252
      }
253 427ab7bf Leszek Koltunski
  
254 bc0a685b Leszek Koltunski
    mGFFA.setBitmap(bitmapGFFA);
255 427ab7bf Leszek Koltunski
      
256 bc0a685b Leszek Koltunski
    ///// create Logo ///////////////////
257
    mLogo  = new DistortedBitmap(bitmapLogo, 1);
258 427ab7bf Leszek Koltunski
      
259 bc0a685b Leszek Koltunski
    ///// create CRAWL //////////////////
260
    mCrawl = new DistortedBitmap(CRAWL_WIDTH, CRAWL_HEIGHT, 1);
261
    bitmapText = Bitmap.createBitmap(CRAWL_WIDTH,CRAWL_HEIGHT,Bitmap.Config.ARGB_8888);
262
    bitmapText.eraseColor(0x00000000);
263
    Canvas textCanvas = new Canvas(bitmapText);
264
    paint.setColor(CRAWL_COLOR);
265 427ab7bf Leszek Koltunski
  
266 bc0a685b Leszek Koltunski
    for(int i=0; i<mCRAWLString.length; i++)
267
      {
268
      displayJustified(mCRAWLString[i], 0, (i+1)*(FONT_HEIGHT+VERTICAL_SPACING), CRAWL_WIDTH, textCanvas, paint);  
269
      }
270 427ab7bf Leszek Koltunski
      
271 bc0a685b Leszek Koltunski
    mCrawl.setBitmap(bitmapText);
272 427ab7bf Leszek Koltunski
      
273 bc0a685b Leszek Koltunski
    ///// create Stars ///////////////////
274 427ab7bf Leszek Koltunski
      
275 bc0a685b Leszek Koltunski
    mStars = new DistortedBitmap[NUM_STARS];
276 427ab7bf Leszek Koltunski
      
277 bc0a685b Leszek Koltunski
    mStars[0] = new DistortedBitmap(bitmapStar,1);
278 427ab7bf Leszek Koltunski
      
279 bc0a685b Leszek Koltunski
    for(int i=1; i<NUM_STARS; i++)
280
      {
281
      mStars[i] = new DistortedBitmap(mStars[0], Distorted.CLONE_BITMAP|Distorted.CLONE_VERTEX);  
282 427ab7bf Leszek Koltunski
      }
283 bc0a685b Leszek Koltunski
      
284
    gffaID = mGFFA.getID();
285
    logoID = mLogo.getID();
286
    crawlID= mCrawl.getID();
287
    }
288 427ab7bf Leszek Koltunski
 
289
///////////////////////////////////////////////////////////////////////////////////////////////////
290
291 bc0a685b Leszek Koltunski
  private void displayJustified(String str, int x, int y, int length, Canvas c, Paint paint)
292
    { 
293
    int len       = str.length();
294
    int numspaces = retNumSpaces(str);
295 427ab7bf Leszek Koltunski
    
296 bc0a685b Leszek Koltunski
    if( len>0 && str.charAt(len-1) == ' ' ) numspaces--;
297 427ab7bf Leszek Koltunski
298 bc0a685b Leszek Koltunski
    float left=x,w = (numspaces>0 ? (length-paint.measureText(str))/numspaces : 0);
299
    String tmp;
300
    int begin,end=0;
301 427ab7bf Leszek Koltunski
302 bc0a685b Leszek Koltunski
    while( end<len )
303
      {
304
      begin=end;
305
      end = str.indexOf(' ', begin)+1;
306
      if( end<=0 ) end = len;
307 427ab7bf Leszek Koltunski
308 bc0a685b Leszek Koltunski
      tmp = str.substring(begin,end);
309
      c.drawText( tmp, left, y, paint);
310
      left+= (paint.measureText(tmp)+w);
311
      }  
312
    }
313 427ab7bf Leszek Koltunski
314
///////////////////////////////////////////////////////////////////////////////////////////////////
315
   
316 bc0a685b Leszek Koltunski
  private int retNumSpaces(String str)
317
    {
318
    int begin=0, ret=0;
319 427ab7bf Leszek Koltunski
320 bc0a685b Leszek Koltunski
    while( true )
321
      {
322
      begin = str.indexOf(' ', begin) +1;
323 427ab7bf Leszek Koltunski
324 bc0a685b Leszek Koltunski
      if( begin>0 ) ret++;
325
      else break;
326 427ab7bf Leszek Koltunski
      }
327 bc0a685b Leszek Koltunski
328
    return ret;
329
    }
330 427ab7bf Leszek Koltunski
    
331
///////////////////////////////////////////////////////////////////////////////////////////////////
332
333 bc0a685b Leszek Koltunski
  public void effectMessage(final EffectMessage em, final long effectID, final int effectName, final long bitmapID, final String message)
334
    {
335
    if( em==EffectMessage.EFFECT_FINISHED )
336 427ab7bf Leszek Koltunski
      {
337 bc0a685b Leszek Koltunski
      if( bitmapID == gffaID )
338 427ab7bf Leszek Koltunski
        {
339 bc0a685b Leszek Koltunski
        mRoot.detach(mGFFA);   
340
        mGFFA.removeEventListener(this);
341 427ab7bf Leszek Koltunski
       
342 bc0a685b Leszek Koltunski
        int screenW=mScreen.getWidth();
343
        int screenH=mScreen.getHeight();
344 427ab7bf Leszek Koltunski
        
345 bc0a685b Leszek Koltunski
        int logoW = mLogo.getWidth();
346
        int logoH = mLogo.getHeight();
347 427ab7bf Leszek Koltunski
      
348 bc0a685b Leszek Koltunski
        int initSize= (int)(3.0f*screenW/logoW);
349
        int finaSize= (int)(0.1f*screenW/logoW);
350 427ab7bf Leszek Koltunski
      
351 bc0a685b Leszek Koltunski
        Interpolator3D di = new Interpolator3D();
352 427ab7bf Leszek Koltunski
      
353 bc0a685b Leszek Koltunski
        di.add(new Float3D(initSize,initSize,1));
354
        di.add(new Float3D(finaSize,finaSize,1));
355
        di.setCount(0.5f);
356
        di.setDuration(10000);
357 427ab7bf Leszek Koltunski
      
358 bc0a685b Leszek Koltunski
        mLogo.move( screenW/2, screenH/2, 0);
359
        mLogo.scale(di);
360
        mLogo.move( -logoW/2, -logoH/2, 0);
361 427ab7bf Leszek Koltunski
      
362 bc0a685b Leszek Koltunski
        mRoot.attach(mLogo);
363
        mLogo.addEventListener(this);
364
        }
365
      else if( bitmapID==logoID )
366
        {
367
        mRoot.detach(mLogo);   
368
        mLogo.removeEventListener(this);
369 427ab7bf Leszek Koltunski
        
370 bc0a685b Leszek Koltunski
        int crawlW = mCrawl.getWidth();
371
        int crawlH = mCrawl.getHeight();
372
        int screenW= mScreen.getWidth();
373
        int screenH= mScreen.getHeight();
374
        int backH  = mCrawlBackground.getHeight();
375
        float scale= (float)screenW/crawlW;
376
      
377
        Interpolator3D di = new Interpolator3D();
378
        di.setCount(0.5f);
379
        di.setDuration(60000);
380
        di.add(new Float3D(screenW/2,+backH       , 0));
381
        di.add(new Float3D(screenW/2,-scale*crawlH, 0));
382 427ab7bf Leszek Koltunski
        
383 bc0a685b Leszek Koltunski
        mCrawlBackground.move(0,screenH-backH,0);
384
        mCrawlBackground.rotate(new Float3D(screenW/2,backH,0), CRAWL_ANGLE, 1.0f, 0.0f, 0.0f);
385 427ab7bf Leszek Koltunski
        
386 bc0a685b Leszek Koltunski
        final int transpDistance = 5;
387
        mCrawlBackground.smooth_alpha((1-transpDistance/2)*1.0f, new Float4D(0,0,transpDistance*backH,transpDistance*backH), new Float2D(screenW/2,(1-transpDistance)*backH));
388 427ab7bf Leszek Koltunski
        
389 bc0a685b Leszek Koltunski
        mCrawl.move(di);
390
        mCrawl.scale(scale);
391
        mCrawl.move(-crawlW/2,0,0);
392 427ab7bf Leszek Koltunski
        
393 bc0a685b Leszek Koltunski
        mBackground = mRoot.attach(mCrawlBackground);
394
        mBackground.attach(mCrawl);
395
        mCrawl.addEventListener(this);
396
        }
397
      else if( bitmapID==crawlID )
398
        {
399
        mRoot.detach(mBackground);
400
        mBackground.detach(mCrawl);
401
        mCrawl.removeEventListener(this);
402 427ab7bf Leszek Koltunski
        }
403
      }
404 bc0a685b Leszek Koltunski
    }
405
  }