#include "../include/menus.h"
#include <gint/display.h>
#include <gint/keyboard.h>
#include <gint/hardware.h>
#include "../parameters.h"

#include <gint/usb.h>
#include <gint/usb-ff-bulk.h>
#include <gint/std/stdlib.h>

#include "../include/utils.h"
#include "../include/saves.h"


extern BestRanking HallOfFame[10][5];

extern uint8_t NB_CARS_TRAFFIC;

extern bool isOCPossible;
extern bool isOCActivated;

extern bopti_image_t mainscreen, mainscrbright;
extern bopti_image_t africa1, desert1, finland1, plains1, usa1;
extern bopti_image_t flag, linear, circular;
extern font_t autofont, autofontsmall, speedfont;

extern bool record;
extern bool screenshot;


bool doneTitle = false;

bool doneMainMenu = false;
uint8_t MainMenuSelection=0;

bool doneMenuCircuit = false;
int8_t CircuitSelection=0;

bool doneMenuDetailsCircuit = false;
int8_t CircuitDetailsSelection=0;

bool doneMenuBestTime = false;
int8_t BestTimeSelection=0;

bool doneMenuCredit = false;

bool doneOptions = false;
uint8_t OptionsSelection=0;

bool doneGameOptions = false;
uint8_t GameOptionsSelection=0;

bool donePauseQuit = false;
uint8_t PauseQuitSelection=0;

extern bool BDrawDeco;
extern bool BDrawClds;
extern bool BDrawCars;
extern bool BDrawFPS;
extern bool BDrawBack;

extern bool exitToOS;
extern bool stop;
extern bool SkipTime;

extern uint8_t DiffLevel;
extern uint8_t CarsNumb;
extern char PlayerName[3];
bool nameCurrentlyWritten = false;
uint8_t selectedChar=0;

extern char version[5];

void drawStartTitle( void )
{
    uint16_t pulse=0;
    doneTitle = false;
    while (!doneTitle)
    {

        dclear( C_BLACK );
        dsubimage( 0, 32, &mainscrbright, 0, 32, 396, 160,  DIMAGE_NONE);

        dfont(&autofont);

        if (gint[HWCALC] == HWCALC_FXCG50 || gint[HWCALC] == HWCALC_FXCG_MANAGER) dprint_opt(198, 2, C_RGB(255,255,255), C_NONE, DTEXT_CENTER, DTEXT_TOP, "OUTRUN FOR FX-CG50" );
        else if (gint[HWCALC] == HWCALC_PRIZM) dprint_opt(198, 2, C_RGB(255,255,255), C_NONE, DTEXT_CENTER, DTEXT_TOP, "OUTRUN FOR PRIZM" );

        dprint_opt(198, 222, C_RGB(pulse%256,0,pulse%256), C_NONE, DTEXT_CENTER, DTEXT_BOTTOM, "PRESS EXE TO START" );


        dprint_opt(394, 196, C_RGB(255,255,255), C_NONE, DTEXT_RIGHT, DTEXT_BOTTOM, "%s", version );

        // CODE HERE TO PRINT WELCOME MENU

        dupdate();

        pulse++;
        getInputStartTitle();

#if IS_FXLIB==1
        if (screenshot && usb_is_open())
        {
            usb_fxlink_screenshot(false);
            screenshot = false;
        }

        if(record && usb_is_open())
        {
            usb_fxlink_videocapture(false);
        }
#endif
    }
}

void getInputStartTitle( void )
{
    int opt = GETKEY_DEFAULT & ~GETKEY_REP_ARROWS;
    int timeout = 1;

    while(1)
    {
        key_event_t ev = getkey_opt(opt, &timeout);
        if(ev.type == KEYEV_NONE) return;

        int key = ev.key;

        if (key==KEY_EXE)
            doneTitle = true;

#if IS_FXLIB==1
        if(keydown(KEY_7))    screenshot = true;
        if(keydown(KEY_8))    record = !record;
#endif // IS_FXLIB

    }
}




int drawMainMenu( void )
{
    uint16_t pulse=0;
    doneMainMenu = false;

    MainMenuSelection=0;

    while (!doneMainMenu)
    {

        dclear(0x0000);
        dimage( 0, 0, &mainscreen);

        dfont(&autofont);

        dprint_opt(94, 22, C_RGB(0,0,0), C_NONE, DTEXT_LEFT, DTEXT_TOP, "ARCADE GAME" );
        if (MainMenuSelection!=0) dprint_opt(92, 20, C_RGB(255,255,255), C_NONE, DTEXT_LEFT, DTEXT_TOP, "ARCADE GAME" );
        else dprint_opt(92, 20, C_RGB(pulse%256,0,pulse%256), C_NONE, DTEXT_LEFT, DTEXT_TOP, "ARCADE GAME" );

        dprint_opt(94, 52, C_RGB(0,0,0), C_NONE, DTEXT_LEFT, DTEXT_TOP, "TIME ATTACK" );
        if (MainMenuSelection!=1) dprint_opt(92, 50, C_RGB(255,255,255), C_NONE, DTEXT_LEFT, DTEXT_TOP, "TIME ATTACK" );
        else dprint_opt(92, 50, C_RGB(pulse%256,0,pulse%256), C_NONE, DTEXT_LEFT, DTEXT_TOP, "TIME ATTACK" );

        dprint_opt(94, 82, C_RGB(0,0,0), C_NONE, DTEXT_LEFT, DTEXT_TOP, "GAME OPTIONS" );
        if (MainMenuSelection!=2) dprint_opt(92, 80, C_RGB(255,255,255), C_NONE, DTEXT_LEFT, DTEXT_TOP, "GAME OPTIONS" );
        else dprint_opt(92, 80, C_RGB(pulse%256,0,pulse%256), C_NONE, DTEXT_LEFT, DTEXT_TOP, "GAME OPTIONS" );

        dprint_opt(94, 112, C_RGB(0,0,0), C_NONE, DTEXT_LEFT, DTEXT_TOP, "VISUAL OPTIONS" );
        if (MainMenuSelection!=3) dprint_opt(92, 110, C_RGB(255,255,255), C_NONE, DTEXT_LEFT, DTEXT_TOP, "VISUAL OPTIONS" );
        else dprint_opt(92, 110, C_RGB(pulse%256,0,pulse%256), C_NONE, DTEXT_LEFT, DTEXT_TOP, "VISUAL OPTIONS" );

        dprint_opt(94, 142, C_RGB(0,0,0), C_NONE, DTEXT_LEFT, DTEXT_TOP, "CREDITS" );
        if (MainMenuSelection!=4) dprint_opt(92, 140, C_RGB(255,255,255), C_NONE, DTEXT_LEFT, DTEXT_TOP, "CREDITS" );
        else dprint_opt(92, 140, C_RGB(pulse%256,0,pulse%256), C_NONE, DTEXT_LEFT, DTEXT_TOP, "CREDITS" );

        dprint_opt(94, 172, C_RGB(0,0,0), C_NONE, DTEXT_LEFT, DTEXT_TOP, "QUIT TO OS" );
        if (MainMenuSelection!=5) dprint_opt(92, 170, C_RGB(255,255,255), C_NONE, DTEXT_LEFT, DTEXT_TOP, "QUIT TO OS" );
        else dprint_opt(92, 170, C_RGB(pulse%256,0,pulse%256), C_NONE, DTEXT_LEFT, DTEXT_TOP, "QUIT TO OS" );

        dimage( 2, 10+30*MainMenuSelection, &flag );

        // CODE HERE TO PRINT WELCOME MENU

        dupdate();

        pulse++;
        getInputMainMenu();

#if IS_FXLIB==1
        if (screenshot && usb_is_open())
        {
            usb_fxlink_screenshot(false);
            screenshot = false;
        }

        if(record && usb_is_open())
        {
            usb_fxlink_videocapture(false);
        }
#endif
    }

    return MainMenuSelection;
}

