Project

General

Profile

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

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

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 7589635e Leszek Koltunski
import org.distorted.library.type.Dynamic1D;
32
import org.distorted.library.type.Dynamic3D;
33 5068fa06 Leszek Koltunski
import org.distorted.library.DistortedNode;
34 7589635e Leszek Koltunski
import org.distorted.library.type.Static1D;
35
import org.distorted.library.type.Static3D;
36
import org.distorted.library.type.Static4D;
37 13efa930 Leszek Koltunski
import org.distorted.library.message.EffectListener;
38
import org.distorted.library.message.EffectMessage;
39 5068fa06 Leszek Koltunski
import org.distorted.library.Distorted;
40
import org.distorted.library.DistortedBitmap;
41 427ab7bf Leszek Koltunski
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 bc0a685b Leszek Koltunski
  {
54
  private final String[] mGFFAString = 
55 427ab7bf Leszek Koltunski
         
56
            { "A long time ago, in a galaxy far,",
57
              "far away...." };                    // yes, there really was a four-dot ellipsis
58
59 bc0a685b Leszek Koltunski
  private final String[] mCRAWLString = 
60 427ab7bf Leszek Koltunski
      
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 bc0a685b Leszek Koltunski
  private final int NUM_STARS = 40;
86 427ab7bf Leszek Koltunski
   
87 bc0a685b Leszek Koltunski
  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 427ab7bf Leszek Koltunski
   
93 bc0a685b Leszek Koltunski
  private final int CRAWL_WIDTH = 500;
94
  private final int CRAWL_HEIGHT= (FONT_HEIGHT+VERTICAL_SPACING)*(mCRAWLString.length+1);
95 427ab7bf Leszek Koltunski
   
96 bc0a685b Leszek Koltunski
  private final int GFFA_WIDTH = 600;
97
  private final int GFFA_HEIGHT= (FONT_HEIGHT+VERTICAL_SPACING)*(mGFFAString.length+1);
98 427ab7bf Leszek Koltunski
   
99 bc0a685b Leszek Koltunski
  private final float CRAWL_ANGLE = -30.0f;
100 427ab7bf Leszek Koltunski
   
101 bc0a685b Leszek Koltunski
  private GLSurfaceView mView;
102
  private DistortedBitmap mScreen, mGFFA, mLogo, mCrawl, mCrawlBackground;
103
  private DistortedBitmap[] mStars;
104
  private long gffaID, logoID, crawlID;
105 427ab7bf Leszek Koltunski
    
106 bc0a685b Leszek Koltunski
  private Random mRnd = new Random(0);
107
  private DistortedNode mRoot, mBackground;
108 427ab7bf Leszek Koltunski
    
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110
111 bc0a685b Leszek Koltunski
  public StarWarsRenderer(GLSurfaceView v) 
112
    {
113
    mView = v;
114 427ab7bf Leszek Koltunski
     
115 bc0a685b Leszek Koltunski
    Distorted.setFov(60.0f);
116
    }
117 427ab7bf Leszek Koltunski
118
///////////////////////////////////////////////////////////////////////////////////////////////////
119
   
120 bc0a685b Leszek Koltunski
  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 427ab7bf Leszek Koltunski
     
125 bc0a685b Leszek Koltunski
    mRoot.draw(System.currentTimeMillis());
126
    }
127 427ab7bf Leszek Koltunski
128
///////////////////////////////////////////////////////////////////////////////////////////////////
129
    
130 bc0a685b Leszek Koltunski
  public void onSurfaceChanged(GL10 glUnused, int width, int height) 
131
    { 
132
    Distorted.onSurfaceChanged(width, height); 
133
    setupScreen(width,height);
134
    }
135 427ab7bf Leszek Koltunski
136
///////////////////////////////////////////////////////////////////////////////////////////////////
137
    
138 bc0a685b Leszek Koltunski
  public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
