/
affichage.c
938 lines (872 loc) · 42.3 KB
/
affichage.c
1
2
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
#include <SDL.h>
#include <SDL_image.h>
#include <SDL_ttf.h>
#include <time.h>
#include <math.h>
#include "affichage.h"
//declaration des images//Les différentes parties de la feuille de sprites qui vont être blittés
Sprite sprTerre = {.pathName = pathNameTileset, .clip = {11*SPRITE_WIDTH,4*SPRITE_HEIGHT, SPRITE_WIDTH, SPRITE_HEIGHT}, .nbImageAnim = 1};
Sprite sprHerbe = {.pathName = pathNameTileset, .clip = {15*SPRITE_WIDTH,1*SPRITE_HEIGHT, SPRITE_WIDTH, SPRITE_HEIGHT}, .nbImageAnim = 1};
const Sprite gems = {.pathName = pathNameTileset, .clip = {10*SPRITE_WIDTH,9*SPRITE_HEIGHT, SPRITE_WIDTH, SPRITE_HEIGHT}, .nbImageAnim = 1};
const SDL_Rect imgArrivee = {4*SPRITE_WIDTH,4*SPRITE_HEIGHT, SPRITE_WIDTH, SPRITE_HEIGHT};//8,13
const SDL_Rect contourMap = {4*SPRITE_WIDTH,5*SPRITE_HEIGHT, SPRITE_WIDTH, SPRITE_HEIGHT};//4,5//7,7
Sprite araignee = {.pathName = pathNameTileset,.clip = {9*SPRITE_WIDTH,12*SPRITE_HEIGHT, SPRITE_WIDTH, SPRITE_HEIGHT}, .nbImageAnim = 3};
Sprite squelette = {.pathName = pathNameTileset,.clip = {6*SPRITE_WIDTH,8*SPRITE_HEIGHT, SPRITE_WIDTH, SPRITE_HEIGHT}, .nbImageAnim = 3};
//{pathNameCharset, 0,0,charset.w/4,charset.h/4,4}
Sprite farmer = {.pathName = "Image/143-Farmer01-inaka.png", .clip = {0*SPRITE_WIDTH,0*(192/4), SPRITE_WIDTH, (192/4)}, .nbImageAnim = 4};
Sprite yoda = {.pathName = "Image/Yoda.png", .clip = {0*SPRITE_WIDTH,0*(192/4), SPRITE_WIDTH, (192/4)}, .nbImageAnim = 4};
extern SDL_Rect* boutons;
/**
* \fn void affichageInitial(OptionDAffichage *optAffichage, SDL_Surface *ecran, Jeu *game)
* \brief affichage complet du jeu
*
* \param optAffichage Les options d'affichage
* \param ecran La surface sur laquelle on veut l'afficher
* \param game Le jeu qu'on veut afficher
*/
void affichageInitial(OptionDAffichage *optAffichage, SDL_Surface *ecran, Jeu *game){
///Déclaration des variables///
int i=0;
SDL_FillRect(ecran, NULL, SDL_MapRGB(ecran->format, 119, 181, 254)); //couleur de fond de la fenetre
///On applique les sprites sur l'écran///
///affichage map///
afficherMap(optAffichage, ecran, game);
///affichage ressources///
afficherRessources(optAffichage, ecran, game);
///affichage perso///
for (i=0; i<game->nbPlayer; i++){ //pour chaque joueur
SDL_Surface *charset = game->players[i]->sprite->image; //IMG_Load(game->J1.sprite->pathName); //load le charset
//orientation perso
SDL_Rect clip = game->players[i]->sprite->clip;
clip.y += game->players[i]->orientation * game->players[i]->sprite->clip.h;
clip.x = game->players[i]->sprite->clip.x;
if (game->nbPlayer>1){
char affichageNb[2+1];
SDL_Color white = {255,255,255};
sprintf(affichageNb, "J%d",i+1);
int hauteurChar = 10;
apply_text((game->players[i]->position[0] + optAffichage->origineMapX)*SPRITE_WIDTH, (game->players[i]->position[1] + optAffichage->origineMapY)*SPRITE_HEIGHT-game->players[i]->sprite->clip.h+SPRITE_HEIGHT - hauteurChar, affichageNb, ecran, "Fonts/OCRAStd.otf", 20, white);
}
apply_surface((game->players[i]->position[0] + optAffichage->origineMapX)*SPRITE_WIDTH, (game->players[i]->position[1] + optAffichage->origineMapY)*SPRITE_HEIGHT-game->players[i]->sprite->clip.h+SPRITE_HEIGHT, charset, ecran, &clip);
}
///affichage score///
afficherScore(ecran, game);
///affichage monde flottant///
if (optAffichage->contourAffichee == 1){
afficherContourMap(optAffichage, ecran, game);
}
SDL_Flip(ecran); // Mise à jour de l'écran
}
/**
* \fn void afficherDpl(OptionDAffichage *optAffichage, SDL_Surface *ecran, Jeu *game, Joueur* player, char anciennePosition[2], int vitesse)
* \brief affichage du deplacement d'un joueur, la nouvelle position du joueur est donné dans sa structure par le parametre position[2]
*
* \param optAffichage Les options d'affichage
* \param ecran La surface sur laquelle on veut l'afficher
* \param game Le jeu qu'on veut afficher
* \param player Le joueur qui se deplace
* \param anciennePosition[2] La position du joueur avant son deplacement
* \param vitesse Nombre de milliseconde que la fonction met pour deplacer le joueur
*/
void afficherDpl(OptionDAffichage *optAffichage, SDL_Surface *ecran, Jeu *game, Joueur* player, int numPlayeur, char anciennePosition[2], int vitesse){
///Déclaration des variables///
SDL_Surface *charset = player->sprite->image; //IMG_Load(game->J1.sprite->pathName); //load le charset
char i,x,y;
int tempsActuel, tempsPrecedent;
SDL_Event event;
//orientation perso
///Déplacement du joueur///
char dplJoueur[2];
dplJoueur[0] = player->position[0] - anciennePosition[0];
dplJoueur[1] = player->position[1] - anciennePosition[1];
SDL_Rect clip = player->sprite->clip;
if(dplJoueur[0]!=0){//1:d:+2, -1:g:+1
player->orientation = (1+dplJoueur[0])/2+1;
}else if(dplJoueur[1]!=0){//1:b:0, -1:h:3
player->orientation = (1-dplJoueur[1])*3/2;
}
clip.y += player->orientation * player->sprite->clip.h;
///On applique les sprites sur l'écran///
int nbImageAnim = player->sprite->nbImageAnim;
for (i=1; i<=nbImageAnim; i++){
///affichage map///(clipping)
for (y=0;y<=player->sprite->clip.h/SPRITE_HEIGHT;y++){//add: arrondir au nb superieur
for(x=-(player->sprite->clip.w/(2*SPRITE_WIDTH)-1/2);x<=player->sprite->clip.w/(2*SPRITE_WIDTH)-1/2;x++){//add: arrondir au nb superieur
afficherCase(optAffichage, ecran, game, anciennePosition[0]+x, anciennePosition[1]-y);
afficherCase(optAffichage, ecran, game, player->position[0]+x, player->position[1]-y);
}
}
//effacement de laffichage du background precedent//marche uniquement en monde flottant... -> generaliser
//utilisé pour rafrechir score et img perso qui depasse des case de la map
SDL_Surface *background = SDL_CreateRGBSurface(SDL_HWSURFACE, ecran->w, SPRITE_HEIGHT, 32, 0, 0, 0, 0);
SDL_FillRect(background, NULL, SDL_MapRGB(ecran->format, 119, 181, 254)); //couleur de fond de la fenetre
apply_surface(optAffichage->origineMapX-1, optAffichage->origineMapY-1, background, ecran, NULL);
///affichage ressources///
afficherRessources(optAffichage, ecran, game);
///affichage perso///
//changement clip
if (dplJoueur[0]!=0 || dplJoueur[1]!=0){
clip.x = player->sprite->clip.x+i*SPRITE_WIDTH;
if(i==nbImageAnim){clip.x = player->sprite->clip.x;}
}
int positionX = (anciennePosition[0]*nbImageAnim+dplJoueur[0]*i)*SPRITE_WIDTH/nbImageAnim+SPRITE_WIDTH/2-player->sprite->clip.w/2 + optAffichage->origineMapX*SPRITE_WIDTH;
int positionY = (anciennePosition[1]*nbImageAnim+dplJoueur[1]*i)*SPRITE_HEIGHT/nbImageAnim-player->sprite->clip.h+SPRITE_HEIGHT + optAffichage->origineMapY*SPRITE_HEIGHT;
if (game->nbPlayer>1){
char affichageNb[2+1];
SDL_Color white = {255,255,255};
sprintf(affichageNb, "J%d",numPlayeur+1);
int hauteurChar = 10;
apply_text(positionX, positionY-hauteurChar, affichageNb, ecran, "Fonts/OCRAStd.otf", 20, white);
}
apply_surface(positionX, positionY, charset, ecran, &clip);
///affichage score///
afficherScore(ecran, game);
SDL_Flip(ecran); // Mise à jour de l'écran
///Temporisation///
tempsPrecedent = SDL_GetTicks();
do{
SDL_PollEvent(&event);
tempsActuel = SDL_GetTicks();
}while(tempsActuel - tempsPrecedent < vitesse/nbImageAnim); //dpl fini en "vitesse" ms
}
}
/**
* \fn void afficherMap(OptionDAffichage *optAffichage, SDL_Surface *ecran, Jeu *game)
* \brief gère l'affichage de la carte du jeu
*
* \param optAffichage Les options d'affichage
* \param ecran La surface sur laquelle on veut l'afficher
* \param game Le jeu où est la carte qu'on veut afficher
*/
void afficherMap(OptionDAffichage *optAffichage, SDL_Surface *ecran, Jeu *game){
char y,x;
for (y=0;y<game->nbCaseY;y++){
for (x=0;x<game->nbCaseX;x++){
afficherCase(optAffichage, ecran, game, x, y); //ralentissement du au reload de la surface texture
}
}
}
/**
* \fn void afficherCase(OptionDAffichage *optAffichage, SDL_Surface *ecran, Jeu *game, int x, int y)
* \brief gère l'affichage d'une case de la carte du jeu celon sa position sur la carte
*
* \param optAffichage Les options d'affichage
* \param ecran La surface sur laquelle on veut l'afficher
* \param game Le jeu où est la carte qui contient la case qu'on veut afficher
* \param x Position x de la case sur la carte
* \param y Position y de la case sur la carte
*/
void afficherCase(OptionDAffichage *optAffichage, SDL_Surface *ecran, Jeu *game, int x, int y){
SDL_Surface *textures; //IMG_Load("Tiny32-Complete-Spritesheet-Repack3.png"); //load le tileset
SDL_Color white = {255,255,255};
char affichageCase[3]="A";
if (x>=0 && x<game->nbCaseX && y>=0 && y<game->nbCaseY){ //demande daffichage des cases si elles existe
textures = game->map[x][y]->sprite->image;
apply_surface((x + optAffichage->origineMapX)*SPRITE_WIDTH, (y + optAffichage->origineMapY)*SPRITE_HEIGHT, textures, ecran, &game->map[x][y]->sprite->clip); //0->(SCREEN_WIDTH/SPRITE_WIDTH)-1
/**affichage case d'arrivee*/
int i;
for (i=0; i<game->nbPlayer; i++){ //multi joueur
if(game->players[i]->arrivee[0] == x && game->players[i]->arrivee[1] == y){
textures = IMG_Load("Tiny32-Complete-Spritesheet-Repack3.png");
apply_surface((x + optAffichage->origineMapX)*SPRITE_WIDTH, (y + optAffichage->origineMapY)*SPRITE_HEIGHT, textures, ecran, &imgArrivee);
rectangle((x + optAffichage->origineMapX)*SPRITE_WIDTH,(y + optAffichage->origineMapY)*SPRITE_HEIGHT,SPRITE_WIDTH, SPRITE_HEIGHT, white, ecran); //contour de la case
if (game->nbPlayer>1){sprintf(affichageCase, "%s%d", affichageCase, i+1);} //ajout du char joueur
apply_text((x + optAffichage->origineMapX)*SPRITE_WIDTH, (y + optAffichage->origineMapY)*SPRITE_HEIGHT, affichageCase, ecran, "Fonts/OCRAStd.otf", 20, white);
SDL_FreeSurface(textures); // On libère la surface
}
}
}
}
/**
* \fn void afficherScore(SDL_Surface *ecran, Jeu *game)
* \brief gère l'affichage du score
*
* \param optAffichage Les options d'affichage
* \param ecran La surface sur laquelle on veut l'afficher
* \param game Le jeu où est le joueur dont on veut afficher le score
*/
void afficherScore(SDL_Surface *ecran, Jeu *game){
SDL_Color white = {255,255,255};
int i;
for (i=0; i<game->nbPlayer; i++){ //multi joueur
char affichageScore[20] = "Score";
int score;
if (game->nbPlayer>1){ //ajout du char joueur
sprintf(affichageScore, "%s J%d", affichageScore, i+1);
score = game->players[i]->sac[0];
}else{
score = game->nbTourPassee;
}
sprintf(affichageScore, "%s: %d", affichageScore,score); //ajout du char score
apply_text(SPRITE_WIDTH/4 + i*(32 + 192), SPRITE_HEIGHT/4, affichageScore, ecran, "Fonts/OCRAStd.otf", 20, white);
}
}
/**
* \fn void afficherRessources(OptionDAffichage *optAffichage, SDL_Surface *ecran, Jeu *game)
* \brief gère l'affichage des ressources
*
* \param optAffichage Les options d'affichage
* \param ecran La surface sur laquelle on veut l'afficher
* \param game Le jeu où sont les ressources qu'on veut afficher
*/
void afficherRessources(OptionDAffichage *optAffichage, SDL_Surface *ecran, Jeu *game){
SDL_Surface *textures; //load le tileset;
int i;
for (i=0; i < game->nbRessource; i++){
if (game->ressources[i] !=NULL){
textures = IMG_Load(game->ressources[i]->image->pathName); //load le tileset
apply_surface((game->ressources[i]->position[0] + optAffichage->origineMapX)*SPRITE_WIDTH, (game->ressources[i]->position[1] + optAffichage->origineMapY)*SPRITE_HEIGHT, textures, ecran, &game->ressources[i]->image->clip);
SDL_FreeSurface(textures); //On libère la surface
}
}
}
/**
* \fn void afficherContourMap(OptionDAffichage *optAffichage, SDL_Surface *ecran, Jeu *game)
* \brief affiche le contour de la carte
*
* \param optAffichage Les options d'affichage
* \param ecran La surface sur laquelle on veut l'afficher
* \param game Le jeu qu'on veut afficher
*/
void afficherContourMap(OptionDAffichage *optAffichage, SDL_Surface *ecran, Jeu *game){
SDL_Surface *textures = IMG_Load("Tiny32-Complete-Spritesheet-Repack3.png"); //load le tileset
char y = game->nbCaseY-1,x;
for (x=0;x<game->nbCaseX;x++){
apply_surface((x+optAffichage->origineMapX)*SPRITE_WIDTH, (y+optAffichage->origineMapY +1)*SPRITE_HEIGHT, textures, ecran, &contourMap); //0->(SCREEN_WIDTH/SPRITE_WIDTH)-1
}
SDL_FreeSurface(textures); // On libère la surface
}
/**
* \fn SDL_Rect* afficherMenuRejouer(SDL_Surface *ecran)
* \brief affiche un menu avec les options rejouer, quitter et option
*
* \param optAffichage Les options d'affichage
* \return un tableau de SDL_Rect, chaque case contient les position et taille d'un des boutons
*/
SDL_Rect* afficherMenuRejouer(SDL_Surface *ecran){
/**Déclaration des variables*/
SDL_Surface *texte, *popup;
int popWidth = 32*4, popHeight = 32*3/2, marge = 32/4;
SDL_Color white = {255,255,255};
TTF_Init(); //Démarrage de SDL_ttf
TTF_Font *police = TTF_OpenFont("Fonts/OCRAStd.otf", 20); //Charger la police
/**Bouton rejouer*/
popup = SDL_CreateRGBSurface(SDL_HWSURFACE, popWidth, popHeight, 32, 0, 0, 0, 0);
rectangle(0,0,popup->w, popup->h, white, popup); //contour de la case
texte = TTF_RenderText_Blended(police, "Rejouer", white);
apply_surface(popup->w/2-texte->w/2, popup->h/2-texte->h/2, texte, popup, NULL);
apply_surface(ecran->w/2-(popup->w*2+marge)/2, ecran->h/2-popup->h/2, popup, ecran, NULL);
//position du bouton
boutons[0].x = ecran->w/2-(popWidth*2+marge)/2;
boutons[0].y = ecran->h/2-popup->h/2;
boutons[0].w = popup->w;
boutons[0].h = popup->h;
/**Bouton option*/
popup = SDL_CreateRGBSurface(SDL_HWSURFACE, popWidth, popHeight, 32, 0, 0, 0, 0);
rectangle(0,0,popup->w, popup->h, white, popup); //contour de la case
texte = TTF_RenderText_Blended(police, "Option", white);
apply_surface(16, 16, texte, popup, NULL);
apply_surface(0, 0, popup, ecran, NULL);
//position du bouton
boutons[1].x = 0;
boutons[1].y = 0;
boutons[1].w = popup->w;
boutons[1].h = popup->h;
/**Bouton quitter*/
popup = SDL_CreateRGBSurface(SDL_HWSURFACE, popWidth, popHeight, 32, 0, 0, 0, 0);
rectangle(0,0,popup->w, popup->h, white, popup); //contour de la case
texte = TTF_RenderText_Blended(police, "Quitter", white);
apply_surface(popup->w/2-texte->w/2, popup->h/2-texte->h/2, texte, popup, NULL);
apply_surface((ecran->w/2-(popup->w*2+marge)/2)+marge+popup->w, ecran->h/2-popup->h/2, popup, ecran, NULL);
//position du bouton
boutons[2].x = (ecran->w/2-(popup->w*2+marge)/2)+marge+popup->w;
boutons[2].y = ecran->h/2-popup->h/2;
boutons[2].w = popup->w;
boutons[2].h = popup->h;
TTF_CloseFont(police); //Fermer la police
SDL_FreeSurface(texte); // On libère la surface
SDL_FreeSurface(popup); // On libère la surface
TTF_Quit(); //Arrêt de SDL_ttf
SDL_Flip(ecran); // Mise à jour de l'écran
return boutons;
}
/**
* \fn SDL_Rect* afficherMenuOption(SDL_Surface *ecran, Option *opt, int joueurSelect)
* \brief affiche un menu avec les options rejouer, quitter et option
*
* \param optAffichage Les options d'affichage
* \param opt Les options de jeu
* \param joueurSelect Le numero du joueur selectionné pour la modification de sont apparence
* \return un tableau de SDL_Rect, chaque case contient les position et taille d'un des boutons
*/
SDL_Rect* afficherMenuOption(SDL_Surface *ecran, Option *opt, int joueurSelect){//add: choix nb case libre, mode de jeu
/**Déclaration des variables*/
SDL_Surface *texte;
int popWidth = ecran->w, popHeight = ecran->h, marge = 32/4;
SDL_Color white = {255,255,255};
/**Zone menu*/
SDL_Surface *popup = SDL_CreateRGBSurface(SDL_HWSURFACE, popWidth, popHeight, 32, 0, 0, 0, 0);
rectangle(0,0,popup->w, popup->h, white, popup); //contour de la surface
apply_surface(0, 0,popup, ecran, NULL);
TTF_Init(); //Démarrage de SDL_ttf
TTF_Font *police;
/**Titre*/
police = TTF_OpenFont("Fonts/OCRAStd.otf", 20); //Charger la police
texte = TTF_RenderText_Blended(police, "Option", white);
apply_surface(16, 16, texte, ecran, NULL);
police = TTF_OpenFont("Fonts/OCRAStd.otf", 16); //Charger la police
/**Zone nombre de joueurs*/
int firstMarge = 16 + texte->w + marge;
int positionX = firstMarge;
int positionY = 16+texte->h +4* marge;
char affichageNb[21+3+1] = "Nombre de joueurs: ";
texte = TTF_RenderText_Blended(police, affichageNb, white);
apply_surface(positionX, positionY, texte, ecran, NULL);
positionX +=texte->w+marge;
sprintf(affichageNb, "%d",opt->nbJoueur);
texte = TTF_RenderText_Blended(police, affichageNb, white);
apply_surface(positionX, positionY, texte, ecran, NULL);
//position du bouton
boutons[6].x = positionX;
boutons[6].y = positionY;
boutons[6].w = texte->w;
boutons[6].h = texte->h;
/**Zone apparence du joueur*/
positionX = firstMarge;
positionY += texte->h + 2*marge;
texte = TTF_RenderText_Blended(police, "Apparence du joueur: ", white);
apply_surface(positionX, positionY, texte, ecran, NULL);
//btn player
if (opt->nbJoueur>1){
int i=0;
boutons = realloc(boutons, (7 + opt->nbJoueur) * sizeof(SDL_Rect));//reallocation
for (i=0; i<opt->nbJoueur; i++){
positionX +=texte->w+marge;
sprintf(affichageNb, "J%d",i+1); //ajout du char score
texte = TTF_RenderText_Blended(police, affichageNb, white);
apply_surface(positionX, positionY, texte, ecran, NULL);
//position du bouton
boutons[i+7].x = positionX;
boutons[i+7].y = positionY;
boutons[i+7].w = texte->w;
boutons[i+7].h = texte->h;
//affichage du joueur selectionné
if (joueurSelect == i){
rectangle(boutons[i+7].x,boutons[i+7].y,boutons[i+7].w, boutons[i+7].h, white, ecran); //contour de la case
}
}
}
//image charset
positionX = firstMarge;
positionY += texte->h + 2*marge;
SDL_Surface *charset = IMG_Load(araignee.pathName); //load le charset
apply_surface(positionX, positionY, charset, ecran, &araignee.clip);
//position du bouton
boutons[0].x = positionX;
boutons[0].y = positionY;
boutons[0].w = charset->w/16;
boutons[0].h = charset->h/16;
//image charset
positionX += araignee.clip.w+marge;
charset = IMG_Load(squelette.pathName); //load le charset
apply_surface(positionX, positionY, charset, ecran, &squelette.clip);
//position du bouton
boutons[1].x = positionX;
boutons[1].y = positionY;
boutons[1].w = charset->w/16;
boutons[1].h = charset->h/16;
//image charset
positionX += squelette.clip.w+marge;
charset = IMG_Load(farmer.pathName); //load le charset
apply_surface(positionX, positionY, charset, ecran, &farmer.clip);
//position du bouton
boutons[2].x = positionX;
boutons[2].y = positionY;
boutons[2].w = charset->w/farmer.nbImageAnim;
boutons[2].h = charset->h/4;
//affichage de l'apparence selectionné
if (opt->sprites[joueurSelect] == &araignee){
rectangle(boutons[0].x,boutons[0].y,boutons[0].w, boutons[0].h, white, ecran); //contour de la case
}else if (opt->sprites[joueurSelect] == &squelette){
rectangle(boutons[1].x,boutons[1].y,boutons[1].w, boutons[1].h, white, ecran); //contour de la case
}else if (opt->sprites[joueurSelect] == &farmer){
rectangle(boutons[2].x,boutons[2].y,boutons[2].w, boutons[2].h, white, ecran); //contour de la case
}
/**Zone nombre de ressources*/
positionX = firstMarge;
positionY += charset->h/4 + 2*marge;
sprintf(affichageNb, "Nombre de ressources: ");
texte = TTF_RenderText_Blended(police, affichageNb, white);
apply_surface(positionX, positionY, texte, ecran, NULL);
positionX +=texte->w+marge;
sprintf(affichageNb, "%d",opt->nbRessource); //ajout du char score
texte = TTF_RenderText_Blended(police, affichageNb, white);
apply_surface(positionX, positionY, texte, ecran, NULL);
//position du bouton
boutons[3].x = positionX;
boutons[3].y = positionY;
boutons[3].w = texte->w;
boutons[3].h = texte->h;
/**Zone taille de la map*/
positionX = firstMarge;
positionY += texte->h + 2*marge;
texte = TTF_RenderText_Blended(police, "Taille de la map X: ", white);
apply_surface(positionX, positionY, texte, ecran, NULL);
positionX +=texte->w+marge;
sprintf(affichageNb, "%d", opt->nbCaseX); //ajout du char score
texte = TTF_RenderText_Blended(police, affichageNb, white);
apply_surface(positionX, positionY, texte, ecran, NULL);
//position du bouton
boutons[4].x = positionX;
boutons[4].y = positionY;
boutons[4].w = texte->w;
boutons[4].h = texte->h;
positionX +=texte->w+marge;
sprintf(affichageNb, "Y: ");//3+2
texte = TTF_RenderText_Blended(police, affichageNb, white);
apply_surface(positionX, positionY, texte, ecran, NULL);
positionX +=texte->w+marge;
sprintf(affichageNb, "%d",opt->nbCaseY); //ajout du char score
texte = TTF_RenderText_Blended(police, affichageNb, white);
apply_surface(positionX, positionY, texte, ecran, NULL);
//position du bouton
boutons[5].x = positionX;
boutons[5].y = positionY;
boutons[5].w = texte->w;
boutons[5].h = texte->h;
TTF_CloseFont(police); //Fermer la police
SDL_FreeSurface(texte); // On libère la surface
SDL_FreeSurface(popup); // On libère la surface
TTF_Quit(); //Arrêt de SDL_ttf
SDL_Flip(ecran); // Mise à jour de l'écran
return boutons;
}
/**
* \fn char gestionEvent(OptionDAffichage *optAffichage, SDL_Surface *ecran, Jeu *game, Joueur* player, Option *opt)
* \brief gère les interaction avec lutilisateur a travers le clavier
*
* \param optAffichage Les options d'affichage
* \param ecran La surface sur laquelle on veut l'afficher
* \param game Le jeu dont on veut gerer levenement
* \param player Le joueur dont c'est le tour
* \param opt Les options de jeu
* \return l'action demandé par lutilisateur
*/
char gestionEvent(OptionDAffichage *optAffichage, SDL_Surface *ecran, Jeu *game, Joueur* player, Option *opt){
/**Déclaration des variables*/
char choix = choixNull;
SDL_Event event;
int tempsPrecedent = SDL_GetTicks();
int tempsActuel;
do{
if(optAffichage->automatique == 0){
SDL_WaitEvent(&event);
}else{
//temporisation
SDL_PollEvent(&event);
tempsActuel = SDL_GetTicks();
}
if (event.type == SDL_KEYDOWN){
Arrete aTemp;
aTemp.C = (char*)malloc(sizeof(char)*1);
switch(event.key.keysym.sym){
case SDLK_ESCAPE:
choix = gestionMenu(optAffichage, ecran, game, opt);
break;
case SDLK_r:
choix = rejouer;
break;
case SDLK_p:
choix = automatique;
optAffichage->automatique = 1;
break;
case SDLK_UP:
//player->position[1] -= 1;
aTemp.C[0] = 'H';
aTemp.D = 1;
jouerTour(game, player, &aTemp);
break;
case SDLK_DOWN:
//player->position[1] += 1;
aTemp.C[0] = 'B';
aTemp.D = 1;
jouerTour(game, player, &aTemp);
break;
case SDLK_LEFT:
//player->position[0] -= 1;
aTemp.C[0] = 'G';
aTemp.D = 1;
jouerTour(game, player, &aTemp);
break;
case SDLK_RIGHT:
//player->position[0] += 1;
aTemp.C[0] = 'D';
aTemp.D = 1;
jouerTour(game, player, &aTemp);
break;
default:
break;
}
if (event.key.keysym.sym != SDLK_ESCAPE && event.key.keysym.sym != SDLK_r){choix = play;} //key autre que escape et "r"
if(optAffichage->automatique == 1 && choix==NULL){choix=play;}
}else if (event.type == SDL_QUIT){choix = quitter;} //fermer
}while((choix==choixNull) && (optAffichage->automatique == 0||(optAffichage->automatique == 1 && tempsActuel - tempsPrecedent < 250)));
return choix;
}
/**
* \fn char gestionMenu(OptionDAffichage *optAffichage, SDL_Surface *ecran, Jeu *game, Option *opt)
* \brief gère les interaction avec lutilisateur pour le menu de pause
*
* \param optAffichage Les options d'affichage
* \param ecran La surface sur laquelle on veut l'afficher
* \param game Le jeu dont on veut gerer levenement
* \param opt Les options de jeu
* \return l'action demandé par lutilisateur
*/
char gestionMenu(OptionDAffichage *optAffichage, SDL_Surface *ecran, Jeu *game, Option *opt){
/**Déclaration des variables*/
SDL_Surface *ancienEcran = SDL_DisplayFormatAlpha(ecran);
char choix = choixNull;
SDL_Event event;
boutons = afficherMenuRejouer(ecran);
do{SDL_WaitEvent(&event);
switch(event.type){
case SDL_KEYDOWN:
switch (event.key.keysym.sym){
case SDLK_ESCAPE: // Appui sur la touche Echap
choix = 4;
break;
case SDLK_KP_ENTER:
choix = 4;
break;
case SDLK_RETURN:
choix = 4;
break;
case SDLK_a://clavier qwerty
choix = quitter;
break;
case SDLK_r:
choix = rejouer;
break;
case SDLK_o:
gestionMenuOption(ecran,opt);
boutons = afficherMenuRejouer(ecran);//reinitialiser boutons
break;
default:
break;
}
break;
case SDL_MOUSEBUTTONUP:
if (event.button.button == SDL_BUTTON_LEFT){
if(event.button.x>=boutons[0].x && event.button.x<=boutons[0].x+boutons[0].w && event.button.y>=boutons[0].y && event.button.y<=boutons[0].y+boutons[0].h){
choix = rejouer;
}else if(event.button.x>=boutons[1].x && event.button.x<=boutons[1].x+boutons[1].w && event.button.y>=boutons[1].y && event.button.y<=boutons[1].y+boutons[1].h){
gestionMenuOption(ecran,opt);
boutons = afficherMenuRejouer(ecran);//reinitialiser boutons
}else if(event.button.x>=boutons[2].x && event.button.x<=boutons[2].x+boutons[2].w && event.button.y>=boutons[2].y && event.button.y<=boutons[2].y+boutons[2].h){
choix = quitter;
}else{//clic en dehors d boutons
choix = 4;
}
}
break;
default:
break;
}
}while(choix==choixNull);
if(choix!=rejouer){
apply_surface(0,0,ancienEcran,ecran,NULL);//enlever menu de lecran
SDL_Flip(ecran); // Mise à jour de l'écran
}
SDL_FreeSurface(ancienEcran);
return choix;
}
/**
* \fn void gestionMenuOption(SDL_Surface *ecran, Option *opt)
* \brief gère les interaction avec lutilisateur pour le menu option
*
* \param ecran La surface sur laquelle on veut l'afficher
* \param opt Les options de jeu
*/
void gestionMenuOption(SDL_Surface *ecran, Option *opt){
///Déclaration des variables
SDL_Surface *ancienEcran = SDL_DisplayFormatAlpha(ecran);
char choix = choixNull;
SDL_Color white = {255,255,255};
Uint32 whiteRect = {255,255,255};
SDL_Event event;
int apparenceActive=0;
boutons = afficherMenuOption(ecran, opt, apparenceActive);
int i;
do{SDL_WaitEvent(&event);
switch(event.type){
case SDL_KEYDOWN:
switch (event.key.keysym.sym){
case SDLK_ESCAPE: // Appui sur la touche Echap
choix = 4;
break;
case SDLK_KP_ENTER:
choix = 4;
break;
case SDLK_RETURN:
choix = 4;
break;
case SDLK_r:
choix = rejouer;
break;
case SDLK_o:
choix = 4;
break;
default:
break;
}
break;
case SDL_MOUSEBUTTONUP:
if (event.button.button == SDL_BUTTON_LEFT){
if(event.button.x>=boutons[0].x && event.button.x<=boutons[0].x+boutons[0].w && event.button.y>=boutons[0].y && event.button.y<=boutons[0].y+boutons[0].h){
opt->sprites[apparenceActive] = &araignee;
afficherMenuOption(ecran, opt, apparenceActive);
}else if(event.button.x>=boutons[1].x && event.button.x<=boutons[1].x+boutons[1].w && event.button.y>=boutons[1].y && event.button.y<=boutons[1].y+boutons[1].h){
opt->sprites[apparenceActive] = &squelette;
afficherMenuOption(ecran, opt, apparenceActive);
}else if(event.button.x>=boutons[2].x && event.button.x<=boutons[2].x+boutons[2].w && event.button.y>=boutons[2].y && event.button.y<=boutons[2].y+boutons[2].h){
opt->sprites[apparenceActive] = &farmer;
afficherMenuOption(ecran, opt, apparenceActive);
}else if(event.button.x>=boutons[3].x && event.button.x<=boutons[3].x+boutons[3].w && event.button.y>=boutons[3].y && event.button.y<=boutons[3].y+boutons[3].h){
opt->nbRessource = gestionTextBox(opt->nbRessource, ecran, boutons[3], opt->nbCaseLibre);
}else if(event.button.x>=boutons[4].x && event.button.x<=boutons[4].x+boutons[4].w && event.button.y>=boutons[4].y && event.button.y<=boutons[4].y+boutons[4].h){
opt->nbCaseX = gestionTextBox(opt->nbCaseX, ecran, boutons[4], 50);
opt->nbCaseLibre = (opt->nbCaseX*opt->nbCaseY)/3;
}else if(event.button.x>=boutons[5].x && event.button.x<=boutons[5].x+boutons[5].w && event.button.y>=boutons[5].y && event.button.y<=boutons[5].y+boutons[5].h){
opt->nbCaseY = gestionTextBox(opt->nbCaseY, ecran, boutons[5], 28);
opt->nbCaseLibre = (opt->nbCaseX*opt->nbCaseY)/3;
}else if(event.button.x>=0 && event.button.x<=32*4 && event.button.y>=0 && event.button.y<=32*3/2){//clic sur le titre "Option"
choix=4;
}else if(event.button.x>=boutons[6].x && event.button.x<=boutons[6].x+boutons[6].w && event.button.y>=boutons[6].y && event.button.y<=boutons[6].y+boutons[6].h){
int ancienNbJoueur = opt->nbJoueur;
opt->nbJoueur = gestionTextBox(opt->nbJoueur, ecran, boutons[6], 9);
afficherMenuOption(ecran, opt, apparenceActive);
opt->sprites = realloc(opt->sprites, opt->nbJoueur * sizeof(Sprite));//reallocation
for(i=ancienNbJoueur;i<opt->nbJoueur;i++){
opt->sprites[i]=&farmer;
}
}else if (opt->nbJoueur>1){
for(i=0;i<opt->nbJoueur;i++){
if(event.button.x>=boutons[i+7].x && event.button.x<=boutons[i+7].x+boutons[i+7].w && event.button.y>=boutons[i+7].y && event.button.y<=boutons[i+7].y+boutons[i+7].h){
apparenceActive = i;
afficherMenuOption(ecran, opt, apparenceActive);
break;
}
}
}
}
break;
case SDL_MOUSEMOTION:
for(i=0;i<7;i++){
if(event.button.x>=boutons[i].x && event.button.x<=boutons[i].x+boutons[i].w && event.button.y>=boutons[i].y && event.button.y<=boutons[i].y+boutons[i].h){
rectangle(boutons[i].x,boutons[i].y,boutons[i].w, boutons[i].h, white, ecran); //contour de la case
SDL_Flip(ecran); // Mise à jour de l'écran
break;
}
}
if (i==7){
if (opt->nbJoueur>1){
for(i=0;i<opt->nbJoueur;i++){
if(event.button.x>=boutons[i+7].x && event.button.x<=boutons[i+7].x+boutons[i+7].w && event.button.y>=boutons[i+7].y && event.button.y<=boutons[i+7].y+boutons[i+7].h){
rectangle(boutons[i+7].x,boutons[i+7].y,boutons[i+7].w, boutons[i+7].h, white, ecran); //contour de la case
SDL_Flip(ecran); // Mise à jour de l'écran
break;
}
}
if (i==opt->nbJoueur){afficherMenuOption(ecran, opt, apparenceActive);}
}else{
afficherMenuOption(ecran, opt, apparenceActive);
}
}
break;
default:
break;
}
}while(choix==choixNull);
apply_surface(0,0,ancienEcran,ecran,NULL);//enlever menu de lecran
SDL_Flip(ecran); // Mise à jour de l'écran
SDL_FreeSurface(ancienEcran);
}
/**
* \fn int gestionTextBox(int value, SDL_Surface *ecran, SDL_Rect zone, int limite)
* \brief gère les interaction avec lutilisateur pour la modifiation et laffichage d'un nombre
*
* \param value La valeur initiale
* \param ecran La surface sur laquelle on veut l'afficher
* \param zone L'endroit où on veut afficher le nombre
* \param limite Valeur maximum que peut prendre le nombre
* \return la nouvelle valeur
*/
int gestionTextBox(int value, SDL_Surface *ecran, SDL_Rect zone, int limite){
/**Déclaration des variables*/
SDL_Color white = {255,255,255};
SDL_Color black = {0,0,0};
SDL_Surface *texte, *popup;
char valide = 0;
char affichageNb[3] = "";
TTF_Init(); //Démarrage de SDL_ttf
TTF_Font *police = TTF_OpenFont("Fonts/OCRAStd.otf", 16); //Charger la police
SDL_Event event;
do{
/**Affichage de la valeur modifié*/
sprintf(affichageNb, "%d",value); //convertion int -> string
texte = TTF_RenderText_Blended(police, affichageNb, black);
popup = SDL_CreateRGBSurface(SDL_HWSURFACE, texte->w, texte->h, 32, 0,0,0,0); //maj taille popup
SDL_FillRect(popup, NULL, SDL_MapRGB(popup->format, 255, 255, 255)); //couleur de fond de la fenetre
apply_surface(0, 0, texte, popup, NULL);
apply_surface(zone.x, zone.y,popup, ecran, NULL);
SDL_Flip(ecran); // Mise à jour de l'écran
/**Modification de la valeur*/
SDL_WaitEvent(&event);
if(event.type == SDL_KEYDOWN){
switch (event.key.keysym.sym){
case SDLK_KP0:
value = value*10 + 0;
break;
case SDLK_KP1:
value = value*10 + 1;
break;
case SDLK_KP2:
value = value*10 + 2;
break;
case SDLK_KP3:
value = value*10 + 3;
break;
case SDLK_KP4:
value = value*10 + 4;
break;
case SDLK_KP5:
value = value*10 + 5;
break;
case SDLK_KP6:
value = value*10 + 6;
break;
case SDLK_KP7:
value = value*10 + 7;
break;
case SDLK_KP8:
value = value*10 + 8;
break;
case SDLK_KP9:
value = value*10 + 9;
break;
case SDLK_BACKSPACE:
value = (value - value%10) / 10;
break;
case SDLK_KP_ENTER: //validation
valide = 1;
break;
case SDLK_RETURN: //validation
valide = 1;
break;
default:
break;
}
if (value>limite){
value = limite;
}else if(value/1000>0){//limite laffichage a 3 chiffre
value = (value - value%10) / 10;
}
}
/**Masquage de l'ancienne valeur*/
SDL_FillRect(popup, NULL, SDL_MapRGB(popup->format, 0, 0, 0)); //couleur de fond de la fenetre
apply_surface(zone.x, zone.y,popup, ecran, NULL);
}while(valide == 0);
sprintf(affichageNb, "%d",value); //ajout du char score
texte = TTF_RenderText_Blended(police, affichageNb, white);
apply_surface(0, 0, texte, popup, NULL);
apply_surface(zone.x, zone.y,popup, ecran, NULL);
SDL_Flip(ecran); // Mise à jour de l'écran
TTF_CloseFont(police); //Fermer la police
SDL_FreeSurface(texte); // On libère la surface
SDL_FreeSurface(popup); // On libère la surface
TTF_Quit(); //Arrêt de SDL_ttf
return value;//renvoie la nouvelle valeur
}
//Dessin
/**
* \fn void ligneHorizontale(int x, int y, int width, Uint32 couleur, SDL_Surface *surface)
* \brief affiche une ligne horizontale
*
* \param x Position x
* \param y Position y
* \param width Longueur de la ligne
* \param couleur Couleur de la ligne
* \param surface La surface sur laquelle on veut l'afficher
*/
void ligneHorizontale(int x, int y, int width, Uint32 couleur, SDL_Surface *surface){
SDL_Rect rectTemp = {x,y,width,1};
SDL_FillRect(surface, &rectTemp, couleur);
}
/**
* \fn void ligneVerticale(int x, int y, int height, Uint32 couleur, SDL_Surface *surface)
* \brief affiche une ligne verticale
*
* \param x Position x
* \param y Position y
* \param height Longueur de la ligne
* \param couleur Couleur de la ligne
* \param surface La surface sur laquelle on veut l'afficher
*/
void ligneVerticale(int x, int y, int height, Uint32 couleur, SDL_Surface *surface){
SDL_Rect rectTemp = {x,y,1,height};
SDL_FillRect(surface, &rectTemp, couleur);
}
/**
* \fn void rectangle(int x, int y, int width, int height, Uint32 couleur, SDL_Surface *surface)
* \brief affiche un rectangle
*
* \param x Position x
* \param y Position y
* \param width Longueur du rectangle
* \param height Largeur du rectangle
* \param couleur Couleur du rectangle
* \param surface La surface sur laquelle on veut l'afficher
*/
void rectangle(int x, int y, int width, int height, Uint32 couleur, SDL_Surface *surface){
ligneHorizontale(x, y, width, couleur, surface);
ligneHorizontale(x, y + height - 1, width, couleur, surface);
ligneVerticale(x, y + 1, height - 2, couleur, surface);
ligneVerticale(x + width - 1, y + 1, height - 2, couleur, surface);
}
/**
* \fn void apply_surface(int x, int y, SDL_Surface *source, SDL_Surface *destination, SDL_Rect *clip)
* \brief affiche une image
*
* \param x Position x
* \param y Position y
* \param source La surface qu'on veut afficher
* \param destination La surface sur laquelle on veut l'afficher
* \param clip Coontient la taille et position sur la surface qu'on veut afficher de la partie qu'on veut afficher
*/
void apply_surface(int x, int y, SDL_Surface *source, SDL_Surface *destination, SDL_Rect *clip){
SDL_Rect offset = {.x = x, .y = y};
//On blitte la surface
SDL_BlitSurface(source, clip, destination, &offset);
}
/**
* \fn void apply_text(int x, int y, char* txt, SDL_Surface *destination, char* filePolice, int taille, SDL_Color couleur)
* \brief affiche un texte
*
* \param x Position x
* \param y Position y
* \param txt Le tetx a afficher
* \param destination La surface sur laquelle on veut l'afficher
* \param filePolice La police qu'on veut pour le texte
* \param taille La taille qu'on veut pour le texte
* \param couleur Couleur du texte
*/
void apply_text(int x, int y, char* txt, SDL_Surface *destination, char* filePolice, int taille, SDL_Color couleur){
TTF_Init(); //Démarrage de SDL_ttf
TTF_Font *police = TTF_OpenFont(filePolice, taille); //Charger la police
SDL_Surface *texte = TTF_RenderText_Blended(police, txt, couleur); //creer la surface correspondant au texte
apply_surface(x, y, texte, destination, NULL);
SDL_FreeSurface(texte); // On libère la surface
TTF_CloseFont(police); //Fermer la police
TTF_Quit(); //Arrêt de SDL_ttf
}