/TheElements/jni/saveload.c
http://thelements.googlecode.com/ · C · 121 lines · 104 code · 9 blank · 8 comment · 28 complexity · 9d2e6cb27932e868dc263d22821f21d0 MD5 · raw file
- /*
- * saveload.c
- * --------------------------
- * Defines the function definitions for saver
- * and loader, the two functions which save and
- * load an element setup.
- */
- #include "saveload.h"
- int saver(int type)
- {
- FILE *fp;
- if (type == 0) //If it's a normal save
- {
- fp = fopen(SAVE_FILE, "w");
- }
- else if (type == 1) //If it's a quicksave (it's being paused)
- {
- fp = fopen(QUICK_SAVE_FILE, "w");
- }
- if (fp != NULL)
- {
- int counter, added_to_file = 0;
- for (counter = 0; counter < TPoints; counter++)
- {
- if (set[counter] == 1)
- {
- fprintf(fp, "%d %d %d %d ", spawn[counter], (int) x[counter],
- (int) y[counter], element[counter]); //Save the spawn, x y, and element of each current point
- added_to_file = 1;
- }
- }
- fclose(fp);
- if (added_to_file == 0)
- {
- if (type == 0)
- {
- remove(SAVE_FILE);
- }
- else if (type == 1)
- {
- remove(QUICK_SAVE_FILE);
- }
- }
- return 1; //success
- }
- else
- {
- return 0; //error: didn't open file, prolly sdcard not there
- }
- }
- int loader(int type)
- {
- FILE *fp;
- if (type == 0) //normal load
- {
- fp = fopen(SAVE_FILE, "r");
- }
- else if (type == 1) //quickload
- {
- fp = fopen(QUICK_SAVE_FILE, "r");
- }
- rsetup();
- int i;
- int xcoordinate;
- int ycoordinate;
- int loadelement;
- int spawnv;
- if (fp != NULL)
- {
- while (!feof(fp))
- {
- fscanf(fp, "%d%d%d%d", &spawnv, &xcoordinate, &ycoordinate,
- &loadelement);
- spawn[avail[loq - 1]] = spawnv;
- CreatePoint(xcoordinate, ycoordinate, loadelement);
- }
- fclose(fp);
- return 1;
- }
- }
- void removeQuicksave(void)
- {
- remove(QUICK_SAVE_FILE);
- }
- int loadDemoFile()
- {
- FILE *fp;
- fp = fopen(DEMO_SAVE_FILE, "r");
- //__android_log_write(ANDROID_LOG_INFO, "DemoActivity", "demo");
- rsetup();
- int i;
- int xcoordinate;
- int ycoordinate;
- int loadelement;
- int spawnv;
- if (fp != NULL)
- {
- while (!feof(fp))
- {
- fscanf(fp, "%d%d%d%d", &spawnv, &xcoordinate, &ycoordinate,
- &loadelement);
- spawn[avail[loq - 1]] = spawnv;
- CreatePoint(xcoordinate, ycoordinate, loadelement);
- }
- fclose(fp);
- return 1;
- }
- else
- {
- return 0;
- }
- }