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