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