I made this today and I am wondering if there is a better way of making a Game Maker-like game using SDL.
Code:
#include <stdio.h>
#include <stdlib.h>
enum entitytype_enum {
SPRITE = 1,
BACKGROUND,
ROOM,
OBJECT,
INSTANCE
};
typedef struct Entity {
int type;
/*
SDL_Surface *image;
SDL_Rect *rects;
*/
int cols, rows; /* These variables are for sprite sheets */
int x, y;
struct Entity *sprite;
struct Entity *instances;
struct Entity *next;
} Entity;
Entity *startEntity = NULL;
Entity *newEntity(int type) {
Entity *entity = (Entity*)malloc(sizeof(Entity));
entity->type = type;
/*
entity->image = NULL;
entity->rects = NULL;
*/
entity->cols = 0;
entity->rows = 0;
entity->sprite = NULL;
entity->instances = NULL;
entity->next = NULL;
if (startEntity == NULL) {
startEntity = entity;
} else {
Entity *tmp = startEntity;
while (tmp->next != NULL) tmp = tmp->next;
tmp->next = entity;
}
return entity;
}
void freeEntities() {
Entity *current = startEntity;
while (current != NULL) {
Entity *tmp = current->next;
/*
if (current->image != NULL) {
SDL_FreeSurface(current->image);
}
*/
free(current);
current = tmp;
}
}
Entity *getEntity(int type, int index) {
Entity *current = startEntity;
int i = 0;
while (current != NULL) {
if (current->type == type) {
if (i == index) break;
++i;
}
current = current->next;
}
return current;
}
int main(int argc, char *argv[]) {
newEntity(SPRITE);
newEntity(BACKGROUND);
newEntity(ROOM);
newEntity(OBJECT);
newEntity(ROOM);
if (getEntity(ROOM, 0)->type == ROOM) printf("ROOM 0 = ROOM");
freeEntities();
return 0;
}