void getInputMainMenu( void )
{
    int opt = GETKEY_DEFAULT & ~GETKEY_REP_ARROWS;
    int timeout = 1;

    while(1)
    {
        key_event_t ev = getkey_opt(opt, &timeout);
        if(ev.type == KEYEV_NONE) return;

        int key = ev.key;

        if (key==KEY_EXE)
            doneMainMenu = true;

        if (key==KEY_UP)
        {
            if (MainMenuSelection==0) MainMenuSelection=5;
            else MainMenuSelection--;
        }

        if (key==KEY_DOWN)
        {
            if (MainMenuSelection==5) MainMenuSelection=0;
            else MainMenuSelection++;
        }
#if IS_FXLIB==1
        if(keydown(KEY_7))    screenshot = true;
        if(keydown(KEY_8))    record = !record;
#endif // IS_FXLIB
    }
}




void drawRectangle( uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t color, uint8_t thick )
{
    for(int u=0; u<=thick; u++)
    {
        dline( x, y+u, x+w, y+u, color );
        dline( x, y+h-u, x+w, y+h-u, color );
        dline( x+u, y, x+u, y+h, color );
        dline( x+w-u, y, x+w-u, y+h, color );
    }

}

int drawMenuCircuitSelect( void )
{
    uint16_t pulse=0;
    doneMenuCircuit = false;

    CircuitSelection=0;

    while (!doneMenuCircuit)
    {

        dclear( C_BLACK );
        dsubimage( 0, 32, &mainscreen, 0, 32, 396, 160,  DIMAGE_NONE);

        dfont(&autofont);

        dprint_opt(198, 2, C_RGB(pulse%256,0,pulse%256), C_NONE, DTEXT_CENTER, DTEXT_TOP, "SELECT A CIRCUIT" );

        dimage( 16, 45, &plains1 );
        dimage( 16, 45, &linear );
        if (CircuitSelection!=0) drawRectangle( 16, 45, 60, 60, C_WHITE, 3 );
        else
        {
            drawRectangle( 16, 45, 60, 60, C_RGB(pulse%256,0,pulse%256), 3 );
            dprint_opt(198, 222, C_RGB(pulse%256,0,pulse%256), C_NONE, DTEXT_CENTER, DTEXT_BOTTOM, ">EUROPEAN PLAINS" );
        }

        dimage( 92, 45, &desert1 );
        dimage( 92, 45, &linear );
        if (CircuitSelection!=1) drawRectangle( 92, 45, 60, 60, C_WHITE, 3 );
        else
        {
            drawRectangle( 92, 45, 60, 60, C_RGB(pulse%256,0,pulse%256), 3 );
            dprint_opt(198, 222, C_RGB(pulse%256,0,pulse%256), C_NONE, DTEXT_CENTER, DTEXT_BOTTOM, ">ATACAMA DESERT" );
        }

        dimage( 168, 45, &usa1 );
        dimage( 168, 45, &linear );
        if (CircuitSelection!=2) drawRectangle( 168, 45, 60, 60, C_WHITE, 3 );
        else
        {
            drawRectangle( 168, 45, 60, 60, C_RGB(pulse%256,0,pulse%256), 3 );
            dprint_opt(198, 222, C_RGB(pulse%256,0,pulse%256), C_NONE, DTEXT_CENTER, DTEXT_BOTTOM, ">US ROAD 66" );
        }

        dimage( 244, 45, &finland1 );
        dimage( 244, 45, &linear );
        if (CircuitSelection!=3) drawRectangle( 244, 45, 60, 60, C_WHITE, 3 );
        else
        {
            drawRectangle( 244, 45, 60, 60, C_RGB(pulse%256,0,pulse%256), 3 );
            dprint_opt(198, 222, C_RGB(pulse%256,0,pulse%256), C_NONE, DTEXT_CENTER, DTEXT_BOTTOM, ">SNOW IN FINLAND" );
        }

        dimage( 320, 45, &africa1 );
        dimage( 320, 45, &linear );
        if (CircuitSelection!=4) drawRectangle( 320, 45, 60, 60, C_WHITE, 3 );
        else
        {
            drawRectangle( 320, 45, 60, 60, C_RGB(pulse%256,0,pulse%256), 3 );
            dprint_opt(198, 222, C_RGB(pulse%256,0,pulse%256), C_NONE, DTEXT_CENTER, DTEXT_BOTTOM, ">KENYAN SAVANAH" );
        }

        dimage( 16, 120, &plains1 );
        dimage( 16, 120, &circular );
        if (CircuitSelection!=5) drawRectangle( 16, 120, 60, 60, C_WHITE, 3 );
        else
        {
            drawRectangle( 16, 120, 60, 60, C_RGB(pulse%256,0,pulse%256), 3 );
            dprint_opt(198, 222, C_RGB(pulse%256,0,pulse%256), C_NONE, DTEXT_CENTER, DTEXT_BOTTOM, ">EUROPEAN PLAINS" );
        }

        dimage( 92, 120, &desert1 );
        dimage( 92, 120, &circular );
        if (CircuitSelection!=6) drawRectangle( 92, 120, 60, 60, C_WHITE, 3 );
        else
        {
            drawRectangle( 92, 120, 60, 60, C_RGB(pulse%256,0,pulse%256), 3 );
            dprint_opt(198, 222, C_RGB(pulse%256,0,pulse%256), C_NONE, DTEXT_CENTER, DTEXT_BOTTOM, ">ATACAMA DESERT" );
        }

        dimage( 168, 120, &usa1 );
        dimage( 168, 120, &circular );
        if (CircuitSelection!=7) drawRectangle( 168, 120, 60, 60, C_WHITE, 3 );
        else
        {
            drawRectangle( 168, 120, 60, 60, C_RGB(pulse%256,0,pulse%256), 3 );
            dprint_opt(198, 222, C_RGB(pulse%256,0,pulse%256), C_NONE, DTEXT_CENTER, DTEXT_BOTTOM, ">US ROAD 66" );
        }

        dimage( 244, 120, &finland1 );
        dimage( 244, 120, &circular );
        if (CircuitSelection!=8) drawRectangle( 244, 120, 60, 60, C_WHITE, 3 );
        else
        {
            drawRectangle( 244, 120, 60, 60, C_RGB(pulse%256,0,pulse%256), 3 );
            dprint_opt(198, 222, C_RGB(pulse%256,0,pulse%256), C_NONE, DTEXT_CENTER, DTEXT_BOTTOM, ">SNOW IN FINLAND" );
        }

        dimage( 320, 120, &africa1 );
        dimage( 320, 120, &circular );
        if (CircuitSelection!=9) drawRectangle( 320, 120, 60, 60, C_WHITE, 3 );
        else
        {
            drawRectangle( 320, 120, 60, 60, C_RGB(pulse%256,0,pulse%256), 3 );
            dprint_opt(198, 222, C_RGB(pulse%256,0,pulse%256), C_NONE, DTEXT_CENTER, DTEXT_BOTTOM, ">KENYAN SAVANAH" );
        }



        //dimage( 10, 10+40*CircuitSelection, &flag );

        // CODE HERE TO PRINT WELCOME MENU

        dupdate();

        pulse++;
        getInputCircuitSelect();

#if IS_FXLIB==1
        if (screenshot && usb_is_open())
        {
            usb_fxlink_screenshot(false);
            screenshot = false;
        }

        if(record && usb_is_open())
        {
            usb_fxlink_videocapture(false);
        }
#endif

        if (CircuitSelection==-1) return -1;
    }

    return CircuitSelection;
}

