Added worldmap choosing from command-line and menu.
authorRicardo Cruz <rick2@aeiou.pt>
Fri, 9 Jul 2004 14:43:44 +0000 (14:43 +0000)
committerRicardo Cruz <rick2@aeiou.pt>
Fri, 9 Jul 2004 14:43:44 +0000 (14:43 +0000)
Contributed maps must be in data/levels/worldmap, included the ones run from the command-line.
It is possible to feed maps with absolute path, but it is just a hack that converts them to relative paths to the worldmap folder. I did this, in order to make it possible for someone to add a mime-type for Konqueror as 'supertux --worldmap' and press a .stwt file from worldmap dir.

About the Contrib Worlds menu, we might consider just making one Contrib menu, and make levels from different folders to create dynamically a map.
Till that day, let's keep this menu.

SVN-Revision: 1555

src/globals.cpp
src/globals.h
src/menu.cpp
src/menu.h
src/setup.cpp
src/supertux.cpp
src/title.cpp
src/worldmap.cpp
src/worldmap.h

index 6a4aaab..80775fa 100644 (file)
@@ -58,6 +58,7 @@ float game_speed = 1.0f;
 int joystick_num = 0;
 char* level_startup_file = 0;
 bool launch_leveleditor_mode = false;
+bool launch_worldmap_mode = false;
 
 /* SuperTux directory ($HOME/.supertux) and save directory($HOME/.supertux/save) */
 char *st_dir, *st_save_dir;
index e2c4fff..329c4a1 100644 (file)
@@ -69,6 +69,7 @@ extern bool show_fps;
 extern int joystick_num;
 extern char* level_startup_file;
 extern bool launch_leveleditor_mode;
+extern bool launch_worldmap_mode;
 
 /* SuperTux directory ($HOME/.supertux) and save directory($HOME/.supertux/save) */
 extern char* st_dir;
index a4e7ceb..d0eb6a8 100644 (file)
@@ -59,6 +59,7 @@ Menu* options_joystick_menu = 0;
 Menu* highscore_menu = 0;
 Menu* load_game_menu = 0;
 Menu* save_game_menu = 0;
+Menu* contrib_worldmap_menu   = 0;
 Menu* contrib_menu   = 0;
 Menu* contrib_subset_menu   = 0;
 
