//
// only call this function immediately after GetOverlayList()
// which will ensure the list of texture id's are valid
//
int gstTextureManager::prepareTexture(TexTile& tile,
                                      const gstTextureGuard &tex) {
  // if no secondary textures have been loaded, return immediately
  if (getCurrTexID() == 0)
    return -1;

  tile.src = tex->id();

  // determine what is the best tile that exists in our db
  TileExistance* te = tile_existance_cache_->fetch(tile.addr());

  assert(te != NULL);

  // no texture available
  if (te->bestAvailable == 0)
    return -1;


  // now try to get this best one, but take anything less as long
  // as we can get something immediately
  // this will fill our read queue with everything from this tile
  // all the way up the stack until we've found a tile

  requested_level_ = LEVFROMADDR(te->bestAvailable);
  while (tile.lev > requested_level_)
    tile.upLevel();

  uint texid;
  TextureCache* textureCache = tile.alpha() == 0 ? overlay_RGB_texture_cache_ :
                               overlay_alpha_texture_cache_;
  while ((texid = textureCache->fetchAndPin(tile.addr())) == 0) {
    if (tile.upLevel() == -1)
      return -1;
  }

  // we found one, so let's load it up!
  glBindTexture(GL_TEXTURE_2D, texid);

  return tile.lev;
}