Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Linkedlist not looping properly
- int particle_update(struct particle **head ){
- struct particle *current = *head;
- struct particle *next;
- printf("particle_updaten");
- while(current != NULL){
- while(current != NULL && current->lifespan >=0){
- current->lifespan --;
- current->pos.y = current->pos.y + (current->spd.y * current->dir.y);
- current->pos.x = current->pos.x + (current->spd.x * current->dir.x);
- current->pos.z = current->pos.z + (current->spd.z * current->dir.z);
- current = current->next;
- if (current == NULL)
- current = *head;
- }
- }
- particle_destroy(head);
- return 0;
- }
- while(current->lifespan >= 0 && current != NULL){
- int particle_update(struct particle **head ){
- struct particle * current = *head;
- struct particle * prev = NULL;
- while (current != NULL) {
- // lifespan check
- current->lifespan = (current -> lifespan > 0) ? current->lifespan-1:particle_destroy(&prev, ¤t);
- // update position of current
- ...
- // increment counter at end of while loop
- prev = current;
- current = current -> next; //now current is always one node ahead of previous.
- }
- return 0;
- }
- void particle_destroy(struct particle ** prev, struct particle ** current) {
- if (*current = NULL) return; //nothing to do
- struct particle * tmp = *current;
- if (*prev != NULL) { /* need to modify previous node */
- (*prev) -> next = current -> next;
- } else { /* head has expired, so change head ptr to next node */
- (*current) = (*current) -> next;
- }
- /* free resources */
- // do other clean-up, if necessary.
- free(tmp);
- return;
- }
Add Comment
Please, Sign In to add comment