예제 #1
0
int main()
{
	DoubleLinkedList* l = createEmptyList();
	
	append(l, 4);
	append(l, 8);
	append(l, 3);
	append(l, 10);
	append(l, 76);
	append(l, 2);
	append(l, 0);
	append(l, 9);
	append(l, 32);
	append(l, 10);
	
	bubblesort(l);
	
	printListAsNumbersAndNotThatBlownUp(l);
	
	deleteList(l);
}
예제 #2
0
파일: main.c 프로젝트: Alecs94/DSA-lab
nodeL *createListFromTree(nodeT *root){
    nodeL *head=createEmptyList();
    nodeL *p=createList(root,head);
    return p;
}
예제 #3
0
Interface initGameInterface(float width, float height, float positionX, float positionY){
	//Vérif des paramètres
	if(width > 1.0 ) width = 1.0;
	else if (width < 0.0 ) width = 0.0;
	if(height > 1.0 ) height = 1.0;
	else if (height < 0.0 ) height = 0.0;
	if(positionX > 1.0 ) positionX = 1.0;
	else if (positionX < 0.0 ) positionX = 0.0;
	if(positionY > 1.0 ) positionY = 1.0;
	else if (positionY < 0.0 ) positionY = 0.0;
	
	Interface interface;
	
	interface.lstButtons = NULL;
	interface.currentAction = NO_ACTION;
	
	//Calcul des dimensions de l'interface
	interface.width = WINDOW_WIDTH * width;
	interface.height = WINDOW_HEIGHT * height;
	Point3D sdlPosition = PointXYZ(WINDOW_WIDTH *positionX, WINDOW_HEIGHT *positionY, 0.0);
	interface.position = sdlToOpenGL(sdlPosition);
	interface.moneyPosition = interface.position;
	interface.messageDisplayTime = 0;
	interface.lastMoney = 0;
	interface.relativePosX = positionX;
	interface.relativePosY = positionY;
	interface.relativeWidth = width;
	interface.relativeHeight = height;
	
	//Création messages de fin
	createLooseMessage();
	createWinMessage();

	//On positionne en fonction du coin haut gauche
	interface.position.x += interface.width / 2.0;
	interface.position.y -= + interface.height / 2.0;
	 
	//Création des textures affichant du texte
	if(TTF_Init() == -1){
		fprintf(stderr, "Erreur d'initialisation de TTF_Init : %s\n", TTF_GetError());
		exit(EXIT_FAILURE);
	}
	TTF_Font* police = NULL;
	police = TTF_OpenFont("font/Champagne.ttf", 40);
	
	//Création de l'espace et de la texture pour dessiner l'argent restant
	SDL_Color color = {255,255,255};	
	SDL_Surface* moneySurface = TTF_RenderText_Blended(police, "00000", color);
	GAME_TEXTURES_ID.MONEY_ID = makeTextureFromSurface(moneySurface);
	interface.moneyWidth = moneySurface->w > interface.width ? 0.9*interface.width : moneySurface->w;
	interface.moneyHeight = moneySurface->h;
	interface.moneyPosition.y = interface.moneyPosition.y - interface.moneyHeight / 2.0;
	interface.moneyPosition.x = interface.moneyPosition.x + interface.moneyWidth / 2.0;
	
	interface.infoPosition.z = interface.position.z;
	
	//Création de la texture affichant le message "Pause"
	SDL_Color colorPause = {255,0,0};
	SDL_Surface* pauseSurface = TTF_RenderText_Blended(police, "PAUSE", colorPause);
	GAME_TEXTURES_ID.PAUSE_MESSAGE_ID = makeTextureFromSurface(pauseSurface);
	
	//Création de la texture contenant les informations
	SDL_Surface* infoSurface = TTF_RenderText_Blended(police, "ITD", color);
	GAME_TEXTURES_ID.INFO_PANEL_ID = makeTextureFromSurface(infoSurface);
	interface.infoWidth = infoSurface->w;
	interface.infoHeight = infoSurface->h;
	//La position du panel d'info est déterminée après que les boutons ait été créés
	
	SDL_FreeSurface(infoSurface);
	SDL_FreeSurface(pauseSurface);
	SDL_FreeSurface(moneySurface);
	TTF_CloseFont(police);
	TTF_Quit();
	
	//Création des boutons 
	List* lstButtons = createEmptyList();
	if(lstButtons == NULL){
		fprintf(stderr, "Erreur fatale : Impossible d'allouer la mémoire nécessaire, le programme va quiter.\n");
		exit(-1);
	}
	
	//Interface horizontale ou verticale ?
	float buttonWidth = 0.0;
	float buttonHeight = 0.0;
	float xStep = 0.0, yStep = 0.0;
	Point3D buttonStart; 
	//Horizontale
	if(interface.width > interface.height){
		buttonWidth = interface.width / 2.0 * 0.1;
		buttonHeight = buttonWidth > interface.height ? interface.height : buttonWidth;
		interface.infoHeight = interface.infoHeight > interface.height ? interface.height : interface.infoHeight;
		xStep = buttonWidth + interface.width * 0.05;
		yStep = 0.0;
		buttonStart = PointXYZ(interface.moneyPosition.x + interface.moneyWidth + buttonWidth / 2.0, interface.moneyPosition.y - buttonHeight / 2.0, 0.0); 
	}
	//Verticale
	else{ 
		buttonWidth = interface.width / 2.0 * 0.90;
		buttonHeight = buttonWidth;
		interface.infoWidth = interface.infoWidth > interface.width ? interface.width : interface.infoWidth;
		xStep = 0.0;
		yStep = buttonHeight + interface.height * 0.05;
		buttonStart = PointXYZ(interface.moneyPosition.x, interface.moneyPosition.y - interface.moneyHeight - buttonHeight / 2.0, 0.0);
	}
	
	Button* btnLaser = createButton(PUT_LASER, PointXYZ(buttonStart.x , buttonStart.y, 0.0), buttonWidth, buttonHeight);
	Button* btnGun = createButton(PUT_GUN, PointXYZ(buttonStart.x + xStep,  buttonStart.y - yStep, 0.0), buttonWidth, buttonHeight);
	Button* btnRocket = createButton(PUT_ROCKET, PointXYZ(buttonStart.x + xStep*2,  buttonStart.y - yStep*2, 0.0), buttonWidth, buttonHeight);
	Button* btnHybrid = createButton(PUT_HYBRID, PointXYZ(buttonStart.x + xStep*3,  buttonStart.y - yStep*3, 0.0), buttonWidth, buttonHeight);
	Button* btnQuit = createButton(QUIT_GAME, PointXYZ(buttonStart.x + xStep*4,  buttonStart.y - yStep*4, 0.0), buttonWidth, buttonHeight);
	
	if(interface.width > interface.height){
		Point3D droiteInterface;
		droiteInterface.x = interface.position.x + interface.width / 2.0;
		droiteInterface.y = interface.position.y;
		Point3D droiteBouton;
		droiteBouton.x = btnQuit->position.x + buttonWidth / 2.0;
		droiteBouton.y = btnQuit->position.y;
		interface.infoPosition.y = interface.position.y;
		interface.infoPosition.x = droiteInterface.x - fabs(droiteInterface.x - droiteBouton.x) /2.0;
	}
	else{
		Point3D basInterface;
		basInterface.x = interface.position.x;
		basInterface.y = interface.position.y - interface.height / 2.0;
		Point3D basBouton;
		basBouton.x = interface.position.x;
		basBouton.y = btnQuit->position.y - buttonHeight / 2.0;
		interface.infoPosition.y = basInterface.y + fabs(basInterface.y - basBouton.y) / 2.0;
		interface.infoPosition.x = interface.position.x;
	}

	insertBottomCell(lstButtons, btnLaser);
	insertBottomCell(lstButtons, btnGun);
	insertBottomCell(lstButtons, btnRocket);
	insertBottomCell(lstButtons, btnHybrid);
	insertBottomCell(lstButtons, btnQuit);
	
	interface.lstButtons = lstButtons;
	return interface;
}
예제 #4
0
void initMenuGraphics(){
	clearMenuTextures();
	char font1[] = "font/Champagne.ttf";
	char font2[] = "font/lighthouse.ttf";

	TTF_Font* police = NULL;
	char bienvenue[34]="Bienvenue dans Imac Tower Defense";

	SDL_Surface* bienvenue_surface=loadFont(police,bienvenue,font2,100);
	MENU_TEXTURES_ID.BIENVENUE = makeTextureFromSurface (bienvenue_surface);
	SDL_FreeSurface(bienvenue_surface);

	char choix[18]="Choisir une carte";
	SDL_Surface* choix_surface=loadFont(police,choix,font1,100);
	MENU_TEXTURES_ID.MAP_CHOICE_LEGEND =  makeTextureFromSurface (choix_surface);
	SDL_FreeSurface(choix_surface);
	
	char aide[16]="Lire les regles";
	SDL_Surface* aide_surface=loadFont(police,aide,font1,100);
	MENU_TEXTURES_ID.AIDE_LEGEND=makeTextureFromSurface (aide_surface);
	SDL_FreeSurface(aide_surface);
	
	char playLegend[7]="Play !";
	SDL_Surface* play_surface=loadFont(police,playLegend,font1,100);
	MENU_TEXTURES_ID.PLAY_LEGEND = makeTextureFromSurface (play_surface);
	SDL_FreeSurface(play_surface);

	char pushEnter[31]="Appuyer sur ENTER pour valider";
	SDL_Surface* PushEnter_surface=loadFont(police,pushEnter,font1,100);
	MENU_TEXTURES_ID.PUSH_ENTER = makeTextureFromSurface (PushEnter_surface);
	SDL_FreeSurface(PushEnter_surface);

	MENU_TEXTURES_ID.FLECHES = makeTextureFromFile("images/fleches.png");
	MENU_TEXTURES_ID.AIDE_BUTTON = makeTextureFromFile("images/monstrehelp.png");
	MENU_TEXTURES_ID.MAP_CHOICE_BUTTON = makeTextureFromFile("images/monstrecarte.png");
	MENU_TEXTURES_ID.PLAY_BUTTON = makeTextureFromFile("images/monstreplay.png");
	MENU_TEXTURES_ID.RULES = makeTextureFromFile("images/regles.png");
	MENU_TEXTURES_ID.RULES_CLOSE = makeTextureFromFile("images/close.png");
	MENU_TEXTURES_ID.BULLE = makeTextureFromFile("images/bulle.png");


	//Création du menu de choix de carte
	if(BUTTON_OF_MENU.lstMapName != NULL) freeListComplete(BUTTON_OF_MENU.lstMapName);
	if(BUTTON_OF_MENU.lstMapButton != NULL) freeListComplete(BUTTON_OF_MENU.lstMapButton);
	if(BUTTON_OF_MENU.lstMapTextureIndex != NULL) freeListComplete(BUTTON_OF_MENU.lstMapTextureIndex);
	
	BUTTON_OF_MENU.lstMapName = createEmptyList();
	BUTTON_OF_MENU.lstMapButton = createEmptyList();
	BUTTON_OF_MENU.lstMapTextureIndex = createEmptyList();
	BUTTON_OF_MENU.indexButtonClicked = -1;
	BUTTON_OF_MENU.indexFirstButtonDisplayed = 1;
	
	readDirectory(BUTTON_OF_MENU.lstMapName);
	char* ptrMapName;
	char displayedName[MAX_LENGHT];
	int i = 0;
	float xText=0.;
	float yTextInit=140.;
	float yTextCurrent = yTextInit;
	float zText = 0.0;
	goToHeadList(BUTTON_OF_MENU.lstMapName);
	while( (ptrMapName = (char*) nextData(BUTTON_OF_MENU.lstMapName)) != NULL){
		strcpy(displayedName, ptrMapName);
		int j = 0;
		for(j = 0; displayedName[j] != '.';j++);
		displayedName[j] = 0;
		
		SDL_Surface* text=loadFont(police,displayedName,font1,100);
		GLuint* texId = (GLuint*) malloc(sizeof(GLuint));
		if(texId == NULL){
			fprintf(stderr, "Erreur fatale : impossible d'allouer la mémoire nécessaire.\n");
			exit(EXIT_FAILURE);
		}
		*texId = makeTextureFromSurface (text);
		insertBottomCell(BUTTON_OF_MENU.lstMapTextureIndex, texId);
		
		//Création du bouton associé
		yTextCurrent = yTextInit - (70.* (i % NB_MAP_DISPLAYED));
		if(i > NB_MAP_DISPLAYED -1) zText = -2.0;
		Point3D mapPosition = PointXYZ(xText,yTextCurrent, zText);
		int width = text->w;
		if( width > 600){
			width = 600;
		}
		insertBottomCell(BUTTON_OF_MENU.lstMapButton, createButton(MAP_MENU,mapPosition,width,text->h));
		SDL_FreeSurface(text);
		i++;
	}
	
	Point3D aidePosition = PointXYZ(-150.,100.,0.);
	Button* aideButton = createButton(AIDE_MENU,aidePosition,120,120);
	free(BUTTON_OF_MENU.regles);
	BUTTON_OF_MENU.regles = aideButton;
	
	Point3D choixPosition = PointXYZ(140.,100.,0.);
	Button* choixButton = createButton(CHOIX_MENU,choixPosition,120,120);
	free(BUTTON_OF_MENU.choix_carte);
	BUTTON_OF_MENU.choix_carte = choixButton;
	
	Point3D playPosition = PointXYZ(0.,-100.,0.);
	Button* playButton = createButton(PLAY_MENU,playPosition,120,120);
	free(BUTTON_OF_MENU.jouer);
	BUTTON_OF_MENU.jouer=playButton;
		
	Point3D closePosition = PointXYZ(223.,225.,0.);
	Button* closeButton = createButton(CLOSE_RULES_MENU,closePosition,30,30);
	free(BUTTON_OF_MENU.close_rules);
	BUTTON_OF_MENU.close_rules=closeButton;
	
	TTF_CloseFont(police);

}
예제 #5
0
파일: itd.c 프로젝트: ACCITD/ITD
int main(int argc,  char* argv[]) {
	int width = 1024, height = 768;
	if(argc == 5 && strcmp(argv[2], "-r") == 0 ){
		width = atoi(argv[3]);
		height = atoi(argv[4]);
	}
	else if(argc == 4 && strcmp(argv[1], "-r") == 0 ){
		width = atoi(argv[2]);
		height = atoi(argv[3]);	
	}
	
	/*Initialisation SDL, OpenGL etc */
	if( initWindow(width, height) == EXIT_FAILURE){
		perror("Impossible d'initialiser la fenêtre SDL, le programme va fermer.\n");
		exit(-1);
	}

	/* initialisation de SDL_TTF*/
	if(TTF_Init()== -1){
		printf("Error loading TTF: %s\n",TTF_GetError());
		exit(1);
	}
	bool askedForQuit = false;
	World world;
	world.towersList = NULL;
	world.map.name = NULL;
	world.map.tabXYConstruct = NULL;
	world.map.image = NULL;
	world.map.pathNodeList = NULL;
	Interface interface;
	interface.lstButtons = NULL;
	char* mapName= NULL;
	BUTTON_OF_MENU.lstMapName = NULL;
	BUTTON_OF_MENU.lstMapButton = NULL;
	BUTTON_OF_MENU.lstMapTextureIndex = NULL;
	bool play = false;
	
	List* lstMaps = createEmptyList();
	readDirectory(lstMaps);
	/* selection d'une carte en ligne de commande*/
	if (argc >= 2 && strcmp(argv[1], "-r") != 0){
		char* curMap = NULL;
		while( (curMap = (char*) nextData(lstMaps)) != NULL){
			if (strcmp(argv[1],curMap)==0){
				mapName = (char*) malloc(strlen(argv[1])*sizeof(char));
				if(mapName == NULL){
					fprintf(stderr, "Erreur fatale : impossible d'allouer la mémoire nécessaire.\n");
					exit(EXIT_FAILURE);
				}
				strcpy(mapName,argv[1]);
				play = true;
				break;	
			}	

		}
		if(! play) fprintf(stderr, "Erreur le nom donné en paramètre ne correspond à aucun fichier map\n");
		
	}
	freeListComplete(lstMaps);
	
/*-------------- GESTION DU MENU --------------------*/
do{

	interface.lstButtons = NULL;

	/* chargement des polices */
	int playIsPush = 0;
	int menuOpen = 0;
	int aideOpen = 0;
	initMenuGraphics();


	/* ouverture du répertoire data */

	while(!play && askedForQuit == false) {
		/* Récupération du temps au début de la boucle */
		Uint32 startTime = SDL_GetTicks();

		/* Placer ici le code de dessin du menu */		
		drawMenu(&menuOpen,&aideOpen,&playIsPush,mapName);

		/* Echange du front et du back buffer : mise à jour de la fenêtre */
		SDL_GL_SwapBuffers();

		/* Renvoie une chaine de caractère contenant le nom
		du fichier ITD choisi par l'utilisateur ou NULL si rien n'a encore été choisi */
		askedForQuit = handleMenuActions(&mapName,&playIsPush, &menuOpen,&aideOpen);

		if(playIsPush == 2) play = true;
		
		/* Calcul du temps écoulé */
		Uint32 elapsedTime = SDL_GetTicks() - startTime;
		/* Si trop peu de temps s'est écoulé, on met en pause le programme */
		if(elapsedTime < FRAMERATE_MILLISECONDS) {
			SDL_Delay(FRAMERATE_MILLISECONDS - elapsedTime);
		}

	}
	

/*-------------- GESTION DU JEU --------------------*/
	bool gameFinished = false;
	//Surtout à appeler APRES avoir initialisé la SDL
	char mapPath[50] = "data/";
	
	
	float width = .15;//10% de largeur
	float height = 1.; //Toute la hauteur
	float positionX = 0.85; //A 90% de la largeur
	float positionY = .0; //A 100% de la hauter
	
	if(!askedForQuit){
		strcat(mapPath, mapName);
		world = initWorld(mapPath);
		initGameGraphics();
		GAME_TEXTURES_ID.MAP_ID = makeTextureFromSurface(world.map.image);

		//Initialisation interface
		interface = initGameInterface(width, height, positionX, positionY);
	
		startWorld(&world);
	}
	while(!gameFinished && !askedForQuit) {
		/* Récupération du temps au début de la boucle */
		Uint32 startTime = SDL_GetTicks();
		
		/* On tente un nouveau cycle de tours de jeu si besoin. Le temps est 
		 géré par la fonction. La plupart du temps plusieurs tours de jeu sont
		 joués d'affilé. */
		worldNewStep(&world);
		

		drawWorld(&world);
		drawInterface(&interface, &world);
		/* Calcul du temps écoulé, si temps < 10 ms, on ne passe pas 
		au tour suivant.
		 */
		Uint32 elapsedTime = SDL_GetTicks() - startTime;
		/* Si trop peu de temps s'est écoulé, on ne dessine rien. */
		if(elapsedTime < FRAMERATE_MILLISECONDS) {

			 /* Echange du front et du back buffer : mise à jour de la fenêtre */
			SDL_GL_SwapBuffers();
      			SDL_Delay(FRAMERATE_MILLISECONDS - elapsedTime);
    			
		}
		
		/* Boucle traitant les evenements */
		askedForQuit = handleGameActions(&world, &interface, &gameFinished);
	}
	play = false;
	mapName = NULL;
	
	cleanWorld(&world);
	cleanInterface(&interface);
	
}while(! askedForQuit);
	/* Liberation des ressources */ 
	cleanExit(&world, &interface);

	return EXIT_SUCCESS;
}