void getInputCircuitSelect( void )
{
    int opt = GETKEY_DEFAULT & ~GETKEY_REP_ARROWS;
    int timeout = 1;

    while(1)
    {
        key_event_t ev = getkey_opt(opt, &timeout);
        if(ev.type == KEYEV_NONE) return;

        int key = ev.key;

        if (key==KEY_EXE)
            doneMenuCircuit = true;

        if (key==KEY_EXIT)
            CircuitSelection = -1;


        if (key==KEY_LEFT)
        {
            if (CircuitSelection==0) CircuitSelection=9;
            else CircuitSelection--;
        }

        if (key==KEY_RIGHT)
        {
            if (CircuitSelection==9) CircuitSelection=0;
            else CircuitSelection++;
        }

        if (key==KEY_UP)
        {
            CircuitSelection-=5;
            if (CircuitSelection<0) CircuitSelection+=10;
        }

        if (key==KEY_DOWN)
        {
            CircuitSelection+=5;
            if (CircuitSelection>9) CircuitSelection-=10;
        }


#if IS_FXLIB==1
        if(keydown(KEY_7))    screenshot = true;
        if(keydown(KEY_8))    record = !record;
#endif // IS_FXLIB
    }
}





void drawCredit( void )
{
    uint16_t pulse=0;
    int16_t offset=240;

    doneMenuCredit = false;

    while (!doneMenuCredit)
    {

        dclear( 0x0000 );
        dimage( 0, 0, &mainscreen );

        dfont(&autofont);
        if (gint[HWCALC] == HWCALC_FXCG50 || gint[HWCALC] == HWCALC_FXCG_MANAGER)
        {
            dprint_opt(202, offset + 4, C_RGB(0,0,0), C_NONE, DTEXT_CENTER, DTEXT_TOP, "OUTRUN FOR FX-CG50" );
            dprint_opt(200, offset + 2, C_RGB(pulse%256,0,pulse%256), C_NONE, DTEXT_CENTER, DTEXT_TOP, "OUTRUN FOR FX-CG50" );
        }

        else if (gint[HWCALC] == HWCALC_PRIZM)
        {
            dprint_opt(202, offset + 4, C_RGB(0,0,0), C_NONE, DTEXT_CENTER, DTEXT_TOP, "OUTRUN FOR PRIZM" );
            dprint_opt(200, offset + 2, C_RGB(pulse%256,0,pulse%256), C_NONE, DTEXT_CENTER, DTEXT_TOP, "OUTRUN FOR PRIZM" );
        }

        dfont(&autofontsmall);
        dprint_opt(200, offset + 32, C_RGB(255,255,255), C_NONE, DTEXT_CENTER, DTEXT_TOP, "PROUDLY CODED BY" );
        dprint_opt(200, offset + 62, C_RGB(255,255,255), C_NONE, DTEXT_CENTER, DTEXT_TOP, "SLYVTT / MARCH 2022" );

        dfont(&autofont);
        dprint_opt(202, offset + 124, C_RGB(0,0,0), C_NONE, DTEXT_CENTER, DTEXT_TOP, "CODE :" );
        dprint_opt(200, offset + 122, C_RGB(255,0,255), C_NONE, DTEXT_CENTER, DTEXT_TOP, "CODE :" );
        dfont(&autofontsmall);
        dprint_opt(200, offset + 162, C_RGB(255,255,255), C_NONE, DTEXT_CENTER, DTEXT_TOP, "SLYVTT" );
        dprint_opt(200, offset + 192, C_RGB(255,255,255), C_NONE, DTEXT_CENTER, DTEXT_TOP, "WITH SUPPORT FROM LEPHE" );

        dfont(&autofont);
        dprint_opt(202, offset + 254, C_RGB(0,0,0), C_NONE, DTEXT_CENTER, DTEXT_TOP, "VISUALS" );
        dprint_opt(200, offset + 252, C_RGB(255,0,255), C_NONE, DTEXT_CENTER, DTEXT_TOP, "VISUALS" );
        dfont(&autofontsmall);
        dprint_opt(200, offset + 292, C_RGB(255,255,255), C_NONE, DTEXT_CENTER, DTEXT_TOP, "SLYVTT" );
        dprint_opt(200, offset + 322, C_RGB(255,255,255), C_NONE, DTEXT_CENTER, DTEXT_TOP, "WITH SUPPORT FROM INTERNET" );
        dprint_opt(200, offset + 352, C_RGB(255,255,255), C_NONE, DTEXT_CENTER, DTEXT_TOP, "GOOGLE IS MY BEST FRIEND" );

        dfont(&autofont);
        dprint_opt(202, offset + 414, C_RGB(0,0,0), C_NONE, DTEXT_CENTER, DTEXT_TOP, "MUSIC" );
        dprint_opt(200, offset + 412, C_RGB(255,0,255), C_NONE, DTEXT_CENTER, DTEXT_TOP, "MUSIC" );
        dfont(&autofontsmall);
        dprint_opt(200, offset + 452, C_RGB(255,255,255), C_NONE, DTEXT_CENTER, DTEXT_TOP, "SORRY, NO MUSIC ..." );
        dprint_opt(200, offset + 482, C_RGB(255,255,255), C_NONE, DTEXT_CENTER, DTEXT_TOP, "... MAYBE ONE DAY !!!" );

        dfont(&autofont);
        dprint_opt(202, offset + 544, C_RGB(0,0,0), C_NONE, DTEXT_CENTER, DTEXT_TOP, "THANKS FOR PLAYING" );
        dprint_opt(202, offset + 574, C_RGB(0,0,0), C_NONE, DTEXT_CENTER, DTEXT_TOP, "HOPE YOU ENJOYED" );
        dprint_opt(200, offset + 542, C_RGB(pulse%256,0,pulse%256), C_NONE, DTEXT_CENTER, DTEXT_TOP, "THANKS FOR PLAYING" );
        dprint_opt(200, offset + 572, C_RGB(pulse%256,0,pulse%256), C_NONE, DTEXT_CENTER, DTEXT_TOP, "HOPE YOU ENJOYED" );

        dupdate();

#if IS_FXLIB==1
        if (screenshot && usb_is_open())
        {
            usb_fxlink_screenshot(false);
            screenshot = false;
        }

        if(record && usb_is_open())
        {
            usb_fxlink_videocapture(false);
        }
#endif

        pulse++;
        offset--;
        getInputCredit();
    }

    return;
}

void getInputCredit( void )
{
    int opt = GETKEY_DEFAULT & ~GETKEY_REP_ARROWS;
    int timeout = 1;

    while(1)
    {
        key_event_t ev = getkey_opt(opt, &timeout);
        if(ev.type == KEYEV_NONE) return;

        int key = ev.key;

        if (key==KEY_EXE || key==KEY_EXIT)
            doneMenuCredit = true;

#if IS_FXLIB==1
        if(keydown(KEY_7))    screenshot = true;
        if(keydown(KEY_8))    record = !record;
#endif // IS_FXLIB
    }
}


