/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.android.rs.fountain;
import android.renderscript.RSSurfaceView;
import android.renderscript.RenderScript;
import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.provider.Settings.System;
import android.util.Config;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ListView;
import java.lang.Runtime;
public class Fountain extends Activity {
//EventListener mListener = new EventListener();
private static final String LOG_TAG = "libRS_jni";
private static final boolean DEBUG = false;
private static final boolean LOG_ENABLED = DEBUG ? Config.LOGD : Config.LOGV;
private FountainView mView;
// get the current looper (from your Activity UI thread for instance
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
// Create our Preview view and set it as the content of our
// Activity
mView = new FountainView(this);
setContentView(mView);
}
@Override
protected void onResume() {
Log.e("rs", "onResume");
// Ideally a game should implement onResume() and onPause()
// to take appropriate action when the activity looses focus
super.onResume();
mView.resume();
}
@Override
protected void onPause() {
Log.e("rs", "onPause");
// Ideally a game should implement onResume() and onPause()
// to take appropriate action when the activity looses focus
super.onPause();
mView.pause();
//Runtime.getRuntime().exit(0);
}
static void log(String message) {
if (LOG_ENABLED) {
Log.v(LOG_TAG, message);
}
}
}
C:\Java_Dev\sdk\Android\android-sdk\samples\android-13\RenderScript\Fountain\src\com\example\android\rs\fountain\FountainRS.java
/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.android.rs.fountain;
import android.content.res.Resources;
import android.renderscript.*;
import android.util.Log;
public class FountainRS {
public static final int PART_COUNT = 50000;
public FountainRS() {
}
private Resources mRes;
private RenderScriptGL mRS;
private ScriptC_fountain mScript;
public void init(RenderScriptGL rs, Resources res, int width, int height) {
mRS = rs;
mRes = res;
ProgramFragmentFixedFunction.Builder pfb = new ProgramFragmentFixedFunction.Builder(rs);
pfb.setVaryingColor(true);
rs.bindProgramFragment(pfb.create());
ScriptField_Point points = new ScriptField_Point(mRS, PART_COUNT);//
// Allocation.USAGE_GRAPHICS_VERTEX);
Mesh.AllocationBuilder smb = new Mesh.AllocationBuilder(mRS);
smb.addVertexAllocation(points.getAllocation());
smb.addIndexSetType(Mesh.Primitive.POINT);
Mesh sm = smb.create();
mScript = new ScriptC_fountain(mRS, mRes, R.raw.fountain);
mScript.set_partMesh(sm);
mScript.bind_point(points);
mRS.bindRootScript(mScript);
}
boolean holdingColor[] = new boolean[10];
public void newTouchPosition(float x, float y, float pressure, int id) {
if (id >= holdingColor.length) {
return;
}
int rate = (int)(pressure * pressure * 500.f);
if (rate > 500) {
rate = 500;
}
if (rate > 0) {
mScript.invoke_addParticles(rate, x, y, id, !holdingColor[id]);
holdingColor[id] = true;
} else {
holdingColor[id] = false;
}
}
}
C:\Java_Dev\sdk\Android\android-sdk\samples\android-13\RenderScript\Fountain\src\com\example\android\rs\fountain\FountainView.java
/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.android.rs.fountain;
import java.io.Writer;
import java.util.ArrayList;
import java.util.concurrent.Semaphore;
import android.renderscript.RSSurfaceView;
import android.renderscript.RenderScript;
import android.renderscript.RenderScriptGL;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.KeyEvent;
import android.view.MotionEvent;
public class FountainView extends RSSurfaceView {
public FountainView(Context context) {
super(context);
//setFocusable(true);
}
private RenderScriptGL mRS;
private FountainRS mRender;
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
super.surfaceChanged(holder, format, w, h);
if (mRS == null) {
RenderScriptGL.SurfaceConfig sc = new RenderScriptGL.SurfaceConfig();
mRS = createRenderScriptGL(sc);
mRS.setSurface(holder, w, h);
mRender = new FountainRS();
mRender.init(mRS, getResources(), w, h);
}
}
@Override
protected void onDetachedFromWindow() {
if (mRS != null) {
mRS = null;
destroyRenderScriptGL();
}
}
@Override
public boolean onTouchEvent(MotionEvent ev)
{
int act = ev.getActionMasked();
if (act == ev.ACTION_UP) {
mRender.newTouchPosition(0, 0, 0, ev.getPointerId(0));
return false;
} else if (act == MotionEvent.ACTION_POINTER_UP) {
// only one pointer going up, we can get the index like this
int pointerIndex = ev.getActionIndex();
int pointerId = ev.getPointerId(pointerIndex);
mRender.newTouchPosition(0, 0, 0, pointerId);
}
int count = ev.getHistorySize();
int pcount = ev.getPointerCount();
for (int p=0; p < pcount; p++) {
int id = ev.getPointerId(p);
mRender.newTouchPosition(ev.getX(p),
ev.getY(p),
ev.getPressure(p),
id);
for (int i=0; i < count; i++) {
mRender.newTouchPosition(ev.getHistoricalX(p, i),
ev.getHistoricalY(p, i),
ev.getHistoricalPressure(p, i),
id);
}
}
return true;
}
}
C:\Java_Dev\sdk\Android\android-sdk\samples\android-13\RenderScript\Fountain\src\com\example\android\rs\fountain\fountain.rs
// Fountain test script
#pragma version(1)
#pragma rs java_package_name(com.example.android.rs.fountain)
#pragma stateFragment(parent)
#include "rs_graphics.rsh"
static int newPart = 0;
rs_mesh partMesh;
typedef struct __attribute__((packed, aligned(4))) Point {
float2 delta;
float2 position;
uchar4 color;
} Point_t;
Point_t *point;
int root() {
float dt = min(rsGetDt(), 0.1f);
rsgClearColor(0.f, 0.f, 0.f, 1.f);
const float height = rsgGetHeight();
const int size = rsAllocationGetDimX(rsGetAllocation(point));
float dy2 = dt * (10.f);
Point_t * p = point;
for (int ct=0; ct < size; ct++) {
p->delta.y += dy2;
p->position += p->delta;
if ((p->position.y > height) && (p->delta.y > 0)) {
p->delta.y *= -0.3f;
}
p++;
}
rsgDrawMesh(partMesh);
return 1;
}
static float4 partColor[10];
void addParticles(int rate, float x, float y, int index, bool newColor)
{
if (newColor) {
partColor[index].x = rsRand(0.5f, 1.0f);
partColor[index].y = rsRand(1.0f);
partColor[index].z = rsRand(1.0f);
}
float rMax = ((float)rate) * 0.02f;
int size = rsAllocationGetDimX(rsGetAllocation(point));
uchar4 c = rsPackColorTo8888(partColor[index]);
Point_t * np = &point[newPart];
float2 p = {x, y};
while (rate--) {
float angle = rsRand(3.14f * 2.f);
float len = rsRand(rMax);
np->delta.x = len * sin(angle);
np->delta.y = len * cos(angle);
np->position = p;
np->color = c;
newPart++;
np++;
if (newPart >= size) {
newPart = 0;
np = &point[newPart];
}
}
}