// Overlay functions

#include <stdlib.h>
#include <SDL.h>

#include "overlay.h"
#include "cool_mzx/cool_mzx.h"

// Display a part of an overlay in the slowest possible way
void overlaypart(buftype *pic,buftype *ovl,int x,int y,int w,int h)
{
    int ex,yy;
    buftype *o,*p;
#ifndef HICOLOR
    buftype alffa;
#endif

    o=(buftype *)ovl;
    p=pic;

    p+=y*XRES+x;
    o+=y*XRES+x;
    for(yy=y;yy<y+h;yy++) // "Blend"... argh
    {
        for(ex=x;ex<x+w;ex++,o++,p++)
        {
#ifdef HICOLOR
            if(*o&0x20)
                *p=*o;
#else
            if(*o&0xf8000000)
            {
                alffa=*o>>24;
                if(alffa>250)
                    *p=*o;
                else
  #ifdef QUICKBLEND
                ;
  #else
                *p=
                   ((((*o>>16)&0xff)*alffa>>8)<<16)+
                   ((((*p>>16)&0xff)*(255-alffa)>>8)<<16)+
                   ((((*o>>8)&0xff)*alffa>>8)<<8)+
                   ((((*p>>8)&0xff)*(255-alffa)>>8)<<8)+
                   (((*o&0xff)*alffa>>8))+
                   ((((*p)&0xff)*(255-alffa)>>8));
  #endif
            }
#endif
        }
        o+=XRES-w;
        p+=XRES-w;
    }
}

// This is so convoluted I can't even read it myself anymore
void overlay(buftype *pic,OVERLAY *ovl)
{
    int diffi,y,x,r;
    buftype *o,*p;
#ifndef HICOLOR
    buftype alffa;
#endif
    static int prevticks=0;
    OVERLAY *ov=ovl;

    diffi=SDL_GetTicks()-prevticks;

    // Go through the overlays and check if they need to be shown
    while(ov->piccy!=NULL)
    {
        o=(buftype *)ov->piccy->data;
        p=pic;

        if(ov->appear<=mzx_position())
        {
            if(ov->disappear<=mzx_position())
                ov->outcount+=diffi;

            ov->incount+=diffi;
        }
        else
            goto nono;

        if(ov->outtime<=ov->outcount)
            goto nono;

        o+=ov->starty*XRES;
        p+=ov->starty*XRES;
        for(y=ov->starty;y<=ov->endy;y++) // "Blend"... argh
        {
            r=ov->intime-ov->incount;
            if(r>0 && rand()%ov->intime<r)
            {
                o+=XRES;
                p+=XRES;
                continue;
            }
            r=ov->outtime-ov->outcount;
            if(r>0 && rand()%ov->outtime>r)
            {
                o+=XRES;
                p+=XRES;
                continue;
            }
            o+=ov->startx;
            p+=ov->startx;
            for(x=ov->startx;x<=ov->endx;x++,o++,p++)
            {
#ifdef HICOLOR
                if(*o&0x20)
                    *p=*o;
#else
                if(*o&0xf8000000)
                {
                    alffa=*o>>24;
                    if(alffa>250)
                        *p=*o;
                    else
  #ifdef QUICKBLEND
                    ;
  #else
                        *p=
                        ((((*o>>16)&0xff)*alffa>>8)<<16)+
                        ((((*p>>16)&0xff)*(255-alffa)>>8)<<16)+
                        ((((*o>>8)&0xff)*alffa>>8)<<8)+
                        ((((*p>>8)&0xff)*(255-alffa)>>8)<<8)+
                        (((*o&0xff)*alffa>>8))+
                        (((*p&0xff)*(255-alffa)>>8));
  #endif
                }
#endif
            }
            o+=XRES-1-ov->endx;
            p+=XRES-1-ov->endx;
        }

        nono:
        ov++;
    }

    prevticks=SDL_GetTicks();
}
