PageRenderTime 357ms CodeModel.GetById 60ms app.highlight 184ms RepoModel.GetById 98ms app.codeStats 1ms

/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
  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_wallpaper_opengl_DemoRenderer_nativeInit(JNIEnv* env) //Initialize graphics
 33{
 34	//__android_log_write(ANDROID_LOG_INFO, "DemoActivity", "8");
 35	importGLInit();
 36	appInit();
 37	gAppAlive = 1;
 38	//__android_log_write(ANDROID_LOG_INFO, "DemoActivity", "4");
 39}
 40
 41void Java_sand_wallpaper_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_wallpaper_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_wallpaper_opengl_DemoActivity_sendyg(JNIEnv* env, jobject thiz, jfloat ygrav)
 64{
 65	gravy = ygrav;
 66}
 67void Java_sand_wallpaper_opengl_DemoActivity_sendxg(JNIEnv* env, jobject thiz, jfloat xgrav)
 68{
 69	gravx = -xgrav;
 70}
 71void Java_sand_wallpaper_opengl_DemoActivity_quickload(JNIEnv* env)
 72{
 73	loader(1);
 74}
 75void Java_sand_wallpaper_opengl_DemoActivity_quicksave(JNIEnv* env)
 76{
 77	saver(1);
 78}
 79
 80/* Call to render the next GL frame */
 81void Java_sand_wallpaper_opengl_DemoRenderer_nativeRender(JNIEnv* env)
 82{
 83	appRender();
 84}
 85
 86void Java_sand_wallpaper_opengl_DemoActivity_setup(JNIEnv* env, jobject thiz)
 87{
 88	rsetup();
 89}
 90
 91void Java_sand_wallpaper_opengl_DemoActivity_jPause(JNIEnv* env, jobject thiz)
 92{
 93	play = 0;
 94}
 95void Java_sand_wallpaper_opengl_DemoActivity_Play(JNIEnv* env, jobject thiz)
 96{
 97	play = 1;
 98}
 99
