/* Asset Package Compiler */ %{ #include #include #include #include "sprite.h" #include "symbol.h" #include extern void yyerror(); extern int rinit(); extern int yylex(); #define DIR_SIZE 128/* size when all files have been concatenated together */ char dir_files[DIR_SIZE]; FILE* yyin; int populate_yyin(FILE*); int handle_vdat(char *); int eval_png_file(char*); int read_png_file(char*); %} %define parse.error verbose %union{ char* str; } /* %token EXT */ /* %token FW */ /* %token SPR */ /* %token CC */ /* /\* Rules *\/ */ /* %% */ /* SS: SPR '/' MD '.' EXT */ /* MD: CC '_' FW */ /* | FW */ /* | CC */ %token VDAT %token ODAT %token ADAT /* Rules */ %% FLIST : FNAME /* Now output to VDAT.o */ FNAME : VDAT {printf("handle_vdat(%s)\n", $1);handle_vdat(yylval.str);printf("handled vdat\n");} | ADAT | ODAT %% int main(int argc, char** argv) { /* Take arguments */ /* Concatenate all files into buf, they are null-terminated by default */ FILE *fp; fp = fopen("all_files", "w+"); if(!populate_yyin(fp)) printf("error processing the directory"); yyin = fp; rinit(); yyparse(); /* runs handle_vdat after token is scanned */ return 0; } void yyerror (char const *s) { fprintf(stderr, "%s\n", s); } /* TODO: READ INTO SINGLE BUFFER, NOT A FILE */ int populate_yyin(FILE* fp) { int files_pos = 0; int file_len = 0; struct dirent* ep; DIR* dp = opendir("pngfiles"); /* Concatenate all file names, seperated by '\0' into files[] */ if(dp != NULL) { while(ep = readdir(dp)) { file_len = strlen(ep->d_name); fwrite(ep->d_name, 1, file_len, fp); printf("writing %s to file\n", ep->d_name); } fseek(fp, SEEK_SET, 0); return 0; } else {return 1; } closedir(dp); } /* Analyze file_name for information on sprite sheet, store sprite sheet and label into sprite then push sprite onto sprites[]*/ int handle_vdat(char* file_name) { /* Parse the file_name for data */ /* Get the specs of the PNG file */ if(!eval_png_file(file_name)) { printf("Failed in read_png_file\n"); return 1; } /* Insert sprite sheet from PNG into sprite_sheets */ push_sprite(); /* Store dir/filename in label as well as any other data that fits */ } int eval_png_file(char* file_name) { /* Evaluate file_name for metadata */ /* Extracts header info from png_file */ read_png_file(file_name); /* Create the symbol and put it in symbol_table[]*/ return 0; } /* variables for libPNG */ int png_width,png_height; png_byte bit_depth; png_byte color_type; png_structp png_ptr; png_infop info_ptr; int read_png_file( char* file_name) { /* Setup PNG file */ char header[8]; int is_png = 0; char* dir = "pngfiles"; /* TODO: Change this fopen to a real var! */ FILE *fp = fopen("pngfiles/env_street_road03_0.png","rb"); if(!fp) { printf("Could not read file %s in handle_vdat()", file_name); return 1; } fread(header, 1, 8, fp); is_png = !png_sig_cmp(header, 0, 8); if(!is_png) return 1; png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); if (!png_ptr) { printf("Failed to allocate png_ptr\n"); return 1; } info_ptr = png_create_info_struct(png_ptr); if (!info_ptr) { printf("Failed to create info_ptr\n"); png_destroy_read_struct(&png_ptr,(png_infopp)NULL, (png_infopp)NULL); return 1; } png_infop end_info = png_create_info_struct(png_ptr); if (!end_info) { printf("Failed to create end_info \n"); png_destroy_read_struct(&png_ptr,(png_infopp)NULL, (png_infopp)NULL); return 1; } if(setjmp(png_jmpbuf(png_ptr))) { printf("Failed in init_io\n"); png_destroy_read_struct(&png_ptr,(png_infopp)NULL, (png_infopp)NULL); fclose(fp); return 1; } png_init_io(png_ptr, fp); png_set_sig_bytes(png_ptr, 8); png_read_info(png_ptr, info_ptr); png_width = png_get_image_width(png_ptr, info_ptr); png_height = png_get_image_height(png_ptr, info_ptr); color_type = png_get_color_type(png_ptr, info_ptr); bit_depth = png_get_bit_depth(png_ptr, info_ptr); png_read_update_info(png_ptr, info_ptr); return 0; }