index a3b99e0..8957f10 100644 (file)
@@ -33,7 +33,8 @@
 
 enum MainMenuIDs {
   MNID_STARTGAME,
-  MNID_CONTRIB,
+  MNID_WORLDMAP_CONTRIB,
+  MNID_LEVELS_CONTRIB,
   MNID_OPTIONMENU,
   MNID_LEVELEDITOR,
   MNID_CREDITS,
@@ -235,6 +236,7 @@ extern Surface* back;
 extern Surface* arrow_left;
 extern Surface* arrow_right;
 
+extern Menu* contrib_worldmap_menu;
 extern Menu* contrib_menu;
 extern Menu* contrib_subset_menu;
 extern Menu* main_menu;
index 5421cef..f8c63c9 100644 (file)
@@ -383,12 +383,14 @@ void st_menu(void)
   game_menu      = new Menu();
   highscore_menu = new Menu();
   contrib_menu   = new Menu();
+  contrib_worldmap_menu = new Menu();
   contrib_subset_menu   = new Menu();
   worldmap_menu  = new Menu();
 
   main_menu->set_pos(screen->w/2, 335);
   main_menu->additem(MN_GOTO, _("Start Game"),0,load_game_menu, MNID_STARTGAME);
-  main_menu->additem(MN_GOTO, _("Contrib Levels"),0,contrib_menu, MNID_CONTRIB);
+  main_menu->additem(MN_GOTO, _("Contrib Worlds"),0,contrib_worldmap_menu, MNID_WORLDMAP_CONTRIB);
+  main_menu->additem(MN_GOTO, _("Contrib Levels"),0,contrib_menu, MNID_LEVELS_CONTRIB);
   main_menu->additem(MN_GOTO, _("Options"),0,options_menu, MNID_OPTIONMENU);
   main_menu->additem(MN_ACTION, _("Level Editor"),0,0, MNID_LEVELEDITOR);
   main_menu->additem(MN_ACTION, _("Credits"),0,0, MNID_CREDITS);
@@ -641,6 +643,7 @@ void st_general_free(void)
   delete options_keys_menu;
   delete options_joystick_menu;
   delete highscore_menu;
+  delete contrib_worldmap_menu;
   delete contrib_menu;
   delete contrib_subset_menu;
   delete worldmap_menu;
@@ -988,6 +991,10 @@ void parseargs(int argc, char * argv[])
         {
           launch_leveleditor_mode = true;
         }
+      else if (strcmp(argv[i], "--worldmap") == 0)
+        {
+          launch_worldmap_mode = true;
+        }
       else if (strcmp(argv[i], "--datadir") == 0 
                || strcmp(argv[i], "-d") == 0 )
         {
@@ -1065,7 +1072,8 @@ void parseargs(int argc, char * argv[])
                "  -j, --joystick NUM  Use joystick NUM (default: 0)\n" 
                "  --joymap XAXIS:YAXIS:A:B:START\n"
                "                      Define how joystick buttons and axis should be mapped\n"
-               "  --leveleditor       Opens the leveleditor in a file. (Only works when a file is provided.)\n"
+               "  --leveleditor       Opens the leveleditor in a file.\n"
+               "  --worldmap          Opens the specified worldmap file.\n"
                "  -d, --datadir DIR   Load Game data from DIR (default: automatic)\n"
                "  --debug             Enables the debug mode, which is useful for developers.\n"
                "  --help              Display a help message summarizing command-line\n"
@@ -1106,7 +1114,7 @@ void usage(char * prog, int ret)
 
   /* Display the usage message: */
 
-  fprintf(fi, _("Usage: %s [--fullscreen] [--opengl] [--disable-sound] [--disable-music] [--debug] | [--usage | --help | --version] [--leveleditor] FILENAME\n"),
+  fprintf(fi, _("Usage: %s [--fullscreen] [--opengl] [--disable-sound] [--disable-music] [--debug] | [--usage | --help | --version] [--leveleditor] [--worldmap] FILENAME\n"),
           prog);
 
 
index 7c46b74..00fd2e1 100644 (file)
@@ -67,6 +67,18 @@ int main(int argc, char * argv[])
       else
         leveleditor.run();
     }
+    else if (launch_worldmap_mode && level_startup_file)
+    {
+      // hack to make it possible for someone to give an absolute path
+      std::string str(level_startup_file);
+      unsigned int i = str.find_last_of("/", str.size());
+      if(i != std::string::npos)
+        str.erase(0, i+1);
+
+      WorldMapNS::WorldMap worldmap;
+      worldmap.loadmap(str);
+      worldmap.display();
+    }
     else if (level_startup_file)
     {
       GameSession session(level_startup_file, ST_GL_LOAD_LEVEL_FILE);
index e173d36..8be58de 100644 (file)
@@ -45,6 +45,7 @@
 #include "level.h"
 #include "level_subset.h"
 #include "gameloop.h"
+#include "worldmap.h"
 #include "leveleditor.h"
 #include "scene.h"
 #include "player.h"
@@ -53,6 +54,7 @@
 #include "sector.h"
 #include "tilemap.h"
 #include "resources.h"
+#include "type.h"
 #include "gettext.h"
 
 static Surface* bkg_title;
@@ -71,6 +73,8 @@ static GameSession* titlesession;
 static std::vector<LevelSubset*> contrib_subsets;
 static LevelSubset* current_contrib_subset = 0;
 
+static string_list_type worldmap_list;
+
 static LevelEditor* leveleditor;
 
 void free_contrib_menu()
@@ -85,6 +89,7 @@ void free_contrib_menu()
 
 void generate_contrib_menu()
 {
+  /** Generating contrib levels list by making use of Level Subset */
   string_list_type level_subsets = dsubdirs("/levels", "info");
 
   free_contrib_menu();
@@ -107,7 +112,7 @@ void generate_contrib_menu()
   string_list_free(&level_subsets);
 }
 
-void check_contrib_menu()
+void check_levels_contrib_menu()
 {
   static int current_subset = -1;
 
@@ -172,6 +177,19 @@ void check_contrib_subset_menu()
     }  
 }
 
+void check_contrib_worldmap_menu()
+{
+  int index = contrib_worldmap_menu->check();
+  if (index != -1)
+    {
+      WorldMapNS::WorldMap worldmap;
+      worldmap.loadmap(worldmap_list.item[index]);
+      worldmap.display();
+
+      Menu::set_current(main_menu);
+    }
+}
+
 void draw_demo(double frame_ratio)
 {
   Sector* world  = titlesession->get_current_sector();
@@ -233,6 +251,19 @@ void title(void)
   logo = new Surface(datadir + "/images/title/logo.png", USE_ALPHA);
   img_choose_subset = new Surface(datadir + "/images/status/choose-level-subset.png", USE_ALPHA);
 
+  /* Generating contrib maps by only using a string_list */
+  worldmap_list = dfiles("levels/worldmap", NULL, NULL);
+
+  contrib_worldmap_menu->additem(MN_LABEL, _("Contrib Worlds"), 0,0);
+  contrib_worldmap_menu->additem(MN_HL, "", 0,0);
+  for(int i = 0; i < worldmap_list.num_items; i++)
+    contrib_worldmap_menu->additem(MN_ACTION, worldmap_list.item[i],0,0,i);
+  contrib_worldmap_menu->additem(MN_HL,"",0,0);
+  contrib_worldmap_menu->additem(MN_BACK,"Back",0,0);
+
+  titlesession->get_current_sector()->activate();
+  titlesession->set_current();
+
   /* --- Main title loop: --- */
   frame = 0;
 
@@ -295,7 +326,9 @@ void title(void)
                   // Start Game, ie. goto the slots menu
                   update_load_save_game_menu(load_game_menu);
                   break;
-                case MNID_CONTRIB:
+                case MNID_WORLDMAP_CONTRIB:
+                  break;
+                case MNID_LEVELS_CONTRIB:
                   // Contrib Menu
                   puts("Entering contrib menu");
                   generate_contrib_menu();
@@ -350,12 +383,16 @@ void title(void)
             }
           else if(menu == contrib_menu)
             {
-              check_contrib_menu();
+              check_levels_contrib_menu();
             }
           else if (menu == contrib_subset_menu)
             {
               check_contrib_subset_menu();
             }
+          else if(menu == contrib_worldmap_menu)
+            {
+              check_contrib_worldmap_menu();
+            }
         }
 
       mouse_cursor->draw(context);
index c122184..546db1e 100644 (file)
@@ -378,12 +378,12 @@ WorldMap::~WorldMap()
 void
 WorldMap::load_map()
 {
+  std::cout << "Loading map: " << datadir + "/levels/worldmap/" + map_filename << std::endl;
+
   lisp_object_t* root_obj = lisp_read_from_file(datadir + "/levels/worldmap/" + map_filename);
   if (!root_obj)
     st_abort("Couldn't load file", datadir + "/levels/worldmap/" + map_filename);
 
-  std::cout << "Loading map: " << datadir + "/levels/worldmap/" + map_filename << std::endl;
-
   if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-worldmap") == 0)
     {
       lisp_object_t* cur = lisp_cdr(root_obj);
@@ -960,6 +960,9 @@ WorldMap::display()
 void
 WorldMap::savegame(const std::string& filename)
 {
+  if(filename != "")
+    return;
+
   std::cout << "savegame: " << filename << std::endl;
   std::ofstream out(filename.c_str());
 
@@ -1087,6 +1090,14 @@ WorldMap::loadgame(const std::string& filename)
   lisp_free(savegame);
 }
 
+void
+WorldMap::loadmap(const std::string& filename)
+{
+  savegame_file = "";
+  map_filename = filename;
+  load_map();
+}
+
 } // namespace WorldMapNS
 
 /* Local Variables: */
index 7a85f0f..2186c49 100644 (file)
@@ -192,8 +192,13 @@ public:
       if possible, write the new position to \a new_pos */
   bool path_ok(Direction direction, Vector pos, Vector* new_pos);
 
+  /* Save map to slot */
   void savegame(const std::string& filename);
+  /* Load map from slot */
   void loadgame(const std::string& filename);
+  /* Load map directly from file */
+  void loadmap(const std::string& filename);
+
 private:
   void on_escape_press();
 };