summaryrefslogtreecommitdiff
path: root/sdl-test/SDL_tuto/TestParserLemmingsLVL/parse_ini.yy
blob: 9ec277398ad721ea4ede020b49032ebdfc3e1d82 (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
%{
	#include "y.tab.h"
	
	#include "SDL/SDL.h"
	#include "SDL/SDL_image.h"

	#include <sys/types.h>
	#include <dirent.h>

	#include <stdio.h>
	#include <stdlib.h>
	#include <string.h>
	



	//Les attributs de l'écran (640 * 480)
	const int SCREEN_WIDTH = 1280;
	const int SCREEN_HEIGHT = 480;
	const int SCREEN_BPP = 32;


	//Les surfaces
	SDL_Surface **tabGif = NULL;

	SDL_Surface *screen = NULL;

	//La structure d'événements
	SDL_Event event;


	struct list_int {
		int val;
		struct list_int *next;
	};

	struct val {
		int type;
		union ptr_u { char * str; struct list_int *ints; } ptr;
	};
	
	struct objectT {
		int id;
		int x;
		int y;
		int modif;
		struct objectT *next;
	};

	

	#define TYPE_COUNT 10
	enum terrainType { brick , bubble , crystal , dirt , fire , marble , pillar , rock , snow , special};
	
	char tabType[TYPE_COUNT][10] = {
	"brick" , "bubble" , "crystal" , "dirt" , "fire" , "marble" , "pillar" , "rock" , "snow" , "special" };
	
	#define IDENT_COUNT 11
	enum ident { 	releaseRate,
		numLemmings,
		numToRescue,
		timeLimit,
		numClimbers,
		numFloaters,
		numBombers,
		numBlockers,
		numBuilders,
		numBashers,
		numDiggers };
				

	char tabIdent[IDENT_COUNT][32] =  {
		"releaseRate",
		"numLemmings",
		"numToRescue",
		"timeLimit",
		"numClimbers",
		"numFloaters",
		"numBombers",
		"numBlockers",
		"numBuilders",
		"numBashers",
		"numDiggers"
		};

	struct gameInit {
		int para[IDENT_COUNT]; // parametres
		char* name; // name map 
		int xPos; // position de la trappe
		enum terrainType tType; //type map
		struct objectT *objet; //objet
		struct objectT *terrain;//terrain
	};
	
	extern int yylineno; // Bidouille pour avoir le numero de ligne du lexer ^^

	int yylex();
	void yyerror(struct gameInit *gInit, char *s);	
	
	
%}

%parse-param { struct gameInit *gInit }

%union { char* str; int num; struct list_int *ints; struct val *val; }

%token LEXERROR
%token AFF
%token EOL
%token VIR
%token <str> IDENT
%token <str> STR
%token <num> INT

%type <ints> extra_vals
%type <val> val
 
%start ini 

%%

ini:
| ini decl
| ini EOL

decl: IDENT AFF val EOL		{

					enum ident t; // TYPE TERRAIN
					enum terrainType s; // STYLE
					// 0 : objet_ int,int,int,int
					// 1 : terrain_ int,int,int,int
					// 2 : 
					int i,def=-1;
					struct objectT **listeItem;

//					printf("'%s' <- ", $1);

					// code de connard !!! mais donne les parametres de la map
					t=0;while((t<IDENT_COUNT)&&(strcmp($1,tabIdent[t]) == 0 )){t++;}
					if (t<IDENT_COUNT) { 
						gInit->para[t]=$3->ptr.ints->val; 
					} else {
				
						if (strcmp($1,"style") == 0){
							if ( $3->type == 0) { 
								printf("Fichier .ini corrompu (style n'est pas de style int)");
							} else {
								s=0;while((s<TYPE_COUNT)&&(strcmp($3->ptr.str,tabType[s]) == 0 )){s++;}
								if (s<TYPE_COUNT) { 
									gInit->tType=s;
								} else {
								
									printf("Style invalide (inconnu) !!\n");
							
								}
							}
						}
					
						if (strcmp($1,"name") == 0){
							gInit->name=$3->ptr.str;
						}
				
						if (strcmp($1,"xPos") == 0){
							gInit->xPos=$3->ptr.ints->val;
						}
					
						if(strncmp($1,"object_",7) == 0){
								//def = 0;
								listeItem=&gInit->objet;
						}
						if(strncmp($1,"terrain_",8) == 0){
								//def = 1;
								listeItem=&gInit->terrain;
						}
						if (def==0 || def == 1){
							struct list_int *curr=$3->ptr.ints;
							struct objectT *o = malloc(sizeof(struct objectT));
							i=0;
							while (curr!=NULL) {
							//	printf("%i ", curr->val);
								switch(i) {
									case 0: o->id=curr->val; break;
									case 1: o->x=curr->val; break;
									case 2: o->y=curr->val; break;
									case 3: o->modif=curr->val; break;
								}
								curr=curr->next; ++i;
							}
							o->next=*listeItem;
							*listeItem=o;
						}
					} 
				}

val: INT extra_vals		{	$$=malloc(sizeof(struct val));
					$$->type=1;
					$$->ptr.ints=malloc(sizeof(struct list_int));
					$$->ptr.ints->val=$1;
					$$->ptr.ints->next=$2;
				}
| STR				{	$$=malloc(sizeof(struct val));
					$$->type=0;
					$$->ptr.str=$1;
				}

extra_vals:			{ $$=NULL; }
| extra_vals VIR INT		{ $$=malloc(sizeof(struct list_int)); $$->val=$3; $$->next=$1; }


%%

void yyerror(struct gameInit *gInit, char *s)
{
	//fprintf(stderr, "Syntax Error : '%s' at l%d,c%d-l%d,c%d\n", s, @$.first_column, @$.last_line, @$.last_column); // Nécessite l'option %locations et un lexer qui va bien
	fprintf(stderr, "(stdin):%i: error: %s\n", yylineno, s);
	exit(1);
}

SDL_Surface *load_image( char* filename )
{
    //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 );

        //Libération de l'ancienne image
        SDL_FreeSurface( 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, 0, 0xFF, 0xFF ) );
        }
    }

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