void drawOptions( void )
{
    uint16_t pulse=0;
    doneOptions = false;

    OptionsSelection=0;

    while (!doneOptions)
    {

        dclear(0x0000);
        dimage( 0, 0, &mainscreen);

        dfont(&autofont);

        if (BDrawDeco)
        {
            dprint_opt(94, 22, C_RGB(0,0,0), C_NONE, DTEXT_LEFT, DTEXT_TOP, "SCENERY [Y]" );
            if (OptionsSelection!=0) dprint_opt(92, 20, C_RGB(255,255,255), C_NONE, DTEXT_LEFT, DTEXT_TOP, "SCENERY [Y]" );
            else dprint_opt(92, 20, C_RGB(pulse%256,0,pulse%256), C_NONE, DTEXT_LEFT, DTEXT_TOP, "SCENERY [Y]" );
        }
        else
        {
            dprint_opt(94, 22, C_RGB(0,0,0), C_NONE, DTEXT_LEFT, DTEXT_TOP, "SCENERY [N]" );
            if (OptionsSelection!=0) dprint_opt(92, 20, C_RGB(255,255,255), C_NONE, DTEXT_LEFT, DTEXT_TOP, "SCENERY [N]" );
            else dprint_opt(92, 20, C_RGB(pulse%256,0,pulse%256), C_NONE, DTEXT_LEFT, DTEXT_TOP, "SCENERY [N]" );
        }


        if (BDrawCars)
        {
            dprint_opt(94, 52, C_RGB(0,0,0), C_NONE, DTEXT_LEFT, DTEXT_TOP, "OPPONENTS [Y]" );
            if (OptionsSelection!=1) dprint_opt(92, 50, C_RGB(255,255,255), C_NONE, DTEXT_LEFT, DTEXT_TOP, "OPPONENTS [Y]" );
            else dprint_opt(92, 50, C_RGB(pulse%256,0,pulse%256), C_NONE, DTEXT_LEFT, DTEXT_TOP, "OPPONENTS [Y]" );
        }
        else
        {
            dprint_opt(94, 52, C_RGB(0,0,0), C_NONE, DTEXT_LEFT, DTEXT_TOP, "OPPONENTS [N]" );
            if (OptionsSelection!=1) dprint_opt(92, 50, C_RGB(255,255,255), C_NONE, DTEXT_LEFT, DTEXT_TOP, "OPPONENTS [N]" );
            else dprint_opt(92, 50, C_RGB(pulse%256,0,pulse%256), C_NONE, DTEXT_LEFT, DTEXT_TOP, "OPPONENTS [N]" );
        }


        if (BDrawClds)
        {
            dprint_opt(94, 82, C_RGB(0,0,0), C_NONE, DTEXT_LEFT, DTEXT_TOP, "CLOUDS [Y]" );
            if (OptionsSelection!=2) dprint_opt(92, 80, C_RGB(255,255,255), C_NONE, DTEXT_LEFT, DTEXT_TOP, "CLOUDS [Y]" );
            else dprint_opt(92, 80, C_RGB(pulse%256,0,pulse%256), C_NONE, DTEXT_LEFT, DTEXT_TOP, "CLOUDS [Y]" );
        }
        else
        {
            dprint_opt(94, 82, C_RGB(0,0,0), C_NONE, DTEXT_LEFT, DTEXT_TOP, "CLOUDS [N]" );
            if (OptionsSelection!=2) dprint_opt(92, 80, C_RGB(255,255,255), C_NONE, DTEXT_LEFT, DTEXT_TOP, "CLOUDS [N]" );
            else dprint_opt(92, 80, C_RGB(pulse%256,0,pulse%256), C_NONE, DTEXT_LEFT, DTEXT_TOP, "CLOUDS [N]" );
        }

        if (BDrawBack)
        {
            dprint_opt(94, 112, C_RGB(0,0,0), C_NONE, DTEXT_LEFT, DTEXT_TOP, "BACKGROUND [Y]" );
            if (OptionsSelection!=3) dprint_opt(92, 110, C_RGB(255,255,255), C_NONE, DTEXT_LEFT, DTEXT_TOP, "BACKGROUND [Y]" );
            else dprint_opt(92, 110, C_RGB(pulse%256,0,pulse%256), C_NONE, DTEXT_LEFT, DTEXT_TOP, "BACKGROUND [Y]" );
        }
        else
        {
            dprint_opt(94, 112, C_RGB(0,0,0), C_NONE, DTEXT_LEFT, DTEXT_TOP, "BACKGROUND [N]" );
            if (OptionsSelection!=3) dprint_opt(92, 110, C_RGB(255,255,255), C_NONE, DTEXT_LEFT, DTEXT_TOP, "BACKGROUND [N]" );
            else dprint_opt(92, 110, C_RGB(pulse%256,0,pulse%256), C_NONE, DTEXT_LEFT, DTEXT_TOP, "BACKGROUND [N]" );
        }

        if (BDrawFPS)
        {
            dprint_opt(94, 142, C_RGB(0,0,0), C_NONE, DTEXT_LEFT, DTEXT_TOP, "STATS-FPS [Y]" );
            if (OptionsSelection!=4) dprint_opt(92, 140, C_RGB(255,255,255), C_NONE, DTEXT_LEFT, DTEXT_TOP, "STATS-FPS [Y]" );
            else dprint_opt(92, 140, C_RGB(pulse%256,0,pulse%256), C_NONE, DTEXT_LEFT, DTEXT_TOP, "STATS-FPS [Y]" );
        }
        else
        {
            dprint_opt(94, 142, C_RGB(0,0,0), C_NONE, DTEXT_LEFT, DTEXT_TOP, "STATS-FPS [N]" );
            if (OptionsSelection!=4) dprint_opt(92, 140, C_RGB(255,255,255), C_NONE, DTEXT_LEFT, DTEXT_TOP, "STATS-FPS [N]" );
            else dprint_opt(92, 140, C_RGB(pulse%256,0,pulse%256), C_NONE, DTEXT_LEFT, DTEXT_TOP, "STATS-FPS [N]" );
        }


        dprint_opt(94, 172, C_RGB(0,0,0), C_NONE, DTEXT_LEFT, DTEXT_TOP, "BACK TO GAME" );
        if (OptionsSelection!=5) dprint_opt(92, 170, C_RGB(255,255,255), C_NONE, DTEXT_LEFT, DTEXT_TOP, "BACK TO GAME" );
        else dprint_opt(92, 170, C_RGB(pulse%256,0,pulse%256), C_NONE, DTEXT_LEFT, DTEXT_TOP, "BACK TO GAME" );

        dimage( 2, 5+30*OptionsSelection, &flag );

        // CODE HERE TO PRINT WELCOME MENU

        dupdate();

        pulse++;
        getInputOptions();

#if IS_FXLIB==1
        if (screenshot && usb_is_open())
        {
            usb_fxlink_screenshot(false);
            screenshot = false;
        }

        if(record && usb_is_open())
        {
            usb_fxlink_videocapture(false);
        }
#endif
    }

    SkipTime = true;    // To avoid counting or de-counting time during menu

    return;
}

void getInputOptions( void )
{
    int opt = GETKEY_DEFAULT & ~GETKEY_REP_ARROWS;
    int timeout = 1;

    while(1)
    {
        key_event_t ev = getkey_opt(opt, &timeout);
        if(ev.type == KEYEV_NONE) return;

        int key = ev.key;

        if (key==KEY_EXE)
        {
            if (OptionsSelection==0) BDrawDeco = !BDrawDeco;
            else if (OptionsSelection==1) BDrawCars = !BDrawCars;
            else if (OptionsSelection==2) BDrawClds = !BDrawClds;
            else if (OptionsSelection==3) BDrawBack = !BDrawBack;
            else if (OptionsSelection==4) BDrawFPS = !BDrawFPS;
            else doneOptions=true;
        }
        if (key==KEY_UP)
        {
            if (OptionsSelection==0) OptionsSelection=5;
            else OptionsSelection--;
        }

        if (key==KEY_DOWN)
        {
            if (OptionsSelection==5) OptionsSelection=0;
            else OptionsSelection++;
        }

#if IS_FXLIB==1
        if(keydown(KEY_7))    screenshot = true;
        if(keydown(KEY_8))    record = !record;
#endif // IS_FXLIB
    }
}




