static flash_entry_header_t *find_entry(flash_file_token_t token) { for (int s = 0; sector_map[s].address; s++) { h_magic_t *pmagic = (h_magic_t *) sector_map[s].address; h_magic_t *pe = pmagic + (sector_map[s].size / sizeof(h_magic_t)) - 1; /* Hunt for Magic Signature */ cont: while (pmagic != pe && !valid_magic(pmagic)) { pmagic++; } /* Did we reach the end * if so try the next sector */ if (pmagic == pe) { continue; } /* Found a magic So assume it is a file header */ flash_entry_header_t *pf = (flash_entry_header_t *) pmagic; /* Test the CRC */ if (pf->crc == crc32(entry_crc_start(pf), entry_crc_length(pf))) { /* Good CRC is it the one we are looking for ?*/ if (valid_entry(pf) && pf->file_token.t == token.t) { return pf; } else { /* Not the one we wanted but we can trust the size */ pf = next_entry(pf); pmagic = (h_magic_t *) pf; /* If the next one is erased */ if (blank_entry(pf)) { continue; } } goto cont; } else { /* in valid CRC so keep looking */ pmagic++; } } return NULL; }
bool Tileset::New(const QString& img_filename, bool one_image) { _initialized = false; // Retrieve the tileset name from the image filename tileset_name = CreateTilesetName(img_filename); // Prepare the tile vector and load the tileset image if (one_image == true) { tiles.clear(); tiles.resize(1); tiles[0].SetDimensions(16.0f, 16.0f); if (tiles[0].Load(string(img_filename.toAscii()), 16, 16) == false) { qDebug("Failed to load tileset image: %s", img_filename.toStdString().c_str()); return false; } } else { tiles.clear(); tiles.resize(256); for (uint32 i = 0; i < 256; i++) tiles[i].SetDimensions(1.0f, 1.0f); if (ImageDescriptor::LoadMultiImageFromElementGrid(tiles, string(img_filename.toAscii()), 16, 16) == false) { qDebug("Failed to load tileset image: %s", img_filename.toStdString().c_str()); return false; } } // Initialize the rest of the tileset data vector<int32> blank_entry(4, 0); for (uint32 i = 0; i < 16; i++) for (uint32 j = 0; j < 16; j++) walkability.insert(make_pair(i * 16 + j, blank_entry)); autotileability.clear(); _animated_tiles.clear(); _initialized = true; return true; } // Tileset::New(...)
static flash_entry_header_t *find_free(data_size_t required) { for (int s = 0; sector_map[s].address; s++) { h_magic_t *pmagic = (h_magic_t *) sector_map[s].address; h_magic_t *pe = pmagic + (sector_map[s].size / sizeof(h_magic_t)) - 1; /* Hunt for Magic Signature */ do { if (valid_magic(pmagic)) { flash_entry_header_t *pf = (flash_entry_header_t *) pmagic; /* Test the CRC */ if (pf->crc == crc32(entry_crc_start(pf), entry_crc_length(pf))) { /* Valid Magic and CRC look for the next record*/ pmagic = ((uint32_t *) next_entry(pf)); } else { pmagic++; } } if (blank_magic(pmagic)) { flash_entry_header_t *pf = (flash_entry_header_t *) pmagic; if (blank_entry(pf) && blank_check(pf, required)) { return pf; } } } while (++pmagic != pe); } return NULL; }
bool Tileset::New(const QString &img_filename, const QString& root_folder, bool one_image) { if (img_filename.isEmpty()) return false; _initialized = false; // Retrieve the tileset name from the image filename _tileset_name = CreateTilesetName(img_filename); _tileset_definition_filename = "data/tilesets/" + _tileset_name + ".lua"; // Check existence of a previous lua definition file if (QFile::exists(_tileset_definition_filename)) { _tileset_definition_filename.clear(); _tileset_name.clear(); qDebug("Failed to create tileset, it already exists: %s", _tileset_definition_filename.toStdString().c_str()); return false; } _tileset_image_filename = "data/tilesets/" + _tileset_name + img_filename.mid(img_filename.length() - 4, 4); // Prepare the tile vector and load the tileset image tiles.clear(); tiles.resize(256); QRect rectangle; QImage entire_tileset; QString tileset_full_path = root_folder + _tileset_image_filename; if (!entire_tileset.load(tileset_full_path, "png")) { qDebug("Failed to load tileset image: %s", tileset_full_path.toStdString().c_str()); return false; } if (one_image) { tiles[0].convertFromImage(entire_tileset); } else { for(uint32_t row = 0; row < num_rows; ++row) { for(uint32_t col = 0; col < num_cols; ++col) { rectangle.setRect(col * TILE_WIDTH, row * TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT); QImage tile = entire_tileset.copy(rectangle); if(!tile.isNull()) { // linearize the tile index uint32_t i = num_rows * row + col; tiles[i].convertFromImage(tile); } else { qDebug("Image loading error!"); } } } } // Initialize the rest of the tileset data std::vector<int32_t> blank_entry(4, 0); for(uint32_t i = 0; i < 16; ++i) for(uint32_t j = 0; j < 16; ++j) walkability.insert(std::make_pair(i * 16 + j, blank_entry)); autotileability.clear(); _animated_tiles.clear(); _initialized = true; return true; } // Tileset::New(...)