From c6be694faa16d4594694916e915906e7600c9fb2 Mon Sep 17 00:00:00 2001 From: Damien Appert Date: Sun, 19 Sep 2010 19:41:17 +0000 Subject: super version qui marche pas ! on en est juste avant de placer les texture sur le terrain git-svn-id: file:///var/svn/2010-netlemmings/trunk@45 077b3477-7977-48bd-8428-443f22f7bfda --- sdl-test/SDL_tuto/TestParserLemmingsLVL/Makefile | 2 +- .../SDL_tuto/TestParserLemmingsLVL/parse_ini.lex | 1 + .../SDL_tuto/TestParserLemmingsLVL/parse_ini.yy | 391 ++++++++++++++++++++- 3 files changed, 374 insertions(+), 20 deletions(-) (limited to 'sdl-test') diff --git a/sdl-test/SDL_tuto/TestParserLemmingsLVL/Makefile b/sdl-test/SDL_tuto/TestParserLemmingsLVL/Makefile index 3285451..0498459 100644 --- a/sdl-test/SDL_tuto/TestParserLemmingsLVL/Makefile +++ b/sdl-test/SDL_tuto/TestParserLemmingsLVL/Makefile @@ -1,7 +1,7 @@ # Makefile #PATH = /usr/include/SDL TARGET = main -OBJECTS = main.o +OBJECTS = CFLAGS = -O3 -Wall -g -I/usr/include/SDL -L/usr/lib LIBS = -lSDL -lSDL_image -lfl diff --git a/sdl-test/SDL_tuto/TestParserLemmingsLVL/parse_ini.lex b/sdl-test/SDL_tuto/TestParserLemmingsLVL/parse_ini.lex index 1fc51b9..b665ae3 100644 --- a/sdl-test/SDL_tuto/TestParserLemmingsLVL/parse_ini.lex +++ b/sdl-test/SDL_tuto/TestParserLemmingsLVL/parse_ini.lex @@ -22,6 +22,7 @@ BLANK [ \t] NOT_BLANK [^ \t] IDENT [a-zA-Z][a-zA-Z0-9_-]* + %x INI_VALUE /* %s : inclusive start condition (inclus aussi les règles sans ) %x : exclusive start condition (règles sans inactives) diff --git a/sdl-test/SDL_tuto/TestParserLemmingsLVL/parse_ini.yy b/sdl-test/SDL_tuto/TestParserLemmingsLVL/parse_ini.yy index 3d922f3..9ec2773 100644 --- a/sdl-test/SDL_tuto/TestParserLemmingsLVL/parse_ini.yy +++ b/sdl-test/SDL_tuto/TestParserLemmingsLVL/parse_ini.yy @@ -1,15 +1,32 @@ %{ #include "y.tab.h" + + #include "SDL/SDL.h" + #include "SDL/SDL_image.h" + + #include + #include #include #include #include + - extern int yylineno; // Bidouille pour avoir le numero de ligne du lexer ^^ - int yylex(); - void yyerror(char *s); + //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 { @@ -21,8 +38,70 @@ 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 @@ -36,7 +115,7 @@ %type extra_vals %type val -%start ini +%start ini %% @@ -45,21 +124,72 @@ ini: | ini EOL decl: IDENT AFF val EOL { - printf("'%s' <- ", $1); - switch($3->type) { - case 1:// list INT - printf("( "); + + 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((tpara[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((sptr.str,tabType[s]) == 0 )){s++;} + if (stType=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); - curr=curr->next; + // 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; } - printf(")\n"); - break; - case 0:// STR - printf("'%s'\n",$3->ptr.str); - break; - } + o->next=*listeItem; + *listeItem=o; + } + } } val: INT extra_vals { $$=malloc(sizeof(struct val)); @@ -79,18 +209,241 @@ extra_vals: { $$=NULL; } %% -void yyerror(char *s) +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 ! - yyparse(); + +int res; + //Ce qui va nous permettre de quitter + int quit = 0; - return 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