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 }
int main() { int i; printf("Test: New list.\n"); list_t* list = listInit(freeItem); listDebugShow(list,LIST_DEBUG_SHOW_BACKWARD|LIST_DEBUG_SHOW_FORWARD); listItem* it; printf("\nTest: Appending 4 items to list.\n"); listAppendData(list, mkItem(1, "Fisk") ); listAppendData(list, mkItem(2, "Hund") ); it=listAppendData(list, mkItem(3, "Hest") ); listAppendData(list, mkItem(4, "Rose") ); listDebugShow(list,LIST_DEBUG_SHOW_SHORT); showItems(list); printf("\nTest: Prepending 4 items.\n"); listPrependData(list, mkItem(5, "Laks") ); listPrependData(list, mkItem(6, "Ko") ); listPrependData(list, mkItem(7, "Tiger") ); listPrependData(list, mkItem(8, "Zebra") ); listDebugShow(list,LIST_DEBUG_SHOW_SHORT); showItems(list); printf("\nTest: Removing item (Hest) from list.\n"); listRemoveItem(list,it, LIST_PREV); listDebugShow(list,LIST_DEBUG_SHOW_SHORT); showItems(list); printf("\nTest: Inserting 4 items\n"); listInsertAtIdx(list, mkItem(1, "New Pos 0"), 0 ); it=listInsertAtIdx(list, mkItem(1, "New Pos 2"), 2 ); listInsertAtIdx(list, mkItem(1, "New Pos 8"), 8 ); listInsertAtIdx(list, mkItem(list->count, "New Last Pos"), list->count ); listDebugShow(list,LIST_DEBUG_SHOW_SHORT); showItems(list); printf("\nTest: Insert after item %p (New Pos 2)\n"); listInsertAfterItem(list, it, mkItem(2, "New Pos 3") ); listDebugShow(list,LIST_DEBUG_SHOW_SHORT); showItems(list); printf("\nTest: Getting data.\n"); for(i=0; i < list->count; i++) { printf(" Idx %i Got data: %p\n", i, listGetItemAt(list, i )->data ); } printf("\nTest: Filling list into array.\n"); item** arr = (item**)listAddToArray( malloc(sizeof(void*)*list->count), list ); int idx; for(idx=0; idx < list->count; idx++) { printf(" Arr[%i] = %p\n",idx,arr[idx] ); showItem(arr[idx]); } printf("\nTest: Appending array to list2.\n"); list_t* list2 = listAddFromArray(listInit(NULL), (void*)arr, list->count); listDebugShow(list2,LIST_DEBUG_SHOW_SHORT); showItems(list2); list_t* list3 = listAddFromArray(listInit(NULL), (void*)arr, list->count); free(arr); printf("\nRemoving all list2 elements individually in a forward loop."); it=&list2->begin; while( LISTFWD(list2,it) ) { it=listRemoveItem(list2,it, LIST_PREV); } listDebugShow(list2, LIST_DEBUG_SHOW_BACKWARD|LIST_DEBUG_SHOW_FORWARD); printf("\nRemoving all list3 elements individually in a reverse loop."); it=&list3->end; while( LISTBCK(list3, it) ) { it=listRemoveItem(list3,it,LIST_NEXT); } listDebugShow(list3, LIST_DEBUG_SHOW_BACKWARD|LIST_DEBUG_SHOW_FORWARD); printf("\nTest: Freeing lists.\n"); listFree(list); listFree(list2); printf("\nTests done, it's up to you to figure out if they passed, <nelson>hahaaa!</nelson>\n"); return(0); }