/TheElements/jni/app-android.c

http://thelements.googlecode.com/ · C · 361 lines · 286 code · 35 blank · 40 comment · 28 complexity · 698982ba0cdfa589833439b922aef3c2 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_falling_opengl_DemoRenderer_nativeInit(JNIEnv* env) //Initialize graphics
  30. {
  31. //__android_log_write(ANDROID_LOG_INFO, "MainActivity", "8");
  32. importGLInit();
  33. appInit();
  34. gAppAlive = 1;
  35. //__android_log_write(ANDROID_LOG_INFO, "MainActivity", "4");
  36. }
  37. void Java_sand_falling_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_falling_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_falling_opengl_MainActivity_sendYGrav(JNIEnv* env, jobject thiz, jfloat ygrav)
  58. {
  59. gravy = ygrav;
  60. }
  61. void Java_sand_falling_opengl_MainActivity_sendXGrav(JNIEnv* env, jobject thiz, jfloat xgrav)
  62. {
  63. gravx = -xgrav;
  64. }
  65. void Java_sand_falling_opengl_MainActivity_quickLoad(JNIEnv* env)
  66. {
  67. loader(1);
  68. }
  69. void Java_sand_falling_opengl_MainActivity_quickSave(JNIEnv* env)
  70. {
  71. saver(1);
  72. }
  73. /* Call to render the next GL frame */
  74. void Java_sand_falling_opengl_DemoRenderer_nativeRender(JNIEnv* env)
  75. {
  76. appRender();
  77. }
  78. void Java_sand_falling_opengl_MainActivity_setup(JNIEnv* env, jobject thiz)
  79. {
  80. shouldClear = 1;
  81. }
  82. void Java_sand_falling_opengl_MainActivity_pause(JNIEnv* env, jobject thiz)
  83. {
  84. play = 0;
  85. }
  86. void Java_sand_falling_opengl_MainActivity_play(JNIEnv* env, jobject thiz)
  87. {
  88. play = 1;
  89. }
  90. int Java_sand_falling_opengl_MainActivity_getPlayState(JNIEnv* env, jobject thiz)
  91. {
  92. return play;
  93. }
  94. void Java_sand_falling_opengl_MainActivity_setBackgroundColor(JNIEnv* env, jobject thiz, jint colorcode)
  95. {
  96. if (colorcode == 0)
  97. {
  98. red[3] = 0; //3 is eraser
  99. blue[3] = 0;
  100. green[3] = 0;
  101. //__android_log_write(ANDROID_LOG_INFO, "MainActivity", "color");
  102. rsetup();
  103. }
  104. else if (colorcode == 1)
  105. {
  106. red[3] = 255;
  107. blue[3] = 255;
  108. green[3] = 255;
  109. //__android_log_write(ANDROID_LOG_INFO, "MainActivity", "color");
  110. rsetup();
  111. }
  112. loader(1);
  113. }
  114. void Java_sand_falling_opengl_MainActivity_setExplosiveness(JNIEnv* env, jobject thiz, jint explosiveness)
  115. {
  116. exploness[22] = explosiveness;
  117. }
  118. void Java_sand_falling_opengl_MainActivity_setRed(JNIEnv* env, jobject thiz, jint redness)
  119. {
  120. red[22] = redness;
  121. }
  122. void Java_sand_falling_opengl_MainActivity_setGreen(JNIEnv* env, jobject thiz, jint greenness)
  123. {
  124. green[22] = greenness;
  125. }
  126. void Java_sand_falling_opengl_MainActivity_setBlue(JNIEnv* env, jobject thiz, jint blueness)
  127. {
  128. blue[22] = blueness;
  129. }
  130. void Java_sand_falling_opengl_MainActivity_setDensity(JNIEnv* env, jobject thiz, jint jdensity)
  131. {
  132. density[22] = jdensity;
  133. }
  134. void Java_sand_falling_opengl_MainActivity_setFlip(JNIEnv* env, jobject thiz, jint jflipped)
  135. {
  136. flipped = jflipped;
  137. }
  138. void Java_sand_falling_opengl_MainActivity_setCollision(JNIEnv* env, jobject thiz, jint custnum, jint elementnumb, jint colspot, jint colnum)
  139. {
  140. if (custnum == 1)
  141. {
  142. collision[22][colspot] = colnum;
  143. colliseelement1[colspot] = elementnumb;
  144. collision[colspot][22] = colnum;
  145. }
  146. else
  147. {
  148. collision[22][colspot] = colnum;
  149. colliseelement1[colspot] = elementnumb;
  150. collision[colspot][22] = colnum;
  151. }
  152. int counter124;
  153. char foundfire = 0;
  154. for (counter124 = 0; counter124 < TElements; counter124++)
  155. {
  156. if (collision[22][counter124] == 6)
  157. {
  158. foundfire = 1;
  159. }
  160. }
  161. if (foundfire == 0)
  162. {
  163. fireburn[22] = 0;
  164. }
  165. else
  166. {
  167. fireburn[22] = 1;
  168. }
  169. }
  170. void Java_sand_falling_opengl_MainActivity_setFingerState(JNIEnv* env, jobject thiz, jint fstate)
  171. {
  172. //setting finger up or down from onTouch
  173. fd = fstate;
  174. if (fd == 1)
  175. {
  176. xm = -1; // to prevent drawing from the previous point, invalidate the mouse pointer
  177. }
  178. return;
  179. }
  180. void Java_sand_falling_opengl_MainActivity_setMouseLocation(JNIEnv* env, jobject thiz, jint xpos, jint ypos)
  181. {
  182. //__android_log_write(ANDROID_LOG_INFO, "MainActivity", "mp start");
  183. //setting the mouse position when given stuff from jdk
  184. if (xm != -1)
  185. {
  186. lmx = xm;
  187. lmy = ym;
  188. int xc = xpos - lmx; //change in x (delta x)
  189. int yc = ypos - lmy; //change in y (delta y)
  190. int dist = sqrt(xc * xc + yc * yc); //distance between two points
  191. if (dist > 0 && celement != 16) //if it's not the same place and not wind
  192. {
  193. float xd = (float)xc / (float)dist; // change divided by distance
  194. float yd = (float)yc / (float)dist;
  195. int counter;
  196. int oldplay = play;
  197. play = 0;
  198. for (counter = 0; counter <= dist; counter++)
  199. {
  200. ym = yd * counter + lmy;
  201. xm = xd * counter + lmx;
  202. UpdateView();
  203. }
  204. play = oldplay;
  205. }
  206. }
  207. xm = xpos;
  208. ym = ypos;
  209. //__android_log_write(ANDROID_LOG_INFO, "MainActivity", "mp end");
  210. return;
  211. }
  212. void Java_sand_falling_opengl_MainActivity_clearQuickSave(JNIEnv* env, jobject thiz)
  213. {
  214. removeQuicksave();
  215. return;
  216. }
  217. void Java_sand_falling_opengl_MainActivity_setElement(JNIEnv* env, jobject thiz, jint jelement)
  218. {
  219. celement = jelement;
  220. return;
  221. }
  222. int Java_sand_falling_opengl_MainActivity_getElement(JNIEnv* env, jobject thiz)
  223. {
  224. return celement;
  225. }
  226. void Java_sand_falling_opengl_MainActivity_setBrushSize(JNIEnv* env, jobject thiz, jint jsize)
  227. {
  228. size = jsize;
  229. return;
  230. }
  231. void Java_sand_falling_opengl_MainActivity_setAccelOnOff(JNIEnv* env, jobject thiz, jint state)
  232. {
  233. accelcon = state;
  234. return;
  235. }
  236. void Java_sand_falling_opengl_MainActivity_toggleSize(JNIEnv* env, jobject thiz)
  237. {
  238. if (screensize == 0) //not zoomed in, *2 to zoom out
  239. {
  240. screensize = 1;
  241. maxx = maxx * 2;
  242. maxy = maxy * 2;
  243. }
  244. else
  245. {
  246. screensize = 0; //zoomed in
  247. maxx = maxx / 2;
  248. maxy = maxy / 2;
  249. }
  250. }
  251. int Java_sand_falling_opengl_MainActivity_save(JNIEnv* env, jobject thiz)
  252. {
  253. return saver(0); //Do a normal save
  254. }
  255. int Java_sand_falling_opengl_MainActivity_load(JNIEnv* env, jobject thiz)
  256. {
  257. return loader(0); // call the load function, normal load
  258. }
  259. void Java_sand_falling_opengl_MainActivity_loadDemo(JNIEnv* env, jobject thiz)
  260. {
  261. loadDemoFile();
  262. }
  263. void Java_sand_falling_opengl_MainActivity_setPassword(JNIEnv *env, jobject thiz, jbyteArray minut)
  264. {
  265. int i; //Counter variable
  266. //Get the array length of the password
  267. jsize len = (*env)->GetArrayLength(env,minut);
  268. //Create a byte array with the size of the array in the byteArray object
  269. jbyte* minut1 = (jbyte *)malloc(len * sizeof(jbyte));
  270. //Extract the byteArray object to a byte array
  271. (*env)->GetByteArrayRegion(env,minut,0,len,minut1);
  272. //Copy the byte array over into the password
  273. for(i = 0; i < len; i++)
  274. {
  275. password[i] = minut1[i];
  276. }
  277. //Add the null byte
  278. password[len] = 0;
  279. //Set the length variable
  280. passlength = len;
  281. //Free the created byte array
  282. free(minut1);
  283. }
  284. void Java_sand_falling_opengl_MainActivity_setUsername(JNIEnv *env, jobject thiz, jbyteArray minut)
  285. {
  286. int i; //Counter variable
  287. //Get the array length of the username
  288. jsize len = (*env)->GetArrayLength(env,minut);
  289. //Create a byte array with the size of the array in the byteArray object
  290. jbyte* minut1 = (jbyte *)malloc(len * sizeof(jbyte));
  291. //Extract the byteArray object to a byte array
  292. (*env)->GetByteArrayRegion(env,minut,0,len,minut1);
  293. //Copy the byte array over into the username
  294. for(i = 0; i < len; i++)
  295. {
  296. username[i] = minut1[i];
  297. }
  298. //Add the null byte
  299. username[len] = 0;
  300. //Set the length variable
  301. userlength = len;
  302. //Free the created byte array
  303. free(minut1);
  304. }
  305. int Java_sand_falling_opengl_MainActivity_login(JNIEnv *env, jobject thiz)
  306. {
  307. buildbuffer(3);
  308. if(!sendbuffer())
  309. {
  310. return -1;
  311. }
  312. return 0;
  313. }
  314. int Java_sand_falling_opengl_MainActivity_register(JNIEnv *env, jobject thiz)
  315. {
  316. buildbuffer(2);
  317. if(!sendbuffer())
  318. {
  319. return -1;
  320. }
  321. return 0;
  322. }
  323. char* Java_sand_falling_opengl_MainActivity_viewErr (JNIEnv *env, jobject thiz)
  324. {
  325. return error;
  326. }