void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip )
{
    SDL_Rect offset;
    offset.x = x;
    offset.y = y;

    //On blitte la surface
    SDL_BlitSurface( source, clip, destination, &offset );
}

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

    //Mise en place de l'écran
    screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );

    //S'il y a une erreur lors de la mise en place de l'écran
    if( screen == NULL )
    {
        return 2;
    }

    //Mise en place de la barre caption
    SDL_WM_SetCaption( "Test_LVL", NULL );

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

SDL_Surface* flipSurfaceUD(SDL_Surface* src) {
	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);

//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++) {
				dest[ (src->h-i-1)*src->w + j ] = source[ i*src->w + j];
			}
		}
	}

	return copy_surface;
}


int load_fields(char *folder, int *nb_files, enum terrainType t ) {
	int i, res,lt;
	char *temp, filepath[255]; // FIXME
	char *styleT;
	char *styleTunder;
	struct dirent *lecture;
	DIR *rep;
	// TODO : faire pareil avec les objets
	styleT = tabType[t];
	
	styleTunder = strcat(styleT,"_");
	
	lt = strlen(styleTunder);
	
	temp = malloc(sizeof(char)*(lt+1));
	
	*nb_files=0;
	rep = opendir(folder);
	while ((lecture = readdir(rep))) {
		
		if ( strlen(lecture->d_name) > lt ) {
			strncpy(temp,lecture->d_name,lt);
			if ( (res=strcmp(temp, styleTunder)) == 0 ) {
				++(*nb_files);	
				printf("TRACE FICHIER: %s\n", lecture->d_name);
			} else {
				printf("BUG : %s != %s : %d\n", temp, styleTunder, res);
			}
		}
	}
	closedir(rep);
	
	tabGif=malloc(*nb_files * sizeof(SDL_Surface *));
	
	for(i=0; i < *nb_files; i++) {
		sprintf(filepath, "%s/%s%d.gif", folder, styleTunder , i);
		tabGif[i] = load_image(filepath);
		if ( tabGif[i] == NULL ) { 
			printf("ERREUR load_file: %s\n", filepath);
      return 1;
		}
	}
	
	return 0;
}

int load_files(struct gameInit *gInit)
{
	int res, nb_files;
	char *folder;
	
		yyparse(gInit);
		
		folder = strcat("/home/jazzblue/Bureau/Projet-Lemmings/trunk/styles/", tabType[gInit->tType]);

		
		res = load_fields(folder, &nb_files, gInit->tType);
		if ( res != 0 ) {
			return -1;
		}

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

void clean_up()
{
    //On libère la feuille de sprites
//    SDL_FreeSurface( faces );

    //On quitte SDL
    SDL_Quit();
}


int main (int argc, char **argv)
{	// Par défaut, c'est l'analyse LEXICALE qui est lancée !
	
int  res;
    //Ce qui va nous permettre de quitter
    int quit = 0;

		struct gameInit gInit;
	
    //Initialisation
    res=init();
		if ( res != 0 ) {
			return res;
		}

    //Chargement des fichiers
		res=load_files(&gInit);
 		if ( res < 1 ) {
			return res;
		}

		//On remplit l'écran de blanc
    SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0xCC, 0xCC, 0xCC ) );
    
    
		/*for(i=0;i<res;i++) {
		printf("%i\n", i);
			apply_surface( i*32, 0, tabGif[i], screen, NULL);
		}*/

    //Tant que l'utilisateur n'a pas quitter
    while( quit == 0 )
    {
				//Mise à jour de l'écran
				if( SDL_Flip( screen ) == -1 )
				{
				    return 1;
				}
    
        //Tant qu'il y a un événement
        while( SDL_PollEvent( &event ) )
        {
            //Si l'utilisateur à appuyer sur le X de la fenêtre
            if( event.type == SDL_QUIT )
            {
                //On quitte le programme
                quit = 1;
            }
        }
        
    	 SDL_Delay(100);
    }

    //On libère les images et on quitte SDL
    clean_up();

    return 0;
}