PageRenderTime 105ms CodeModel.GetById 40ms app.highlight 22ms RepoModel.GetById 38ms app.codeStats 0ms

/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
  9#include <jni.h>
 10#include <math.h>
 11#include <stdlib.h>
 12#include <stdio.h>
 13//#include <android/log.h>
 14
 15//Include the global variables
 16#include "app.h"
 17//Include the global macros
 18#include "macros.h"
 19//Include the initializing function
 20#include "setup.h"
 21//Include the element characteristics file
 22#include "elementproperties.h"
 23//Include the collisions data file
 24#include "collisions.h"
 25//Include the saving and loading functions
 26#include "saveload.h"
 27//Include the server access functions
 28#include "server.h"
 29//Include the rendering functions
 30#include "gl.h"
 31
 32void Java_sand_falling_opengl_DemoRenderer_nativeInit(JNIEnv* env) //Initialize graphics
 33{
 34	//__android_log_write(ANDROID_LOG_INFO, "MainActivity", "8");
 35	importGLInit();
 36	appInit();
 37	gAppAlive = 1;
 38	//__android_log_write(ANDROID_LOG_INFO, "MainActivity", "4");
 39}
 40
 41void Java_sand_falling_opengl_DemoRenderer_nativeResize(JNIEnv* env, jobject thiz, jint w, jint h)
 42{
 43	if (screensize == 0) //this didn't work before becuase tej used = instead of == (green girdle)
 44	{
 45		maxx = w / 2;
 46		maxy = h / 2;
 47	}
 48	else
 49	{
 50		maxx = w;
 51		maxy = h;
 52	}
 53	appInit();
 54}
 55
 56void Java_sand_falling_opengl_DemoRenderer_nativeDone(JNIEnv* env)
 57{
 58	appDeinit();
 59	importGLDeinit();
 60}
 61
 62//these two get the gravity from the java code
 63void Java_sand_falling_opengl_MainActivity_sendYGrav(JNIEnv* env, jobject thiz, jfloat ygrav)
 64{
 65	gravy = ygrav;
 66}
 67void Java_sand_falling_opengl_MainActivity_sendXGrav(JNIEnv* env, jobject thiz, jfloat xgrav)
 68{
 69	gravx = -xgrav;
 70}
 71void Java_sand_falling_opengl_MainActivity_quickLoad(JNIEnv* env)
 72{
 73	loader(1);
 74}
 75void Java_sand_falling_opengl_MainActivity_quickSave(JNIEnv* env)
 76{
 77	saver(1);
 78}
 79
 80/* Call to render the next GL frame */
 81void Java_sand_falling_opengl_DemoRenderer_nativeRender(JNIEnv* env)
 82{
 83	appRender();
 84}
 85
 86void Java_sand_falling_opengl_MainActivity_setup(JNIEnv* env, jobject thiz)
 87{
 88	shouldClear = 1;
 89}
 90
 91void Java_sand_falling_opengl_MainActivity_pause(JNIEnv* env, jobject thiz)
 92{
 93	play = 0;
 94}
 95void Java_sand_falling_opengl_MainActivity_play(JNIEnv* env, jobject thiz)
 96{
 97	play = 1;
 98}
 99int Java_sand_falling_opengl_MainActivity_getPlayState(JNIEnv* env, jobject thiz)
