Beispiel #1
0
int	main(int ac, char **av)
{ 
  if (ac == 4)
    {
      try
	{
	  Game			*game = new Game(atoi(av[1]), atoi(av[2]));
	  std::list<ISnake *>	tmp;
	  std::list<IFood *>	tmpf;
	  std::list<IHole *>	tmph;
	  int			ret;
	  maker_Display		pMaker;
	  void			*hndl;
	  void			*mkr;
	  
	  srand(getpid());
	  hndl = dlopen(av[3], RTLD_LAZY);
	  if (hndl == NULL)
	    throw GameException("Error load librairie");
	  mkr = dlsym(hndl, "make_display");
	  if (mkr == NULL)
	    {
	      dlclose(hndl);
	      throw GameException("Error load function librairie");
	    }
	  pMaker = (maker_Display)mkr;
	  Display *my_graph = pMaker();
	  ret = 0;
	  if (my_graph->Init() == false)
	    throw GameException("Error on creation of Window");
	  while (my_graph->Window() == true && ret != -1)
	    {
	      tmp = game->getSList();
	      tmpf = game->getFList();
	      tmph = game->getHole();
	      my_graph->Play(tmp, tmpf, tmph, game->getScore());
	      ret = game->checkCollision(tmp, tmpf, tmph);
	      if (ret == 1)
		{
		  game->setScore();
		  game->updateSList(tmp);
		  game->updateFList(tmpf);
		}
	      game->setSList(tmp);
	      game->setFList(tmpf);
	      game->analyseLevel();
	      usleep(game->getSpeed());
	    }
	  my_graph->Finish();
	  dlclose(hndl);
	}
      catch (const std::exception &e)
	{
	  std::cerr << e.what() << std::endl;
	}
    }
  else
    std::cout << "Usage : ./nibbler LEN WIDTH LIB" << std::endl;
  return (0);
}
void* ConcretePluginManager::loadObjectFromBinary( const std::string& soFile, const std::string& makerFunction )
{
	typedef void *(*maker_type)(IApplication*);

	void *hndl;
	maker_type pMaker;

	// library loading
	try {
		hndl = dlopen(soFile.c_str(), RTLD_LAZY);
	}
	catch(...) {
		std::cout << "Error while loading the plg file" << std::endl;
	}

	//in case the handler is not defined, due to errors (most of the cases the library file is not found, corrupted, or unreadable)
	if(hndl == NULL)
	{
		std::cout << BOLDRED << "dlopenError" << RESTORECOLOR << std::endl;
		std::cerr << BOLDRED << "dlopen : "<< dlerror() << RESTORECOLOR << std::endl;
		return(NULL);
	}

	// maker loading
	void *mkr;
	try {
		mkr = dlsym(hndl, makerFunction.c_str());
	}
	catch(...) {
		std::cout<< "Error while looking for the factory function" <<std::endl;
	}

	//in case mkr is not defined, due to errors (most of the cases the factory function is not found)
	if (mkr == NULL)
	{
  		std::cerr << BOLDRED << "dlsym : " << dlerror() << RESTORECOLOR << std::endl;
  		return(NULL);
	}

	//let's make mkr callable
	void *s;
	try {
		pMaker = (maker_type)mkr;

		IApplication* app = (IApplication*) _app;

		s = pMaker(app);
	}
	catch(...) {
		std::cout << "error while calling the factory function" <<std::endl;	
	}

	//byebye
	return s;
}