/* ProcessDst.c         */
/* R Griffin 1997       */

#include "Popcorn:Popcorn.h"

static BOOL check_delete(struct dust_object *, int, int);

void Popcorn_ProcessDust(struct dust_object_table *table, int moves) {
  register struct dust_object *obj;
  int scan, x, y;
#ifdef MemCheck_MEMCHECK
  moves = 1;
#endif
  do {
    scan = -1;
    while (++scan != table->max_objects) {
      obj = &(table->object[scan]);
      if (obj->object_id != 0) {

        if (obj->flags.bits.velocities) {
          if (obj->flags.bits.gravity) {
            obj->xv += table->grav_x;
            obj->yv += table->grav_y;
          }
          obj->x += obj->xv;
          obj->y += obj->yv;
        }
        --obj->timer_value;
        if (obj->flags.bits.plot_offset) {
          x = (obj->x + plot_offset.x) >> 12;
          y = (obj->y + plot_offset.y) >> 12;
        } else {
          x = obj->x >> 12;
          y = obj->y >> 12;
        }

        /* check if dust is outside windows before plotting */
        if (check_delete(obj, x, y)) Popcorn_DeleteDust(obj);
        else if (obj->flags.bits.std_plot && moves == 1 && obj->colours != NULL)
          Popcorn_PlotPoint(x, y, ((char *)obj->colours)
            [(obj->timer_value / obj->colour_div) % obj->colour_num]);
      } /* endif (obj->id == 0) */
    } /* endwhile (++scan != max_objects) */
  } while (--moves != 0);
}

static BOOL check_delete(struct dust_object *obj, int x, int y) {
  if ((obj->flags.bits.kill_timer && obj->timer_value <= 0) ||
    (obj->flags.bits.kill_plotout && Popcorn_FastOutside(x, y, &plot_window)) ||
    (obj->flags.bits.kill_gameout && Popcorn_FastOutside(x, y, &game_window)) ||
    (obj->flags.bits.kill_userout && Popcorn_FastOutside(x, y, &user_window)))
    return TRUE;
  return FALSE;
}