139
    {
140
    setupBitmaps();
141 427ab7bf Leszek Koltunski
         
142 bc0a685b Leszek Koltunski
    try
143
      {
144
      Distorted.onSurfaceCreated(mView.getContext());
145
      }
146
    catch(Exception ex)
147
      {
148
      android.util.Log.e("Star Wars", ex.getMessage() );
149 427ab7bf Leszek Koltunski
      }
150 bc0a685b Leszek Koltunski
    }
151 427ab7bf Leszek Koltunski
    
152
///////////////////////////////////////////////////////////////////////////////////////////////////
153
154 bc0a685b Leszek Koltunski
  private void setupScreen(int w, int h)
155
    {
156
    mScreen = new DistortedBitmap(w,h,1);
157
    mRoot = new DistortedNode(mScreen);
158 427ab7bf Leszek Koltunski
      
159 bc0a685b Leszek Koltunski
    mCrawlBackground = new DistortedBitmap(w,(int)(Math.sqrt(3.0)*h),1);
160 427ab7bf Leszek Koltunski
       
161 bc0a685b Leszek Koltunski
    int randomA, randomX, randomY, randomTime;
162
    float randomS, randomAlpha1, randomAlpha2;
163 427ab7bf Leszek Koltunski
       
164 7589635e Leszek Koltunski
    Static3D center = new Static3D(0,0,0);
165
    Static3D axis   = new Static3D(0,0,1);
166
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 7589635e Leszek Koltunski
      mStars[i].move( new Static3D(randomX,randomY,0) );
178
      mStars[i].scale( new Static3D(randomS,randomS,randomS) );
179 59bbb86a Leszek Koltunski
      mStars[i].rotate( new Static1D(randomA), axis, center );
180 427ab7bf Leszek Koltunski
      
181 f988589e Leszek Koltunski
      Dynamic1D di = new Dynamic1D(randomTime,0.0f);
182 59759251 Leszek Koltunski
      di.setNoise(0.5f);
183 7589635e Leszek Koltunski
      di.add(new Static1D(randomAlpha1));
184
      di.add(new Static1D(randomAlpha2));
185 427ab7bf Leszek Koltunski
      
186 59759251 Leszek Koltunski
      mStars[i].alpha(di);
187 427ab7bf Leszek Koltunski
      
188 bc0a685b Leszek Koltunski
      mRoot.attach(mStars[i]);
189 427ab7bf Leszek Koltunski
      }
190 bc0a685b Leszek Koltunski
      
191
    float scale = (0.5f*w/mGFFA.getWidth());
192
    
193 f988589e Leszek Koltunski
    Dynamic1D di = new Dynamic1D(6000,0.5f);
194 7589635e Leszek Koltunski
    di.add(new Static1D(1.0f));
195
    di.add(new Static1D(1.0f));
196
    di.add(new Static1D(0.0f));
197 bc0a685b Leszek Koltunski
    
198 7589635e Leszek Koltunski
    mGFFA.move( new Static3D(w/5,h/3,0) );
199
    mGFFA.scale( new Static3D(scale,scale,scale) );
200 59759251 Leszek Koltunski
    mGFFA.alpha(di);
201 bc0a685b Leszek Koltunski
      
202
    mRoot.attach(mGFFA);
203
    mGFFA.addEventListener(this); 
204
    }
205 427ab7bf Leszek Koltunski
    
206
///////////////////////////////////////////////////////////////////////////////////////////////////
207
208 bc0a685b Leszek Koltunski
  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 427ab7bf Leszek Koltunski
    
213 bc0a685b Leszek Koltunski
    Bitmap bitmapStar, bitmapGFFA, bitmapLogo, bitmapText;
214
      
215
    try 
216
      {
217
      bitmapLogo = BitmapFactory.decodeStream(is1);
218
      bitmapStar = BitmapFactory.decodeStream(is2);
219
      } 
220
    finally 
221
      {
222 427ab7bf Leszek Koltunski
      try 
223
        {
224 bc0a685b Leszek Koltunski
        is1.close();
225
        is2.close();
226 427ab7bf Leszek Koltunski
        } 
227 bc0a685b Leszek Koltunski
      catch(IOException e) { }
228
      } 
