PREFIX spConfigPointer spConfigRead(char* filename,char* subfolder) { spConfigPointer config = (spConfigPointer)malloc(sizeof(spConfig)); config->firstEntry = NULL; config->lastEntry = NULL; config->filename = (char*)malloc(strlen(filename)+strlen(subfolder)+128); spConfigGetPath(config->filename,subfolder,filename); spFilePointer file = SDL_RWFromFile(config->filename,"rb"); if (file == NULL) return config; char buffer[1024]; char key[64]; char value[512]; while (spReadOneLine(file,buffer,1024) == 0) { if (buffer[0] == '#') //comment { key[0] = 0; sprintf(value,"%s",&(buffer[1])); internalNewEntry(config,key,value); continue; } char* middle = strchr(buffer,':'); if (middle == NULL) continue; middle[0] = 0; sprintf(key,"%s",buffer); middle++; while (middle[0] == ' ') middle++; sprintf(value,"%s",middle); internalNewEntry(config,key,value); } SDL_RWclose(file); return config; }
PREFIX spSpriteCollectionPointer spLoadSpriteCollection(char* filename,SDL_Surface* fallback_surface) { SDL_RWops *file = SDL_RWFromFile(filename, "rt"); if (!file) return NULL; spSpriteCollectionPointer collection = spNewSpriteCollection(); //Loading the file line by line int end = 0; spSpritePointer sprite = NULL; char surface_d[1024] = ""; //border default int bw_d = 0; int bh_d = 0; //frame default int fw_d = 0; int fh_d = 0; int fps_d = 0; char sprite_d[1024] = ""; char surface[1024] = ""; int bw = bw_d; int bh = bh_d; int fw = fw_d; int fh = fh_d; int fps = fps_d; while (!end) { char line[512]; end = spReadOneLine(file,line,512); //parsing the line if (line[0] == '#' || line[0] == ';') {} //comment else if (line[0] == '[') //new sprite { int i; for (i = 1; line[i]!=']' && line[i]!=0; i++); line[i] = 0; sprite = spNewSprite(&(line[1])); spAddSpriteToCollection(collection,sprite); sprintf(surface,"%s",surface_d); bw = bw_d; bh = bh_d; fw = fw_d; fh = fh_d; fps = fps_d; } else //some keywords { //searching the '=', ' ' or \0. int i; for (i = 1; line[i]!='=' && line[i]!=' ' && line[i]!=0; i++); if (line[i]==0) //hm, not good... continue; int keyword = 0; if (line[i]=='=') { line[i] = 0; keyword = spSpriteCollectionGetKeyword(line); line[i] = '='; } else // ' ' { line[i] = 0; keyword = spSpriteCollectionGetKeyword(line); line[i] =' '; for (i++;line[i]!='=' && line[i]!=0; i++); if (line[i]==0) //hm, not good... continue; } for (i++;line[i]==' '; i++); char* value = &(line[i]); int j; for (j = strlen(value)-1;value[j]==' ' && j>=0;j--); value[j+1] = 0; int x,y,n; switch (keyword) { case 1: //"default" sprintf(sprite_d,"%s",value); break; case 2: //"image" if (sprite) sprintf(surface,"%s",value); else sprintf(surface_d,"%s",value); break; case 3: //fps if (sprite) fps = atoi(value); else fps_d = atoi(value); break; case 4: //framesize if (sprite) fw = atoi(value); else fw_d = atoi(value); for (i++;line[i]!=',' && line[i]!=0; i++); if (line[i] == 0) continue; value = &(line[i+1]); if (sprite) fh = atoi(value); else fh_d = atoi(value); break; case 5: //bordersize if (sprite) bw = atoi(value); else bw_d = atoi(value); for (i++;line[i]!=',' && line[i]!=0; i++); if (line[i] == 0) continue; value = &(line[i+1]); if (sprite) bh = atoi(value); else bh_d = atoi(value); break; case 6: //frame if (!sprite) break; x = atoi(value); for (i++;line[i]!=',' && line[i]!=0; i++); if (line[i] == 0) continue; value = &(line[i+1]); y = atoi(value); for (i++;line[i]!=',' && line[i]!=0; i++); SDL_Surface* s = spLoadSurface( surface ); if (s == NULL) s = fallback_surface; if (line[i] == 0) spNewSubSpriteWithTiling(sprite,s,x+(bw-fw)/2,y+(bh-fh)/2,fw,fh,1000/(fps>0?fps:1)); else { value = &(line[i+1]); n = atoi(value); if (n > 0) spNewSubSpriteTilingRow(sprite,s,x+(bw-fw)/2,y+(bh-fh)/2,fw,fh,bw,bh,n,1000/(fps>0?fps:1)); } //We loaded the surface one time more, than we will it delete later, so lets decrease the ref counter: spDeleteSurface( s ); break; } } } spSelectSprite(collection,sprite_d); SDL_RWclose(file); return collection; }