Store user data in $HOME
authorMiriam Ruiz <miriam@debian.org>
Wed, 3 Dec 2008 11:08:48 +0000 (3 12:08 +0100)
committerMiriam Ruiz <miriam@debian.org>
Wed, 3 Dec 2008 11:08:48 +0000 (3 12:08 +0100)
src/gamemenu.cpp

index 6f6e6ab..f74db67 100644 (file)
@@ -16,6 +16,9 @@
 #include "gamemenu.h" // I need my prototypes
 #include "sound.h" // I need the sound system
 
+#include <stdlib.h> // getenv
+#include <stdio.h> // snprintf
+
 // some globals needed by callback
 CParticleManager menu_particle_manager; // our own particle managar :^D -- REMEMBER TO CALL NUKE PARTICLES BEFORE LEAVING!
 BITMAP *bmp_real_background = NULL; // menu real background
@@ -214,7 +217,19 @@ void CGameMenu::do_main_menu()
 
        while (ret != 4) // 4 = quit
        {
-               set_config_file("kball.cfg");
+               char str[4096], *home = getenv("HOME");
+               if (home != NULL)
+               {
+                       snprintf(str, sizeof(str), "%s/.kball", home);
+                       mkdir(str, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
+                       snprintf(str, sizeof(str), "%s/.kball/kball.cfg", home);
+                       set_config_file(str);
+               }
+               else
+               {
+                       set_config_file("kball.cfg");
+               }
+               
                menu.control.load_configuration_of_controller("KBALL_CONTROLLER");
                
                soundw.music_resume();
@@ -298,7 +313,18 @@ void CGameMenu::do_file_level_selector()
 
        mtracer.add("CGameMenu::do_file_level_selector() started\n");
 
-       set_config_file("kball.cfg");
+       char str[4096], *home = getenv("HOME");
+       if (home != NULL)
+       {
+               snprintf(str, sizeof(str), "%s/.kball", home);
+               mkdir(str, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
+               snprintf(str, sizeof(str), "%s/.kball/kball.cfg", home);
+               set_config_file(str);
+       }
+       else
+       {
+               set_config_file("kball.cfg");
+       }
        menu.control.load_configuration_of_controller("KBALL_CONTROLLER");
 
        menu.set_to_bitmap(screen);
@@ -320,12 +346,34 @@ void CGameMenu::do_file_level_selector()
        menu.clear_menu_list();
        menu.add_item_to_menu("Full campaign");
        menu.add_item_to_menu("Single level");
+       
+       int personal_levels;
+       personal_levels = 0;
+       if (home != NULL)
+       {
+               char t[2048];
+               usprintf(t, "%s/.kball/levels", home);
+               if (file_exists(t, (FA_RDONLY | FA_HIDDEN | FA_SYSTEM | FA_LABEL | FA_DIREC), NULL))
+               {
+                       menu.add_item_to_menu("Personal levels");
+                       personal_levels = 1;
+               }
+       }
+       else
+       {
+               if (file_exists("levels", (FA_RDONLY | FA_HIDDEN | FA_SYSTEM | FA_LABEL | FA_DIREC), NULL))
+               {
+                       menu.add_item_to_menu("Personal levels");
+                       personal_levels = 1;
+               }
+       }
        menu.add_item_to_menu("< Cancel and return >");
 
        // get level
-       get_executable_name(level_path, 2048);
-       replace_filename(level_path, level_path, "", 2048);
-       append_filename(level_path, level_path, "levels", 2048);
+       //get_executable_name(level_path, 2048);
+       //replace_filename(level_path, level_path, "", 2048);
+       //append_filename(level_path, level_path, "levels", 2048);
+       usprintf(level_path, "/usr/share/games/kball/levels");
 
        fix_filename_slashes(level_path);
        mtracer.add("--> \t Level path == %s", level_path);
@@ -348,6 +396,26 @@ void CGameMenu::do_file_level_selector()
                full_campaign = false;
                break;
 
+               case 2:
+               if (personal_levels == 0)
+               {
+                       soundw.music_pause();
+                       return ;
+               }               
+               soundw.music_pause();
+               if (home != NULL)
+               {
+                       usprintf(path_str, "%s/.kball/levels/*", home);
+                       usprintf(level_path, "%s/.kball/levels", home);
+               }
+               else
+               {
+                       usprintf(path_str, "levels/*");
+                       usprintf(level_path, "levels");
+               }
+               full_campaign = false;
+               break;
+
                default:
                soundw.music_pause();
                return ;
@@ -372,7 +440,9 @@ void CGameMenu::do_file_level_selector()
        menu.clear_menu_list();
 
        // read files and populate menu with them
-       ret = al_findfirst(path_str, &dir_reader, FA_ARCH);
+       //ret = al_findfirst(path_str, &dir_reader, FA_ARCH);
+       mtracer.add("--> \t Path string to search for levels == '%s'", path_str);
+       ret = al_findfirst(path_str, &dir_reader,(FA_RDONLY | FA_HIDDEN | FA_SYSTEM | FA_LABEL | FA_ARCH));
 
        while (!ret)
        {
@@ -413,7 +483,18 @@ void CGameMenu::do_file_level_selector()
        fix_filename_slashes(path_str);
 
        // play the game - rock 'n roll!
-       set_config_file("kball.cfg");
+       home = getenv("HOME");
+       if (home != NULL)
+       {
+               snprintf(str, sizeof(str), "%s/.kball", home);
+               mkdir(str, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
+               snprintf(str, sizeof(str), "%s/.kball/kball.cfg", home);
+               set_config_file(str);
+       }
+       else
+       {
+               set_config_file("kball.cfg");
+       }
 
        game_kernel.player_ball.control.load_configuration_of_controller("KBALL_CONTROLLER");
 
@@ -561,7 +642,19 @@ void CGameMenu::do_options_menu()
        int ret = 0;
        mtracer.add("CGameMenu::do_options_menu() started\n");
 
-       set_config_file("kball.cfg");
+       char str[4096], *home = getenv("HOME");
+       if (home != NULL)
+       {
+               snprintf(str, sizeof(str), "%s/.kball", home);
+               mkdir(str, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
+               snprintf(str, sizeof(str), "%s/.kball/kball.cfg", home);
+               set_config_file(str);
+       }
+       else
+       {
+               set_config_file("kball.cfg");
+       }
+       
        menu.control.load_configuration_of_controller("KBALL_CONTROLLER");
        game_kernel.player_ball.control.load_configuration_of_controller("KBALL_CONTROLLER");
 
@@ -617,7 +710,18 @@ void CGameMenu::do_options_menu()
 
                menu.add_item_to_menu("< Return >");
 
-               set_config_file("kball.cfg");
+               char str[4096], *home = getenv("HOME");
+               if (home != NULL)
+               {
+                       snprintf(str, sizeof(str), "%s/.kball", home);
+                       mkdir(str, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
+                       snprintf(str, sizeof(str), "%s/.kball/kball.cfg", home);
+                       set_config_file(str);
+               }
+               else
+               {
+                       set_config_file("kball.cfg");
+               }
 
                menu.control.load_configuration_of_controller("KBALL_CONTROLLER");
                
@@ -679,7 +783,19 @@ void CGameMenu::do_options_menu()
 
                }
 
-               set_config_file("kball.cfg");
+               home = getenv("HOME");
+               if (home != NULL)
+               {
+                       snprintf(str, sizeof(str), "%s/.kball", home);
+                       mkdir(str, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
+                       snprintf(str, sizeof(str), "%s/.kball/kball.cfg", home);
+                       set_config_file(str);
+               }
+               else
+               {
+                       set_config_file("kball.cfg");
+               }
+               
                game_kernel.player_ball.control.save_configuration_of_controller("KBALL_CONTROLLER");
        }