// Fonctions Ressources
int OuvertureFenetreGraphique()
{
  if (OuvrirGrilleSDL(19,19,32,"!!! SPACE TENNIS !!!") < 0)
    return -1;
 
  ChargementImages();
  return 0;
}
Beispiel #2
0
int main(int argc,char* argv[])
{
  EVENT_GRILLE_SDL event;
	int i,j;
  char buffer[80];
  char ok;
	pthread_t threadHandleDefileMessage;
	pthread_t threadHandlePiece;
	pthread_t threadHandleEvent;
	pthread_t threadHandleScore;
	pthread_t threadHandleGravite;
 
  srand((unsigned)time(NULL));
	
	sigemptyset(&sigAct.sa_mask);
	sigAct.sa_handler = handlerSIGUSR1;
	sigaction(SIGUSR1,&sigAct,NULL);
	
	sigemptyset(&sigAct.sa_mask);
	sigaction(SIGUSR2,&sigAct,NULL);

  // Ouverture de la grille de jeu (SDL)
  printf("(THREAD MAIN) Ouverture de la grille de jeu\n");
  fflush(stdout);
  sprintf(buffer,"!!! TETRIS ZERO GRAVITY !!!");
  if (OuvrirGrilleSDL(NB_LIGNES,NB_COLONNES,40,buffer) < 0)
  {
    printf("Erreur de OuvrirGrilleSDL\n");
    fflush(stdout);
    exit(1);
  }

  // Chargement des sprites et de l'image de fond
  ChargementImages();
  DessineSprite(12,11,VOYANT_VERT);

	pthread_mutex_init(&mutexMessage,NULL);
	pthread_mutex_init(&mutexCasesInserees,NULL);
	pthread_cond_init(&condCasesInserees,NULL);

	//Creation du thread threadDefileMessage
	if(pthread_create(&threadHandleDefileMessage,NULL,(void*(*)(void*))threadDefileMessage,(void*)NULL)!=0)
	{
		printf("(THREAD MAIN) Erreur de creation threadDefileMessage\n");
		exit(-1);
	}
	//Creation thread piece
	if(pthread_create(&threadHandlePiece,NULL,(void*(*)(void*))threadPiece,(void*)NULL)!=0)
	{
		printf("(THREAD MAIN) Erreur de creation threadPiece\n");
		exit(-1);
	}
	//Creation du thread event
	if(pthread_create(&threadHandleEvent,NULL,(void*(*)(void*))threadEvent,(void*)NULL)!=0)
	{
		printf("(THREAD MAIN) Erreur de creation threadEvent\n");
		exit(-1);
	}
	//Creation du thread Score
	if(pthread_create(&threadHandleScore,NULL,(void*(*)(void*))threadScore,(void*)NULL)!=0)
	{
		printf("(THREAD MAIN) Erreur de creation threadScore\n");
		exit(-1);
	}
	//Creation des threads Cases
	pthread_key_create(&cle,NULL);
	for(i=0;i<14;i++)
	{
		for(j=0;j<10;j++)
		{
			pthread_mutex_lock(&mutexCaseSet);
			if(!pCases)
				pCases=(CASE*)malloc(sizeof(CASE));
			pCases->ligne=i;
			pCases->colonne=j;
			if(pthread_create(&tabThreadCases[i][j],NULL,(void*(*)(void*))threadCases,(void*)pCases)!=0)
			{
				printf("(THREAD MAIN) Erreur de creation threadCases\n");
				exit(-1);
			}
		}
	}
	//Creation threadGravite
	if(pthread_create(&threadHandleGravite,NULL,(void*(*)(void*))threadGravite,(void*)NULL)!=0)
	{
		printf("(THREAD MAIN) Erreur de creation threadGravite\n");
		exit(-1);
	}
	//Creation threadFinPartie
	if(pthread_create(&threadHandleFinPartie,NULL,(void*(*)(void*))threadFinPartie,(void*)NULL)!=0)
	{
		printf("(THREAD MAIN) Erreur de creation threadFinPartie\n");
		exit(-1);
	}

	setMessage("Bienvenue dans Tetris Zero Gravity");
  ok = 0;
  while(!ok)
  {
    event = ReadEvent();
    if (event.type == CROIX) ok = 1;
  }

	//pthread_join(threadHandleFinPartie,NULL);

  // Fermeture de la grille de jeu (SDL)
  printf("(THREAD MAIN) Fermeture de la grille..."); fflush(stdout);
  FermerGrilleSDL();
	pthread_cancel(threadHandleDefileMessage);
	pthread_cancel(threadHandlePiece);
	pthread_cancel(threadHandleEvent);
	pthread_cancel(threadHandleScore);
	for(i=0;i<14;i++)
	{
		for(j=0;j<10;j++)
		{
			pthread_cancel(tabThreadCases[i][j]);
		}
	}
	pthread_cancel(threadHandleGravite);
	pthread_cancel(threadHandleFinPartie);
  printf("OK\n"); fflush(stdout);

  exit(0);
}
int main(int argc,char* argv[])
{
	int i, j;
	CASE *pcase;
	char buffer[80];
	srand((unsigned)time(NULL)); // Pour permettre d'avoir des v. aléatoires.
	pieceEnCours.nbCases = 4;

	printf("(THREAD MAIN) Masquage de tous les signaux\n");
	sigset_t mask;
	sigfillset(&mask);
	pthread_sigmask(SIG_BLOCK, &mask, NULL);

	//Initialisation des mutex, variables de conditions et clés d'accès aux variables spécifiques
	pthread_mutex_init(&mutexMessage, NULL);
	pthread_mutex_init(&mutexCasesInserees, NULL);
	pthread_mutex_init(&mutexScore, NULL);
	pthread_mutex_init(&mutexAnalyse, NULL);
	pthread_mutex_init(&mutexTraitement, NULL);
	pthread_cond_init(&condCasesInserees, NULL);
	pthread_cond_init(&condScore, NULL);
	pthread_cond_init(&condAnalyse, NULL);
	pthread_key_create(&cle, free_case);

	// Ouverture de la grille de jeu (SDL)
	printf("(THREAD MAIN) Ouverture de la grille de jeu\n");
	fflush(stdout);
	sprintf(buffer,"!!! TETRIS ZERO GRAVITY !!!");

	if (OuvrirGrilleSDL(NB_LIGNES,NB_COLONNES,40,buffer) < 0)
	{
		printf("Erreur de OuvrirGrilleSDL\n");
		fflush(stdout);
		exit(1);
	}

// Chargement des sprites et de l'image de fond
	ChargementImages();
	DessineSprite(12,11,VOYANT_VERT);

	printf("Nombre d'arguments: %d\n", argc);
	if (argc > 1)
	{
		if ( argc != 3)
		{
			printf("Il faut executer le programme de la façon suivante : ./Tetris cleServer pseudo\n");
			exit(1);
		}

///////////////////////////////////////// Mode online /////////////////////////////////////////////
		cleServer = atoi(argv[1]);
		printf("Connexion au serveur...\n");
		if (ConnectionServeur( (key_t)cleServer, argv[2]) == 0)
		{
			printf("Connexion réussie.\n");
//Creation du thread joueurs connectés ////////////////////////////////////////////////////////////
			if (errno = pthread_create(&threadHandleJoueurs, NULL, threadJoueursConnectes, NULL) !=0 )
				perror("Erreur de lancement threadJoueursConnectes");

//Creation du thread de TopScore //////////////////////////////////////////////////////////////////
			if (errno = pthread_create(&threadHandleTopScore, NULL, threadTopScore, NULL) !=0 )
				perror("Erreur de lancement threadTopScore");
		}
		else
		{
			printf("\n---------------------------\nImpossible d'établir la connexion au serveur\n");
		}
	}

//Creation du thread de defilement du message /////////////////////////////////////////////////////

	setMessage("Bienvenue dans Tetris Zero Gravity");

	if (errno = pthread_create(&threadHandleMessage, NULL, threadDefileMessage, NULL) !=0 )
		perror("Erreur de lancement threadDefileMessage");

//Creation du thread de defilement du message /////////////////////////////////////////////////////

	if (errno = pthread_create(&threadHandlePiece, NULL, threadPiece, NULL) !=0 )
		perror("Erreur de lancement threadPiece");

//Creation du thread Event ////////////////////////////////////////////////////////////////////////

	if (errno = pthread_create(&threadHandleEvent, NULL, threadEvent, NULL) !=0 )
		perror("Erreur de lancement threadEvent");

//Creation du thread Score ////////////////////////////////////////////////////////////////////////

	if (errno = pthread_create(&threadHandleScore, NULL, threadScore, NULL) !=0 )
		perror("Erreur de lancement threadScore");

	/**/printf("(Main) Thread Score cree. errno = %d\n", (int)errno);

//Creation des threads Case ///////////////////////////////////////////////////////////////////////

	for (i = 0 ; i < NB_LIGNES ; ++i)
	{
		for (j = 0 ; j < 10 ; ++j)
		{
			pcase = (CASE*)malloc(sizeof(CASE));
			pcase->ligne = i;
			pcase->colonne = j;
			if (errno = pthread_create(&threadHandleCase[i][j], NULL, threadCase, (void*)pcase)!=0)
				perror("Erreur de lancement threadCase");
		}
	}

//Creation du thread Gravité //////////////////////////////////////////////////////////////////////

	if (errno = pthread_create(&threadHandleGravite, NULL, threadGravite, NULL) !=0 )
		perror("Erreur de lancement threadGravite");

//Creation du thread FinPartie ////////////////////////////////////////////////////////////////////

	if (errno = pthread_create(&threadHandleFinPartie, NULL, threadFinPartie, NULL) !=0 )
		perror("Erreur de lancement threadFinPartie");


///////////////////////////////////////////////////////////////////////////////////////////////////

	printf("(Main) Pret a jouer.\n");

	// Attente de la fin de la partie
	pthread_join(threadHandleFinPartie, NULL);
	terminerProgramme();
}