#ifndef COMPILE_H
#include "COMPILE.H"
#endif
#ifdef MEDIA763_AU1E_AU2E
#if (EXETYPE == C06_CEDT)
extern void F2045_FadeToPalette() ;
#endif

unsigned char* G0348_Bitmap_Screen;
#if (EXETYPE == C07_HINT)
int16_t G3846_;
struct RastPort* G3839_;
struct cprlist* G3836_;
struct cprlist* G3837_;
struct RastPort* G3843_;
struct cprlist* G3840_;
struct cprlist* G3841_;
struct RastPort** G3844_;
struct View G3830_s_View;
struct View* G3845_;
struct ViewPort G3831_s_ViewPort;
long G3867_l_Unreferenced;
struct RastPort G3842_s_RastPort;
struct RastPort G3838_s_RastPort;
struct BitMap* G3832_;
struct RastPort* G3833_;
struct RasInfo G3849_s_RasInfo;
struct BitMap G3835_s_BitMap_Screen;
#endif
unsigned int16_t G3829_aui_Palette[32];
long G3269_l_IDCMPFlagsBackup;
unsigned int16_t G3848_aui_Palette_Intermediate[16]; /* static ? */
unsigned int16_t G3847_aui_Palette_Source[16]; /* static ? */


#if (EXETYPE == C07_HINT)
void F2054_(struct BitMap* P5936_) {
        if ((P5936_->Planes[0] = AllocMem(32000L, MEMF_CHIP | MEMF_CLEAR)) == NULL) {
                F9073_DisplayError(0x82010000);
        }
        P5936_->Planes[1] = P5936_->Planes[0] + 8000;
        P5936_->Planes[2] = P5936_->Planes[1] + 8000;
        P5936_->Planes[3] = P5936_->Planes[2] + 8000;
}

void F2031_(struct BitMap* P5937_) {
        if (P5937_->Planes[0]) {
                FreeMem(P5937_->Planes[0], 32000L);
        }
        P5937_->Planes[0] = NULL;
}

void F2032_(
)
{
        InitView(&G3830_s_View);
        InitVPort(&G3831_s_ViewPort);
        InitBitMap(&G3835_s_BitMap_Screen, 4L, 320L, 200L);
        InitRastPort(&G3842_s_RastPort);
        InitRastPort(&G3838_s_RastPort);
        G3830_s_View.ViewPort = &G3831_s_ViewPort;
        G3830_s_View.Modes = 0;
        G3831_s_ViewPort.DWidth = 320;
        G3831_s_ViewPort.DHeight = 200;
        G3831_s_ViewPort.RasInfo = &G3849_s_RasInfo;
        G3831_s_ViewPort.ColorMap = NULL;
        if ((G3831_s_ViewPort.ColorMap = GetColorMap(16L)) == NULL) {
                F9073_DisplayError(0x82010000);
        }
        G3831_s_ViewPort.Modes = 0;
        G3842_s_RastPort.BitMap = &G3191_ps_MsgPort_FTL_DRIA_Video->_0x078_Bitmap;
        G3838_s_RastPort.BitMap = &G3835_s_BitMap_Screen;
        F2054_(&G3835_s_BitMap_Screen);
        SetRast(&G3838_s_RastPort, 0L);
}

unsigned int16_t* F2033_(
)
{
        return G3191_ps_MsgPort_FTL_DRIA_Video->_0x108_Screen->ViewPort.ColorMap->ColorTable;
}

void F2034_(int16_t* P5942_) {
        LoadRGB4(&G3191_ps_MsgPort_FTL_DRIA_Video->_0x108_Screen->ViewPort, P5942_, 16L);
        WaitTOF();
}

void F2035_(
)
{
        F2034_(G3191_ps_MsgPort_FTL_DRIA_Video->_0x108_Screen->ViewPort.ColorMap->ColorTable);
}

void F2036_(
)
{
        if (G3844_ == &G3839_) {
                G3191_ps_MsgPort_FTL_DRIA_Video->_0x108_Screen->ViewPort.RasInfo->BitMap = &G3835_s_BitMap_Screen;
                G3844_ = &G3843_;
        } else {
                G3191_ps_MsgPort_FTL_DRIA_Video->_0x108_Screen->ViewPort.RasInfo->BitMap = &G3191_ps_MsgPort_FTL_DRIA_Video->_0x078_Bitmap;
                G3844_ = &G3839_;
        }
        G3191_ps_MsgPort_FTL_DRIA_Video->_0x10C_Window->RPort = *G3844_;
        G3191_ps_MsgPort_FTL_DRIA_Video->_0x108_Screen->RastPort.BitMap = G3191_ps_MsgPort_FTL_DRIA_Video->_0x108_Screen->ViewPort.RasInfo->BitMap;
        G0348_Bitmap_Screen = G3191_ps_MsgPort_FTL_DRIA_Video->_0x108_Screen->ViewPort.RasInfo->BitMap->Planes[0];
        MakeScreen(G3191_ps_MsgPort_FTL_DRIA_Video->_0x108_Screen);
        RethinkDisplay();
        WaitTOF();
}