void drawGameOptions( void )
{
    uint16_t pulse=0;
    doneGameOptions = false;

    GameOptionsSelection=0;
    selectedChar = 0;

    while (!doneGameOptions)
    {

        dclear(0x0000);
        dimage( 0, 0, &mainscreen);

        dfont(&autofont);

        if (DiffLevel==0)
        {
            dprint_opt(94, 22, C_RGB(0,0,0), C_NONE, DTEXT_LEFT, DTEXT_TOP, "LVL EASY >" );
            if (GameOptionsSelection!=0) dprint_opt(92, 20, C_RGB(255,255,255), C_NONE, DTEXT_LEFT, DTEXT_TOP, "LVL EASY >" );
            else dprint_opt(92, 20, C_RGB(pulse%256,0,pulse%256), C_NONE, DTEXT_LEFT, DTEXT_TOP, "LVL EASY >" );
        }
        else if (DiffLevel==1)
        {
            dprint_opt(94, 22, C_RGB(0,0,0), C_NONE, DTEXT_LEFT, DTEXT_TOP, "LVL < NORMAL >" );
            if (GameOptionsSelection!=0) dprint_opt(92, 20, C_RGB(255,255,255), C_NONE, DTEXT_LEFT, DTEXT_TOP, "LVL < NORMAL >" );
            else dprint_opt(92, 20, C_RGB(pulse%256,0,pulse%256), C_NONE, DTEXT_LEFT, DTEXT_TOP, "LVL < NORMAL >" );
        }
        else if (DiffLevel==2)
        {
            dprint_opt(94, 22, C_RGB(0,0,0), C_NONE, DTEXT_LEFT, DTEXT_TOP, "LVL < HARD" );
            if (GameOptionsSelection!=0) dprint_opt(92, 20, C_RGB(255,255,255), C_NONE, DTEXT_LEFT, DTEXT_TOP, "LVL < HARD" );
            else dprint_opt(92, 20, C_RGB(pulse%256,0,pulse%256), C_NONE, DTEXT_LEFT, DTEXT_TOP, "LVL < HARD" );
        }


        if (CarsNumb==0)
        {
            dprint_opt(94, 52, C_RGB(0,0,0), C_NONE, DTEXT_LEFT, DTEXT_TOP, "CARS 50 >" );
            if (GameOptionsSelection!=1) dprint_opt(92, 50, C_RGB(255,255,255), C_NONE, DTEXT_LEFT, DTEXT_TOP, "CARS 50 >" );
            else dprint_opt(92, 50, C_RGB(pulse%256,0,pulse%256), C_NONE, DTEXT_LEFT, DTEXT_TOP, "CARS 50 >" );
        }
        else if (CarsNumb==1)
        {
            dprint_opt(94, 52, C_RGB(0,0,0), C_NONE, DTEXT_LEFT, DTEXT_TOP, "CARS < 100 >" );
            if (GameOptionsSelection!=1) dprint_opt(92, 50, C_RGB(255,255,255), C_NONE, DTEXT_LEFT, DTEXT_TOP, "CARS < 100 >" );
            else dprint_opt(92, 50, C_RGB(pulse%256,0,pulse%256), C_NONE, DTEXT_LEFT, DTEXT_TOP, "CARS < 100 >" );
        }
        else if (CarsNumb==2)
        {
            dprint_opt(94, 52, C_RGB(0,0,0), C_NONE, DTEXT_LEFT, DTEXT_TOP, "CARS < 200 >" );
            if (GameOptionsSelection!=1) dprint_opt(92, 50, C_RGB(255,255,255), C_NONE, DTEXT_LEFT, DTEXT_TOP, "CARS < 200 >" );
            else dprint_opt(92, 50, C_RGB(pulse%256,0,pulse%256), C_NONE, DTEXT_LEFT, DTEXT_TOP, "CARS < 200 >" );
        }
        else if (CarsNumb==3)
        {
            dprint_opt(94, 52, C_RGB(0,0,0), C_NONE, DTEXT_LEFT, DTEXT_TOP, "CARS < 300" );
            if (GameOptionsSelection!=1) dprint_opt(92, 50, C_RGB(255,255,255), C_NONE, DTEXT_LEFT, DTEXT_TOP, "CARS < 300" );
            else dprint_opt(92, 50, C_RGB(pulse%256,0,pulse%256), C_NONE, DTEXT_LEFT, DTEXT_TOP, "CARS < 300" );
        }
/*
        dprint_opt(94, 82, C_RGB(0,0,0), C_NONE, DTEXT_LEFT, DTEXT_TOP, "PLAYER" );
        dprint_opt(92, 80, C_RGB(255,255,255), C_NONE, DTEXT_LEFT, DTEXT_TOP, "PLAYER" );
*/

        dprint_opt(94, 82, C_RGB(0,0,0), C_NONE, DTEXT_LEFT, DTEXT_TOP, "PLAYER NAME" );
        if (GameOptionsSelection!=2) dprint_opt(92, 80, C_RGB(255,255,255), C_NONE, DTEXT_LEFT, DTEXT_TOP, "PLAYER NAME" );
        else dprint_opt(92, 80, C_RGB(pulse%256,0,pulse%256), C_NONE, DTEXT_LEFT, DTEXT_TOP, "PLAYER NAME" );

        if(nameCurrentlyWritten==true)
        {
            dfont(&autofont);

            dprint_opt(124, 112, C_RGB(0,0,0), C_NONE, DTEXT_LEFT, DTEXT_TOP, "%c", PlayerName[0] );
            if (selectedChar!=0) dprint_opt(122, 110, C_RGB(255,255,255), C_NONE, DTEXT_LEFT, DTEXT_TOP, "%c", PlayerName[0] );
            else dprint_opt(122, 110, C_RGB(pulse%256,0,pulse%256), C_NONE, DTEXT_LEFT, DTEXT_TOP, "%c", PlayerName[0] );

            dprint_opt(154, 112, C_RGB(0,0,0), C_NONE, DTEXT_LEFT, DTEXT_TOP, "%c", PlayerName[1] );
            if (selectedChar!=1) dprint_opt(152, 110, C_RGB(255,255,255), C_NONE, DTEXT_LEFT, DTEXT_TOP, "%c", PlayerName[1] );
            else dprint_opt(152, 110, C_RGB(pulse%256,0,pulse%256), C_NONE, DTEXT_LEFT, DTEXT_TOP, "%c", PlayerName[1] );

            dprint_opt(184, 112, C_RGB(0,0,0), C_NONE, DTEXT_LEFT, DTEXT_TOP, "%c", PlayerName[2] );
            if (selectedChar!=2) dprint_opt(182, 110, C_RGB(255,255,255), C_NONE, DTEXT_LEFT, DTEXT_TOP, "%c", PlayerName[2] );
            else dprint_opt(182, 110, C_RGB(pulse%256,0,pulse%256), C_NONE, DTEXT_LEFT, DTEXT_TOP, "%c", PlayerName[2] );

            dfont(&speedfont);
            dprint_opt(214, 112, C_RGB(0,0,0), C_NONE, DTEXT_LEFT, DTEXT_TOP, "@" );
            if (selectedChar!=3) dprint_opt(212, 110, C_RGB(255,255,255), C_NONE, DTEXT_LEFT, DTEXT_TOP, "@" );
            else dprint_opt(212, 110, C_RGB(pulse%256,0,pulse%256), C_NONE, DTEXT_LEFT, DTEXT_TOP, "@" );
        }
        else
        {
                    dfont(&autofont);
                    dprint_opt(124, 112, C_RGB(0,0,0), C_NONE, DTEXT_LEFT, DTEXT_TOP, "%c", PlayerName[0] );
                    dprint_opt(122, 110, C_RGB(255,255,255), C_NONE, DTEXT_LEFT, DTEXT_TOP, "%c", PlayerName[0] );
                    dprint_opt(154, 112, C_RGB(0,0,0), C_NONE, DTEXT_LEFT, DTEXT_TOP, "%c", PlayerName[1] );
                    dprint_opt(152, 110, C_RGB(255,255,255), C_NONE, DTEXT_LEFT, DTEXT_TOP, "%c", PlayerName[1] );
                    dprint_opt(184, 112, C_RGB(0,0,0), C_NONE, DTEXT_LEFT, DTEXT_TOP, "%c", PlayerName[2] );
                    dprint_opt(182, 110, C_RGB(255,255,255), C_NONE, DTEXT_LEFT, DTEXT_TOP, "%c", PlayerName[2] );

                    dfont(&speedfont);
                    dprint_opt(214, 112, C_RGB(0,0,0), C_NONE, DTEXT_LEFT, DTEXT_TOP, "@" );
                    dprint_opt(212, 110, C_RGB(255,255,255), C_NONE, DTEXT_LEFT, DTEXT_TOP, "@" );
        }



        dfont(&autofont);
        if(isOCPossible)
        {
            if (isOCActivated==false)
            {
                dprint_opt(94, 142, C_RGB(0,0,0), C_NONE, DTEXT_LEFT, DTEXT_TOP, "OVERCLOCK [N]" );
                if (GameOptionsSelection!=3) dprint_opt(92, 140, C_RGB(255,255,255), C_NONE, DTEXT_LEFT, DTEXT_TOP, "OVERCLOCK [N]" );
                else dprint_opt(92, 140, C_RGB(pulse%256,0,pulse%256), C_NONE, DTEXT_LEFT, DTEXT_TOP, "OVERCLOCK [N]" );
            }
            else
            {
                dprint_opt(94, 142, C_RGB(0,0,0), C_NONE, DTEXT_LEFT, DTEXT_TOP, "OVERCLOCK [Y]" );
                if (GameOptionsSelection!=3) dprint_opt(92, 140, C_RGB(255,255,255), C_NONE, DTEXT_LEFT, DTEXT_TOP, "OVERCLOCK [Y]" );
                else dprint_opt(92, 140, C_RGB(pulse%256,0,pulse%256), C_NONE, DTEXT_LEFT, DTEXT_TOP, "OVERCLOCK [Y]" );
            }
        }



        dfont(&autofont);
        dprint_opt(94, 172, C_RGB(0,0,0), C_NONE, DTEXT_LEFT, DTEXT_TOP, "BACK TO GAME" );
        if (GameOptionsSelection!=4) dprint_opt(92, 170, C_RGB(255,255,255), C_NONE, DTEXT_LEFT, DTEXT_TOP, "BACK TO GAME" );
        else dprint_opt(92, 170, C_RGB(pulse%256,0,pulse%256), C_NONE, DTEXT_LEFT, DTEXT_TOP, "BACK TO GAME" );

        if (GameOptionsSelection==0) dimage( 2, 5, &flag );
        else if (GameOptionsSelection==1) dimage( 2, 35, &flag );
        else if (GameOptionsSelection==2) dimage( 2, 65, &flag );
        else if (GameOptionsSelection==3) dimage( 2, 125, &flag );
        else if (GameOptionsSelection==4) dimage( 2, 155, &flag );

        // CODE HERE TO PRINT WELCOME MENU

        dupdate();

        pulse++;
        getInputGameOptions();

#if IS_FXLIB==1
        if (screenshot && usb_is_open())
        {
            usb_fxlink_screenshot(false);
            screenshot = false;
        }

        if(record && usb_is_open())
        {
            usb_fxlink_videocapture(false);
        }
#endif
    }

    SkipTime = true;    // To avoid counting or de-counting time during menu

    return;
}

