From: Ricardo Cruz Date: Sat, 17 Jul 2004 13:14:20 +0000 (+0000) Subject: Optmized code for reading level's name. Instead of loading the all levels, just parse... X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=7ecce94a3fb2831c4962c941c53e1d522c8c1dcd;p=supertux.git Optmized code for reading level's name. Instead of loading the all levels, just parses now the title. 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 --- diff --git a/src/title.cpp b/src/title.cpp index 5d195206b..e046e9c1c 100644 --- a/src/title.cpp +++ b/src/title.cpp @@ -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 = ""; + + 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); diff --git a/src/worldmap.cpp b/src/worldmap.cpp index c4021503f..edbcc0683 100644 --- a/src/worldmap.cpp +++ b/src/worldmap.cpp @@ -489,33 +489,15 @@ void WorldMap::get_level_title(Level& level) /** get level's title */ level.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