unsigned char* F2037_(
)
{
        return (G3844_ == &G3839_) ? G3843_->BitMap->Planes[0] : G3839_->BitMap->Planes[0];
}

unsigned char* F2038_(
)
{
        return (*G3844_)->BitMap->Planes[0];
}

void F2039_(int16_t P5941_) {
        if (P5941_) {
                G3191_ps_MsgPort_FTL_DRIA_Video->_0x108_Screen->ViewPort.RasInfo->BitMap = &G3835_s_BitMap_Screen;
                G3844_ = &G3843_;
        } else {
                G3191_ps_MsgPort_FTL_DRIA_Video->_0x108_Screen->ViewPort.RasInfo->BitMap = &G3191_ps_MsgPort_FTL_DRIA_Video->_0x078_Bitmap;
                G3844_ = &G3839_;
        }
        G3191_ps_MsgPort_FTL_DRIA_Video->_0x10C_Window->RPort = *G3844_;
        G3191_ps_MsgPort_FTL_DRIA_Video->_0x108_Screen->RastPort.BitMap = G3191_ps_MsgPort_FTL_DRIA_Video->_0x108_Screen->ViewPort.RasInfo->BitMap;
        G0348_Bitmap_Screen = G3191_ps_MsgPort_FTL_DRIA_Video->_0x108_Screen->ViewPort.RasInfo->BitMap->Planes[0];
        MakeScreen(G3191_ps_MsgPort_FTL_DRIA_Video->_0x108_Screen);
        RethinkDisplay();
        WaitTOF();
}
#endif

void F0078_MOUSE_DisableScreenUpdate(
)
{
        SetPointer(G3191_ps_MsgPort_FTL_DRIA_Video->_0x10C_Window, G3191_ps_MsgPort_FTL_DRIA_Video->_ai_0x154, 16L, 16L, -1L, 0L);
}

void F0077_MOUSE_EnableScreenUpdate_CPSE(
)
{
        SetPointer(G3191_ps_MsgPort_FTL_DRIA_Video->_0x10C_Window, G3191_ps_MsgPort_FTL_DRIA_Video->_0x148_SpriteImage, 1L, 16L, -1L, 0L);
}

void F2042_InitializeScreen(
)
{
        if (F1138_() == 0) {
                F9073_DisplayError(0x80F1000B);
        }
        G3269_l_IDCMPFlagsBackup = G3191_ps_MsgPort_FTL_DRIA_Video->_0x10C_Window->IDCMPFlags;
#if (EXETYPE == C07_HINT)
        G3832_ = G3191_ps_MsgPort_FTL_DRIA_Video->_0x108_Screen->ViewPort.RasInfo->BitMap;
        G3833_ = G3191_ps_MsgPort_FTL_DRIA_Video->_0x10C_Window->RPort;
#endif
        ModifyIDCMP(G3191_ps_MsgPort_FTL_DRIA_Video->_0x10C_Window, VANILLAKEY | INACTIVEWINDOW | ACTIVEWINDOW | DISKREMOVED | DISKINSERTED | CLOSEWINDOW | MENUPICK | GADGETUP | GADGETDOWN | MOUSEBUTTONS);
#if (EXETYPE == C06_CEDT)
        F0077_MOUSE_EnableScreenUpdate_CPSE();
#endif
        ScreenToFront(G3191_ps_MsgPort_FTL_DRIA_Video->_0x108_Screen);
        F1141_EnablePlayfieldDisplayAndCopper();
#if (EXETYPE == C06_CEDT)
        F2045_FadeToPalette(G3829_aui_Palette, 0);
#endif
        SetRGB4(&G3191_ps_MsgPort_FTL_DRIA_Video->_0x108_Screen->ViewPort, 17L, 15L, 15L, 0L);
        SetRGB4(&G3191_ps_MsgPort_FTL_DRIA_Video->_0x108_Screen->ViewPort, 18L, 0L, 0L, 0L);
        SetRGB4(&G3191_ps_MsgPort_FTL_DRIA_Video->_0x108_Screen->ViewPort, 19L, 0L, 0L, 0L);
#if (EXETYPE == C06_CEDT)
        SetRast(&G3191_ps_MsgPort_FTL_DRIA_Video->_0x108_Screen->RastPort, 0L);
#endif
#if (EXETYPE == C07_HINT)
        F2032_();
        G3849_s_RasInfo.BitMap = &G3191_ps_MsgPort_FTL_DRIA_Video->_0x078_Bitmap;
        MakeVPort(&G3830_s_View, &G3831_s_ViewPort);
        MrgCop(&G3830_s_View);
        G3836_ = G3830_s_View.SHFCprList;
        G3837_ = G3830_s_View.LOFCprList;
        G3839_ = &G3838_s_RastPort;
        G3830_s_View.LOFCprList = NULL;
        G3830_s_View.SHFCprList = NULL;
        G3849_s_RasInfo.BitMap = &G3835_s_BitMap_Screen;
        MakeVPort(&G3830_s_View, &G3831_s_ViewPort);
        MrgCop(&G3830_s_View);
        G3840_ = G3830_s_View.SHFCprList;
        G3841_ = G3830_s_View.LOFCprList;
        G3843_ = &G3842_s_RastPort;
        G3844_ = &G3843_;
        G3845_ = GfxBase->ActiView;
        F2036_();
        G3846_ = 1;
#endif
}