void getInputGameOptions( void )
{
    int opt = GETKEY_DEFAULT & ~GETKEY_REP_ARROWS;
    int timeout = 1;

    while(1)
    {
        key_event_t ev = getkey_opt(opt, &timeout);
        if(ev.type == KEYEV_NONE) return;

        int key = ev.key;

        if (key==KEY_EXE)
        {
            if (GameOptionsSelection==2 && nameCurrentlyWritten==false)
            {
                nameCurrentlyWritten=true;
                selectedChar=0;
            }
            else if (GameOptionsSelection==2 && nameCurrentlyWritten==true && selectedChar<3) selectedChar++;
            else if (GameOptionsSelection==2 && nameCurrentlyWritten==true && selectedChar==3)
            {
                nameCurrentlyWritten=false;
                selectedChar=0;
            }
            if (GameOptionsSelection==3 && isOCPossible==true) isOCActivated=!isOCActivated;
            else if (GameOptionsSelection==4) doneGameOptions=true;
        }

        if (key==KEY_UP && !nameCurrentlyWritten)
        {
            if (GameOptionsSelection==0) GameOptionsSelection=4;
            else if (GameOptionsSelection==4 && isOCPossible==false) GameOptionsSelection=2;
            else GameOptionsSelection--;
        }

        if (key==KEY_UP && nameCurrentlyWritten)
        {
            if(GameOptionsSelection==2 && selectedChar>=0 && selectedChar<=2)
            {
                if (PlayerName[selectedChar]>'A') PlayerName[selectedChar]--;
                else PlayerName[selectedChar]='Z';
            }
            else if(GameOptionsSelection==2 && selectedChar==3)
            {
                nameCurrentlyWritten=false;
            }
        }

        if (key==KEY_DOWN && !nameCurrentlyWritten)
        {
            if (GameOptionsSelection==4) GameOptionsSelection=0;
            else if (GameOptionsSelection==2 && isOCPossible==false) GameOptionsSelection=4;
            else GameOptionsSelection++;
        }

        if (key==KEY_DOWN && nameCurrentlyWritten)
        {
            if(GameOptionsSelection==2 && selectedChar>=0 && selectedChar<=2)
            {
                if (PlayerName[selectedChar]<'Z') PlayerName[selectedChar]++;
                else PlayerName[selectedChar]='A';
            }
            else if(GameOptionsSelection==2 && selectedChar==3)
            {
                nameCurrentlyWritten=false;
            }
        }

        if (key==KEY_LEFT)
        {
            if (GameOptionsSelection==2 && selectedChar>=0 && selectedChar<=2) nameCurrentlyWritten=true;
            else if (GameOptionsSelection==2 && selectedChar==3) nameCurrentlyWritten=false;

            if (GameOptionsSelection==2 && selectedChar>0) selectedChar--;
            else if (GameOptionsSelection==2) selectedChar=3;

            if (GameOptionsSelection==0 && DiffLevel>0) DiffLevel--;
            else if (GameOptionsSelection==1 && CarsNumb>0) CarsNumb--;
            else if (GameOptionsSelection==3) isOCActivated=false;
        }

        if (key==KEY_RIGHT)
        {
            if (GameOptionsSelection==2 && selectedChar>=0 && selectedChar<=2) nameCurrentlyWritten=true;
            else if (GameOptionsSelection==2 && selectedChar==3) nameCurrentlyWritten=false;

            if (GameOptionsSelection==2 && selectedChar<4) selectedChar++;
            else if (GameOptionsSelection==2) selectedChar=0;

            if (GameOptionsSelection==0 && DiffLevel<2) DiffLevel++;
            else if (GameOptionsSelection==1 && CarsNumb<3) CarsNumb++;
            else if (GameOptionsSelection==3) isOCActivated=true;
        }

#if IS_FXLIB==1
        if(keydown(KEY_7))    screenshot = true;
        if(keydown(KEY_8))    record = !record;
#endif // IS_FXLIB
    }
}





