Optmized code for reading level's name. Instead of loading the all levels, just parse...
authorRicardo Cruz <rick2@aeiou.pt>
Sat, 17 Jul 2004 13:14:20 +0000 (13:14 +0000)
committerRicardo Cruz <rick2@aeiou.pt>
Sat, 17 Jul 2004 13:14:20 +0000 (13:14 +0000)
Both worldmap loading and contrib menu generating were optmized.
I couldn't time the worldmap loading speedup, since it was already instantaneously in my machine. But the contrib menu takes less than 1 sec versus the 4 seconds (and a few ms) of the old code.

SVN-Revision: 1585

src/title.cpp
src/worldmap.cpp

index 5d19520..e046e9c 100644 (file)
@@ -144,10 +144,20 @@ void check_levels_contrib_menu()
               
       for (int i = 0; i < subset.get_num_levels(); ++i)
         {
-        Level* level = new Level;
-        level->load(subset.get_level_filename(i));
-        contrib_subset_menu->additem(MN_ACTION, level->get_name(), 0, 0, i);
-        delete level;
+        /** get level's title */
+        std::string level_title = "<no title>";
+
+        LispReader* reader = LispReader::load(subset.get_level_filename(i), "supertux-level");
+        if(!reader)
+          {
+          std::cerr << "Error: Could not open level file. Ignoring...\n";
+          return;
+          }
+
+        reader->read_string("name", level_title, true);
+        delete reader;
+
+        contrib_subset_menu->additem(MN_ACTION, level_title, 0, 0, i);
         }
 
       contrib_subset_menu->additem(MN_HL,"",0,0);      
index c402150..edbcc06 100644 (file)
@@ -489,33 +489,15 @@ void WorldMap::get_level_title(Level& level)
   /** get level's title */
   level.title = "<no title>";
 
-  FILE * fi;
-  lisp_object_t* root_obj = 0;
-  fi = fopen((datadir +  "/levels/" + level.name).c_str(), "r");
-  if (fi == NULL)
-  {
-    perror((datadir +  "/levels/" + level.name).c_str());
+  LispReader* reader = LispReader::load(datadir + "/levels/" + level.name, "supertux-level");
+  if(!reader)
+    {
+    std::cerr << "Error: Could not open level file. Ignoring...\n";
     return;
-  }
-
-  lisp_stream_t stream;
-  lisp_stream_init_file (&stream, fi);
-  root_obj = lisp_read (&stream);
-
-  if (root_obj->type == LISP_TYPE_EOF || root_obj->type == LISP_TYPE_PARSE_ERROR)
-  {
-    printf("World: Parse Error in file %s", level.name.c_str());
-  }
-
-  if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-level") == 0)
-  {
-    LispReader reader(lisp_cdr(root_obj));
-    reader.read_string("name", level.title, true);
-  }
-
-  lisp_free(root_obj);
+    }
 
-  fclose(fi);
+  reader->read_string("name", level.title, true);
+  delete reader;
 }
 
 void