Guest User

Untitled

a guest
Aug 14th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. Linkedlist not looping properly
  2. int particle_update(struct particle **head ){
  3. struct particle *current = *head;
  4. struct particle *next;
  5. printf("particle_updaten");
  6.  
  7. while(current != NULL){
  8. while(current != NULL && current->lifespan >=0){
  9. current->lifespan --;
  10.  
  11. current->pos.y = current->pos.y + (current->spd.y * current->dir.y);
  12. current->pos.x = current->pos.x + (current->spd.x * current->dir.x);
  13. current->pos.z = current->pos.z + (current->spd.z * current->dir.z);
  14.  
  15. current = current->next;
  16. if (current == NULL)
  17. current = *head;
  18. }
  19. }
  20.  
  21. particle_destroy(head);
  22. return 0;
  23. }
  24.  
  25. while(current->lifespan >= 0 && current != NULL){
  26.  
  27. int particle_update(struct particle **head ){
  28.  
  29. struct particle * current = *head;
  30. struct particle * prev = NULL;
  31.  
  32. while (current != NULL) {
  33. // lifespan check
  34. current->lifespan = (current -> lifespan > 0) ? current->lifespan-1:particle_destroy(&prev, &current);
  35.  
  36. // update position of current
  37. ...
  38.  
  39.  
  40. // increment counter at end of while loop
  41. prev = current;
  42. current = current -> next; //now current is always one node ahead of previous.
  43. }
  44.  
  45. return 0;
  46. }
  47.  
  48. void particle_destroy(struct particle ** prev, struct particle ** current) {
  49. if (*current = NULL) return; //nothing to do
  50.  
  51. struct particle * tmp = *current;
  52. if (*prev != NULL) { /* need to modify previous node */
  53. (*prev) -> next = current -> next;
  54. } else { /* head has expired, so change head ptr to next node */
  55. (*current) = (*current) -> next;
  56. }
  57.  
  58. /* free resources */
  59.  
  60. // do other clean-up, if necessary.
  61. free(tmp);
  62.  
  63. return;
  64. }
Add Comment
Please, Sign In to add comment