void drawPauseQuit( void )
{
    uint16_t pulse=0;
    donePauseQuit = false;

    PauseQuitSelection=0;

    while (!donePauseQuit)
    {

        dclear(0x0000);
        dimage( 0, 0, &mainscreen);

        dfont(&autofont);


        dprint_opt(102, 22, C_RGB(0,0,0), C_NONE, DTEXT_LEFT, DTEXT_TOP, "GAME PAUSED" );
        dprint_opt(100, 20, C_RGB(255,0,255), C_NONE, DTEXT_LEFT, DTEXT_TOP, "GAME PAUSED" );



        dprint_opt(102, 82, C_RGB(0,0,0), C_NONE, DTEXT_LEFT, DTEXT_TOP, "FINISH RACE" );
        if (PauseQuitSelection!=0) dprint_opt(100, 80, C_RGB(255,255,255), C_NONE, DTEXT_LEFT, DTEXT_TOP, "FINISH RACE" );
        else dprint_opt(100, 80, C_RGB(pulse%256,0,pulse%256), C_NONE, DTEXT_LEFT, DTEXT_TOP, "FINISH RACE" );


        dprint_opt(102, 122, C_RGB(0,0,0), C_NONE, DTEXT_LEFT, DTEXT_TOP, "MAIN MENU" );
        if (PauseQuitSelection!=1) dprint_opt(100, 120, C_RGB(255,255,255), C_NONE, DTEXT_LEFT, DTEXT_TOP, "MAIN MENU" );
        else dprint_opt(100, 120, C_RGB(pulse%256,0,pulse%256), C_NONE, DTEXT_LEFT, DTEXT_TOP, "MAIN MENU" );

        dprint_opt(102, 162, C_RGB(0,0,0), C_NONE, DTEXT_LEFT, DTEXT_TOP, "BACK TO OS" );
        if (PauseQuitSelection!=2) dprint_opt(100, 160, C_RGB(255,255,255), C_NONE, DTEXT_LEFT, DTEXT_TOP, "BACK TO OS" );
        else dprint_opt(100, 160, C_RGB(pulse%256,0,pulse%256), C_NONE, DTEXT_LEFT, DTEXT_TOP, "BACK TO OS" );

        dimage( 10, 70+40*PauseQuitSelection, &flag );

        // CODE HERE TO PRINT WELCOME MENU

        dupdate();

        pulse++;
        getInputPauseQuit();

#if IS_FXLIB==1
        if (screenshot && usb_is_open())
        {
            usb_fxlink_screenshot(false);
            screenshot = false;
        }

        if(record && usb_is_open())
        {
            usb_fxlink_videocapture(false);
        }
#endif
    }

    SkipTime = true;  // To avoid counting or de-counting time during menu

    return;
}

void getInputPauseQuit( void )
{
    int opt = GETKEY_DEFAULT & ~GETKEY_REP_ARROWS;
    int timeout = 1;

    while(1)
    {
        key_event_t ev = getkey_opt(opt, &timeout);
        if(ev.type == KEYEV_NONE) return;

        int key = ev.key;

        if (key==KEY_EXE)
        {
            if (PauseQuitSelection==0)
            {
                donePauseQuit=true;
            }
            else if (PauseQuitSelection==1)
            {
                stop=true;
                donePauseQuit=true;
            }
            else if (PauseQuitSelection==2)
            {
                stop =true;
                exitToOS=true;
                donePauseQuit=true;
            }
        }
        if (key==KEY_UP)
        {
            if (PauseQuitSelection==0) PauseQuitSelection=2;
            else PauseQuitSelection--;
        }

        if (key==KEY_DOWN)
        {
            if (PauseQuitSelection==2) PauseQuitSelection=0;
            else PauseQuitSelection++;
        }

#if IS_FXLIB==1
        if(keydown(KEY_7))    screenshot = true;
        if(keydown(KEY_8))    record = !record;
#endif // IS_FXLIB
    }
}



int drawMenuCircuitDetails( int circuit )
{
    uint16_t pulse=0;
    doneMenuDetailsCircuit = false;

    CircuitDetailsSelection=0;

    bopti_image_t *preview1=nullptr, *preview2=nullptr, *src1=nullptr, *src2=nullptr;

    if (circuit==0)  src1=&plains1, src2=&linear;
    else if (circuit==1)  src1=&desert1, src2=&linear;
    else if (circuit==2)  src1=&usa1, src2=&linear;
    else if (circuit==3)  src1=&finland1, src2=&linear;
    else if (circuit==4)  src1=&usa1, src2=&linear;
    else if (circuit==5)  src1=&plains1, src2=&circular;
    else if (circuit==6)  src1=&desert1, src2=&circular;
    else if (circuit==7)  src1=&usa1, src2=&circular;
    else if (circuit==8)  src1=&finland1, src2=&circular;
    else if (circuit==9)  src1=&usa1, src2=&circular;

    preview1 = resize( src1, src1->width*1.5, src1->height*1.5 );
    preview2 = resize( src2, src2->width*1.5, src2->height*1.5 );


    while (!doneMenuDetailsCircuit)
    {

        dclear( C_BLACK );
        dsubimage( 0, 32, &mainscreen, 0, 32, 396, 140,  DIMAGE_NONE);

        dfont(&autofont);

        dimage( 16, 45, preview1 );
        dimage( 16, 45, preview2 );

        drawRectangle( 16, 45, 90, 90, C_RGB(255,255,255), 3 );

        if (circuit==0 || circuit==5) dprint_opt(198, 2, C_RGB(255,255,255), C_NONE, DTEXT_CENTER, DTEXT_TOP, "EUROPEAN PLAINS" );
        else if (circuit==1 || circuit==6) dprint_opt(198, 2, C_RGB(255,255,255), C_NONE, DTEXT_CENTER, DTEXT_TOP, "ATACAMA DESERT" );
        else if (circuit==2 || circuit==7) dprint_opt(198, 2, C_RGB(255,255,255), C_NONE, DTEXT_CENTER, DTEXT_TOP, "US ROAD 66" );
        else if (circuit==3 || circuit==8) dprint_opt(198, 2, C_RGB(255,255,255), C_NONE, DTEXT_CENTER, DTEXT_TOP, "SNOW IN FINLAND" );
        else if (circuit==4 || circuit==9) dprint_opt(198, 2, C_RGB(255,255,255), C_NONE, DTEXT_CENTER, DTEXT_TOP, "KENYAN SAVANAH" );


        if (circuit==0 || circuit==1 || circuit==2 || circuit==5 || circuit==6 || circuit==7)
        {
            dprint_opt(122, 42, C_RGB(0,0,0), C_NONE, DTEXT_LEFT, DTEXT_TOP, "GRIP - GOOD" );
            dprint_opt(120, 40, C_RGB(255,255,255), C_NONE, DTEXT_LEFT, DTEXT_TOP, "GRIP - GOOD" );
        }
        else if (circuit==3 || circuit==8)
        {
            dprint_opt(122, 42, C_RGB(0,0,0), C_NONE, DTEXT_LEFT, DTEXT_TOP, "GRIP - LOW" );
            dprint_opt(120, 40, C_RGB(255,255,255), C_NONE, DTEXT_LEFT, DTEXT_TOP, "GRIP - LOW" );
        }
        else if (circuit==4 || circuit==9)
        {
            dprint_opt(122, 42, C_RGB(0,0,0), C_NONE, DTEXT_LEFT, DTEXT_TOP, "GRIP - SLIDE" );
            dprint_opt(120, 40, C_RGB(255,255,255), C_NONE, DTEXT_LEFT, DTEXT_TOP, "GRIP - SLIDE" );
        }


        if (circuit==0 || circuit==1 || circuit==2 || circuit==5 || circuit==6 || circuit==7)
        {
            dprint_opt(122, 82, C_RGB(0,0,0), C_NONE, DTEXT_LEFT, DTEXT_TOP, "ASPHALT" );
            dprint_opt(120, 80, C_RGB(255,255,255), C_NONE, DTEXT_LEFT, DTEXT_TOP, "ASPHALT" );
        }
        else if (circuit==3 || circuit==8)
        {
            dprint_opt(122, 82, C_RGB(0,0,0), C_NONE, DTEXT_LEFT, DTEXT_TOP, "ICE AND SNOW" );
            dprint_opt(120, 80, C_RGB(255,255,255), C_NONE, DTEXT_LEFT, DTEXT_TOP, "ICE AND SNOW" );
        }
        else if (circuit==4 || circuit==9)
        {
            dprint_opt(122, 82, C_RGB(0,0,0), C_NONE, DTEXT_LEFT, DTEXT_TOP, "DIRT AND DUST" );
            dprint_opt(120, 80, C_RGB(255,255,255), C_NONE, DTEXT_LEFT, DTEXT_TOP, "DIRT AND DUST" );
        }


        if (circuit==0 || circuit==1 || circuit==2 || circuit==3 || circuit==4)
        {
            dprint_opt(122, 122, C_RGB(0,0,0), C_NONE, DTEXT_LEFT, DTEXT_TOP, "ONE LAP" );
            dprint_opt(120, 120, C_RGB(255,255,255), C_NONE, DTEXT_LEFT, DTEXT_TOP, "ONE LAP" );
        }
        else
        {
            dprint_opt(122, 122, C_RGB(0,0,0), C_NONE, DTEXT_LEFT, DTEXT_TOP, "THREE LAPS" );
            dprint_opt(120, 120, C_RGB(255,255,255), C_NONE, DTEXT_LEFT, DTEXT_TOP, "THREE LAPS" );
        }

        dprint_opt(198, 197, C_RGB(pulse%256,0,pulse%256), C_NONE, DTEXT_CENTER, DTEXT_BOTTOM, ">> HANG ON TO <<" );
        dprint_opt(198, 222, C_RGB(pulse%256,0,pulse%256), C_NONE, DTEXT_CENTER, DTEXT_BOTTOM, ">> YOUR HELMET <<" );

        dupdate();

        pulse++;
        getInputCircuitDetails();

#if IS_FXLIB==1
        if (screenshot && usb_is_open())
        {
            usb_fxlink_screenshot(false);
            screenshot = false;
        }

        if(record && usb_is_open())
        {
            usb_fxlink_videocapture(false);
        }
#endif

    }

    free(preview1);
    free(preview2);

    return CircuitDetailsSelection;
}

