#include <stdlib.h>

#ifdef DESKLIB
#include "DeskLib:h.Error"
#else
#include <stdio.h>
#endif

#include "Popcorn:h.Popcorn"

struct game_object *Popcorn_NewPrototype(struct object_table *table,
       		   			  int	 	      prototype_id,
       		   			  int		      x,
       		   			  int		      y)
{
  int                  c = 0;
  struct game_object   *obj;

  while ( prototype[c]->id != prototype_id && c != prototype_free )
    c++; /* Find prototype */

  if (c == prototype_free) {
#ifdef DESKLIB
    Error_ReportFatal(0, "Prototype %08X not found", prototype_id);
#else
    fprintf(stderr, "Prototype %08X not found", prototype_id);
#endif
  }
  obj = Popcorn_NewObject(table);
  if (obj == NULL) return NULL; /* Check we've got space in the table */

/* Fill object's information */

  obj->object_id = prototype_id;
  obj->x = x; obj->y = y;
  obj->flags = prototype[c]->flags;
  obj->handler = prototype[c]->handler;
  obj->frame = obj->timer.value = obj->timer.decrement = obj->xv = obj->yv = 0;
/*  obj->user_data = NULL; Rik - removed to work with Popcorn_NewSubTable */

  obj->size.x = prototype[c]->animation.frame[0].size_x;
  obj->size.y = prototype[c]->animation.frame[0].size_y;
  obj->plot_offset.x = prototype[c]->animation.frame[0].centre_x;
  obj->plot_offset.y = prototype[c]->animation.frame[0].centre_y;

  if (prototype[c]->animation.frames == 1)
    obj->plot_id.sprite_anchor = prototype[c]->animation.frame[0].sprite_anchor;
  else
    obj->plot_id.animation = &prototype[c]->animation;

  return obj;
}