void F2043_UninitializeScreen(
)
{
#if (EXETYPE == C07_HINT)
        G3846_ = 0;
        if (G3191_ps_MsgPort_FTL_DRIA_Video) {
                G3191_ps_MsgPort_FTL_DRIA_Video->_0x108_Screen->ViewPort.RasInfo->BitMap = G3832_;
                G3191_ps_MsgPort_FTL_DRIA_Video->_0x10C_Window->RPort = G3833_;
                MakeScreen(G3191_ps_MsgPort_FTL_DRIA_Video->_0x108_Screen);
                RethinkDisplay();
                WaitTOF();
        }
        if (G3845_) {
                LoadView(G3845_);
                WaitTOF();
                FreeVPortCopLists(&G3831_s_ViewPort);
        }
        if (G3836_) {
                FreeCprList(G3836_);
        }
        G3836_ = NULL;
        if (G3837_) {
                FreeCprList(G3837_);
        }
        G3837_ = NULL;
        if (G3840_) {
                FreeCprList(G3840_);
        }
        G3840_ = NULL;
        if (G3841_) {
                FreeCprList(G3841_);
        }
        G3841_ = NULL;
        if (G3831_s_ViewPort.ColorMap) {
                FreeColorMap(G3831_s_ViewPort.ColorMap);
        }
        G3831_s_ViewPort.ColorMap = NULL;
        F2031_(&G3835_s_BitMap_Screen);
#endif
        if (G3191_ps_MsgPort_FTL_DRIA_Video) {
                F0077_MOUSE_EnableScreenUpdate_CPSE();
                ModifyIDCMP(G3191_ps_MsgPort_FTL_DRIA_Video->_0x10C_Window, G3269_l_IDCMPFlagsBackup);
        }
}