void getInputCircuitDetails( void )
{
    int opt = GETKEY_DEFAULT & ~GETKEY_REP_ARROWS;
    int timeout = 1;

    while(1)
    {
        key_event_t ev = getkey_opt(opt, &timeout);
        if(ev.type == KEYEV_NONE) return;

        int key = ev.key;

        if (key==KEY_EXE)
        {
            doneMenuDetailsCircuit = true;
            CircuitDetailsSelection = 1;
        }

        if (key==KEY_EXIT)
        {
            CircuitDetailsSelection = -1;
            doneMenuDetailsCircuit = true;
        }


        /*
                if (key==KEY_UP || key==KEY_LEFT)
                {
                    if (CircuitSelection==0) CircuitSelection=9;
                    else CircuitSelection--;
                }

                if (key==KEY_DOWN || key==KEY_RIGHT)
                {
                    if (CircuitSelection==9) CircuitSelection=0;
                    else CircuitSelection++;
                }
        */

#if IS_FXLIB==1
        if(keydown(KEY_7))    screenshot = true;
        if(keydown(KEY_8))    record = !record;
#endif // IS_FXLIB
    }
}




int drawMenuBestTime( int circuit )
{
    uint16_t pulse=0;
    doneMenuBestTime = false;

    BestTimeSelection=0;


    while (!doneMenuBestTime)
    {

        dclear( C_BLACK );
        dsubimage( 0, 32, &mainscreen, 0, 32, 396, 160,  DIMAGE_NONE);

        dfont(&autofont);

        dprint_opt(198, 2, C_RGB(pulse%256,0,pulse%256), C_NONE, DTEXT_CENTER, DTEXT_TOP, "HALL OF FAME" );

        for( int k=0; k<5; k++)
        {
            if (HallOfFame[circuit][k].bestTime != 0x0FFFFFFF)
            {
                uint32_t temp1 = HallOfFame[circuit][k].bestTime/1000000/60;
                uint32_t temp2 = HallOfFame[circuit][k].bestTime/1000000 - temp1*60;
                uint32_t temp3 = (HallOfFame[circuit][k].bestTime - temp1*60*1000000 - temp2*1000000)/1000;
                dprint_opt(12, 40+k*30, C_RGB(0,0,0), C_NONE, DTEXT_LEFT, DTEXT_TOP, "%d %c%c%c - %D:%D,%D", k+1, HallOfFame[circuit][k].playerName[0],HallOfFame[circuit][k].playerName[1],HallOfFame[circuit][k].playerName[2], temp1, temp2, temp3 );
                dprint_opt(12, 38+k*30, C_RGB(255, 255, 255), C_NONE, DTEXT_LEFT, DTEXT_TOP, "%d %c%c%c - %D:%D,%D", k+1, HallOfFame[circuit][k].playerName[0],HallOfFame[circuit][k].playerName[1],HallOfFame[circuit][k].playerName[2], temp1, temp2, temp3 );
            }
            else
            {
                dprint_opt(12, 40+k*30, C_RGB(0,0,0), C_NONE, DTEXT_LEFT, DTEXT_TOP, "%d ... - ...", k+1 );
                dprint_opt(12, 38+k*30, C_RGB(255, 255, 255), C_NONE, DTEXT_LEFT, DTEXT_TOP, "%d ... - ...", k+1 );
            }
        }

        dprint_opt(198, 222, C_RGB(pulse%256,0,pulse%256), C_NONE, DTEXT_CENTER, DTEXT_BOTTOM, ">> START <<" );

        dupdate();

        pulse++;
        getInputBestTime();

#if IS_FXLIB==1
        if (screenshot && usb_is_open())
        {
            usb_fxlink_screenshot(false);
            screenshot = false;
        }

        if(record && usb_is_open())
        {
            usb_fxlink_videocapture(false);
        }
#endif

    }

    return BestTimeSelection;
}

void getInputBestTime( void )
{
    int opt = GETKEY_DEFAULT & ~GETKEY_REP_ARROWS;
    int timeout = 1;

    while(1)
    {
        key_event_t ev = getkey_opt(opt, &timeout);
        if(ev.type == KEYEV_NONE) return;

        int key = ev.key;

        if (key==KEY_EXE)
        {
            doneMenuBestTime = true;
            BestTimeSelection = 1;
        }

        if (key==KEY_EXIT)
        {
            BestTimeSelection = -1;
            doneMenuBestTime = true;
        }


        /*
                if (key==KEY_UP || key==KEY_LEFT)
                {
                    if (CircuitSelection==0) CircuitSelection=9;
                    else CircuitSelection--;
                }

                if (key==KEY_DOWN || key==KEY_RIGHT)
                {
                    if (CircuitSelection==9) CircuitSelection=0;
                    else CircuitSelection++;
                }
        */

#if IS_FXLIB==1
        if(keydown(KEY_7))    screenshot = true;
        if(keydown(KEY_8))    record = !record;
#endif // IS_FXLIB
    }
}
