| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 
 | /**********************************************************
*
* NOM : jeu.cpp
* SUJET : Fonctions du jeu.
*
* AUTEUR : VN
* VERSION : 0.3.1
* CREATION : 24/10/2012
* DER. MODIF. : 25/10/2012
*
* ACCES SRC : C:\Users\Nico\Desktop\Projet Tamagoshi\src
* FABRICATION : Lancer le .exe
*
* CONTRAINTES : Avoir les dll et sdl
*
********************************************************/
 
#include <stdlib.h>
#include <stdio.h>
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <SDL/SDL_ttf.h>
#include <fmodex/fmod.h>
 
#include "constantes.h"
#include "jeu.h"
#include "fichiers.h"
#include "chronometre.h"
 
#define _CRT_SECURE_NO_WARNINGS
 
void nouvellePartie(SDL_Surface* ecran)
{
	int continuer = 1;
	int i = 0, j = 0 ; // pour parcourir le tableau
	char tempsTotal[255];
	unsigned int tempsDebutPartie = 0, tempsCoursDePartie = 0, tempsFinDePartie = 0; 
	unsigned int tempsPrecedentSandwich = 0, tempsActuelSandwich = 0, tempsPrecedentEau = 0, tempsActuelEau = 0;
	int pointsViePersonnage = 10;
	//int nombreSandwich = 5;
 
	// Variable pour placer les differents objets
	int objetsSandwich = 0; // pour placer les sandwichs sur l'ecran
	int objetsEau = 0; // pour placer les verres sur l'ecran
	int objetsMur = 0; // pour placer les Lit sur l'ecran
	int objetsArbre = 0; // pour placer les Wc sur l'ecran
 
	// Tableau a deux dimensions
    int carte[NB_BLOCS_LARGEUR][NB_BLOCS_HAUTEUR] = {0};
 
	SDL_Surface *personnage[4] = {NULL, NULL, NULL, NULL}; // Les 4 directions du personnage
    SDL_Surface *vide = NULL, *mur = NULL, *sandwich = NULL, *arbre = NULL, *eau = NULL, *personnageActuel = NULL; // *personnageActuel est un pointeur sur une surface (en fonction de où il est orienté)
	SDL_Surface *Quitter = NULL, *Sauvegarder = NULL, *viePersonnage = NULL;
 
	SDL_Surface *imageSandwich[NB_SANDWICH];
	SDL_Surface *imageVerre[NB_VERRE];
	SDL_Surface *imageVie[NB_VIE];
 
	SDL_Rect position;		 // position des elements
	SDL_Rect positionJoueur; // ou se trouve le joueur
	SDL_Rect positionSauvegarder, positionQuitter, positionViePersonnage;
	SDL_Rect positionImageSandwich, positionImageVerre;
    SDL_Event event;
 
	TTF_Font *police = NULL;
    SDL_Color couleurBlanche = {255, 255, 255};
 
	FMOD_SYSTEM *systemSonLose = NULL;
    FMOD_SOUND *musiqueLose = NULL;
    FMOD_RESULT resultatSonLose;
	FMOD_System_Create(&systemSonLose);
    FMOD_System_Init(systemSonLose, 1, FMOD_INIT_NORMAL, NULL);
 
	tempsDebutPartie = SDL_GetTicks();
	/******* LINUX *******/
	//sprintf(tempsTotal, "%u", tempsDebutPartie); 
	/******* WINDOWS *******/
	sprintf_s(tempsTotal, 255, "%u", tempsDebutPartie); // Parametre supplementaire que sprintf n a pas pour verifier la taille
 
	TTF_Init();
 
	/* Chargement de la police */
    police = TTF_OpenFont("comic.ttf", 40);
    /* Écriture du texte dans la SDL_Surface texte en mode Blended (optimal) */
    Sauvegarder = TTF_RenderText_Blended(police, "s : Sauvegarger   ", couleurBlanche);
	Quitter = TTF_RenderText_Blended(police, "q : Quitter", couleurBlanche);
	// CALCULER VIE DU PERSO A REVOIR
	viePersonnage = TTF_RenderText_Blended(police, tempsTotal, couleurBlanche);
	/*********************/
 
	chargerImages(personnage, vide, mur, sandwich, arbre, eau, *imageSandwich, *imageVerre, *imageVie);
 
    personnageActuel = personnage[BAS]; // Au debut le personnage sera vers le bas
 
    // Chargement du niveau
    if (!chargerPartie(carte))
	{
        exit(EXIT_FAILURE); // On arrête le jeu si on n'a pas pu charger le niveau
	}
 
    // Recherche de la position de personnage au départ
    for (i = 0 ; i < NB_BLOCS_LARGEUR ; i++)
    {
        for (j = 0 ; j < NB_BLOCS_HAUTEUR ; j++)
        {
            if (carte[i][j] == PERSONNAGE) // Si personnage se trouve à cette position sur la carte
            {
                positionJoueur.x = i;
                positionJoueur.y = j;
                carte[i][j] = VIDE;
            }
        }
    }
 
    // Activation de la répétition des touches, si on reste appuye
    SDL_EnableKeyRepeat(100, 100);
 
    while (continuer)
    {
        SDL_WaitEvent(&event);
		switch(event.type)
		{
			case SDL_QUIT:
				continuer = 0;
				break;
			case SDL_KEYDOWN:
				switch(event.key.keysym.sym)
				{
					case SDLK_ESCAPE:	// touche ECHAP
						continuer = 0;
						break;
			/********** Touche q **********/			
					case SDLK_a:	// touche q en qwerty
						continuer = 0;
						break;
			/********** Touche p **********/			
					case SDLK_p:	// touche p
						/*TODO*/
						break;
			/********** Touche s **********/
					case SDLK_s:	// touche s
						// TODO sauvegarderPartie();
						break;	
			/********** HAUT BAS GAUCHE DROITE **********/
					case SDLK_UP:
						personnageActuel = personnage[HAUT];
						deplacerJoueur(carte, &positionJoueur, HAUT);
						break;
					case SDLK_DOWN:
						personnageActuel = personnage[BAS];
						deplacerJoueur(carte, &positionJoueur, BAS);
						break;
					case SDLK_RIGHT:
						personnageActuel = personnage[DROITE];
						deplacerJoueur(carte, &positionJoueur, DROITE);
						break;
					case SDLK_LEFT:
						personnageActuel = personnage[GAUCHE];
						deplacerJoueur(carte, &positionJoueur, GAUCHE);
						break;
				}
				break;
		}
 
		// Coloration de la surface ecran en bleu-vert
		SDL_FillRect(ecran, NULL, SDL_MapRGB(ecran->format, 0, 0, 0));
        // Placement des objets à l'écran
        objetsSandwich = 0;
 
		// FAIRE FONCTION //
        for (i = 0 ; i < NB_BLOCS_LARGEUR ; i++)
        {
            for (j = 0 ; j < NB_BLOCS_HAUTEUR ; j++)
            {
                position.x = i * TAILLE_BLOC;
                position.y = j * TAILLE_BLOC;
 
                switch(carte[i][j])
                {
					case 0: // VIDE:
                        SDL_BlitSurface(vide, NULL, ecran, &position);
                        break;
					case 2: // MUR:
                        SDL_BlitSurface(mur, NULL, ecran, &position);
                        break;
					case 3: // SANDWICH:
                        SDL_BlitSurface(sandwich, NULL, ecran, &position);
                        break;
					case 4: // EAU:
                        SDL_BlitSurface(eau, NULL, ecran, &position);
                        break;
					case 9: // ARBRE:
                        SDL_BlitSurface(arbre, NULL, ecran, &position);
						objetsSandwich = 1;
                        break;
                }
            }
        }
		// JUSQUE LA //
 
		tempsActuelSandwich = SDL_GetTicks();
		if (tempsActuelSandwich - tempsPrecedentSandwich > 10000) // Temps en ms soit si 10 secondes ecoulees
		{/*
				if(imageSandwich == 1)
				{
					pointsViePersonnage = 0;
					keybd_event('a',0,0,0); // simule un appuie sur la touche q (en azerty)
					continuer = 0;
					resultatSonLose = FMOD_System_CreateSound(systemSonLose, "sons/Lose.wav", FMOD_SOFTWARE | FMOD_2D | FMOD_CREATESTREAM, 0, &musiqueLose);
					// On vérifie si elle a bien été ouverte (IMPORTANT) 
					if (resultatSonLose != FMOD_OK)
					{
						fprintf(stderr, "Impossible de lire le fichier wav\n");
						exit(EXIT_FAILURE);
					}
					// On joue la musique 
					FMOD_System_PlaySound(systemSonLose, FMOD_CHANNEL_FREE, musiqueLose, 0, NULL);
 
					positionImageSandwich.x = 200;
					positionImageSandwich.y = 400;
					SDL_BlitSurface(sandwich1, NULL, ecran, &positionImageSandwich);
					imageSandwich--;
					pointsViePersonnage--;	
				}
				else
				{
					SDL_BlitSurface(sandwich, NULL, ecran, &positionImageSandwich);
					imageSandwich--;
					pointsViePersonnage--;
				}*/
		}
		tempsPrecedentSandwich = tempsActuelSandwich; // Le temps "actuel" devient le temps "precedent" pour nos futurs calculs 
 
		tempsActuelEau = SDL_GetTicks();
		if (tempsActuelEau - tempsPrecedentEau > 10000) // Temps en ms soit si 10 secondes ecoulees
		{
			// TODO perdre une vie eau
			tempsPrecedentEau = tempsActuelEau; // Le temps "actuel" devient le temps "precedent" pour nos futurs calculs 
		}
 
        // On place le joueur à la bonne position
        position.x = positionJoueur.x * TAILLE_BLOC;
        position.y = positionJoueur.y * TAILLE_BLOC;
        SDL_BlitSurface(personnageActuel, NULL, ecran, &position);
 
		/***** POSITION TEXTE *****/
		positionSauvegarder.x = 10;
        positionSauvegarder.y = 450;
        SDL_BlitSurface(Sauvegarder, NULL, ecran, &positionSauvegarder); /* Blit du texte */
		position.x = 340;
        position.y = 450;
		SDL_BlitSurface(Quitter, NULL, ecran, &position); /* Blit du texte */
		positionViePersonnage.x = 40;
        positionViePersonnage.y = 50;
		SDL_BlitSurface(viePersonnage, NULL, ecran, &positionViePersonnage); /* Blit du texte */
		/**************************/
 
		SDL_Flip(ecran); // afficher a l ecran
    }
 
    // Désactivation de la répétition des touches (remise à 0)
    SDL_EnableKeyRepeat(0, 0);
 
	TTF_CloseFont(police);
    TTF_Quit();
 
    // Libération des surfaces chargées
	SDL_FreeSurface(vide);
    SDL_FreeSurface(mur);
    SDL_FreeSurface(arbre);
    SDL_FreeSurface(sandwich);
    SDL_FreeSurface(eau);
 
    for (i = 0 ; i < 4 ; i++)
        SDL_FreeSurface(personnage[i]);
 
	// TODO liberer toute les autres surfaces
}
 