229 427ab7bf Leszek Koltunski
      
230 bc0a685b Leszek Koltunski
    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 427ab7bf Leszek Koltunski
      
236 bc0a685b Leszek Koltunski
    Typeface tf = Typeface.create(CRAWL_TYPEFACE, Typeface.BOLD);
237
    paint.setTypeface(tf);     
238 427ab7bf Leszek Koltunski
 
239 bc0a685b Leszek Koltunski
    ///// 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 427ab7bf Leszek Koltunski
      
245 bc0a685b Leszek Koltunski
    for(int i=0; i<mGFFAString.length; i++)
246
      {
247
      gffaCanvas.drawText(mGFFAString[i], 0, (i+1)*(FONT_HEIGHT+VERTICAL_SPACING), paint);  
248
      }
249 427ab7bf Leszek Koltunski
  
250 bc0a685b Leszek Koltunski
    mGFFA.setBitmap(bitmapGFFA);
251 427ab7bf Leszek Koltunski
      
252 bc0a685b Leszek Koltunski
    ///// create Logo ///////////////////
253
    mLogo  = new DistortedBitmap(bitmapLogo, 1);
254 427ab7bf Leszek Koltunski
      
255 bc0a685b Leszek Koltunski
    ///// 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 427ab7bf Leszek Koltunski
  
262 bc0a685b Leszek Koltunski
    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 427ab7bf Leszek Koltunski
      
267 bc0a685b Leszek Koltunski
    mCrawl.setBitmap(bitmapText);
268 427ab7bf Leszek Koltunski
      
269 bc0a685b Leszek Koltunski
    ///// create Stars ///////////////////
270 427ab7bf Leszek Koltunski
      
271 bc0a685b Leszek Koltunski
    mStars = new DistortedBitmap[NUM_STARS];
272 427ab7bf Leszek Koltunski
      
273 bc0a685b Leszek Koltunski
    mStars[0] = new DistortedBitmap(bitmapStar,1);
274 427ab7bf Leszek Koltunski
      
275 bc0a685b Leszek Koltunski
    for(int i=1; i<NUM_STARS; i++)
276
      {
277
      mStars[i] = new DistortedBitmap(mStars[0], Distorted.CLONE_BITMAP|Distorted.CLONE_VERTEX);  
278 427ab7bf Leszek Koltunski
      }
279 bc0a685b Leszek Koltunski
      
280
    gffaID = mGFFA.getID();
281
    logoID = mLogo.getID();
282
    crawlID= mCrawl.getID();
283
    }
284 427ab7bf Leszek Koltunski
 
285
///////////////////////////////////////////////////////////////////////////////////////////////////
286
287 bc0a685b Leszek Koltunski
  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 427ab7bf Leszek Koltunski
    
292 bc0a685b Leszek Koltunski
    if( len>0 && str.charAt(len-1) == ' ' ) numspaces--;
293 427ab7bf Leszek Koltunski
294 bc0a685b Leszek Koltunski
    float left=x,w = (numspaces>0 ? (length-paint.measureText(str))/numspaces : 0);
295
    String tmp;
296
    int begin,end=0;
297 427ab7bf Leszek Koltunski
298 bc0a685b Leszek Koltunski
    while( end<len )
299
      {
300
      begin=end;
301
      end = str.indexOf(' ', begin)+1;
302
      if( end<=0 ) end = len;
303 427ab7bf Leszek Koltunski
304 bc0a685b Leszek Koltunski
      tmp = str.substring(begin,end);
305
      c.drawText( tmp, left, y, paint);
306
      left+= (paint.measureText(tmp)+w);
307
      }  
308
    }
309 427ab7bf Leszek Koltunski
310
///////////////////////////////////////////////////////////////////////////////////////////////////
311
   
312 bc0a685b Leszek Koltunski
  private int retNumSpaces(String str)
