PageRenderTime 83ms CodeModel.GetById 40ms app.highlight 19ms RepoModel.GetById 21ms app.codeStats 0ms

/FallingSandpaper/src/sand/wallpaper/opengl/DemoActivity.java

http://thelements.googlecode.com/
Java | 266 lines | 191 code | 34 blank | 41 comment | 28 complexity | 3ef37783108a7ff58528db8a3626ed3c MD5 | raw file
  1/*
  2 * Element Works: Copyright (C) 2010 IDKJava
  3 * ----------------------------------------------------------
  4 * A sandbox type game in which you can play with different elements
  5 * which interact with each other in unique ways.
  6 */
  7
  8package sand.wallpaper.opengl;
  9
 10import java.util.Random;
 11
 12import javax.microedition.khronos.egl.EGLConfig;
 13import javax.microedition.khronos.opengles.GL10;
 14
 15import android.content.SharedPreferences;
 16import android.hardware.Sensor;
 17import android.hardware.SensorEvent;
 18import android.hardware.SensorEventListener;
 19import android.hardware.SensorListener;
 20import android.hardware.SensorManager;
 21import android.opengl.GLWallpaperService;
 22import android.os.Handler;
 23import android.util.Log;
 24import android.view.MotionEvent;
 25
 26public class DemoActivity extends GLWallpaperService implements SensorListener
 27{
 28	class SandView extends GLEngine
 29	{
 30		private int fd; // Set the "finger down" variable
 31
 32		DemoRenderer mRenderer;
 33		
 34		SharedPreferences myPrefs = getSharedPreferences("TheElementsPrefs", 0);
 35
 36		SandView()
 37		{
 38			super();
 39			//Handle prefs, other initialization
 40			mRenderer = new DemoRenderer();
 41			setRenderer(mRenderer);
 42			setTouchEventsEnabled(true);
 43		}
 44
 45		// When finger is held down, flood of events killing framerate, need to
 46		// put in it's own thread at some point and then use the sleep tactic
 47		public void onTouchEvent(final MotionEvent event)
 48		{
 49			// Gets the touch position
 50			//Touch down
 51			if (event.getAction() == MotionEvent.ACTION_DOWN && fd != 1)
 52			{
 53				//If the finger state has changed, set the state in jni
 54				DemoActivity.fd(1);
 55			}
 56			//Touch released
 57			else if (event.getAction() == MotionEvent.ACTION_UP)
 58			{
 59				//Tell jni
 60				DemoActivity.fd(2);
 61				if(myPrefs.getBoolean("random_pref", true))
 62				{
 63					//Randomly set the next element
 64					do
 65					{
 66						random_num = generator.nextInt(25);
 67					}
 68					while(random_num == 3 || random_num == 8);
 69					
 70					setelement(random_num);
 71				}
 72				else
 73				{
 74					//Sequentially set the next element
 75					if(getelement() == 22)
 76					{
 77						//Reset to 0 if last element
 78						setelement(0);
 79					}
 80					else if(getelement() == 2)
 81					{
 82						//Set element to 4, cuz 3 is gone
 83						setelement(4);
 84					}
 85					else if(getelement() == 7)
 86					{
 87						//Set element to 9, cuz 8 is gone
 88						setelement(9);
 89					}
 90					else
 91					{
 92						//Otherwise increment by one
 93						setelement(getelement()+1);
 94					}
 95				}
 96			}
 97
 98			//If we're on the small size
 99			if (DemoActivity.size == 0)
100			{
101				// Both x and y are halved because it needs to be zoomed in
102				DemoActivity.mp((int) event.getX() / 2, (int) event.getY() / 2);
103			}
104			else
105			{
106				DemoActivity.mp((int) event.getX(), (int) event.getY());
107			}
108		}
109	}
110
111	public static boolean loaddemov = false;
112
113	/*
114	 * Not needed any more
115	 * static final int maxx = 319; // 319 for g1, 479 for droid
116	 * static final int maxy = 414; // 454 for g1, 815 for droid
117	 */
118	static public boolean play = true;
119	private static final int SHAKE_THRESHOLD = 800;
120	static public int size = 0;
121	/*
122	 * Not needed any more
123	 * static public int skip = 1;
124	 * static public int speed = 1;
125	 */
126
127	static
128	{
129		System.loadLibrary("thelements"); // Load the JNI library
130	}
131
132	// JNI functions
133	public native static void clearquicksave();
134	public native static void fd(int fstate);
135	public native static int getelement();
136	public native static void jPause();
137	public native static int load();
138	public native static void loadcustom();
139	public native static int loaddemo();
140	public native static void mp(int jxm, int jym);
141	public native static void nativePause();
142	public native static void Play();
143	public native static void quickload();
144	public native static void quicksave();
145	public native static int save();
146	public native static void savecustom();
147	public native static void sendxg(float xgrav);
148	public native static void sendyg(float ygrav);
149	public native static void setAccelOnOff(int state);
150	public native static void setBackgroundColor(int colorcode);
151	public native static void setblue(int blueness);
152	public native static void setBrushSize(int jsize);
153	public native static void setcollision(int custnumber, int elementnumb, int collisionspot, int collisionnumber);
154	public native static void setelement(int element);
155	public native static void setexplosiveness(int explosiveness);
156	public native static void setFlip(int flipped);
157	public native static void setgreen(int greenness);
158	public native static void setred(int redness);
159	public native static void setup();
160	public native static void tester();
161	public native static void togglesize();
162
163	Random generator = new Random();
164
165	private float last_x, last_y, last_z;
166	private long lastUpdate = -1;
167	private int random_num;
168
169	private SensorManager sensorMgr;
170
171	private float x, y, z;
172
173	public DemoActivity()
174	{
175		super();
176	}
177
178	//Required to have this
179	public void onAccuracyChanged(int arg0, int arg1) {}
180
181	@Override
182	public void onCreate()
183	{
184		super.onCreate(); // Uses onCreate from the general
185
186		//Load custom elements
187		loadcustom();
188
189		//Create the sensor manager
190		sensorMgr = (SensorManager) getSystemService(SENSOR_SERVICE);
191		//Find out if the accelerometer is supported
192		boolean accelSupported = sensorMgr.registerListener(this, SensorManager.SENSOR_ACCELEROMETER, SensorManager.SENSOR_DELAY_GAME);
193
194		//If accel is not supported
195		if (!accelSupported)
196		{
197			//Remove the listener
198			sensorMgr.unregisterListener(this, SensorManager.SENSOR_ACCELEROMETER);
199		}
200		
201		Preferences.setPreferences(this);
202	}
203
204	@Override
205	public Engine onCreateEngine()
206	{
207		return new SandView();
208	}
209
210	public void onSensorChanged(int sensor, float[] values)
211	{
212		if (sensor == SensorManager.SENSOR_ACCELEROMETER)
213		{
214			long curTime = System.currentTimeMillis();
215			// only allow one update every 100ms.
216			if ((curTime - lastUpdate) > 100)
217			{
218				long diffTime = (curTime - lastUpdate);
219				lastUpdate = curTime;
220
221				x = values[SensorManager.DATA_X];
222				y = values[SensorManager.DATA_Y];
223				z = values[SensorManager.DATA_Z];
224
225				float speed = Math.abs(x + y + z - last_x - last_y - last_z)
226						/ diffTime * 10000;
227				if (speed > SHAKE_THRESHOLD)
228				{
229					clearquicksave();
230					setup();
231				}
232				last_x = x;
233				last_y = y;
234				last_z = z;
235			}
236		}
237	}
238}
239
240class DemoRenderer implements GLWallpaperService.Renderer
241{
242	public void onDrawFrame(GL10 gl)
243	{
244		nativeRender(); // Actual rendering - everything happens here
245	}
246
247	public void onSurfaceChanged(GL10 gl, int w, int h)
248	{
249		nativeResize(w, h);
250		//Load the demo from sdcard on first run
251		if (DemoActivity.loaddemov == true)
252		{
253			DemoActivity.loaddemo();
254			DemoActivity.loaddemov = false;
255		}
256	}
257
258	public void onSurfaceCreated(GL10 gl, EGLConfig config)
259	{
260		nativeInit();
261	}
262
263    private static native void nativeInit(); //Jni init
264    private static native void nativeResize(int w, int h); //Jni resize
265    private static native void nativeRender(); //Jni rendering function - everything happens here
266}