Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <SDL2/SDL.h>
- #include <SDL2/SDL_image.h>
- #include "structdata.h"
- // List contains, type, sprite_index, hp, x, y, next object pointer.
- int countObjects(ObjectList* base)
- {
- return base->length;
- }
- ObjectList* createObjectList()
- {
- ObjectList* olist = NULL;
- olist = malloc(sizeof(ObjectList));
- olist->length = 0;
- olist->objs = malloc(sizeof(ObjectNode));
- olist->objs->next = NULL;
- return olist;
- }
- void addObject(ObjectList* base, Object* value)
- {
- ObjectNode* current = base->objs;
- while(current->obj != NULL && current->next != NULL)
- {
- current = current->next;
- }
- current->next = NULL;
- current->next = malloc(sizeof(ObjectNode));
- current->next->obj = value;
- current->next->next = NULL;
- base->length++;
- }
- Object* getObjectAt(ObjectList* base, int index)
- {
- register int i = 0;
- ObjectNode *tmp = NULL;
- ObjectNode *current = base->objs;
- while( (current->obj != NULL && current->next != NULL) && (i < index-1))
- {
- i++;
- current = current->next;
- }
- return current->obj;
- }
- Object* findObjectByType(ObjectList* base, int type, int index)
- {
- register int i = 0;
- ObjectNode* current = base->objs;
- while(current->obj != NULL && current->next != NULL)
- {
- if(current->obj->type == type)
- {
- if(i == index)
- {
- return current->obj;
- }
- else
- {
- i++;
- }
- }
- current = current->next;
- }
- return NULL;
- }
- void removeObject_ptr(ObjectList* base, ObjectNode* obj)
- {
- if(obj == base->objs)
- {
- ObjectNode** oBase = obj;
- if(*oBase == NULL)
- {
- return -1;
- }
- ObjectNode *next_object = (*oBase)->next;
- free(*oBase);
- *oBase = next_object;
- }
- ObjectNode *tmp = NULL;
- ObjectNode *current = obj;
- tmp = current->next;
- current->next = tmp->next;
- free(tmp);
- base->length--;
- }
- void removeObjectAt(ObjectList* base, int index)
- {
- if(index == 0)
- {
- ObjectNode** oBase = base->objs;
- if(*oBase == NULL)
- {
- return -1;
- }
- ObjectNode *next_object = (*oBase)->next;
- free(*oBase);
- *oBase = next_object;
- }
- int ret = -1;
- register int i = 0;
- ObjectNode *tmp = NULL;
- ObjectNode *current = base->objs;
- while(current != NULL && (i < index-1))
- {
- i++;
- current = current->next;
- }
- if(i < index-1)
- {
- puts("[ObjectManager: Fatal Error] Invalid node index.");
- return -1;
- }
- tmp = current->next;
- current->next = tmp->next;
- free(tmp);
- base->length--;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement