PageRenderTime 143ms CodeModel.GetById 81ms app.highlight 7ms RepoModel.GetById 53ms app.codeStats 0ms

/TheElements/jni/saveload.c

http://thelements.googlecode.com/
C | 121 lines | 104 code | 9 blank | 8 comment | 28 complexity | 9d2e6cb27932e868dc263d22821f21d0 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}