Advertisement
Redxone

[SDL] Object manager (WIP)

Feb 16th, 2018
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.83 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <SDL2/SDL.h>
  4. #include <SDL2/SDL_image.h>
  5. #include "structdata.h"
  6.  
  7.  
  8. // List contains, type, sprite_index, hp, x, y, next object pointer.
  9.  
  10. int countObjects(ObjectList* base)
  11. {
  12.     return base->length;
  13. }
  14.  
  15. ObjectList* createObjectList()
  16. {
  17.     ObjectList* olist = NULL;
  18.     olist = malloc(sizeof(ObjectList));
  19.     olist->length = 0;
  20.     olist->objs = malloc(sizeof(ObjectNode));
  21.     olist->objs->next = NULL;
  22.  
  23.     return olist;
  24. }
  25.  
  26. void addObject(ObjectList* base, Object* value)
  27. {
  28.     ObjectNode* current = base->objs;
  29.     while(current->obj != NULL && current->next != NULL)
  30.     {
  31.         current = current->next;
  32.     }
  33.     current->next = NULL;
  34.     current->next = malloc(sizeof(ObjectNode));
  35.     current->next->obj = value;
  36.     current->next->next = NULL;
  37.     base->length++;
  38. }
  39.  
  40. Object* getObjectAt(ObjectList* base, int index)
  41. {
  42.     register int i = 0;
  43.     ObjectNode *tmp = NULL;
  44.     ObjectNode *current = base->objs;
  45.     while( (current->obj != NULL && current->next != NULL) && (i < index-1))
  46.     {
  47.         i++;
  48.         current = current->next;
  49.     }
  50.     return current->obj;
  51. }
  52.  
  53. Object* findObjectByType(ObjectList* base, int type, int index)
  54. {
  55.     register int i = 0;
  56.     ObjectNode* current = base->objs;
  57.     while(current->obj != NULL && current->next != NULL)
  58.     {
  59.         if(current->obj->type == type)
  60.         {
  61.             if(i == index)
  62.             {
  63.                 return current->obj;
  64.             }
  65.             else
  66.             {
  67.                 i++;
  68.             }
  69.         }
  70.         current = current->next;
  71.     }
  72.     return NULL;
  73. }
  74.  
  75. void removeObject_ptr(ObjectList* base, ObjectNode* obj)
  76. {
  77.     if(obj == base->objs)
  78.     {
  79.         ObjectNode** oBase = obj;
  80.         if(*oBase == NULL)
  81.         {
  82.             return -1;
  83.         }
  84.         ObjectNode *next_object = (*oBase)->next;
  85.         free(*oBase);
  86.         *oBase = next_object;
  87.     }
  88.  
  89.     ObjectNode *tmp = NULL;
  90.     ObjectNode *current = obj;
  91.     tmp = current->next;
  92.     current->next = tmp->next;
  93.     free(tmp);
  94.     base->length--;
  95. }
  96.  
  97. void removeObjectAt(ObjectList* base, int index)
  98. {
  99.     if(index == 0)
  100.     {
  101.         ObjectNode** oBase = base->objs;
  102.         if(*oBase == NULL)
  103.         {
  104.             return -1;
  105.         }
  106.         ObjectNode *next_object = (*oBase)->next;
  107.         free(*oBase);
  108.         *oBase = next_object;
  109.     }
  110.     int ret = -1;
  111.     register int i = 0;
  112.     ObjectNode *tmp = NULL;
  113.     ObjectNode *current = base->objs;
  114.     while(current != NULL && (i < index-1))
  115.     {
  116.         i++;
  117.         current = current->next;
  118.     }
  119.     if(i < index-1)
  120.     {
  121.         puts("[ObjectManager: Fatal Error] Invalid node index.");
  122.         return -1;
  123.     }
  124.     tmp = current->next;
  125.     current->next = tmp->next;
  126.     free(tmp);
  127.     base->length--;
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement