#include <stdlib.h>
#include "Popcorn:h.Popcorn"

struct object_table* Popcorn_NewTable(int max_objects, BOOL collisions) {
  int c;
  struct object_table *table;

  table = malloc((sizeof(struct object_table)) +
    ((max_objects-1) * sizeof(struct game_object)));

  table->max_objects = max_objects;
  table->next_free = 0;
  table->grav_x = table->grav_y = 0;

  if (collisions) table->collisions = malloc(sizeof(struct collision_table) +
    sizeof(struct collision_object) * (max_objects-1));
  else
    table->collisions = NULL;

  for (c=0; c<max_objects; c++) table->object[c].object_id = 0;
  table->header = TABLE_HEADER_WORD;
  return table;
}

/* added by Rik */
/* returns FALSE if sub table creation fails */
BOOL Popcorn_NewSubTable(struct object_table *table, size_t size) {
  int l;
  void *sub;
  if ((sub = malloc(size * table->max_objects)) == NULL) return FALSE;
  for (l=0; l<table->max_objects; l++) {
    table->object[l].user_data = (void *)((int)sub + l * size);
  }
  return TRUE;
}