void deplacerJoueur(int carte[][NB_BLOCS_HAUTEUR], SDL_Rect *pos, int direction)
{
    switch(direction)
    {
        case HAUT:
            if (pos->y - 1 < 0) // Si le joueur dépasse l'écran, on arrête
                break;
            if (carte[pos->x][pos->y - 1] == MUR) // S'il y a un mur, on arrête
                break;
 
			pos->y--; // On peut faire monter le joueur 
            // Si on arrive là, c'est qu'on peut realiser l action
			/*if (carte[pos->x][pos->y] == SANDWICH) // Il y a un sandwich, on mange
			{
				// TODO
			}
			else if (carte[pos->x][pos->y] == VERRE) // Il y a un sandwich, on mange
			{
				// TODO 
			}*/
            break;
 
 
        case BAS:
            if (pos->y + 1 >= NB_BLOCS_HAUTEUR)
                break;
            if (carte[pos->x][pos->y + 1] == MUR)
                break;
 
			pos->y++; // descendre
			/*if (carte[pos->x][pos->y] == SANDWICH) // Il y a un sandwich, on mange
			{
				// TODO
			}
			else if (carte[pos->x][pos->y] == VERRE) // Il y a un sandwich, on mange
			{
				// TODO 
			}*/
            break;
 
 
        case GAUCHE:
            if (pos->x - 1 < 0)
                break;
            if (carte[pos->x - 1][pos->y] == MUR)
                break;
 
			pos->x--; // gauche
			/*if (carte[pos->x][pos->y] == SANDWICH) // Il y a un sandwich, on mange
			{
				// TODO
			}
			else if (carte[pos->x][pos->y] == VERRE) // Il y a un sandwich, on mange
			{
				// TODO 
			}*/
            break;
 
 
        case DROITE:
            if (pos->x + 1 >= NB_BLOCS_LARGEUR)
                break;
            if (carte[pos->x + 1][pos->y] == MUR)
                break;
 
            pos->x++; // droite
            /*if (carte[pos->x][pos->y] == SANDWICH) // Il y a un sandwich, on mange
			{
				// TODO
			}
			else if (carte[pos->x][pos->y] == VERRE) // Il y a un sandwich, on mange
			{
				// TODO 
			}*/
            break;
    }
}
 
