コード例 #1
0
ファイル: Tileset.cpp プロジェクト: akimi634/solarus
/**
 * \brief Loads the tileset from its file by creating all tile patterns.
 */
void Tileset::load() {

  // Load the tileset data file.
  std::string file_name = std::string("tilesets/") + id + ".dat";
  TilesetData data;
  bool success = data.import_from_quest_file(file_name);
  if (success) {
    // Get the imported data.
    this->background_color = data.get_background_color();
    for (const auto& kvp : data.get_patterns()) {
      add_tile_pattern(kvp.first, kvp.second);
    }
  }

  // Load the tileset images.
  file_name = std::string("tilesets/") + id + ".tiles.png";
  tiles_image = Surface::create(file_name, Surface::DIR_DATA);
  if (tiles_image == nullptr) {
    Debug::error(std::string("Missing tiles image for tileset '") + id + "': " + file_name);
    tiles_image = Surface::create(16, 16);
  }

  file_name = std::string("tilesets/") + id + ".entities.png";
  entities_image = Surface::create(file_name, Surface::DIR_DATA);
  if (entities_image == nullptr) {
    Debug::error(std::string("Missing entities image for tileset '") + id + "': " + file_name);
    entities_image = Surface::create(16, 16);
  }
}
コード例 #2
0
ファイル: Tileset.cpp プロジェクト: ziz/solarus
/**
 * @brief Loads the tileset from its file by creating all tile patterns.
 */
void Tileset::load() {

  // compute the file name, depending on the id
  std::ostringstream oss;
  oss << "tilesets/tileset" << std::setfill('0') << std::setw(4) << id << ".dat";

  // open the tileset file
  std::string file_name = oss.str();
  std::istream &tileset_file = FileTools::data_file_open(file_name);

  // parse the tileset file
  std::string line;

  // first line: tileset general info
  if (!std::getline(tileset_file, line)) {
    Debug::die(StringConcat() << "Empty file '" << file_name << "'");
  }

  int r, g, b;

  std::istringstream iss(line);
  FileTools::read(iss, r);
  FileTools::read(iss, g);
  FileTools::read(iss, b);
  background_color = Color(r, g, b);

  // read the tile patterns
  int tile_pattern_id, animation, obstacle, default_layer;
  while (std::getline(tileset_file, line)) {

    iss.str(line);
    iss.clear();
    FileTools::read(iss, tile_pattern_id);
    FileTools::read(iss, animation);
    FileTools::read(iss, obstacle);
    FileTools::read(iss, default_layer);

    int width, height;

    if (animation != 1) { // simple tile pattern, parallax scrolling or scrolling

      int x, y;

      FileTools::read(iss, x);
      FileTools::read(iss, y);
      FileTools::read(iss, width);
      FileTools::read(iss, height);

      TilePattern *pattern = NULL;
      if (animation == 0) {
	pattern = new SimpleTilePattern(Obstacle(obstacle), x, y, width, height);
      }
      else if (animation == 2) {
	pattern = new ParallaxTilePattern(Obstacle(obstacle), x, y, width, height);
      }
      else if (animation == 3) {
        pattern = new ScrollingTilePattern(Obstacle(obstacle), x, y, width, height);
      }
      else {
	Debug::die(StringConcat() << "Unknown tile pattern animation: " << animation);
      }
      add_tile_pattern(tile_pattern_id, pattern);
    }
    else { // animated tile pattern
      int sequence, x1, y1, x2, y2, x3, y3;

      FileTools::read(iss, sequence);
      FileTools::read(iss, width);
      FileTools::read(iss, height);
      FileTools::read(iss, x1);
      FileTools::read(iss, y1);
      FileTools::read(iss, x2);
      FileTools::read(iss, y2);
      FileTools::read(iss, x3);
      FileTools::read(iss, y3);
      add_tile_pattern(tile_pattern_id, new AnimatedTilePattern(Obstacle(obstacle),
	    AnimatedTilePattern::AnimationSequence(sequence),
	    width, height, x1, y1, x2, y2, x3, y3));
    }
  }
  FileTools::data_file_close(tileset_file);

  // load the tileset images
  oss.str("");
  oss << "tilesets/tileset" << std::setfill('0') << std::setw(4) << id << "_tiles.png";
  tiles_image = new Surface(oss.str(), Surface::DIR_DATA);

  oss.str("");
  oss << "tilesets/tileset" << std::setfill('0') << std::setw(4) << id << "_entities.png";
  entities_image = new Surface(oss.str(), Surface::DIR_DATA);
}