100void Java_sand_wallpaper_opengl_DemoActivity_setBackgroundColor(JNIEnv* env, jobject thiz, jint colorcode)
101{
102	if (colorcode == 0)
103	{
104		red[3] = 0; //3 is eraser
105		blue[3] = 0;
106		green[3] = 0;
107		//__android_log_write(ANDROID_LOG_INFO, "DemoActivity", "color");
108		rsetup();
109	}
110	else if (colorcode == 1)
111	{
112		red[3] = 255;
113		blue[3] = 255;
114		green[3] = 255;
115		//__android_log_write(ANDROID_LOG_INFO, "DemoActivity", "color");
116		rsetup();
117	}
118	loader(1);
119}
120void Java_sand_wallpaper_opengl_DemoActivity_setexplosiveness(JNIEnv* env, jobject thiz, jint explosiveness)
121{
122	exploness[22] = explosiveness;
123}
124void Java_sand_wallpaper_opengl_DemoActivity_setred(JNIEnv* env, jobject thiz, jint redness)
125{
126	red[22] = redness;
127}
128void Java_sand_wallpaper_opengl_DemoActivity_setgreen(JNIEnv* env, jobject thiz, jint greenness)
129{
130	green[22] = greenness;
131}
132void Java_sand_wallpaper_opengl_DemoActivity_setblue(JNIEnv* env, jobject thiz, jint blueness)
133{
134	blue[22] = blueness;
135}
136
137void Java_sand_wallpaper_opengl_DemoActivity_setdensity(JNIEnv* env, jobject thiz, jint jdensity)
138{
139	density[22] = jdensity;
140}
141void Java_sand_wallpaper_opengl_DemoActivity_setFlip(JNIEnv* env, jobject thiz, jint jflipped)
142{
143	flipped = jflipped;
144}
145void Java_sand_wallpaper_opengl_DemoActivity_setcollision(JNIEnv* env, jobject thiz, jint custnum, jint elementnumb, jint colspot, jint colnum)
146{
147	if (custnum == 1)
148	{
149		collision[22][colspot] = colnum;
150		colliseelement1[colspot] = elementnumb;
151		collision[colspot][22] = colnum;
152	}
153	else
154	{
155		collision[22][colspot] = colnum;
156		colliseelement1[colspot] = elementnumb;
157		collision[colspot][22] = colnum;
158	}
159	int counter124;
160	char foundfire = 0;
161	for (counter124 = 0; counter124 < TElements; counter124++)
162	{
163		if (collision[22][counter124] == 6)
164		{
165			foundfire = 1;
166		}
167	}
168	if (foundfire == 0)
169	{
170		fireburn[22] = 0;
171	}
172	else
173	{
174		fireburn[22] = 1;
175	}
176}
177
178void Java_sand_wallpaper_opengl_DemoActivity_fd(JNIEnv* env, jobject thiz, jint fstate)
179{
180	//setting finger up or down from onTouch
181
182	fd = fstate;
183	if (fd == 1)
184	{
185		xm = -1;
186	}
187	return;
188}
189void Java_sand_wallpaper_opengl_DemoActivity_mp(JNIEnv* env, jobject thiz, jint jxm, jint jym)
190{
191	//setting the mouse position when given stuff from jdk
192	if (xm != -1)
193	{
194		lmx = xm;
195		lmy = ym;
196
197		int xc = jxm - lmx; //change in x (delta x)
198		int yc = jym - lmy; //change in y (delta y)
199
200
201		int dist = sqrt(xc * xc + yc * yc); //distance between two points
202
203
204		if (dist > 0 && celement != 16) //if it's not the same place and 
205		{
206			int xd = xc / dist; // change divided by distance
207			int yd = yc / dist;
208			int counter;
209			int oldplay = play;
210			play = 0;
211			for (counter = 0; counter <= dist; counter++)
212			{
213				ym = yd * counter + lmy;
214				xm = xd * counter + lmx;
215				UpdateView();
216			}
217			play = oldplay;
218		}
219	}
220	xm = jxm;
221	ym = jym;
222	return;
223}
224void Java_sand_wallpaper_opengl_DemoActivity_clearquicksave(JNIEnv* env, jobject thiz)
225{
226	removeQuicksave();
227	return;
228}
229void Java_sand_wallpaper_opengl_DemoActivity_setelement(JNIEnv* env, jobject thiz, jint jelement)
230{
231	celement = jelement;
232	return;
233}
234int Java_sand_wallpaper_opengl_DemoActivity_getelement(JNIEnv* env, jobject thiz)
235{
236	return celement;
237}
238void Java_sand_wallpaper_opengl_DemoActivity_setBrushSize(JNIEnv* env, jobject thiz, jint jsize)
239{
240	size = jsize;
241	return;
242}
243void Java_sand_wallpaper_opengl_DemoActivity_setAccelOnOff(JNIEnv* env, jobject thiz, jint state)
244{
245	accelcon = state;
246	return;
247}
248void Java_sand_wallpaper_opengl_DemoActivity_togglesize(JNIEnv* env, jobject thiz)
249{
250	if (screensize == 0) //not zoomed in, *2 to zoom out
251	{
252		screensize = 1;
253		maxx = maxx * 2;
254		maxy = maxy * 2;
255	}
256	else
257	{
258		screensize = 0; //zoomed in
259		maxx = maxx / 2;
260		maxy = maxy / 2;
261	}
262}
263int Java_sand_wallpaper_opengl_DemoActivity_save(JNIEnv* env, jobject thiz)
264{
265	return saver(0); //Do a normal save
266}
267
268int Java_sand_wallpaper_opengl_DemoActivity_load(JNIEnv* env, jobject thiz)
269{
270	return loader(0); // call the load function, normal load
271}
272
273void Java_sand_wallpaper_opengl_DemoActivity_loaddemo(JNIEnv* env, jobject thiz)
274{
275	loadDemoFile();
276}
277void Java_sand_wallpaper_opengl_DemoActivity_loadcustom(JNIEnv* env, jobject thiz)
278{
279	loadCustomFile();
280}
281void Java_sand_wallpaper_opengl_DemoActivity_savecustom(JNIEnv* env, jobject thiz)
282{
283	saveCustomFile();
284}
285
286void Java_sand_wallpaper_opengl_DemoActivity_setPassword(JNIEnv *env, jobject thiz, jbyteArray minut)
287{
288	int i; //Counter variable
289
290	//Get the array length of the password
291	jsize len  = (*env)->GetArrayLength(env,minut);
292	//Create a byte array with the size of the array in the byteArray object
293	jbyte* minut1 = (jbyte *)malloc(len * sizeof(jbyte));
294
295	//Extract the byteArray object to a byte array
296	(*env)->GetByteArrayRegion(env,minut,0,len,minut1);
297
298	//Copy the byte array over into the password
299	for(i = 0; i < len; i++)
300	{
301		password[i] = minut1[i];
302	}
303	//Add the null byte
304	password[len] = 0;
305	//Set the length variable
306	passlength = len;
307
308	//Free the created byte array
309	free(minut1);
310}
311
312void Java_sand_wallpaper_opengl_DemoActivity_setUserName(JNIEnv *env, jobject thiz, jbyteArray minut)
313{
314	int i; //Counter variable
315
316	//Get the array length of the username
317	jsize len  = (*env)->GetArrayLength(env,minut);
318	//Create a byte array with the size of the array in the byteArray object
319	jbyte* minut1 = (jbyte *)malloc(len * sizeof(jbyte));
320
321	//Extract the byteArray object to a byte array
322	(*env)->GetByteArrayRegion(env,minut,0,len,minut1);
323
324	//Copy the byte array over into the username
325	for(i = 0; i < len; i++)
326	{
327		username[i] = minut1[i];
328	}
329	//Add the null byte
330	username[len] = 0;
331	//Set the length variable
332	userlength = len;
333
334	//Free the created byte array
335	free(minut1);
336}
337
338int Java_sand_wallpaper_opengl_DemoActivity_login(JNIEnv *env, jobject thiz)
339{
340	buildbuffer(3);
341	if(!sendbuffer())
342	{
343		return -1;
344	}
345
346	return 0;
347}
348int Java_sand_wallpaper_opengl_DemoActivity_register(JNIEnv *env, jobject thiz)
349{
350	buildbuffer(2);
351	if(!sendbuffer())
352	{
353		return -1;
354	}
355
356
357	return 0;
358}
359
360char* Java_sand_wallpaper_opengl_DemoActivity_viewerr (JNIEnv *env, jobject thiz)
361{
362	return error;
363}