summaryrefslogtreecommitdiff
path: root/sdl-test/SDL_tuto/TestParserLemmingsLVL/fonctions_integrees.c
blob: 0b9cc5b551cf1b061aa7bf31f54d8b56fb830c94 (plain)
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
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
// Est inclus en direct dans parse_ini.yy
int init()
{

	//Initialisation de tous les sous-systèmes de SDL
	if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
	{
		return 90;
	}

	//Mise en place de l'écran
	screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_DOUBLEBUF | SDL_HWSURFACE | SDL_ASYNCBLIT);

	//-DEBUG-printf("DEBUG ludo :  screen->w==%i,  screen->h==%i\n",  screen->w,  screen->h);
	//S'il y a une erreur lors de la mise en place de l'écran
	if( screen == NULL ) {
		//-DEBUG-printf("problème\n");
		return 2;
	}

	pTerrain = SDL_CreateRGBSurface(SDL_HWSURFACE|SDL_HWACCEL|SDL_ASYNCBLIT|SDL_RLEACCEL, LEVEL_WIDTH, LEVEL_HEIGHT,
			SCREEN_BPP,0,0,0,0);//(ccc_black>> 16) & 0xff, (ccc_black>> 8)&0xff,(ccc_black& 0xff),0);
	if( pTerrain == NULL ) {
		return 3;
	}

	pSpr_Lem = SDL_CreateRGBSurface(SDL_HWSURFACE|SDL_HWACCEL|SDL_ASYNCBLIT|SDL_RLEACCEL, LEVEL_WIDTH, LEVEL_HEIGHT,
			SCREEN_BPP,0,0,0,0);

	if( pSpr_Lem == NULL ) {
		return 3;
	}
	
	pSprLemBack = SDL_CreateRGBSurface(SDL_HWSURFACE|SDL_HWACCEL|SDL_ASYNCBLIT|SDL_RLEACCEL , LEVEL_WIDTH, LEVEL_HEIGHT,
			SCREEN_BPP,0,0,0,0);

	if( pSprLemBack == NULL ) {
		return 3;
	}
	pStencil = SDL_CreateRGBSurface(SDL_HWSURFACE|SDL_HWACCEL|SDL_ASYNCBLIT|SDL_RLEACCEL, LEVEL_WIDTH, LEVEL_HEIGHT,
			SCREEN_BPP,0,0,0,0);
	if( pStencil == NULL ) {
		return 3;
	}

	pStencilFixe = SDL_CreateRGBSurface(SDL_HWSURFACE|SDL_HWACCEL|SDL_ASYNCBLIT|SDL_RLEACCEL, LEVEL_WIDTH, LEVEL_HEIGHT,
			SCREEN_BPP,0,0,0,0);
	if( pStencil == NULL ) {
		return 3;
	}
	
	pInterface = SDL_CreateRGBSurface(SDL_HWSURFACE|SDL_HWACCEL|SDL_ASYNCBLIT|SDL_RLEACCEL, SCREEN_WIDTH, INTERFACE_HEIGHT,
			SCREEN_BPP,0,0,0,0);
	if( pInterface == NULL ) {
		return 3;
	}
	//Mise en place de la barre caption
	SDL_WM_SetCaption( "Test_LVL", NULL );
	//No pointer of mouse
	SDL_ShowCursor(0);

	//Si tout s'est bien passé
	return 0;
}

Uint32 string_to_Uint32 (char* c,int taille){

	Uint32 u32c;
	int i,n,lc = strlen(c);
	int t[taille];
	if(lc-3 != taille){
		//-DEBUG-printf("ERREUR: wrong format .. expect (strlen[%s] - 3)==%d but it's = %d\n",c,taille,lc-3);
		return(ccc_error);
	}
	u32c = 0;
	for(i=0;i<taille;++i){
		n = c[i+2];
		//-DEBUG-printf("n = %d, %c\n",n);
		if (n > 47 && n < 58 ) { t[i]=(n - 48) ;}
		if (n > 64 && n < 71 ) { t[i]=(n - 55) ;}
		if (n > 96 && n < 103 ){ t[i]=(n - 87) ;}
	}
	n=0;
	for(i=0;i<taille;++i){
		//-DEBUG-printf("t[%d]= %d, *(%d), %d\n",i,t[i],(1<<(4*(taille-i-1))),t[i]*(1<<(4*(taille-i-1))) );

		n+=t[i]*(1<<(4*(taille-i-1)));


		//-DEBUG-printf("n= %d\n",n);
	}
	u32c= n & 0xffffff;
	return u32c;
}


