/FallingSandpaper/jni/app-android.c

http://thelements.googlecode.com/ · C · 363 lines · 290 code · 35 blank · 38 comment · 28 complexity · ad1ccf33e99cd70506eab83cc1bbc350 MD5 · raw file

  1. /*
  2. * app-android.c
  3. * -----------------------------
  4. * Contains the entire API for our JNI code. Any
  5. * access to the native code from Java will be made
  6. * using these accessible functions.
  7. */
  8. #include <jni.h>
  9. #include <math.h>
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12. //#include <android/log.h>
  13. //Include the global variables
  14. #include "app.h"
  15. //Include the global macros
  16. #include "macros.h"
  17. //Include the initializing function
  18. #include "setup.h"
  19. //Include the element characteristics file
  20. #include "elementproperties.h"
  21. //Include the collisions data file
  22. #include "collisions.h"
  23. //Include the saving and loading functions
  24. #include "saveload.h"
  25. //Include the server access functions
  26. #include "server.h"
  27. //Include the rendering functions
  28. #include "gl.h"
  29. void Java_sand_wallpaper_opengl_DemoRenderer_nativeInit(JNIEnv* env) //Initialize graphics
  30. {
  31. //__android_log_write(ANDROID_LOG_INFO, "DemoActivity", "8");
  32. importGLInit();
  33. appInit();
  34. gAppAlive = 1;
  35. //__android_log_write(ANDROID_LOG_INFO, "DemoActivity", "4");
  36. }
  37. void Java_sand_wallpaper_opengl_DemoRenderer_nativeResize(JNIEnv* env, jobject thiz, jint w, jint h)
  38. {
  39. if (screensize == 0) //this didn't work before becuase tej used = instead of == (green girdle)
  40. {
  41. maxx = w / 2;
  42. maxy = h / 2;
  43. }
  44. else
  45. {
  46. maxx = w;
  47. maxy = h;
  48. }
  49. appInit();
  50. }
  51. void Java_sand_wallpaper_opengl_DemoRenderer_nativeDone(JNIEnv* env)
  52. {
  53. appDeinit();
  54. importGLDeinit();
  55. }
  56. //these two get the gravity from the java code
  57. void Java_sand_wallpaper_opengl_DemoActivity_sendyg(JNIEnv* env, jobject thiz, jfloat ygrav)
  58. {
  59. gravy = ygrav;
  60. }
  61. void Java_sand_wallpaper_opengl_DemoActivity_sendxg(JNIEnv* env, jobject thiz, jfloat xgrav)
  62. {
  63. gravx = -xgrav;
  64. }
  65. void Java_sand_wallpaper_opengl_DemoActivity_quickload(JNIEnv* env)
  66. {
  67. loader(1);
  68. }
  69. void Java_sand_wallpaper_opengl_DemoActivity_quicksave(JNIEnv* env)
  70. {
  71. saver(1);
  72. }
  73. /* Call to render the next GL frame */
  74. void Java_sand_wallpaper_opengl_DemoRenderer_nativeRender(JNIEnv* env)
  75. {
  76. appRender();
  77. }
  78. void Java_sand_wallpaper_opengl_DemoActivity_setup(JNIEnv* env, jobject thiz)
  79. {
  80. rsetup();
  81. }
  82. void Java_sand_wallpaper_opengl_DemoActivity_jPause(JNIEnv* env, jobject thiz)
  83. {
  84. play = 0;
  85. }
  86. void Java_sand_wallpaper_opengl_DemoActivity_Play(JNIEnv* env, jobject thiz)
  87. {
  88. play = 1;
  89. }
  90. void Java_sand_wallpaper_opengl_DemoActivity_setBackgroundColor(JNIEnv* env, jobject thiz, jint colorcode)
  91. {
  92. if (colorcode == 0)
  93. {
  94. red[3] = 0; //3 is eraser
  95. blue[3] = 0;
  96. green[3] = 0;
  97. //__android_log_write(ANDROID_LOG_INFO, "DemoActivity", "color");
  98. rsetup();
  99. }
  100. else if (colorcode == 1)
  101. {
  102. red[3] = 255;
  103. blue[3] = 255;
  104. green[3] = 255;
  105. //__android_log_write(ANDROID_LOG_INFO, "DemoActivity", "color");
  106. rsetup();
  107. }
  108. loader(1);
  109. }
  110. void Java_sand_wallpaper_opengl_DemoActivity_setexplosiveness(JNIEnv* env, jobject thiz, jint explosiveness)
  111. {
  112. exploness[22] = explosiveness;
  113. }
  114. void Java_sand_wallpaper_opengl_DemoActivity_setred(JNIEnv* env, jobject thiz, jint redness)
  115. {
  116. red[22] = redness;
  117. }
  118. void Java_sand_wallpaper_opengl_DemoActivity_setgreen(JNIEnv* env, jobject thiz, jint greenness)
  119. {
  120. green[22] = greenness;
  121. }
  122. void Java_sand_wallpaper_opengl_DemoActivity_setblue(JNIEnv* env, jobject thiz, jint blueness)
  123. {
  124. blue[22] = blueness;
  125. }
  126. void Java_sand_wallpaper_opengl_DemoActivity_setdensity(JNIEnv* env, jobject thiz, jint jdensity)
  127. {
  128. density[22] = jdensity;
  129. }
  130. void Java_sand_wallpaper_opengl_DemoActivity_setFlip(JNIEnv* env, jobject thiz, jint jflipped)
  131. {
  132. flipped = jflipped;
  133. }
  134. void Java_sand_wallpaper_opengl_DemoActivity_setcollision(JNIEnv* env, jobject thiz, jint custnum, jint elementnumb, jint colspot, jint colnum)
  135. {
  136. if (custnum == 1)
  137. {
  138. collision[22][colspot] = colnum;
  139. colliseelement1[colspot] = elementnumb;
  140. collision[colspot][22] = colnum;
  141. }
  142. else
  143. {
  144. collision[22][colspot] = colnum;
  145. colliseelement1[colspot] = elementnumb;
  146. collision[colspot][22] = colnum;
  147. }
  148. int counter124;
  149. char foundfire = 0;
  150. for (counter124 = 0; counter124 < TElements; counter124++)
  151. {
  152. if (collision[22][counter124] == 6)
  153. {
  154. foundfire = 1;
  155. }
  156. }
  157. if (foundfire == 0)
  158. {
  159. fireburn[22] = 0;
  160. }
  161. else
  162. {
  163. fireburn[22] = 1;
  164. }
  165. }
  166. void Java_sand_wallpaper_opengl_DemoActivity_fd(JNIEnv* env, jobject thiz, jint fstate)
  167. {
  168. //setting finger up or down from onTouch
  169. fd = fstate;
  170. if (fd == 1)
  171. {
  172. xm = -1;
  173. }
  174. return;
  175. }
  176. void Java_sand_wallpaper_opengl_DemoActivity_mp(JNIEnv* env, jobject thiz, jint jxm, jint jym)
  177. {
  178. //setting the mouse position when given stuff from jdk
  179. if (xm != -1)
  180. {
  181. lmx = xm;
  182. lmy = ym;
  183. int xc = jxm - lmx; //change in x (delta x)
  184. int yc = jym - lmy; //change in y (delta y)
  185. int dist = sqrt(xc * xc + yc * yc); //distance between two points
  186. if (dist > 0 && celement != 16) //if it's not the same place and
  187. {
  188. int xd = xc / dist; // change divided by distance
  189. int yd = yc / dist;
  190. int counter;
  191. int oldplay = play;
  192. play = 0;
  193. for (counter = 0; counter <= dist; counter++)
  194. {
  195. ym = yd * counter + lmy;
  196. xm = xd * counter + lmx;
  197. UpdateView();
  198. }
  199. play = oldplay;
  200. }
  201. }
  202. xm = jxm;
  203. ym = jym;
  204. return;
  205. }
  206. void Java_sand_wallpaper_opengl_DemoActivity_clearquicksave(JNIEnv* env, jobject thiz)
  207. {
  208. removeQuicksave();
  209. return;
  210. }
  211. void Java_sand_wallpaper_opengl_DemoActivity_setelement(JNIEnv* env, jobject thiz, jint jelement)
  212. {
  213. celement = jelement;
  214. return;
  215. }
  216. int Java_sand_wallpaper_opengl_DemoActivity_getelement(JNIEnv* env, jobject thiz)
  217. {
  218. return celement;
  219. }
  220. void Java_sand_wallpaper_opengl_DemoActivity_setBrushSize(JNIEnv* env, jobject thiz, jint jsize)
  221. {
  222. size = jsize;
  223. return;
  224. }
  225. void Java_sand_wallpaper_opengl_DemoActivity_setAccelOnOff(JNIEnv* env, jobject thiz, jint state)
  226. {
  227. accelcon = state;
  228. return;
  229. }
  230. void Java_sand_wallpaper_opengl_DemoActivity_togglesize(JNIEnv* env, jobject thiz)
  231. {
  232. if (screensize == 0) //not zoomed in, *2 to zoom out
  233. {
  234. screensize = 1;
  235. maxx = maxx * 2;
  236. maxy = maxy * 2;
  237. }
  238. else
  239. {
  240. screensize = 0; //zoomed in
  241. maxx = maxx / 2;
  242. maxy = maxy / 2;
  243. }
  244. }
  245. int Java_sand_wallpaper_opengl_DemoActivity_save(JNIEnv* env, jobject thiz)
  246. {
  247. return saver(0); //Do a normal save
  248. }
  249. int Java_sand_wallpaper_opengl_DemoActivity_load(JNIEnv* env, jobject thiz)
  250. {
  251. return loader(0); // call the load function, normal load
  252. }
  253. void Java_sand_wallpaper_opengl_DemoActivity_loaddemo(JNIEnv* env, jobject thiz)
  254. {
  255. loadDemoFile();
  256. }
  257. void Java_sand_wallpaper_opengl_DemoActivity_loadcustom(JNIEnv* env, jobject thiz)
  258. {
  259. loadCustomFile();
  260. }
  261. void Java_sand_wallpaper_opengl_DemoActivity_savecustom(JNIEnv* env, jobject thiz)
  262. {
  263. saveCustomFile();
  264. }
  265. void Java_sand_wallpaper_opengl_DemoActivity_setPassword(JNIEnv *env, jobject thiz, jbyteArray minut)
  266. {
  267. int i; //Counter variable
  268. //Get the array length of the password
  269. jsize len = (*env)->GetArrayLength(env,minut);
  270. //Create a byte array with the size of the array in the byteArray object
  271. jbyte* minut1 = (jbyte *)malloc(len * sizeof(jbyte));
  272. //Extract the byteArray object to a byte array
  273. (*env)->GetByteArrayRegion(env,minut,0,len,minut1);
  274. //Copy the byte array over into the password
  275. for(i = 0; i < len; i++)
  276. {
  277. password[i] = minut1[i];
  278. }
  279. //Add the null byte
  280. password[len] = 0;
  281. //Set the length variable
  282. passlength = len;
  283. //Free the created byte array
  284. free(minut1);
  285. }
  286. void Java_sand_wallpaper_opengl_DemoActivity_setUserName(JNIEnv *env, jobject thiz, jbyteArray minut)
  287. {
  288. int i; //Counter variable
  289. //Get the array length of the username
  290. jsize len = (*env)->GetArrayLength(env,minut);
  291. //Create a byte array with the size of the array in the byteArray object
  292. jbyte* minut1 = (jbyte *)malloc(len * sizeof(jbyte));
  293. //Extract the byteArray object to a byte array
  294. (*env)->GetByteArrayRegion(env,minut,0,len,minut1);
  295. //Copy the byte array over into the username
  296. for(i = 0; i < len; i++)
  297. {
  298. username[i] = minut1[i];
  299. }
  300. //Add the null byte
  301. username[len] = 0;
  302. //Set the length variable
  303. userlength = len;
  304. //Free the created byte array
  305. free(minut1);
  306. }
  307. int Java_sand_wallpaper_opengl_DemoActivity_login(JNIEnv *env, jobject thiz)
  308. {
  309. buildbuffer(3);
  310. if(!sendbuffer())
  311. {
  312. return -1;
  313. }
  314. return 0;
  315. }
  316. int Java_sand_wallpaper_opengl_DemoActivity_register(JNIEnv *env, jobject thiz)
  317. {
  318. buildbuffer(2);
  319. if(!sendbuffer())
  320. {
  321. return -1;
  322. }
  323. return 0;
  324. }
  325. char* Java_sand_wallpaper_opengl_DemoActivity_viewerr (JNIEnv *env, jobject thiz)
  326. {
  327. return error;
  328. }