313
    {
314
    int begin=0, ret=0;
315 427ab7bf Leszek Koltunski
316 bc0a685b Leszek Koltunski
    while( true )
317
      {
318
      begin = str.indexOf(' ', begin) +1;
319 427ab7bf Leszek Koltunski
320 bc0a685b Leszek Koltunski
      if( begin>0 ) ret++;
321
      else break;
322 427ab7bf Leszek Koltunski
      }
323 bc0a685b Leszek Koltunski
324
    return ret;
325
    }
326 427ab7bf Leszek Koltunski
    
327
///////////////////////////////////////////////////////////////////////////////////////////////////
328
329 bc0a685b Leszek Koltunski
  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 427ab7bf Leszek Koltunski
      {
333 bc0a685b Leszek Koltunski
      if( bitmapID == gffaID )
334 427ab7bf Leszek Koltunski
        {
335 bc0a685b Leszek Koltunski
        mRoot.detach(mGFFA);   
336
        mGFFA.removeEventListener(this);
337 427ab7bf Leszek Koltunski
       
338 bc0a685b Leszek Koltunski
        int screenW=mScreen.getWidth();
339
        int screenH=mScreen.getHeight();
340 427ab7bf Leszek Koltunski
        
341 bc0a685b Leszek Koltunski
        int logoW = mLogo.getWidth();
342
        int logoH = mLogo.getHeight();
343 427ab7bf Leszek Koltunski
      
344 bc0a685b Leszek Koltunski
        int initSize= (int)(3.0f*screenW/logoW);
345
        int finaSize= (int)(0.1f*screenW/logoW);
346 427ab7bf Leszek Koltunski
      
347 f988589e Leszek Koltunski
        Dynamic3D di = new Dynamic3D(10000,0.5f);
348 7589635e Leszek Koltunski
        di.add(new Static3D(initSize,initSize,1));
349
        di.add(new Static3D(finaSize,finaSize,1));
350 f988589e Leszek Koltunski
351 7589635e Leszek Koltunski
        mLogo.move( new Static3D(screenW/2,screenH/2,0) );
352 bc0a685b Leszek Koltunski
        mLogo.scale(di);
353 7589635e Leszek Koltunski
        mLogo.move( new Static3D(-logoW/2,-logoH/2,0) );
354 427ab7bf Leszek Koltunski
      
355 bc0a685b Leszek Koltunski
        mRoot.attach(mLogo);
356
        mLogo.addEventListener(this);
357
        }
358
      else if( bitmapID==logoID )
359
        {
360
        mRoot.detach(mLogo);   
361
        mLogo.removeEventListener(this);
362 427ab7bf Leszek Koltunski
        
363 bc0a685b Leszek Koltunski
        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 f988589e Leszek Koltunski
        Dynamic3D di = new Dynamic3D(60000,0.5f);
371 7589635e Leszek Koltunski
        di.add(new Static3D(screenW/2,+backH       , 0));
372
        di.add(new Static3D(screenW/2,-scale*crawlH, 0));
373 427ab7bf Leszek Koltunski
        
374 7589635e Leszek Koltunski
        mCrawlBackground.move( new Static3D(0,screenH-backH,0) );
375 59bbb86a Leszek Koltunski
        mCrawlBackground.rotate( new Static1D(CRAWL_ANGLE), new Static3D(1,0,0), new Static3D(screenW/2,backH,0) );
376 427ab7bf Leszek Koltunski
        
377 59759251 Leszek Koltunski
        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 427ab7bf Leszek Koltunski
        
381 bc0a685b Leszek Koltunski
        mCrawl.move(di);
382 7589635e Leszek Koltunski
        mCrawl.scale( new Static3D(scale,scale,scale) );
383
        mCrawl.move( new Static3D(-crawlW/2,0,0) );
384 427ab7bf Leszek Koltunski
        
385 bc0a685b Leszek Koltunski
        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 427ab7bf Leszek Koltunski
        }
395
      }
396 bc0a685b Leszek Koltunski
    }
397
  }