/*
   spacemaze - Fly a space ship through a maze and collect all the items.
   Copyright (C) 2000, 2001, 2002 John Ericson

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

   2000-03-25 High / John Ericson john.ericson@home.se
*/

#include "gfx.h"

void initdrawrect(const int size) {

   int i;

   /* Set up the update rectangle pointers */
   for (i = 0; i < size; ++i) {
      drawrect[i].rectsrc = &drawrectsrc[i];
      drawrect[i].rectdst = &drawrectdst[i];
   }
}

/* Completely erase the 'drawrect' vector on it's content */
void cleardrawrect(void) {
   /*
   int i;

   for (i = 0;i <= MAXDRAWRECT;i++)
      drawrect[i].used = false;
   */
   drawrect_numupdate = 0;
}

/* Redraws all rectangles in the 'drawrect' vector */
void updatescreen(void) {
   int i;

   //printf("drawrect_numupdate: %d\n", drawrect_numupdate);

   for (i = 0; i < drawrect_numupdate; i++) {
      //printf("%d (%d) dst: x: %d y: %d w: %d h: %d src: x: %d y: %d w: %d h: %d\n", i, drawrect_numupdate, drawrect[i].rectdst->x, drawrect[i].rectdst->y, drawrect[i].rectdst->w, drawrect[i].rectdst->h, drawrect[i].rectsrc->x, drawrect[i].rectsrc->y, drawrect[i].rectsrc->w, drawrect[i].rectsrc->h);

      if (drawrect[i].src != NULL)
         SDL_LowerBlit(drawrect[i].src, drawrect[i].rectsrc,
            screen, drawrect[i].rectdst);

     }

   SDL_UpdateRects(screen, drawrect_numupdate, drawrectdst);

   cleardrawrect();
}

/* Add an rectangle that should be redrawed when function 'updatescreen'
   is called.
*/
void inputdrawrectsrc(SDL_Rect *rect, SDL_Surface *src) {
   struct struct_drawrect *u;

   u = &drawrect[drawrect_numupdate++];

   u->src = src;

   u->rectsrc->x = 0;
   u->rectsrc->y = 0;
   u->rectsrc->w = rect->w;
   u->rectsrc->h = rect->h;

   u->rectdst->x = rect->x;
   u->rectdst->y = rect->y;
   u->rectdst->w = rect->w;
   u->rectdst->h = rect->h;
}

void inputdrawrect(SDL_Rect *rect) {
   struct struct_drawrect *u;

   u = &drawrect[drawrect_numupdate++];

   u->src = NULL;

   u->rectsrc->x = 0;
   u->rectsrc->y = 0;
   u->rectsrc->w = rect->w;
   u->rectsrc->h = rect->h;

   u->rectdst->x = rect->x;
   u->rectdst->y = rect->y;
   u->rectdst->w = rect->w;
   u->rectdst->h = rect->h;
}

/* Putpixel function for 8bpp mode with out-of-screen clipping
*/
void putpixel(Uint16 x, Uint16 y, Uint32 pixel) {
   if (x <= screen->w && y <= screen->h)
      *((Uint8 *)screen->pixels + y * screen->pitch + x) = pixel;
}