void F1127_TransitionFromPaletteToPalette(
unsigned int16_t* P2814_pui_Palette_Destination,
unsigned int16_t* P2815_pui_Palette_Source)
{
        register int16_t L2632_i_Color;
        register int16_t L2633_i_Counter;
        register int16_t L2634_i_ColorIndex;
        int16_t L2635_i_SourceRedComponent;
        int16_t L2636_i_DestinationRedComponent;
        int16_t L2637_i_DeltaRed;
        int16_t L2638_i_IntermediateRedComponent;
        int16_t L2639_i_RedIncrement;
        int16_t L2640_i_SourceGreenComponent;
        int16_t L2641_i_DestinationGreenComponent;
        int16_t L2642_i_DeltaGreen;
        int16_t L2643_i_IntermediateGreenComponent;
        int16_t L2644_i_GreenIncrement;
        int16_t L2645_i_SourceBlueComponent;
        int16_t L2646_i_DestinationBlueComponent;
        int16_t L2647_i_DeltaBlue;
        int16_t L2648_i_IntermediateBlueComponent;
        int16_t L2649_i_BlueIncrement;


        for (L2633_i_Counter = 0; L2633_i_Counter < 16; L2633_i_Counter++) {
                G3848_aui_Palette_Intermediate[L2633_i_Counter] = P2815_pui_Palette_Source[L2633_i_Counter];
        }
        for (L2633_i_Counter = 0; L2633_i_Counter < 15; L2633_i_Counter++) {
                for (L2634_i_ColorIndex = 0; L2634_i_ColorIndex < 16; L2634_i_ColorIndex++) {
                        L2632_i_Color = P2815_pui_Palette_Source[L2634_i_ColorIndex];
                        L2635_i_SourceRedComponent = (L2632_i_Color >> 8) & 0x0F;
                        L2640_i_SourceGreenComponent = (L2632_i_Color >> 4) & 0x0F;
                        L2645_i_SourceBlueComponent = L2632_i_Color & 0x0F;
                        L2632_i_Color = P2814_pui_Palette_Destination[L2634_i_ColorIndex];
                        L2636_i_DestinationRedComponent = (L2632_i_Color >> 8) & 0x0F;
                        L2641_i_DestinationGreenComponent = (L2632_i_Color >> 4) & 0x0F;
                        L2646_i_DestinationBlueComponent = L2632_i_Color & 0x0F;
                        L2632_i_Color = G3848_aui_Palette_Intermediate[L2634_i_ColorIndex];
                        L2638_i_IntermediateRedComponent = (L2632_i_Color >> 8) & 0x0F;
                        L2643_i_IntermediateGreenComponent = (L2632_i_Color >> 4) & 0x0F;
                        L2648_i_IntermediateBlueComponent = L2632_i_Color & 0x0F;
                        L2637_i_DeltaRed = ((L2636_i_DestinationRedComponent - L2635_i_SourceRedComponent) < 0) ? (L2636_i_DestinationRedComponent - L2635_i_SourceRedComponent) * -1 : (L2636_i_DestinationRedComponent - L2635_i_SourceRedComponent);
                        L2642_i_DeltaGreen = ((L2641_i_DestinationGreenComponent - L2640_i_SourceGreenComponent) < 0) ? (L2641_i_DestinationGreenComponent - L2640_i_SourceGreenComponent) * -1 : (L2641_i_DestinationGreenComponent - L2640_i_SourceGreenComponent);
                        L2647_i_DeltaBlue = ((L2646_i_DestinationBlueComponent - L2645_i_SourceBlueComponent) < 0) ? (L2646_i_DestinationBlueComponent - L2645_i_SourceBlueComponent) * -1 : (L2646_i_DestinationBlueComponent - L2645_i_SourceBlueComponent);
                        L2639_i_RedIncrement = G3191_ps_MsgPort_FTL_DRIA_Video->ColorComponentIncrements[L2633_i_Counter][L2637_i_DeltaRed];
                        L2644_i_GreenIncrement = G3191_ps_MsgPort_FTL_DRIA_Video->ColorComponentIncrements[L2633_i_Counter][L2642_i_DeltaGreen];
                        L2649_i_BlueIncrement = G3191_ps_MsgPort_FTL_DRIA_Video->ColorComponentIncrements[L2633_i_Counter][L2647_i_DeltaBlue];
                        if ((L2636_i_DestinationRedComponent - L2635_i_SourceRedComponent) < 0) {
                                L2639_i_RedIncrement *= -1;
                        }
                        if ((L2641_i_DestinationGreenComponent - L2640_i_SourceGreenComponent) < 0) {
                                L2644_i_GreenIncrement *= -1;
                        }
                        if ((L2646_i_DestinationBlueComponent - L2645_i_SourceBlueComponent) < 0) {
                                L2649_i_BlueIncrement *= -1;
                        }
                        L2638_i_IntermediateRedComponent += L2639_i_RedIncrement;
                        L2643_i_IntermediateGreenComponent += L2644_i_GreenIncrement;
                        L2648_i_IntermediateBlueComponent += L2649_i_BlueIncrement;
                        L2632_i_Color = (L2638_i_IntermediateRedComponent << 8) | (L2643_i_IntermediateGreenComponent << 4) | L2648_i_IntermediateBlueComponent;
                        G3848_aui_Palette_Intermediate[L2634_i_ColorIndex] = L2632_i_Color;
                }
                LoadRGB4(&G3191_ps_MsgPort_FTL_DRIA_Video->_0x108_Screen->ViewPort, G3848_aui_Palette_Intermediate, 16L);
                WaitTOF();
        }
}

void F2045_FadeToPalette(unsigned int16_t* P5938_pui_Palette) {
        register unsigned int16_t* L5829_pui_Color;
        register int16_t L5830_i_ColorIndex;


        L5830_i_ColorIndex = 0;
        L5829_pui_Color = G3191_ps_MsgPort_FTL_DRIA_Video->_0x108_Screen->ViewPort.ColorMap->ColorTable;
        while (L5830_i_ColorIndex < 16) {
                G3847_aui_Palette_Source[L5830_i_ColorIndex] = *L5829_pui_Color++;
                L5830_i_ColorIndex++;
        }
        F1127_TransitionFromPaletteToPalette(P5938_pui_Palette, G3847_aui_Palette_Source);
}
#endif
