PageRenderTime 15ms CodeModel.GetById 1ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/FallingSandpaper/jni/saveload.c

http://thelements.googlecode.com/
C | 178 lines | 154 code | 15 blank | 9 comment | 36 complexity | 6661a046d9126e37ef9000a015522dd5 MD5 | raw file
  1/*
  2 * saveload.c
  3 * --------------------------
  4 * Defines the function definitions for saver
  5 * and loader, the two functions which save and
  6 * load an element setup.
  7 */
  8
  9#include "saveload.h"
 10
 11int saver(int type)
 12{
 13	FILE *fp;
 14	if (type == 0) //If it's a normal save
 15	{
 16		fp = fopen(SAVE_FILE, "w");
 17	}
 18	else if (type == 1) //If it's a quicksave (it's being paused)
 19	{
 20		fp = fopen(QUICK_SAVE_FILE, "w");
 21	}
 22	if (fp != NULL)
 23	{
 24		int counter, added_to_file = 0;
 25		for (counter = 0; counter < TPoints; counter++)
 26		{
 27			if (set[counter] == 1)
 28			{
 29				fprintf(fp, "%d %d %d %d ", spawn[counter], (int) x[counter],
 30						(int) y[counter], element[counter]); //Save the spawn, x y, and element of each current point
 31				added_to_file = 1;
 32			}
 33		}
 34		fclose(fp);
 35		if (added_to_file == 0)
 36		{
 37			if (type == 0)
 38			{
 39				remove(SAVE_FILE);
 40			}
 41			else if (type == 1)
 42			{
 43				remove(QUICK_SAVE_FILE);
 44			}
 45		}
 46		return 1; //success
 47	}
 48	else
 49	{
 50		return 0; //error: didn't open file, prolly sdcard not there
 51	}
 52}
 53
 54int loader(int type)
 55{
 56	FILE *fp;
 57	if (type == 0) //normal load
 58	{
 59		fp = fopen(SAVE_FILE, "r");
 60	}
 61	else if (type == 1) //quickload
 62	{
 63		fp = fopen(QUICK_SAVE_FILE, "r");
 64	}
 65	rsetup();
 66	int i;
 67	int xcoordinate;
 68	int ycoordinate;
 69	int loadelement;
 70	int spawnv;
 71
 72	if (fp != NULL)
 73	{
 74		while (!feof(fp))
 75		{
 76			fscanf(fp, "%d%d%d%d", &spawnv, &xcoordinate, &ycoordinate,
 77					&loadelement);
 78			spawn[avail[loq - 1]] = spawnv;
 79			CreatePoint(xcoordinate, ycoordinate, loadelement);
 80		}
 81
 82		fclose(fp);
 83		return 1;
 84	}
 85}
 86
 87void removeQuicksave(void)
 88{
 89	remove(QUICK_SAVE_FILE);
 90}
 91
 92int loadDemoFile()
 93{
 94	FILE *fp;
 95	fp = fopen(DEMO_SAVE_FILE, "r");
 96	//__android_log_write(ANDROID_LOG_INFO, "DemoActivity", "demo");
 97	rsetup();
 98	int i;
 99	int xcoordinate;
100	int ycoordinate;
101	int loadelement;
102	int spawnv;
103
104	if (fp != NULL)
105	{
106		while (!feof(fp))
107		{
108			fscanf(fp, "%d%d%d%d", &spawnv, &xcoordinate, &ycoordinate,
109					&loadelement);
110			spawn[avail[loq - 1]] = spawnv;
111			CreatePoint(xcoordinate, ycoordinate, loadelement);
112		}
113
114		fclose(fp);
115		return 1;
116	}
117	else
118	{
119		return 0;
120	}
121}
122
123int loadCustomFile(void)
124{
125
126	FILE *fp;
127	fp = fopen(CUSTOM_ELEMENT_FILE, "r");
128	//__android_log_write(ANDROID_LOG_INFO, "DemoActivity", "custom");
129	rsetup();
130	int i;
131	int collisiondata;
132
133	if (fp != NULL)
134	{
135		for (i = 0; i < TElements; i++)
136		{
137			fscanf(fp, "%d", &collisiondata);
138			colliseelement1[i] = collisiondata;
139		}
140		for (i = 0; i < TElements; i++)
141		{
142			fscanf(fp, "%d", &collisiondata);
143			collision[22][i] = collisiondata;
144		}
145
146		fclose(fp);
147		return 1;
148	}
149	else
150	{
151		return 0;
152	}
153}
154
155int saveCustomFile(void)
156{
157	FILE *fp;
158	int i;
159	fp = fopen(CUSTOM_ELEMENT_FILE, "w");
160
161	if (fp != NULL)
162	{
163		for (i = 0; i < TElements; i++)
164		{
165			fprintf(fp, "%d", colliseelement1[i]);
166		}
167		for (i = 0; i < TElements; i++)
168		{
169			fprintf(fp, "%d", collision[22][i]);
170		}
171		fclose(fp);
172		return 1;
173	}
174	else
175	{
176		return 0;
177	}
178}