Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. #include "ini.h"
  2. #include "game.h"
  3. #include "options.h"
  4. #include "PHL.h"
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include "text.h"
  8. #ifdef EMSCRIPTEN
  9. #include <emscripten.h>
  10. #endif
  11.  
  12. //char* getFileLocation();
  13. char* trimString(char* orig);
  14.  
  15. void screenLoad(char* first, char* second);
  16. void sizeLoad(char* first, char* second);
  17. void blurLoad(char* first, char* second);
  18. void xbrzLoad(char* first, char* second);
  19. void languageLoad(char* first, char* second);
  20. void autosaveLoad(char* first, char* second);
  21. void musictypeLoad(char* first, char* second);
  22. void musicvolumeLoad(char* first, char* second);
  23.  
  24. void iniInit()
  25. {
  26.         //Build filepath
  27.         char fullPath[128];
  28.         {
  29.         #ifdef _SDL
  30.                 #if defined(__amigaos4__) || defined(__MORPHOS__)
  31.                 strcpy(fullPath, "PROGDIR:.hydracastlelabyrinth/");
  32.                 #elif defined(EMSCRIPTEN)
  33.                 strcpy(fullPath, "hcl_data/");
  34.                 #elif defined(_KOLIBRI)
  35.                 strcpy(fullPath, KOS_HCL_SAVES_PATH"/");
  36.                 #else
  37.                 strcpy(fullPath, getenv("HOME"));
  38.                 strcat(fullPath, "/.hydracastlelabyrinth/");
  39.                 #endif 
  40.         #elif defined(_3DS)
  41.                 strcpy(fullPath, "sdmc:/3ds/appdata/HydraCastleLabyrinth/");
  42.         #else
  43.                 strcpy(fullPath, "");
  44.         #endif
  45.                 strcat(fullPath, "system.ini");
  46.         }
  47.        
  48.         FILE* f;
  49.  
  50.         if ( (f = fopen(fullPath, "rt")) )
  51.         {
  52.                 //File exists - read it
  53.                 fclose(f);
  54.                 loadSettings();
  55.         }else{
  56.                 //File does not exists - create it (with default hardcoded settings)
  57.                 saveSettings();
  58.         }
  59.        
  60. }
  61.  
  62. void saveSettings()
  63. {      
  64.         //Build filepath
  65.         char fullPath[128];
  66.         {
  67.                 #ifdef _SDL
  68.                 #if defined(__amigaos4__) || defined(__MORPHOS__)
  69.                 strcpy(fullPath, "PROGDIR:.hydracastlelabyrinth/");
  70.                 #elif defined(EMSCRIPTEN)
  71.                 strcpy(fullPath, "hcl_data/");
  72.                 #elif defined(_KOLIBRI)
  73.                 strcpy(fullPath, KOS_HCL_SAVES_PATH"/");
  74.                 #else
  75.                 strcpy(fullPath, getenv("HOME"));
  76.                 strcat(fullPath, "/.hydracastlelabyrinth/");
  77.                 #endif
  78.                 #elif defined(_3DS)
  79.                 strcpy(fullPath, "sdmc:/3ds/appdata/HydraCastleLabyrinth/");
  80.                 #else
  81.                 strcpy(fullPath, "");
  82.                 #endif
  83.                 strcat(fullPath, "system.ini");
  84.         }
  85.  
  86.         FILE* f;
  87.        
  88.         if ( (f = fopen(fullPath, "wt")) )
  89.         {
  90.                 fprintf(f, "[disp]");
  91.                 #ifdef _3DS
  92.                         //Screen
  93.                         fprintf(f, "\r\nscreen=");
  94.                         if (activeScreen->screen == GFX_BOTTOM) {
  95.                                 fprintf(f, "bottom");
  96.                         }else{
  97.                                 fprintf(f, "top");
  98.                         }
  99.                 #endif
  100.                
  101.                 #ifdef _PSP
  102.                         //Screen Size
  103.                         fprintf(f, "\r\nsize=");
  104.                         if (getScreenSize() == 1) {
  105.                                 fprintf(f, "1");
  106.                         }
  107.                         else if (getScreenSize() == 2) {
  108.                                 fprintf(f, "2");
  109.                         }
  110.                         else {
  111.                                 fprintf(f, "0");
  112.                         }
  113.                        
  114.                         //Screen Blur
  115.                         fprintf(f, "\r\nblur=");
  116.                         if (getBlur() == 1) {
  117.                                 fprintf(f, "on");
  118.                         }else{
  119.                                 fprintf(f, "off");
  120.                         }                      
  121.                 #endif
  122.                 #ifdef _SDL
  123.                         //xBRZ Scaling
  124.                         fprintf(f, "\r\nxbrz=");
  125.                         if (getXBRZ() == 1) {
  126.                                 fprintf(f, "on");
  127.                         }else{
  128.                                 fprintf(f, "off");
  129.                         }                      
  130.                 #endif
  131.  
  132.                 fprintf(f, "\r\n[system]");
  133.                
  134.                 //Language
  135.                 fprintf(f, "\r\nlanguage=");
  136.                 if (getLanguage() == 0) {
  137.                         fprintf(f, "jp");
  138.                 }
  139.                 if (getLanguage() == 1) {
  140.                         fprintf(f, "en");
  141.                 }
  142.                
  143.                 //Autosave
  144.                 fprintf(f, "\r\nautosave=");
  145.                 if (getAutoSave() == 1) {
  146.                         fprintf(f, "on");
  147.                 }else{
  148.                         fprintf(f, "off");
  149.                 }
  150.  
  151.                 #ifdef _SDL
  152.                 fprintf(f, "\r\n[audio]");
  153.                 fprintf(f, "\r\nmusic_type=%s", getMusicType()?"ogg":"midi");
  154.                 fprintf(f, "\r\nmusic=%d", music_volume);
  155.                 // Audio
  156.                 #endif
  157.                 fclose(f);
  158.         }
  159.         #ifdef EMSCRIPTEN
  160.         EM_ASM(
  161.                 FS.syncfs(false,function () {
  162.                         Module.print("File sych'd")
  163.                 });
  164.         );
  165.         #endif
  166. }
  167.  
  168. void loadSettings()
  169. {
  170.         //Build filepath
  171.         char fullPath[128];
  172.         {
  173.                 #ifdef _SDL
  174.                 #if defined(__amigaos4__) || defined(__MORPHOS__)
  175.                 strcpy(fullPath, "PROGDIR:.hydracastlelabyrinth/");
  176.                 #elif defined(EMSCRIPTEN)
  177.                 strcpy(fullPath, "hcl_data/");
  178.                 #elif defined(_KOLIBRI)
  179.                 strcat(fullPath, KOS_HCL_SAVES_PATH"/");
  180.                 #else
  181.                 strcpy(fullPath, getenv("HOME"));
  182.                 strcat(fullPath, "/.hydracastlelabyrinth/");
  183.                 #endif
  184.                 #elif defined(_3DS)
  185.                 strcpy(fullPath, "sdmc:/3ds/appdata/HydraCastleLabyrinth/");
  186.                 #else
  187.                 strcpy(fullPath, "");
  188.                 #endif
  189.                 strcat(fullPath, "system.ini");
  190.         }
  191.        
  192.         FILE* f;
  193.        
  194.         if ( (f = fopen(fullPath, "rt")) )
  195.         {              
  196.                 char line[80];
  197.                
  198.                 while ( (fgets(line, 80, f) != NULL) )
  199.                 {
  200.                         char* lineptr = line;
  201.                         lineptr = trimString(lineptr);
  202.                        
  203.                         if (lineptr != NULL) {                         
  204.                                 //Ignore category lines
  205.                                 if (lineptr[0] != '[')
  206.                                 {
  207.                                         //Check if it has a = delimiter first
  208.                                         int i;
  209.                                         for (i = 0; i < 80; i++) {
  210.                                                 if (line[i] == '=')
  211.                                                 {                                                      
  212.                                                         //Begin line splitting
  213.                                                         char* half;
  214.                                                         if ( (half = strsep(&lineptr, "=")) != NULL)
  215.                                                         {
  216.                                                                 //first half
  217.                                                                 char* fhalf = half;
  218.                                                                
  219.                                                                 if ( (half = strsep(&lineptr, "=")) != NULL) {
  220.                                                                         //Second half
  221.                                                                         char* shalf = half;
  222.                                                                        
  223.                                                                         //Load options 
  224.                                                                         screenLoad(fhalf, shalf);
  225.                                                                         sizeLoad(fhalf, shalf);                                                                
  226.                                                                         blurLoad(fhalf, shalf);
  227.                                                                         xbrzLoad(fhalf, shalf);
  228.                                                                         languageLoad(fhalf, shalf);
  229.                                                                         autosaveLoad(fhalf, shalf);
  230.                                                                         musictypeLoad(fhalf, shalf);
  231.                                                                         musicvolumeLoad(fhalf, shalf);
  232.                                                                 }
  233.                                                         }
  234.                                                        
  235.                                                         //End
  236.                                                         i = 81;
  237.                                                 }
  238.                                         }                                      
  239.                                 }
  240.                         }
  241.                 }
  242.                 fclose(f);
  243.         }
  244.        
  245. }
  246.  
  247. //Build file path
  248. /*
  249. char* getFileLocation()
  250. {      
  251.         char fullPath[128];
  252.         strcpy(fullPath, "");
  253.         #ifdef _CIA
  254.                 strcat(fullPath, "sdmc:/3ds/HydraCastleLabyrinth/");
  255.         #endif
  256.         strcat(fullPath, "system.ini");
  257.        
  258.         return fullPath;
  259. }
  260. */
  261.  
  262. char* trimString(char* orig)
  263. {
  264.         char* output = orig;
  265.        
  266.         int i, r = 0;
  267.         for (i = 0; i < strlen(orig); i++) {
  268.                 if (orig[i] != ' ' && orig[i] != '\n' && orig[i] != '\r') {
  269.                         output[r] = orig[i];
  270.                         r++;
  271.                 }
  272.         }              
  273.        
  274.         orig[r] = 0;
  275.        
  276.         return output;
  277. }
  278.  
  279. void screenLoad(char* first, char* second)
  280. {
  281.         #ifdef _3DS
  282.         if (strcmp(first, "screen") == 0) {
  283.                 if (strcmp(second, "top") == 0) {
  284.                         swapScreen(GFX_TOP, GFX_LEFT);
  285.                 }
  286.                 if (strcmp(second, "bottom") == 0) {
  287.                         swapScreen(GFX_BOTTOM, GFX_LEFT);
  288.                 }
  289.         }
  290.         #endif
  291. }
  292.  
  293. void sizeLoad(char* first, char* second)
  294. {
  295.         #ifdef _PSP
  296.         if (strcmp(first, "size") == 0) {
  297.                 if (second[0] == '0') {
  298.                         //fprintf(debug, "\nsize is 0");
  299.                         setScreenSize(0);
  300.                 }
  301.                 if (second[0] == '1') {
  302.                         //fprintf(debug, "\nsize is 1");
  303.                         setScreenSize(1);
  304.                 }
  305.                 if (second[0] == '2') {
  306.                         //fprintf(debug, "\nsize is 2");
  307.                         setScreenSize(2);
  308.                 }
  309.         }
  310.         #endif
  311. }
  312.  
  313. void blurLoad(char* first, char* second)
  314. {
  315.         #ifdef _PSP
  316.         if (strcmp(first, "blur") == 0) {
  317.                 if (strcmp(second, "on") == 0) {
  318.                         //fprintf(debug, "\nblur is on");
  319.                         //oslSetBilinearFilter(1);
  320.                         setBlur(1);
  321.                 }
  322.                 if (strcmp(second, "off") == 0) {
  323.                         //fprintf(debug, "\nblur is off");
  324.                         //oslSetBilinearFilter(0);
  325.                         setBlur(0);
  326.                 }
  327.         }
  328.         #endif
  329. }
  330.  
  331. void xbrzLoad(char* first, char* second)
  332. {
  333.         #ifdef _SDL
  334.         if (strcmp(first, "xbrz") == 0) {
  335.                 if (strcmp(second, "on") == 0) {
  336.                         setXBRZ(1);
  337.                 }
  338.                 if (strcmp(second, "off") == 0) {
  339.                         setXBRZ(0);
  340.                 }
  341.         }
  342.         #endif
  343. }
  344.  
  345. void languageLoad(char* first, char* second)
  346. {
  347.         if (strcmp(first, "language") == 0) {
  348.                 if (strcmp(second, "en") == 0) {
  349.                         setLanguage(ENGLISH);
  350.                 }
  351.                 if (strcmp(second, "jp") == 0) {
  352.                         setLanguage(JAPANESE);
  353.                 }
  354.         }
  355. }
  356.  
  357. void autosaveLoad(char* first, char* second)
  358. {
  359.         if (strcmp(first, "autosave") == 0) {
  360.                 if (strcmp(second, "on") == 0) {
  361.                         //fprintf(debug, "\nautosave is on");
  362.                         setAutoSave(1);
  363.                 }
  364.                 if (strcmp(second, "off") == 0) {
  365.                         //fprintf(debug, "\nautosave is off");
  366.                         setAutoSave(0);
  367.                 }
  368.         }
  369. }
  370.  
  371. void musicvolumeLoad(char* first, char* second)
  372. {
  373.         #ifdef _SDL
  374.         if (strcmp(first, "music") == 0) {
  375.                 if (second[0] >= '0' && second[0] <= '4') {
  376.                         music_volume = second[0]-'0';
  377.                         PHL_MusicVolume(0.25f * music_volume);
  378.                 }
  379.         }
  380.         #endif
  381. }
  382.  
  383. void musictypeLoad(char* first, char* second)
  384. {
  385.         #ifdef _SDL
  386.         if (strcmp(first, "music_type") == 0) {
  387.                 if (strcmp(second, "ogg") == 0) {
  388.                         setMusicType(1);
  389.                 }
  390.                 if (strcmp(second, "midi") == 0) {
  391.                         setMusicType(0);
  392.                 }
  393.         }
  394.         #endif
  395. }
  396.