#include "game_types.h"

/* Image_DrawGradient @ 0x08013D8C (real 136B) -- magic-gated gradient flag dispatcher. Mirrors src/matched/Image_DrawGradient.c. */
/* When word 0 holds the magic, bumps it and dispatches the flag nibbles:
   low byte to +5, 0xF00 group (plus 12-byte zero fan at +80/64) to +6,
   0xF0000 group to +7, the 0xB000000 group RMWs the IO byte at 0x04000089,
   and the 0xF00000 group calls FillRect; restores the magic on exit. */
extern void Image_FillRect(int flags);

void Image_DrawGradient(int flags)
{
    int *q = *(int **)0x03007FF0;
    int i;
    if (q[0] != 0x68736D53)
        return;
    q[0]++;
    if ((flags & 255) != 0)
        ((unsigned char *)q)[5] = (unsigned char)(flags & 127);
    if ((flags & 0xF00) != 0) {
        ((unsigned char *)q)[6] = (unsigned char)((flags & 0xF00) >> 8);
        for (i = 0; i < 12; i++)
            ((unsigned char *)q)[80 + i * 64] = 0;
    }
    if ((flags & 0xF0000) != 0)
        ((unsigned char *)q)[7] = (unsigned char)((flags & 0xF0000) >> 12);
    if ((flags & 0xB000000) != 0) {
        volatile unsigned char *io = (volatile unsigned char *)0x04000089;
        *io = (unsigned char)((*io & 63) | ((flags & 0x800000) >> 14));
    }
    if ((flags & 0xF00000) != 0)
        Image_FillRect(flags & 0xF00000);
    q[0] = 0x68736D53;
}
