void makeUserLevelList() { //List userlevels userLevelFiles = initList(); int i=0; levelInfo_t* tl; char* buf = malloc(sizeof(char)*256); while(1) { sprintf(buf, "%s/level%03i.wzp", getUserLevelDir(), i); tl=mkLevelInfo( buf ); if( tl ) { listAddData(userLevelFiles, (void*)tl); } else { break; } i++; } numUserLevels=listSize(userLevelFiles); free(buf); }
int parseMultiPart(char *boundary) { int bound_len=strlen(boundary), type, startat, finish=false; char *name=NULL, *ctyp=NULL, *fname=NULL; MFILE *mf=mfopen(); while((startat=miscFReadLn(stdin, mf))!=-1) { if(strncmp(boundary, mfGetDataAt(mf, startat), bound_len)) continue; /* new part found - insert old part and init new one... * no insert at FIRST header ... * at end of multipart ??? "--boundary--" line ? */ if(!strncmp("--", mfGetDataAt(mf, mfGetLength(mf)-4), 2)) finish=true; mfSetLength(mf, startat); if(name!=NULL) { /* strip memfile, since one more <cr><lf> or only <lf> at end */ mf->used--; if(*(char*)((int)mf->data+mf->used-1) == '\r') mf->used--; if(type==CgiKindFile) { listAddData(type, name, fname, ctyp, mf); mf=mfopen(); } else listAddData(type, name, mfGetData(mf), ctyp, NULL); } if(finish==true) return(true); type=parseMultiHead(&name, &fname, &ctyp); mfSetLength(mf, 0); } mfclose(mf); free(name); free(fname); free(ctyp); return true; }
void makeLevelList(listItem** list, const char* dir) { int i=0; char* buf = malloc(sizeof(char)*1024); //Init the list to hold the filenames *list = initList(); levelInfo_t* tl; //List all levels in dir. while(1) { sprintf(buf, "%s/levels/level%03i.wzp",dir, i); tl=mkLevelInfo( buf ); if(tl) { listAddData(*list, (void*)tl); } else { break; } i++; //Increment file-number. } // Add a "Completed" level at the very end of the list tl=malloc(sizeof(levelInfo_t)); memset(tl, 0, sizeof(levelInfo_t)); sprintf( buf, packGetFile(".","complete.png"),dir ); tl->imgFile = malloc( sizeof(char)*(strlen(buf)+1) ); strcpy(tl->imgFile, buf); listAddData(*list, (void*)tl); free(buf); buf=0; }
void _camRecord() { //add frame to list camData* d = malloc( sizeof(camData) ); *d = cam; if( camPlaybackPosition == CAM_PLAYBACK_POSITION_RELATIVE ) { // d->pos = eoVec3FromPoints( relStartPos,d->pos ); // d->target = eoVec3FromPoints( relStartTar, d->target ); } listAddData( camRecordData, (void*)d ); }
void initStars(SDL_Surface* screen) { rockets = initList(); stars = initList(); star_t* star; int i; uint8_t col; for(i=0; i < NUMSTARS; i++) { star = malloc(sizeof(star_t)); star->x = rand()%(SCREENW*10); star->y = rand()%(SCREENH*10); star->sx = rand()%10; //xpos and ypos are to be divided by 10 star->sy = 0; col = rand()%230+25; //from 25 to 255 star->color = SDL_MapRGB(screen->format, col,col,col); listAddData(stars, (void*)star); //Add star to list } }
//Adds the level to list if it's not allready there. void addUserLevel(const char* fn) { levelInfo_t* tl; listItem* l=userLevelFiles; //Check if it's there while( (l=l->next) ) { if(strcmp( ((levelInfo_t*)l->data)->file, fn )==0) { return; } } tl=mkLevelInfo(fn); if(tl) { listAddData(userLevelFiles, (void*)tl); numUserLevels=listSize(userLevelFiles); } else { printf("Strange error, couldn't open saved level.\n"); } }
/* nearly untouched function from cgic0.5 - works fine */ int parseQueryString(const char *str, int length) /* if str is NULL, read from stdin */ { unsigned int i = 0; int c, valuei, namei; /* char *name=(char*)strdup(""), *value=(char*)strdup(""); */ char *name=malloc(length); /* Fixed by BCH 1/15/01 */ char *value=malloc(length); /* Fixed by BCD 1/15/01 */ strcpy(name, ""); strcpy(value, ""); while (i < length) { namei=0; while (i < length) { /* if str!=NULL => read string, we got from getenv - else, read stdin */ c=locgetc(str, i); if (c == EOF) return false; i++; if (c == '=' || c == '&') break; /* name=realloc(name, namei+1); */ /* Fixed by BCH 1/15/01 */ name[namei++]=c; } name[namei] = 0; /* After an entry name, always expecting to find '='. */ if (c != '=') { cgi_errno = CGIERR_IURLENC; return false; } valuei=0; while (i < length) { c = locgetc(str, i); if (c == EOF) return false; i++; if (c == '&' || c == '=') break; /* value=realloc(value, valuei+1); */ /* Fixed by BCH 1/15/01 */ value[valuei++]=c; } /* If this isn't the last entry, expecting to find '&' after it. If it's the last, but there is '&' - error. */ if (i < length) { if (c != '&') { cgi_errno = CGIERR_IURLENC; return 0; } } else if (c == '&') { cgi_errno = CGIERR_IURLENC; return 0; } /* Here check is not needed, because valbuf_index is always less than CGI_MAXVAL and valbuf is CGI_MAXVAL+1 bytes long. */ value[valuei] = 0; if (!miscStringDecode(name) || !miscStringDecode(value)) { cgi_errno = CGIERR_IURLENC; return 0; } if(!listAddData(CgiKindValue, name, value, NULL, NULL)) return(false); } free(name); free(value); return true; }
listItem* eoLoadScene( const char* fileName, engObjInitFunc objInit ) { char line[2048]; char set[1024]; char val[1024]; listItem* objs = NULL; engObj_s* obj = NULL; int i=0; char **vec; FILE* fp = fopen( fileName, "r" ); //Open file if( fp ) { objs = initList(); eoPrint("Loading %s ...", fileName); while( fgets(line, 1023, fp) ) { stripNewLine(line); if( strlen(line) > 2 ) { if( strcmp(line,"[model]") == 0 ) { obj = eoObjCreate( ENGOBJ_MODEL ); } else if( strcmp(line,"[sprite]" ) == 0 ) { eoPrint("Created Sprite entity."); obj = eoObjCreate( ENGOBJ_SPRITE ); } else if( strcmp(line,"[emitter]") == 0 ) { eoPrint("Created Emitter entity."); obj = eoObjCreate( ENGOBJ_PAREMIT ); } else if( strcmp(line, "[sound]" ) == 0 ) { eoPrint("Created Sound entity."); obj = eoObjCreate( ENGOBJ_SOUND ); } else if( strcmp(line,"[end]") == 0 ) { if( objInit ) { objInit(obj); } listAddData( objs, (void*)obj ); } else { if( splitVals( '=', line, set, val ) ) { if( strcmp(set,"class") == 0 ) { obj->className = malloc(strlen(val)+1); strcpy(obj->className,val); } else if( strcmp(set,"rot") == 0 ) { vec = explode(',', val, 3); obj->rot.x=atof(vec[0]); obj->rot.y=atof(vec[1]); obj->rot.z=atof(vec[2]); free(vec[0]); free(vec[1]); free(vec[2]); free(vec); } else if( strcmp(set,"pos") == 0 ) { vec = explode(',', val, 3); obj->pos.x=atof(vec[0]); obj->pos.y=atof(vec[1]); obj->pos.z=atof(vec[2]); free(vec[0]); free(vec[1]); free(vec[2]); free(vec); } else if( strcmp(set,"file") == 0 ) { //Todo: Cache models i=charrpos(val,'/')+1; strcpy(set, val+i); val[i]=0; obj->model = eoModelLoad( val, set ); } else { eoPrint("Unknown data: %s ('%s' and '%s')",line,set,val); } } else eoPrint("Invalid text: %s",line); } } // free(objName); } } else { eoPrint("Error, could not open file %s for reading...", fileName); } return(objs); }
int dirScan( const char* dir,int type, listItem* list ) { entity* fe; FILE* f; struct dirent *pent; struct stat st; char* buf=malloc(sizeof(char)*2048); DIR *pdir= opendir( dir ); if(pdir) { while( (pent=readdir(pdir)) ) { //We're not going to read hidden files or . / .. if(pent->d_name[0] != '.') { sprintf(buf, "%s/%s",dir,pent->d_name); if(stat(buf, &st)==0) { if( (st.st_mode&S_IFDIR)==S_IFDIR ) { if( type==TYPE_DIR) { fe = malloc( sizeof( entity ) ); fe->type=TYPE_DIR; fe->dataSize = 0; strcpy( fe->name, buf ); listAddData( list, (void*)fe ); } if(!dirScan( buf, type, list )) { return(0); } } else if( (st.st_mode&&S_IFREG)) { if( type== TYPE_FILE ) { fe = malloc( sizeof( entity ) ); fe->type=TYPE_FILE; strcpy( fe->name, buf ); f = fopen( buf, "rb" ); if(!f) { printf("Could not read file %s\n", buf); return(0); } fseek(f, 0L, SEEK_END); fe->dataSize = ftell(f); fseek(f, 0L, SEEK_SET); fe->data = malloc( fe->dataSize+strlen(fe->name) ); strcpy( fe->data, fe->name ); if( fread( (void*)(((char*)fe->data)+strlen(fe->name)), fe->dataSize, 1, f ) != 1 ) { printf("Could not read input data from file '%s'.\n", fe->name); return(0); } listAddData( list, (void*)fe ); } } else { printf("Bundles must only contain directories and regular files.\n"); return(0); } } } } return(1); } printf("Could not open directory: %s\n",dir); return(0); }
void bundle( const char* file, const char* inDir) { FILE* f; entity* e; listItem* entryList = initList(); listItem* it=entryList; bundleHeader_t header; bundleFileEntry* be; uint32_t dataOffset=0; header.version = bundleFileVersion; sprintf( header.ident, "%s", bundleFileIdentString ); char endMark[] = bundleFileEndMarkString; e = malloc(sizeof(entity) ); e->data=0; e->dataOffset=0; e->dataSize=0; strcpy( e->name, inDir ); e->type = TYPE_DIR; listAddData( entryList, (void*)e ); printf("Listing directories...\n"); if( dirScan( inDir, TYPE_DIR, entryList ) ) { header.numEntries = listSize(entryList); printf("Added %i directories.\n", header.numEntries ); printf("Listing files...\n"); if( dirScan( inDir, TYPE_FILE, entryList ) ) { printf("Added %i files.\n", (listSize(entryList)-header.numEntries) ); header.numEntries = listSize(entryList); printf("There are now %i entries.\n", header.numEntries); //First dataoffset is after the list of all entries dataOffset = sizeof(bundleHeader_t) + (sizeof(bundleFileEntry)*header.numEntries); printf("Bundling...\n"); f = fopen( file, "wb" ); if(f) { //Write header fwrite( (void*)(&header), sizeof(bundleHeader_t),1, f ); //Write entries while( (it=it->next) ) { e = (entity*)it->data; be = malloc( sizeof(bundleFileEntry) ); be->type = e->type; be->nameLen = strlen( e->name ); be->dataOffset = dataOffset; be->dataLen = e->dataSize; fwrite( (void*)be, sizeof(bundleFileEntry), 1, f ); dataOffset += be->dataLen+be->nameLen; free(be); } it=entryList; //Write file data while( (it=it->next) ) { e = (entity*)it->data; //Write file-name if( e->type == TYPE_DIR ) { fwrite( (void*)e->name, sizeof(char)*strlen(e->name), 1, f); printf("Added Dir: %s\n", e->name); } //If it's a file, write content if( e->type == TYPE_FILE ) { fwrite( e->data, e->dataSize+strlen(e->name), 1, f); printf("Added File: %s\n", e->name); } } fwrite( (void*)&endMark, strlen(endMark), 1, f ); fclose(f); } else { printf("Could not open outputfile %s for writing.\n", file); } } else { printf("fileScan of %s failed.\n", inDir); } } else { printf("dirScan of %s failed.\n", inDir); } }
void fireWorks(SDL_Surface* screen) { rocket_t* tempRocket; star_t* tempStar; listItem* it; uint32_t colWhite = SDL_MapRGB(screen->format, 255,255,255); uint32_t colYellow = SDL_MapRGB(screen->format, 255,255,0); /* New Rockets */ nextExpl -= getTicks(); if(nextExpl < 1) { //Set new timer nextExpl = rand()%2000; //Fire a new rocket tempRocket = malloc(sizeof(rocket_t)); //Set initial position at y 240, and some random x tempRocket->y=(SCREENH*10); tempRocket->x=rand()%(SCREENW*10); //Set a direction that flies towards the middle tempRocket->sx = rand()%5; if(tempRocket->x > (HSCREENW*10) ) { tempRocket->sx *= -1; } tempRocket->sy = 0-rand()%30-20; //Set life tempRocket->life=rand()%1000+250+10; tempRocket->p = initList(); //Init particles for explosion int i, r=rand()%100; for(i=0; i < r; i++) { tempStar = malloc( sizeof(star_t) ); //Set dir to something random tempStar->sx = rand()%500-250; tempStar->sy = rand()%500-250; tempStar->color = SDL_MapRGB( screen->format, rand()%128+128,rand()%256,rand()%128); tempStar->life = rand()%3000+500; //Add to list listAddData( tempRocket->p, (void*)tempStar ); } //Add rocket to list listAddData(rockets, (void*)tempRocket); //Play launch sound sndPlay(SND_ROCKETLAUNCH, tempRocket->x/10); } /* Going through rockets and their particles */ it=rockets; listItem* itt; while( (it=it->next) ) { tempRocket=(rocket_t*)it->data; //If rocket is still alive, fly it if(tempRocket->life > 0) { //Age tempRocket->life -= getTicks(); //Set position for particles if it got too old if(tempRocket->life < 1) { itt=tempRocket->p; while( (itt=itt->next) ) { tempStar=(star_t*)itt->data; tempStar->x = tempRocket->x*10; tempStar->y = tempRocket->y*10; } //Play "Explosion" sound sndPlay(SND_ROCKETBOOM, tempRocket->x/10); } //Fly tempRocket->x += tempRocket->sx; tempRocket->y += tempRocket->sy; //Draw plotPixel(screen, tempRocket->x/10, tempRocket->y/10, colWhite ); plotPixel(screen, tempRocket->x/10, tempRocket->y/10+1, colYellow ); } else { //iterate through stars itt=tempRocket->p; int liveStars=0; while( (itt=itt->next) ) { tempStar=(star_t*)itt->data; //alive? if(tempStar->life > 0) { //Fly tempStar->x += tempStar->sx/10; tempStar->y += tempStar->sy/10; //Gravity if(tempStar->y < 10) tempStar->y += 20; //Draw if(tempStar->life > 1000 || tempStar->life % 2 == 0) plotPixel(screen, tempStar->x/100, tempStar->y/100, tempStar->color); else if(tempStar->life % 3 == 0) plotPixel(screen, tempStar->x/100, tempStar->y/100, colWhite); //age tempStar->life -= getTicks(); liveStars++; } //alive } //Check if it should still survice if(liveStars == 0) { //Remove stars itt=tempRocket->p; while( (itt=itt->next) ) { free(itt->data); } freeList(tempRocket->p); //Remove rocket free(it->data); listRemoveItem(rockets, it); } } //Sim rocket stars } //iterate through rockets }