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