Example #1
0
void
PingusLevel::load(const std::string& resname,
                  const Pathname& pathname)
{
  impl->checksum = System::checksum(pathname);

  impl->resname = resname;
  ReaderObject reader_object = FileReader::parse(pathname);

  if (reader_object.get_name() != "pingus-level")
  {
    raise_exception(std::runtime_error, "Error: " << pathname.str() << ": not a 'pingus-level' file");
  }
  else
  {
    ReaderMapping reader = reader_object.get_mapping();

    int version;
    if (reader.read_int("version", version))
      log_info("Levelfile Version: %1%", version);
    else
      log_info("Unknown Levelfile Version: %1%", version);

    ReaderMapping head;
    if (!reader.read_mapping("head", head))
    {
      raise_exception(std::runtime_error, "Error: (head) section not found in '" << pathname.str() << "'");
    }
    else
    {
      log_info("Reading head");
      head.read_string("levelname",        impl->levelname);
      head.read_string("description",      impl->description);
      head.read_size  ("levelsize",        impl->size);
      head.read_string("music",            impl->music);
      head.read_int   ("time",             impl->time);
      head.read_int   ("number-of-pingus", impl->number_of_pingus);
      head.read_int   ("number-to-save",   impl->number_to_save);
      head.read_colorf("ambient-light",    impl->ambient_light);
      head.read_string("author",           impl->author);

      log_info("Size: %1%x%2%", impl->size.width, impl->size.height);

      ReaderMapping actions;
      if (head.read_mapping("actions", actions))
      {
        std::vector<std::string> lst = actions.get_keys();
        for(std::vector<std::string>::iterator i = lst.begin(); i != lst.end(); ++i)
        {
          int count = 0;
          log_info("Actions: %1%", i->c_str());
          if (actions.read_int(i->c_str(), count))
            impl->actions[*i] = count;
        }
      }
      else
      {
        raise_exception(std::runtime_error,
                        "Error: (pingus-level head actions) not found in '" << pathname.str() << "'");
      }
    }

    ReaderCollection collection;
    if (reader.read_collection("objects", collection))
    {
      std::vector<ReaderObject> object_lst = collection.get_objects();
      for(auto i = object_lst.begin(); i != object_lst.end(); ++i)
      {
        impl->objects.push_back(*i);
      }
    }
  }
}