// "4,5,6,7" -> un tableau de Uint32
Uint32* tab_of_string_to_Uint32 (char* c, int taille){
	int i,lc = strlen(c);
	char t[10];
	int cpt=0;
	int cpt2=0;
	Uint32* tab=malloc(sizeof(Uint32)*(lc/(taille+3)));
	t[0]='0';t[1]='x';t[8]=',';t[9]=0;

	for(i=0;i<lc;++i){
		// token
		if(c[i]=='x'){cpt=0;}
		if(cpt<6){
			t[cpt+2]=c[i+1];		
			++cpt;
			if(cpt==6){
				tab[cpt2]=string_to_Uint32(t,taille);
				cpt2++;		
			}
		}
	}
	return tab;
}

	int putBrickPx(int x,int y,SDL_Surface *s,Uint32 bgColor,Uint32 debrisColor){
		int err;
		SDL_Rect from,to;

		from.x=0;
		from.y=0;
		from.w=s->w;
		from.h=s->h;

		to.x=x;
		to.y=y;
		to.w=x+s->w;
		to.h=y+s->h;

		if((err=paint_manip(s,from,pTerrain,to,bgColor,ccc_lemming,17,pStencilFixe))!=0){
			return err;
		}
		if((err=paint_manip(s,from,pStencilFixe,to,ccc_bgStencil,ccc_lemming,18,NULL))!=0){
			return err;
		}

		return 0;
	}

	int ereasePx(int x,int y,SDL_Surface *s,Uint32 bgColor){
		int err;
		SDL_Rect from,to;

		from.x=0;
		from.y=0;
		from.w=s->w;
		from.h=s->h;

		to.x=x;
		to.y=y;
		to.w=x+s->w;
		to.h=y+s->h;

		if((err=paint_manip(s,from,pTerrain,to,bgColor,ccc_lemming,17,pStencil))!=0){
			return err;
		}
		
		if((err=paint_manip(s,from,pSpr_Lem,to,bgColor,ccc_lemming,17,pStencil))!=0){
			return err;
		}
		
		if((err=paint_manip(s,from,pSprLemBack,to,bgColor,ccc_lemming,17,pStencil))!=0){
			return err;
		}
		
		if((err=paint_manip(s,from,pStencil,to,ccc_bgStencil,ccc_lemming,18,NULL))!=0){
			return err;
		}


		return 0;	
	}

	SDL_Surface* flipSurfaceUD_LR(SDL_Surface* src, enum sens sensO, Uint32 c) {
		int i,j;

		SDL_Surface* copy_surface = NULL;

		// This only works for 32 bit pixel format
		if( src->format->BitsPerPixel == 32 ) {
			// This surface must be freed by the caller.
			copy_surface = SDL_CreateRGBSurface(src->flags, src->w, src->h,
					src->format->BitsPerPixel,

					src->format->Rmask,
					src->format->Gmask,
					src->format->Bmask,
					src->format->Amask);

			SDL_SetColorKey(	copy_surface, 
					SDL_RLEACCEL | SDL_SRCCOLORKEY,
					//src->format->colorkey
					SDL_MapRGB( copy_surface->format,
						(c >> 16) & 0xff,
						(c >> 8) & 0xff,
						(c & 0xff))
				       );		

			//FIXME : lock surface
			Uint32 *source = src->pixels;
			Uint32 *dest = copy_surface->pixels;

			for(i = 0; i < src->h; i++) {
				for(j = 0; j < src->w; j++) {
					if (sensO == UD){// Sens UD
						dest[ (src->h-i-1)*src->w + j ] = source[ i*src->w + j];
					}
					if (sensO == LR){// Sens LR
						dest[ i*src->w + (src->w-j-1) ] = source[ i*src->w + j];
					}
				}
			}
		}

		return copy_surface;
	}

	SDL_Surface *load_image( char* filename, Uint32 cbg )
	{
		//L'image qui est chargée
		SDL_Surface* loadedImage = NULL;

		//L'image optimisée qu'on va utiliser
		SDL_Surface* optimizedImage = NULL;

		//Chargement de l'image
		loadedImage = IMG_Load( filename );

		//Si l'image est chargée
		if( loadedImage != NULL )
		{
			//Création de l'image optimisée
			optimizedImage = SDL_DisplayFormat( loadedImage );



			//Si la création de l'image optimisée s'est bien passée
			if( optimizedImage != NULL )
			{
				SDL_SetColorKey(	optimizedImage, 
						SDL_RLEACCEL | SDL_SRCCOLORKEY,
						SDL_MapRGB( optimizedImage->format,
							(cbg >> 16) & 0xff,
							(cbg >> 8) & 0xff,
							(cbg & 0xff)
							)
					       );
			}
			//Libération de l'ancienne image
			SDL_FreeSurface( loadedImage );
		}

		//On retourne l'image optimisée
		return optimizedImage;

		//return loadedImage;
	}


	int load_files(struct gameInit *gInit)
	{
		int lt,ltt,ltlem;
		int err=0;
		int res, lterrain, lobjet, lsteel;
		char *folder;
		char *spriteINI;
		char *lemINI;
		char *terrainGif;
		char *objetGif;
		char *steelGif;
		char *temp;
		int tabNum[255];
		int i,tempi,max,j;
		struct dirent *lecture;
		DIR *rep;

		yyparse(gInit);

		fclose(yyin);
		//-DEBUG-printf("CLOSE 1\n");


		lt = (strlen("../../../styles/"));
		ltlem = (strlen("../../../misc/lemming.ini"));
		ltt = (strlen(tabString_eMapStyle[gInit->mapI.map.style]));

		folder = malloc(sizeof(char)*(lt+ltt+1));
		spriteINI = malloc(sizeof(char)*(lt+ltt*2+5));// *2:dossier/fichier, +4: .ini, +1: string
		lemINI = malloc(sizeof(char)*ltlem+1);


		//folder = "/home/jazzblue/Bureau/Projet-Lemmings/trunk/styles/dirt";
		//-DEBUG-printf("test segFault\n");
		sprintf(folder,"../../../styles/%s",tabString_eMapStyle[gInit->mapI.map.style]);
		sprintf(spriteINI,"%s/%s.ini",folder,tabString_eMapStyle[gInit->mapI.map.style]);
		sprintf(lemINI,"%s","../../../misc/lemming.ini");
		//-DEBUG-printf("test2\n");

		temp = tabString_eMapStyle[gInit->mapI.map.style];
		lt = strlen(temp);
		lterrain = lt+1;
		lobjet = lt+2;
		lsteel = lt+3;	
		terrainGif=malloc(sizeof(char)*(lterrain+1));
		objetGif=malloc(sizeof(char)*(lobjet+1));
		steelGif=malloc(sizeof(char)*(lsteel+1));
		sprintf(terrainGif,"%s_",temp);
		sprintf(objetGif,"%so_",temp);
		sprintf(steelGif,"%som_",temp);

		// Compter les textures de terrain	
		rep = opendir(folder);
		if (rep==NULL) {
			//-DEBUG-printf("Erreur opendir('%s')\n", folder);
			return(31);
		}

		while ((lecture = readdir(rep))) {
			if( (res=strncmp(terrainGif, lecture->d_name,lterrain)) == 0 ){
				++(gInit->mapI.map.tiles);
			} else {
				if( (res=strncmp(objetGif, lecture->d_name,lobjet)) == 0 ) {
					++(gInit->mapI.map.tilesObjet);
				} else {
					if ( (res=strncmp(steelGif, lecture->d_name,lsteel)) == 0 ) {
						sscanf(lecture->d_name+lsteel, "%i", &(tabNum[gInit->mapI.map.tilesSteel]));
						++(gInit->mapI.map.tilesSteel);

					} else {
						//-DEBUG-printf("BUG : type de fichier non reconnu [%s] \n", lecture->d_name);
					}
				}
			}
		}
		closedir(rep);

		//tri tableau
		for(j=0;j<2;++j){
			for(i=0;i<gInit->mapI.map.tilesSteel-1;++i){
				max=tabNum[i];
				tempi=tabNum[i+1];

				if(max > tempi){
					tabNum[i]=tempi;
					tabNum[i+1]=max;
					i=0;
				}
			}
		}

		yyin=fopen(spriteINI, "r");
		if (yyin==NULL) { fprintf(stderr,"Filename INVALIDE: %s\n",spriteINI);
			perror("Impossible d'ouvrir le fichier de configuration des sprite"); return(32); 
		}
		yyparse(gInit);
		fclose(yyin);
		//-DEBUG-printf("CLOSE 2\n");

		yyin=fopen(lemINI, "r");
		if (yyin==NULL) { fprintf(stderr,"Filename INVALIDE: %s\n",lemINI);
			perror("Impossible d'ouvrir le fichier de configuration des lemmings"); return(33); 
		}
		yyparse(gInit);
		fclose(yyin);
		//-DEBUG-printf("CLOSE 3\n");

		//-DEBUG-printf("REVERSE t.lt\n");	
		gInit->t.lt=(struct listeSimplementChainee*)rev_listeO(gInit->t.lt);
		//-DEBUG-printf("REVERSE o.lo\n");	
		gInit->o.lo=(struct listeSimplementChainee*)rev_listeO(gInit->o.lo);
		//-DEBUG-printf("REVERSE s.ls\n");	
		gInit->s.ls=(struct listeSimplementChainee*)rev_listeO(gInit->s.ls);	

		err=load_fields(folder, gInit, tabNum);
		if(err!=0){return err;}


		// AFFICHAGE DES INFOS DE LA MAP
		//err=afficher(gInit);
		//if(err!=0){return err;}

		//Si tout s'est bien passé
		free(terrainGif);
		free(objetGif);
		free(steelGif);
		free(folder);
		free(spriteINI);
		free(lemINI);

		return 0;
	}



	int afficher(struct gameInit *gInit){

		int i;
		struct listeSimplementChainee *k;

		printf("AFFICHAGE DE GAME INIT\n");
		printf("LES PARAMETRES:\n");
		for(i=0;i<IDENT_COUNT;++i){
			printf("\t%s = %d\n",tabString_eParaMap[i],gInit->mapI.paraMap[i]);
		}
		printf("\txPos = %d\n",gInit->mapI.xPos);
		printf("\tstyle = %s\n",tabString_eMapStyle[gInit->mapI.map.style]);

		printf("LE NOM DU NIVEAU:\n");
		printf("name = %s\n",gInit->mapI.name);
		printf("LES COULEURS DU NIVEAU:\n");
		printf("bgColor R = %X G = %X B = %X\n",
				((gInit->mapI.map.cmap.bgColor>>16) & 0xff),
				((gInit->mapI.map.cmap.bgColor>>8) & 0xff),
				(gInit->mapI.map.cmap.bgColor & 0xff) );
		printf("debrisColor R = %X G = %X B = %X\n",
				((gInit->mapI.map.cmap.debrisColor>>16) & 0xff),
				((gInit->mapI.map.cmap.debrisColor>>8) & 0xff),
				(gInit->mapI.map.cmap.debrisColor & 0xff) );
		printf("debrisColor:\n");
		for(i=0;i<PARTICULE_COUNT;++i){
			printf("\tP%d: R = %X G = %X B = %X\n",i,
					((gInit->mapI.map.cmap.particleColor[i]>>16) & 0xff),
					((gInit->mapI.map.cmap.particleColor[i]>>8) & 0xff),
					(gInit->mapI.map.cmap.particleColor[i] & 0xff) );	

		}

		printf("TERRAINS:\n");
		k=gInit->t.lt;
		i=0;
		while ( k !=NULL ) {
			if(k->data.pt != NULL){//{printf("paraTerrain non initialiser\n");return(34);}
				printf("\tterrain_%d = %d, %d, %d, %d\n",i,k->ID, k->x, k->y,k->data.pt->modif);
			}
			++i;
			k=k->next;
		}

		printf("OBJETS:\n");

		k=gInit->o.lo;
		i=0;
		while ( k !=NULL ) {
			if(k->data.po != NULL){ //{printf("paraObjet non initialiser\n");return(35);}
				printf("\tobject_%d = %d, %d, %d, %d, %d\n",i,k->ID, k->x, k->y, k->data.po->paintMode,k->data.po->UD);
			}
			++i;
			k=k->next;
		}


		printf("STEELS:\n");
		k=gInit->s.ls;
		i=0;
		while ( k !=NULL ) {
			if(k->data.ps != NULL){//{printf("paraSteel non initialiser\n");return(36);}
				printf("\tsteel_%d = %d, %d, %d, %d, %d\n",i,k->ID=i, k->x, k->y, k->data.ps->w,k->data.ps->h); // FIXME big fuck here with "=" 
			}
			++i;
			k=k->next;
		}

		printf("DATA SPRITES OBJET:\n");
		for(i=0;i<gInit->mapI.map.tilesObjet;++i){
			printf("spr_%d:\n\tframes_%d = %d\n\tanim_%d = %d\n\ttype_%d = %d\n\tsound_%d = %d\n",i,i,gInit->mapI.map.tabDataSprO[i].state,i,gInit->mapI.map.tabDataSprO[i].anim,i,gInit->mapI.map.tabDataSprO[i].type,i,gInit->mapI.map.tabDataSprO[i].sound);
		}

		printf("DATA JOB LEMMINGS:\n");
		for(i=0;i<LEM_JOB;++i){
			printf("lem_%d:\n\tlemm_%d = %d, %d, %d\n\tpos_%d = %d, %d, %d\n",i,i,gInit->mapI.lemmingDATA[i].state,gInit->mapI.lemmingDATA[i].dir,gInit->mapI.lemmingDATA[i].anim,i,gInit->mapI.lemmingDATA[i].footX,gInit->mapI.lemmingDATA[i].footY,gInit->mapI.lemmingDATA[i].footSize);

			if (gInit->mapI.lemmingDATA[i].mask != NULL){
				printf("\tmask_%d = %d, %d, %d\n",i,gInit->mapI.lemmingDATA[i].mask->stateM,gInit->mapI.lemmingDATA[i].mask->dirM,gInit->mapI.lemmingDATA[i].mask->cooldown);
			}

			if (gInit->mapI.lemmingDATA[i].imask != NULL){
				printf("\timask_%d = %d , %d\n",i,gInit->mapI.lemmingDATA[i].imask->stateI,gInit->mapI.lemmingDATA[i].imask->dirI);		
			}
		}
		return 0;
	}

	int afficher(struct gameInit *gInit){

		int i;
		struct listeSimplementChainee *k;

		printf("AFFICHAGE DE GAME INIT\n");
		printf("LES PARAMETRES:\n");
		for(i=0;i<IDENT_COUNT;++i){
			printf("\t%s = %d\n",tabString_eParaMap[i],gInit->mapI.paraMap[i]);
		}
		printf("\txPos = %d\n",gInit->mapI.xPos);
		printf("\tstyle = %s\n",tabString_eMapStyle[gInit->mapI.map.style]);

		printf("LE NOM DU NIVEAU:\n");
		printf("name = %s\n",gInit->mapI.name);
		printf("LES COULEURS DU NIVEAU:\n");
		printf("bgColor R = %X G = %X B = %X\n",
				((gInit->mapI.map.cmap.bgColor>>16) & 0xff),
				((gInit->mapI.map.cmap.bgColor>>8) & 0xff),
				(gInit->mapI.map.cmap.bgColor & 0xff) );
		printf("debrisColor R = %X G = %X B = %X\n",
				((gInit->mapI.map.cmap.debrisColor>>16) & 0xff),
				((gInit->mapI.map.cmap.debrisColor>>8) & 0xff),
				(gInit->mapI.map.cmap.debrisColor & 0xff) );
		printf("debrisColor:\n");
		for(i=0;i<PARTICULE_COUNT;++i){
			printf("\tP%d: R = %X G = %X B = %X\n",i,
					((gInit->mapI.map.cmap.particleColor[i]>>16) & 0xff),
					((gInit->mapI.map.cmap.particleColor[i]>>8) & 0xff),
					(gInit->mapI.map.cmap.particleColor[i] & 0xff) );	

		}

		printf("TERRAINS:\n");
		k=gInit->t.lt;
		i=0;
		while ( k !=NULL ) {
			if(k->data.pt != NULL){//{printf("paraTerrain non initialiser\n");return(34);}
				printf("\tterrain_%d = %d, %d, %d, %d\n",i,k->ID, k->x, k->y,k->data.pt->modif);
			}
			++i;
			k=k->next;
		}

		printf("OBJETS:\n");

		k=gInit->o.lo;
		i=0;
		while ( k !=NULL ) {
			if(k->data.po != NULL){ //{printf("paraObjet non initialiser\n");return(35);}
				printf("\tobject_%d = %d, %d, %d, %d, %d\n",i,k->ID, k->x, k->y, k->data.po->paintMode,k->data.po->UD);
			}
			++i;
			k=k->next;
		}


		printf("STEELS:\n");
		k=gInit->s.ls;
		i=0;
		while ( k !=NULL ) {
			if(k->data.ps != NULL){//{printf("paraSteel non initialiser\n");return(36);}
				printf("\tsteel_%d = %d, %d, %d, %d, %d\n",i,k->ID=i, k->x, k->y, k->data.ps->w,k->data.ps->h); // FIXME big fuck here with "=" 
			}
			++i;
			k=k->next;
		}

		printf("DATA SPRITES OBJET:\n");
		for(i=0;i<gInit->mapI.map.tilesObjet;++i){
			printf("spr_%d:\n\tframes_%d = %d\n\tanim_%d = %d\n\ttype_%d = %d\n\tsound_%d = %d\n",i,i,gInit->mapI.map.tabDataSprO[i].state,i,gInit->mapI.map.tabDataSprO[i].anim,i,gInit->mapI.map.tabDataSprO[i].type,i,gInit->mapI.map.tabDataSprO[i].sound);
		}

		printf("DATA JOB LEMMINGS:\n");
		for(i=0;i<LEM_JOB;++i){
			printf("lem_%d:\n\tlemm_%d = %d, %d, %d\n\tpos_%d = %d, %d, %d\n",i,i,gInit->mapI.lemmingDATA[i].state,gInit->mapI.lemmingDATA[i].dir,gInit->mapI.lemmingDATA[i].anim,i,gInit->mapI.lemmingDATA[i].footX,gInit->mapI.lemmingDATA[i].footY,gInit->mapI.lemmingDATA[i].footSize);

			if (gInit->mapI.lemmingDATA[i].mask != NULL){
				printf("\tmask_%d = %d, %d, %d\n",i,gInit->mapI.lemmingDATA[i].mask->stateM,gInit->mapI.lemmingDATA[i].mask->dirM,gInit->mapI.lemmingDATA[i].mask->cooldown);
			}

			if (gInit->mapI.lemmingDATA[i].imask != NULL){
				printf("\timask_%d = %d , %d\n",i,gInit->mapI.lemmingDATA[i].imask->stateI,gInit->mapI.lemmingDATA[i].imask->dirI);		
			}
		}
		return 0;
	}
	int afficher(struct gameInit *gInit){

		int i;
		struct listeSimplementChainee *k;

		printf("AFFICHAGE DE GAME INIT\n");
		printf("LES PARAMETRES:\n");
		for(i=0;i<IDENT_COUNT;++i){
			printf("\t%s = %d\n",tabString_eParaMap[i],gInit->mapI.paraMap[i]);
		}
		printf("\txPos = %d\n",gInit->mapI.xPos);
		printf("\tstyle = %s\n",tabString_eMapStyle[gInit->mapI.map.style]);

		printf("LE NOM DU NIVEAU:\n");
		printf("name = %s\n",gInit->mapI.name);
		printf("LES COULEURS DU NIVEAU:\n");
		printf("bgColor R = %X G = %X B = %X\n",
				((gInit->mapI.map.cmap.bgColor>>16) & 0xff),
				((gInit->mapI.map.cmap.bgColor>>8) & 0xff),
				(gInit->mapI.map.cmap.bgColor & 0xff) );
		printf("debrisColor R = %X G = %X B = %X\n",
				((gInit->mapI.map.cmap.debrisColor>>16) & 0xff),
				((gInit->mapI.map.cmap.debrisColor>>8) & 0xff),
				(gInit->mapI.map.cmap.debrisColor & 0xff) );
		printf("debrisColor:\n");
		for(i=0;i<PARTICULE_COUNT;++i){
			printf("\tP%d: R = %X G = %X B = %X\n",i,
					((gInit->mapI.map.cmap.particleColor[i]>>16) & 0xff),
					((gInit->mapI.map.cmap.particleColor[i]>>8) & 0xff),
					(gInit->mapI.map.cmap.particleColor[i] & 0xff) );	

		}

		printf("TERRAINS:\n");
		k=gInit->t.lt;
		i=0;
		while ( k !=NULL ) {
			if(k->data.pt != NULL){//{printf("paraTerrain non initialiser\n");return(34);}
				printf("\tterrain_%d = %d, %d, %d, %d\n",i,k->ID, k->x, k->y,k->data.pt->modif);
			}
			++i;
			k=k->next;
		}

		printf("OBJETS:\n");

		k=gInit->o.lo;
		i=0;
		while ( k !=NULL ) {
			if(k->data.po != NULL){ //{printf("paraObjet non initialiser\n");return(35);}
				printf("\tobject_%d = %d, %d, %d, %d, %d\n",i,k->ID, k->x, k->y, k->data.po->paintMode,k->data.po->UD);
			}
			++i;
			k=k->next;
		}


		printf("STEELS:\n");
		k=gInit->s.ls;
		i=0;
		while ( k !=NULL ) {
			if(k->data.ps != NULL){//{printf("paraSteel non initialiser\n");return(36);}
				printf("\tsteel_%d = %d, %d, %d, %d, %d\n",i,k->ID=i, k->x, k->y, k->data.ps->w,k->data.ps->h); // FIXME big fuck here with "=" 
			}
			++i;
			k=k->next;
		}

		printf("DATA SPRITES OBJET:\n");
		for(i=0;i<gInit->mapI.map.tilesObjet;++i){
			printf("spr_%d:\n\tframes_%d = %d\n\tanim_%d = %d\n\ttype_%d = %d\n\tsound_%d = %d\n",i,i,gInit->mapI.map.tabDataSprO[i].state,i,gInit->mapI.map.tabDataSprO[i].anim,i,gInit->mapI.map.tabDataSprO[i].type,i,gInit->mapI.map.tabDataSprO[i].sound);
		}

		printf("DATA JOB LEMMINGS:\n");
		for(i=0;i<LEM_JOB;++i){
			printf("lem_%d:\n\tlemm_%d = %d, %d, %d\n\tpos_%d = %d, %d, %d\n",i,i,gInit->mapI.lemmingDATA[i].state,gInit->mapI.lemmingDATA[i].dir,gInit->mapI.lemmingDATA[i].anim,i,gInit->mapI.lemmingDATA[i].footX,gInit->mapI.lemmingDATA[i].footY,gInit->mapI.lemmingDATA[i].footSize);

			if (gInit->mapI.lemmingDATA[i].mask != NULL){
				printf("\tmask_%d = %d, %d, %d\n",i,gInit->mapI.lemmingDATA[i].mask->stateM,gInit->mapI.lemmingDATA[i].mask->dirM,gInit->mapI.lemmingDATA[i].mask->cooldown);
			}

			if (gInit->mapI.lemmingDATA[i].imask != NULL){
				printf("\timask_%d = %d , %d\n",i,gInit->mapI.lemmingDATA[i].imask->stateI,gInit->mapI.lemmingDATA[i].imask->dirI);		
			}
		}
		return 0;
	}


	int test_O_UD(int UD){

		switch(UD){
			case 4 : return 1;break;
			case 6 : return 1;break;
			case 12 : return 1;break;
			case 14 : return 1;break;
			default: return 0;break;
		}
		return 0;
	}

	int paint_terrain (struct gameInit *gInit, int choix){
		struct listeSimplementChainee *k=NULL;
		SDL_Rect offset;
		SDL_Surface *sf;
		int err=0;
		int overwrite=0;
		int remove=0;
		int paint=0;
		int tx,ty=0;
		int cpt=0;
		int ok=0;
		Uint32 color;

		//	//-DEBUG-printf("LES TERRAINS : ");
		k=gInit->t.lt;
		while ( k !=NULL ) {
			offset.x = k->x;
			offset.y = k->y;

			sf = gInit->mapI.map.tabGif[test_O_UD(k->data.pt->modif)+k->ID*2+ADD_OBJ];

			if((k->data.pt->modif != 0)&&
					(k->data.pt->modif != 2)&&
					(k->data.pt->modif != 4)&&
					(k->data.pt->modif != 6)&&
					(k->data.pt->modif != 8)&&
					(k->data.pt->modif != 10)&&
					(k->data.pt->modif != 12)&&
					(k->data.pt->modif != 14)&&
					(k->data.pt->modif != 15)
			  ) {
				//-DEBUG-printf("WTF ... k->data.pt->modif == %d\n",k->data.pt->modif);
				return(40);
			}
			for(ty=k->y;ty<sf->h+k->y;++ty){

				if(ty<0) {ty=-1;continue;}
				if(ty>=pTerrain->h){break;}
				if(ty>=LEVEL_HEIGHT) {break;}

				for(tx=k->x;tx<sf->w+k->x;++tx){

					if(tx<0) {tx=-1;continue;}
					if(tx>=pTerrain->w){break;}
					if(tx>=LEVEL_WIDTH) {break;}		

//FIXME : le bloc suivant ne dépends pas de x et y !
					overwrite = ((k->data.pt->modif & 8) == 8)? 0 : 1;
					remove = ((k->data.pt->modif & 2) == 2)? 1 : 0;
					if(k->data.pt->modif == 15){
						overwrite=0;
						remove=0;
					}


					color = get_pixel32(tx-k->x,ty-k->y,sf);
					if(color==ccc_error){return 250;}
					if (color == ccc_black)
					{	//putPixel(pStencil,tx,ty , ccc_tEmpty );
						continue; 
					}
					paint=0;
					if(!overwrite){
						//FIXME : What the hell avec deux fois ce get_pixel ?!?
						if((get_pixel32(tx,ty,pStencil)==ccc_error)){return 67;}
						// don't overwrite -> only paint if background is transparent
						if(((get_pixel32(tx,ty,pStencil)>>24) & 0xff ) == 0)
						{// ccc_tEmpty){
							paint=1;
						}
						} else if(remove){
							//	if(((get_pixel32(tx,ty,pStencil)) & ccc_tFull) != 0){
							err=putPixel(pTerrain,tx,ty , gInit->mapI.map.cmap.bgColor);
							if(err!=0){return err;}
							err=putPixel(pStencil,tx,ty , ccc_tRemove );//ccc_tEmpty);
							if(err!=0){return err;}
							//	}

						} else {
							paint=1;
						}

						if(paint==1){
							if(!overwrite){
								if(!remove){
									err=putPixel(pTerrain,tx,ty ,color);
									if(err!=0){return err;}
									if(k->data.pt->modif==15){
										err=putPixel(pStencil,tx,ty , ccc_tHidden );//ccc_tBrick);
										if(err!=0){return err;}
									} else {
										err=putPixel(pStencil,tx,ty , ccc_tNoOverwrite );//ccc_tBrick);
										if(err!=0){return err;}
									}
								} else {
									err=putPixel(pTerrain,tx,ty ,color);
									if(err!=0){return err;}
									err=putPixel(pStencil,tx,ty , ccc_tRemoveNoOverwrite );//ccc_tBrick);
									if(err!=0){return err;}
								}
							} else {
								err=putPixel(pTerrain,tx,ty ,color);
								if(err!=0){return err;}
								err=putPixel(pStencil,tx,ty , ccc_tFull );//ccc_tBrick);
								if(err!=0){return err;}
							}
						}
					}//fin tx
				}//fin ty
				k=k->next;
				cpt++;
				ok=0;
				//test_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0x0, 0x0, 0x0 ) ); test_blit(pTerrain, &camera, screen, NULL); if( SDL_Flip( screen ) == -1 ) { return 1;} //scanf("%d",&pas);
			}
			// elimination des pixel remove
			
			
			for(ty=0;ty<LEVEL_HEIGHT;++ty){
				for(tx=0;tx<LEVEL_WIDTH;++tx){
					color = get_pixel32(tx,ty,pStencil);
					if(color==ccc_tRemove){
						err=putPixel(pStencil,tx,ty , ccc_bgStencil );
						if(err!=0){return err;}
					} else {
						if(color!=ccc_bgStencil){
							err=putPixel(pStencil,tx,ty , ccc_tBrick );
							if(err!=0){return err;}
						}
					}
				}
			}
		

			//	//-DEBUG-printf("LES STEEL : ");
			k=gInit->s.ls;
			while ( k !=NULL ) {
				offset.x = k->x;
				offset.y = k->y;

				sf = gInit->mapI.map.tabGif[ADD_OBJ+(gInit->mapI.map.tiles*2)+(gInit->mapI.map.tilesObjet*2)+k->ID];

				offset.w = k->data.ps->w;
				offset.h = k->data.ps->h;
				//-DEBUG-printf("H=)> %d\n",offset.h);

				for(ty=k->y;ty<offset.h+k->y;++ty){

					if(ty<0) {ty=-1;continue;}
					if(ty>pTerrain->h){break;}
					if(ty>=LEVEL_HEIGHT) {break;}

					for(tx=k->x;tx<offset.w+k->x;++tx){

						if(tx<0) {tx=-1;continue;}
						if(tx>pTerrain->w){break;}
						if(tx>=LEVEL_WIDTH) {break;}		

						color = get_pixel32(tx,ty,pStencil);
						if(color==ccc_error){return 219;}
						if (color == ccc_black){
							continue; 
						} else {
							err=putPixel(pStencil,tx,ty , ccc_s );
							if(err!=0){return err;}
						}
					}
				}
				//		test_FillRect(pStencil, &offset, ccc_s);
				k=k->next;
			}
			//}

			return 0;
	}

	int paint_manip(SDL_Surface *spr,SDL_Rect from,SDL_Surface *t,SDL_Rect to,Uint32 ccc_t, Uint32 ccc_spr, int mode, SDL_Surface *stencil){

		int size,i,j,err;
		void *temp_pixels = NULL;
		Uint32 ccc_temp=0x00000000;
		Uint32 ccc_temp2=0x00000000;
		Uint32 ccc_temp3=0x00000000;
		
		//FULL
		if(mode==0){
			err=test_blit(spr,&from, t,&to,0);
			return err;
		}

		//-DEBUG-printf("paint_manip(), from.x==%4i, from.y==%4i, from.h==%4i, from.w==%4i, from to.x==%4i, to.y==%4i\n", from.x, from.y, from.h, from.w, to.x, to.y);

		if(from.y<0 || from.y > spr->h) {
			//-DEBUG-
			printf("paint_manip(1) rectFrom_Y out, from.x==%4i, FROM.Y==%4i, from.h==%4i, from.w==%4i, from to.x==%4i, to.y==%4i\n", from.x, from.y, from.h, from.w, to.x, to.y);
			return 0;
		}

		if(from.x<0 || from.x > spr->w) {
			//-DEBUG-
			printf("paint_manip(2) rectFrom_X out, FROM.X==%4i, from.y==%4i, from.h==%4i, from.w==%4i, from to.x==%4i, to.y==%4i\n", from.x, from.y, from.h, from.w, to.x, to.y);
			return 0;
		}

		if(to.y<0 || to.y > t->h) {
			//-DEBUG-
			printf("paint_manip(3) rectTO_Y out, from.x==%4i, from.y==%4i, from.h==%4i, from.w==%4i, from to.x==%4i, TO.Y==%4i\n", from.x, from.y, from.h, from.w, to.x, to.y);
			return 0;
		}

		if(to.x<0 || to.x > t->w) {
			//-DEBUG-
			printf("paint_manip(4) rectTO_X out, from.x==%4i, from.y==%4i, from.h==%4i, from.w==%4i, from TO.X==%4i, to.y==%4i\n", from.x, from.y, from.h, from.w, to.x, to.y);
			return 0;
		}

		if(from.y+from.h > spr->h) {
			from.h=spr->h - from.y;
		}

		if(from.x+from.w > spr->w) {
			from.w=spr->w - from.x;
		}		

		if(to.y+from.h > t->h) {
			from.h=t->h - to.y;
		}

		if(to.x + from.w > t->w) {
			from.w=t->w - to.x;
		}		

		size=spr->h*spr->pitch;
		temp_pixels=malloc(size);

		SDL_LockSurface(spr);
		SDL_LockSurface(t);
		if(stencil!=NULL) SDL_LockSurface(stencil);
		memcpy(temp_pixels,spr->pixels,size);

		//  NO OVERWRITE
		if(mode==4){

			for(j=0;j<from.h;j++) {
				for(i=0;i<from.w;i++) {
					/*				if((to.y+j)*t->w+(to.x+i)>=(t->w*t->h)){
					//-DEBUG-printf("out of bound1 x=%d + i=%d, y=%d + j=%d * t->w %d == %d >= %d (t->h %d, from.h %d) \n",to.x,i,to.y,j,t->w,(to.y+j)*t->w+(to.x+i),(t->w*t->h),t->h,from.h);
					return 56;
					//continue;
					}*/
					ccc_temp=((Uint32 *)t->pixels)[(to.y+j)*t->w+(to.x+i)];
					//-DEBUG-printf("ccc_temp1 = %p == %p t->format->colorkey\n",ccc_temp,t->format->colorkey);
					if (ccc_temp!=ccc_t) {
						((Uint32 *)spr->pixels)[(from.y+j)*spr->w+from.x+i]=ccc_temp;//spr->format->colorkey;
					} 
				}
			}
		}
		// VIS ON TERRAIN
		if(mode==8){ 
			for(j=0;j<from.h;j++) {
				for(i=0;i<from.w;i++) {
					/*		if((to.y+j)*t->w+(to.x+i)>=(t->w*t->h)){
					//-DEBUG-printf("out of bound2 x=%d + i=%d, y=%d + j=%d * t->w %d == %d >= %d (t->h %d, from.h %d) \n",to.x,i,to.y,j,t->w,(to.y+j)*t->w+(to.x+i),(t->w*t->h),t->h,from.h);
					return 57;
					//continue;
					}*/
					ccc_temp=((Uint32 *)t->pixels)[(to.y+j)*t->w+(to.x+i)];
					//-DEBUG-printf("ccc_temp1 = %p == %p ccc_t\n",ccc_temp,ccc_t);
					if (ccc_temp==ccc_t) {
						((Uint32 *)spr->pixels)[(from.y+j)*spr->w+from.x+i]=ccc_t;//spr->format->colorkey;

					} 
				}
			}
		}
		// erease pixel
		if(mode==17){
			for(j=0;j<from.h;j++) {
				for(i=0;i<from.w;i++) {
					/*		if((to.y+j)*t->w+(to.x+i)>=(t->w*t->h)){
					//-DEBUG-printf("out of bound2 x=%d + i=%d, y=%d + j=%d * t->w %d == %d >= %d (t->h %d, from.h %d) \n",to.x,i,to.y,j,t->w,(to.y+j)*t->w+(to.x+i),(t->w*t->h),t->h,from.h);
					return 57;
					//continue;
					}*/
					ccc_temp=((Uint32 *)spr->pixels)[(from.y+j)*spr->w+from.x+i];//((Uint32 *)t->pixels)[(to.y+j)*t->w+(to.x+i)];
					ccc_temp2=((Uint32 *)t->pixels)[(to.y+j)*t->w+(to.x+i)];
					ccc_temp3=((Uint32 *)stencil->pixels)[(to.y+j)*stencil->w+(to.x+i)];
					//-DEBUG-printf("ccc_temp1 = %p == %p ccc_t\n",ccc_temp,ccc_t);
					if ((ccc_temp!=ccc_spr)&&((ccc_tBrick==ccc_temp3)||((ccc_temp3 & 0xff)==0xcb))) {
						((Uint32 *)spr->pixels)[(from.y+j)*spr->w+from.x+i]=ccc_t;//spr->format->colorkey;
					} else {
						((Uint32 *)spr->pixels)[(from.y+j)*spr->w+from.x+i]=ccc_temp2;
					}
				}
			}
		}
		// erease pixel
		if(mode==18){
			for(j=0;j<from.h;j++) {
				for(i=0;i<from.w;i++) {
					/*		if((to.y+j)*t->w+(to.x+i)>=(t->w*t->h)){
					//-DEBUG-printf("out of bound2 x=%d + i=%d, y=%d + j=%d * t->w %d == %d >= %d (t->h %d, from.h %d) \n",to.x,i,to.y,j,t->w,(to.y+j)*t->w+(to.x+i),(t->w*t->h),t->h,from.h);
					return 57;
					//continue;
					}*/
					ccc_temp=((Uint32 *)spr->pixels)[(from.y+j)*spr->w+from.x+i];//((Uint32 *)t->pixels)[(to.y+j)*t->w+(to.x+i)];
					ccc_temp2=((Uint32 *)t->pixels)[(to.y+j)*t->w+(to.x+i)];
					//-DEBUG-
					//	printf("ccc_temp = %p != %p ccc_spr && ccc_tBrick %p == %p ccc_temp2\n",ccc_temp,ccc_spr,ccc_tBrick,ccc_temp2);
					if ((ccc_temp!=ccc_spr)&&((ccc_tBrick==ccc_temp2)||((ccc_temp2 & 0xff)==0xcb))) {
						((Uint32 *)spr->pixels)[(from.y+j)*spr->w+from.x+i]=ccc_t;//spr->format->colorkey;
					} else {
						((Uint32 *)spr->pixels)[(from.y+j)*spr->w+from.x+i]=ccc_temp2;
					}
				}
			}
		}						
		// objet trap
		if(mode==19){
			for(j=0;j<from.h;j++) {
				for(i=0;i<from.w;i++) {
					/*		if((to.y+j)*t->w+(to.x+i)>=(t->w*t->h)){
					//-DEBUG-printf("out of bound2 x=%d + i=%d, y=%d + j=%d * t->w %d == %d >= %d (t->h %d, from.h %d) \n",to.x,i,to.y,j,t->w,(to.y+j)*t->w+(to.x+i),(t->w*t->h),t->h,from.h);
					return 57;
					//continue;
					}*/
					ccc_temp=((Uint32 *)spr->pixels)[(from.y+j)*spr->w+from.x+i];//((Uint32 *)t->pixels)[(to.y+j)*t->w+(to.x+i)];
					ccc_temp2=((Uint32 *)t->pixels)[(to.y+j)*t->w+(to.x+i)];
					//-DEBUG-printf("ccc_temp1 = %p == %p ccc_t\n",ccc_temp,ccc_t);
					if (ccc_temp!=ccc_spr){
						((Uint32 *)spr->pixels)[(from.y+j)*spr->w+from.x+i]=ccc_t;//spr->format->colorkey;
					} else {
						((Uint32 *)spr->pixels)[(from.y+j)*spr->w+from.x+i]=ccc_temp2;
					}
				}
			}
		}	


		if(stencil!=NULL) SDL_UnlockSurface(stencil);
		SDL_UnlockSurface(t);
		SDL_UnlockSurface(spr);
		test_blit(spr,&from, t,&to,0);

		SDL_LockSurface(spr);
		memcpy(spr->pixels,temp_pixels,size);
		SDL_UnlockSurface(spr);

		free(temp_pixels);

		return 0;

	}