void chargerImages(SDL_Surface* personnage[4], SDL_Surface* vide, SDL_Surface* mur, SDL_Surface* sandwich, SDL_Surface* arbre, SDL_Surface* eau, SDL_Surface** imageSandwich[NB_SANDWICH], SDL_Surface** imageVerre[NB_VERRE], SDL_Surface** imageVie[NB_VIE])
{
	int i = 0;
 
	char imagesNameVie[255];
	char imagesNameVerre[255];
	char imagesNameSandwich[255];
 
	// Chargement des sprites (décors, personnage...)
	vide = IMG_Load("img/vide.jpg");
	mur = IMG_Load("img/mur.jpg");
	arbre = IMG_Load("img/arbre.jpg");
	eau = IMG_Load("img/eau.jpg");
 
	SDL_Surface **imageSandwich[NB_SANDWICH] = {NULL, NULL, NULL, NULL, NULL};
	SDL_Surface **imageVerre[NB_VERRE] = {NULL, NULL, NULL, NULL, NULL};
	SDL_Surface **imageVie[NB_VIE] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
 
	for(i=1 ; i<NB_VERRE ; i++) 
	{
		sprintf(imagesNameVerre, "img/verre%d.jpg", i+1); // car sinon i = 0 et verre0.jpg n'existe pas
		imageVerre[i] = IMG_Load(imagesNameVerre);
	}
 
	SDL_Surface *imageSandwich[NB_SANDWICH];
	for(i=1 ; i<NB_SANDWICH ; i++) 
	{
		sprintf(imagesNameSandwich, "img/sandwich%d.jpg", i);
		imageSandwich[i] = IMG_Load(imagesNameSandwich);
	}
 
	SDL_Surface *imageVie[NB_VIE];
	for(i=1 ; i<NB_VIE ; i++) 
	{
		sprintf(imagesNameVie, "img/vie%d.jpg", i);
		imageVie[i] = IMG_Load(imagesNameVie);
	}
 
    personnage[BAS] = IMG_Load("img/personnage_bas.gif");
    personnage[GAUCHE] = IMG_Load("img/personnage_gauche.gif");
    personnage[HAUT] = IMG_Load("img/personnage_haut.gif");
    personnage[DROITE] = IMG_Load("img/personnage_droite.gif");
} | 
Partager