100{
101	return play;
102}
103
104void Java_sand_falling_opengl_MainActivity_setBackgroundColor(JNIEnv* env, jobject thiz, jint colorcode)
105{
106	if (colorcode == 0)
107	{
108		red[3] = 0; //3 is eraser
109		blue[3] = 0;
110		green[3] = 0;
111		//__android_log_write(ANDROID_LOG_INFO, "MainActivity", "color");
112		rsetup();
113	}
114	else if (colorcode == 1)
115	{
116		red[3] = 255;
117		blue[3] = 255;
118		green[3] = 255;
119		//__android_log_write(ANDROID_LOG_INFO, "MainActivity", "color");
120		rsetup();
121	}
122	loader(1);
123}
124void Java_sand_falling_opengl_MainActivity_setExplosiveness(JNIEnv* env, jobject thiz, jint explosiveness)
125{
126	exploness[22] = explosiveness;
127}
128void Java_sand_falling_opengl_MainActivity_setRed(JNIEnv* env, jobject thiz, jint redness)
129{
130	red[22] = redness;
131}
132void Java_sand_falling_opengl_MainActivity_setGreen(JNIEnv* env, jobject thiz, jint greenness)
133{
134	green[22] = greenness;
135}
136void Java_sand_falling_opengl_MainActivity_setBlue(JNIEnv* env, jobject thiz, jint blueness)
137{
138	blue[22] = blueness;
139}
140
141void Java_sand_falling_opengl_MainActivity_setDensity(JNIEnv* env, jobject thiz, jint jdensity)
142{
143	density[22] = jdensity;
144}
145void Java_sand_falling_opengl_MainActivity_setFlip(JNIEnv* env, jobject thiz, jint jflipped)
146{
147	flipped = jflipped;
148}
149void Java_sand_falling_opengl_MainActivity_setCollision(JNIEnv* env, jobject thiz, jint custnum, jint elementnumb, jint colspot, jint colnum)
150{
151	if (custnum == 1)
152	{
153		collision[22][colspot] = colnum;
154		colliseelement1[colspot] = elementnumb;
155		collision[colspot][22] = colnum;
156	}
157	else
158	{
159		collision[22][colspot] = colnum;
160		colliseelement1[colspot] = elementnumb;
161		collision[colspot][22] = colnum;
162	}
163	int counter124;
164	char foundfire = 0;
165	for (counter124 = 0; counter124 < TElements; counter124++)
166	{
167		if (collision[22][counter124] == 6)
168		{
169			foundfire = 1;
170		}
171	}
172	if (foundfire == 0)
173	{
174		fireburn[22] = 0;
175	}
176	else
177	{
178		fireburn[22] = 1;
179	}
180}
181
182void Java_sand_falling_opengl_MainActivity_setFingerState(JNIEnv* env, jobject thiz, jint fstate)
183{
184	//setting finger up or down from onTouch
185
186	fd = fstate;
187	if (fd == 1)
188	{
189		xm = -1; // to prevent drawing from the previous point, invalidate the mouse pointer
190	}
191	return;
192}
193void Java_sand_falling_opengl_MainActivity_setMouseLocation(JNIEnv* env, jobject thiz, jint xpos, jint ypos)
194{
195	//__android_log_write(ANDROID_LOG_INFO, "MainActivity", "mp start");
196	//setting the mouse position when given stuff from jdk
197	if (xm != -1)
198	{
199		lmx = xm;
200		lmy = ym;
201
202		int xc = xpos - lmx; //change in x (delta x)
203		int yc = ypos - lmy; //change in y (delta y)
204
205
206		int dist = sqrt(xc * xc + yc * yc); //distance between two points
207
208
209		if (dist > 0 && celement != 16) //if it's not the same place and not wind
210		{
211			float xd = (float)xc / (float)dist; // change divided by distance
212			float yd = (float)yc / (float)dist;
213			int counter;
214			int oldplay = play;
215			play = 0;
216			for (counter = 0; counter <= dist; counter++)
217			{
218				ym = yd * counter + lmy;
219				xm = xd * counter + lmx;
220				UpdateView();
221			}
222			play = oldplay;
223		}
224	}
225	xm = xpos;
226	ym = ypos;
227	//__android_log_write(ANDROID_LOG_INFO, "MainActivity", "mp end");
228	return;
229}
230void Java_sand_falling_opengl_MainActivity_clearQuickSave(JNIEnv* env, jobject thiz)
231{
232	removeQuicksave();
233	return;
234}
235void Java_sand_falling_opengl_MainActivity_setElement(JNIEnv* env, jobject thiz, jint jelement)
236{
237	celement = jelement;
238	return;
239}
240int Java_sand_falling_opengl_MainActivity_getElement(JNIEnv* env, jobject thiz)
241{
242	return celement;
243}
244void Java_sand_falling_opengl_MainActivity_setBrushSize(JNIEnv* env, jobject thiz, jint jsize)
245{
246	size = jsize;
247	return;
248}
249void Java_sand_falling_opengl_MainActivity_setAccelOnOff(JNIEnv* env, jobject thiz, jint state)
250{
251	accelcon = state;
252	return;
253}
254void Java_sand_falling_opengl_MainActivity_toggleSize(JNIEnv* env, jobject thiz)
255{
256	if (screensize == 0) //not zoomed in, *2 to zoom out
257	{
258		screensize = 1;
259		maxx = maxx * 2;
260		maxy = maxy * 2;
261	}
262	else
263	{
264		screensize = 0; //zoomed in
265		maxx = maxx / 2;
266		maxy = maxy / 2;
267	}
268}
269int Java_sand_falling_opengl_MainActivity_save(JNIEnv* env, jobject thiz)
270{
271	return saver(0); //Do a normal save
272}
273
274int Java_sand_falling_opengl_MainActivity_load(JNIEnv* env, jobject thiz)
275{
276	return loader(0); // call the load function, normal load
277}
278
279void Java_sand_falling_opengl_MainActivity_loadDemo(JNIEnv* env, jobject thiz)
280{
281	loadDemoFile();
282}
283
284void Java_sand_falling_opengl_MainActivity_setPassword(JNIEnv *env, jobject thiz, jbyteArray minut)
285{
286	int i; //Counter variable
287
288	//Get the array length of the password
289	jsize len  = (*env)->GetArrayLength(env,minut);
290	//Create a byte array with the size of the array in the byteArray object
291	jbyte* minut1 = (jbyte *)malloc(len * sizeof(jbyte));
292
293	//Extract the byteArray object to a byte array
294	(*env)->GetByteArrayRegion(env,minut,0,len,minut1);
295
296	//Copy the byte array over into the password
297	for(i = 0; i < len; i++)
298	{
299		password[i] = minut1[i];
300	}
301	//Add the null byte
302	password[len] = 0;
303	//Set the length variable
304	passlength = len;
305
306	//Free the created byte array
307	free(minut1);
308}
309
310void Java_sand_falling_opengl_MainActivity_setUsername(JNIEnv *env, jobject thiz, jbyteArray minut)
311{
312	int i; //Counter variable
313
314	//Get the array length of the username
315	jsize len  = (*env)->GetArrayLength(env,minut);
316	//Create a byte array with the size of the array in the byteArray object
317	jbyte* minut1 = (jbyte *)malloc(len * sizeof(jbyte));
318
319	//Extract the byteArray object to a byte array
320	(*env)->GetByteArrayRegion(env,minut,0,len,minut1);
321
322	//Copy the byte array over into the username
323	for(i = 0; i < len; i++)
324	{
325		username[i] = minut1[i];
326	}
327	//Add the null byte
328	username[len] = 0;
329	//Set the length variable
330	userlength = len;
331
332	//Free the created byte array
333	free(minut1);
334}
335
336int Java_sand_falling_opengl_MainActivity_login(JNIEnv *env, jobject thiz)
337{
338	buildbuffer(3);
339	if(!sendbuffer())
340	{
341		return -1;
342	}
343
344	return 0;
345}
346int Java_sand_falling_opengl_MainActivity_register(JNIEnv *env, jobject thiz)
347{
348	buildbuffer(2);
349	if(!sendbuffer())
350	{
351		return -1;
352	}
353
354
355	return 0;
356}
357
358char* Java_sand_falling_opengl_MainActivity_viewErr (JNIEnv *env, jobject thiz)
359{
360	return error;
361}