Airport/Source/LoadMenu.c

539 lines
13 KiB
C
Raw Normal View History

2017-02-04 14:49:08 +01:00
/* **************************************
* Includes *
2017-02-04 14:49:08 +01:00
* *************************************/
#include "LoadMenu.h"
#include "Gfx.h"
#include "System.h"
#include "PltParser.h"
#include "Font.h"
#include "Sfx.h"
2017-02-04 14:49:08 +01:00
/* **************************************
* Defines *
2017-02-04 14:49:08 +01:00
* *************************************/
/* **************************************
* Structs and enums *
* *************************************/
enum
{
SMALL_FONT_SIZE = 8,
SMALL_FONT_SPACING = 6
2017-02-04 14:49:08 +01:00
};
enum
{
BG_BLUE_TARGET_VALUE = 0xC0,
BG_WHITE_TARGET_VALUE = /*0x40*/ 0,
BG_INCREASE_STEP = 0x10
};
enum
{
LOADING_BAR_X = 64,
LOADING_BAR_Y = 200,
LOADING_BAR_N_LINES = 4,
2017-02-04 14:49:08 +01:00
LOADING_BAR_WIDTH = 256,
LOADING_BAR_HEIGHT = 16,
2017-02-04 14:49:08 +01:00
LOADING_BAR_LUMINANCE_TARGET = NORMAL_LUMINANCE,
LOADING_BAR_LUMINANCE_STEP = 10
};
enum
{
LOADING_TITLE_CLUT_X = 384,
LOADING_TITLE_CLUT_Y = 496,
LOADING_TITLE_X = 128,
LOADING_TITLE_Y = 32,
2017-02-04 14:49:08 +01:00
LOADING_TITLE_U = 0,
LOADING_TITLE_V = 0,
2017-02-04 14:49:08 +01:00
LOADING_TITLE_LUMINANCE_STEP = 10,
LOADING_TITLE_LUMINANCE_TARGET = NORMAL_LUMINANCE
};
enum
{
2017-02-04 14:49:08 +01:00
PLANE_START_X = 56,
PLANE_START_Y = 200,
2017-02-04 14:49:08 +01:00
PLANE_U = 0,
PLANE_V = 32,
PLANE_SIZE = 16,
2017-02-04 14:49:08 +01:00
PLANE_LUMINANCE_STEP = 0x10,
PLANE_LUMINANCE_TARGET_VALUE = NORMAL_LUMINANCE
};
/* *************************************
* Local Prototypes
* *************************************/
static void LoadMenuInit(void);
static void ISR_LoadMenuVBlank(void);
static bool LoadMenuISRHasEnded(void);
static bool LoadMenuISRHasStarted(void);
static void LoadMenuLoadFileList( const char* fileList[], void* dest[],
2017-02-04 14:49:08 +01:00
uint8_t szFileList, uint8_t szDestList);
/* *************************************
* Local Variables
* *************************************/
static GsGPoly4 loadMenuBg;
static GsSprite LoadMenuPlaneSpr;
static GsSprite LoadMenuTitleSpr;
static GsLine LoadMenuBarLines[LOADING_BAR_N_LINES];
static GsRectangle LoadMenuBarRect;
static const char* LoadMenuFiles[] = { "cdrom:\\DATA\\SPRITES\\PLANE.TIM;1",
"cdrom:\\DATA\\SPRITES\\LOADING.TIM;1",
"cdrom:\\DATA\\FONTS\\FONT_2.FNT;1" };
static void* LoadMenuDest[] = { (GsSprite*)&LoadMenuPlaneSpr,
2017-02-04 14:49:08 +01:00
(GsSprite*)&LoadMenuTitleSpr,
(TYPE_FONT*)&SmallFont };
static char* strCurrentFile;
2017-02-04 14:49:08 +01:00
// Flags to communicate with ISR state
// * startup_flag: background fades in from black to blue.
// * end_flag: tells the background to fade out to black.
// * isr_ended: background has totally faded out to black.
// * isr_started: tells the ISR has finished starting up.
static volatile bool startup_flag;
static volatile bool isr_started;
static volatile bool end_flag;
static volatile bool isr_ended;
// Set to true when LoadMenuInit() has been called, and set to false
// once LoadMenuEnd() is called.
// It's used when multiple modules call LoadMenu() at the same time,
// so load menu does not have to be initialised each time;
static bool load_menu_running;
void LoadMenuInit(void)
{
int i;
static bool first_load = false;
if (first_load == false)
2017-02-04 14:49:08 +01:00
{
first_load = true;
LoadMenuLoadFileList( LoadMenuFiles,
LoadMenuDest,
sizeof (LoadMenuFiles) / sizeof (char*),
sizeof (LoadMenuDest) / sizeof (void*));
2017-02-04 14:49:08 +01:00
}
2017-02-04 14:49:08 +01:00
FontSetSize(&SmallFont, SMALL_FONT_SIZE);
FontSetSpacing(&SmallFont, SMALL_FONT_SPACING);
2017-02-04 14:49:08 +01:00
LoadMenuPlaneSpr.r = 0;
LoadMenuPlaneSpr.g = 0;
LoadMenuPlaneSpr.b = 0;
2017-02-04 14:49:08 +01:00
LoadMenuPlaneSpr.x = PLANE_START_X;
LoadMenuPlaneSpr.y = PLANE_START_Y;
2017-02-04 14:49:08 +01:00
// "Loading..." title init
2017-02-04 14:49:08 +01:00
LoadMenuTitleSpr.r = 0;
LoadMenuTitleSpr.g = 0;
LoadMenuTitleSpr.b = 0;
2017-02-04 14:49:08 +01:00
LoadMenuTitleSpr.x = LOADING_TITLE_X;
LoadMenuTitleSpr.y = LOADING_TITLE_Y;
2017-02-04 14:49:08 +01:00
LoadMenuTitleSpr.cx = LOADING_TITLE_CLUT_X;
LoadMenuTitleSpr.cy = LOADING_TITLE_CLUT_Y;
LoadMenuTitleSpr.u = LOADING_TITLE_U;
LoadMenuTitleSpr.v = LOADING_TITLE_V;
2017-02-04 14:49:08 +01:00
startup_flag = true;
isr_started = false;
end_flag = false;
isr_ended = false;
2017-02-04 14:49:08 +01:00
// Background init
2017-02-04 14:49:08 +01:00
loadMenuBg.x[0] = 0;
loadMenuBg.x[1] = X_SCREEN_RESOLUTION;
loadMenuBg.x[2] = 0;
loadMenuBg.x[3] = X_SCREEN_RESOLUTION;
2017-02-04 14:49:08 +01:00
loadMenuBg.y[0] = 0;
loadMenuBg.y[1] = 0;
loadMenuBg.y[2] = Y_SCREEN_RESOLUTION;
loadMenuBg.y[3] = Y_SCREEN_RESOLUTION;
2017-02-04 14:49:08 +01:00
// Colour components adjustment (default to zero)
for (i = 0; i < 4 ; i++)
2017-02-04 14:49:08 +01:00
{
loadMenuBg.r[i] = 0;
loadMenuBg.g[i] = 0;
loadMenuBg.b[i] = 0;
}
2017-02-04 14:49:08 +01:00
// "Loading" bar line 0 (up left - up right)
2017-02-04 14:49:08 +01:00
LoadMenuBarLines[0].x[0] = LOADING_BAR_X;
LoadMenuBarLines[0].x[1] = LOADING_BAR_X + LOADING_BAR_WIDTH;
2017-02-04 14:49:08 +01:00
LoadMenuBarLines[0].y[0] = LOADING_BAR_Y;
LoadMenuBarLines[0].y[1] = LOADING_BAR_Y;
2017-02-04 14:49:08 +01:00
// "Loading" bar line 1 (up left - down left)
2017-02-04 14:49:08 +01:00
LoadMenuBarLines[1].x[0] = LOADING_BAR_X;
LoadMenuBarLines[1].x[1] = LOADING_BAR_X;
2017-02-04 14:49:08 +01:00
LoadMenuBarLines[1].y[0] = LOADING_BAR_Y;
LoadMenuBarLines[1].y[1] = LOADING_BAR_Y + LOADING_BAR_HEIGHT;
2017-02-04 14:49:08 +01:00
// "Loading" bar line 2 (down left - down right)
2017-02-04 14:49:08 +01:00
LoadMenuBarLines[2].x[0] = LOADING_BAR_X;
LoadMenuBarLines[2].x[1] = LOADING_BAR_X + LOADING_BAR_WIDTH;
2017-02-04 14:49:08 +01:00
LoadMenuBarLines[2].y[0] = LOADING_BAR_Y + LOADING_BAR_HEIGHT;
LoadMenuBarLines[2].y[1] = LOADING_BAR_Y + LOADING_BAR_HEIGHT;
2017-02-04 14:49:08 +01:00
// "Loading" bar line 3 (up right - down right)
2017-02-04 14:49:08 +01:00
LoadMenuBarLines[3].x[0] = LOADING_BAR_X + LOADING_BAR_WIDTH;
LoadMenuBarLines[3].x[1] = LOADING_BAR_X + LOADING_BAR_WIDTH;
2017-02-04 14:49:08 +01:00
LoadMenuBarLines[3].y[0] = LOADING_BAR_Y;
LoadMenuBarLines[3].y[1] = LOADING_BAR_Y + LOADING_BAR_HEIGHT;
for (i = 0; i < LOADING_BAR_N_LINES ; i++)
2017-02-04 14:49:08 +01:00
{
LoadMenuBarLines[i].r = 0;
LoadMenuBarLines[i].g = 0;
LoadMenuBarLines[i].b = 0;
}
2017-02-04 14:49:08 +01:00
LoadMenuBarRect.r = 0;
LoadMenuBarRect.g = 0;
LoadMenuBarRect.b = 0;
2017-02-04 14:49:08 +01:00
// LoadMenuBarRect.attribute |= ENABLE_TRANS | TRANS_MODE(0);
2017-02-04 14:49:08 +01:00
LoadMenuBarRect.x = LOADING_BAR_X;
LoadMenuBarRect.y = LOADING_BAR_Y;
LoadMenuBarRect.w = 0;
LoadMenuBarRect.h = LOADING_BAR_HEIGHT;
2017-02-04 14:49:08 +01:00
LoadMenuBarRect.attribute |= ENABLE_TRANS | TRANS_MODE(0);
2017-02-04 14:49:08 +01:00
load_menu_running = true;
2017-02-04 14:49:08 +01:00
SmallFont.spr.r = 0;
SmallFont.spr.g = 0;
SmallFont.spr.b = 0;
2017-02-04 14:49:08 +01:00
GfxSetGlobalLuminance(0);
Serial_printf("I_MASK = 0x%08X\n", (*(unsigned int*)0x1F801074));
2017-02-04 14:49:08 +01:00
SetVBlankHandler(&ISR_LoadMenuVBlank);
}
void LoadMenuEnd(void)
{
end_flag = true;
load_menu_running = false;
while (LoadMenuISRHasEnded() == false);
Serial_printf("Set default VBlank handler.\n");
2017-02-04 14:49:08 +01:00
SetVBlankHandler(&ISR_SystemDefaultVBlank);
2017-02-04 14:49:08 +01:00
GfxSetGlobalLuminance(NORMAL_LUMINANCE);
}
void ISR_LoadMenuVBlank(void)
{
uint8_t i;
SystemIncreaseGlobalTimer();
if ( (SystemIsBusy() != false)
||
(GfxIsGPUBusy() != false)
||
(SerialIsBusy() != false) )
{
return;
}
/*if ( (SystemIsBusy() != false) || (GfxIsGPUBusy() != false) || (SerialIsBusy() != false) )
2017-02-04 14:49:08 +01:00
{
return;
}*/
if (startup_flag != false)
2017-02-04 14:49:08 +01:00
{
// "Loading..." text
if (LoadMenuTitleSpr.r < LOADING_TITLE_LUMINANCE_TARGET)
2017-02-04 14:49:08 +01:00
{
LoadMenuTitleSpr.r += LOADING_TITLE_LUMINANCE_STEP;
LoadMenuTitleSpr.g += LOADING_TITLE_LUMINANCE_STEP;
LoadMenuTitleSpr.b += LOADING_TITLE_LUMINANCE_STEP;
}
if (loadMenuBg.g[0] < BG_WHITE_TARGET_VALUE)
2017-02-04 14:49:08 +01:00
{
loadMenuBg.r[0] += BG_INCREASE_STEP;
loadMenuBg.r[1] += BG_INCREASE_STEP;
2017-02-04 14:49:08 +01:00
loadMenuBg.g[0] += BG_INCREASE_STEP;
loadMenuBg.g[1] += BG_INCREASE_STEP;
2017-02-04 14:49:08 +01:00
loadMenuBg.b[0] += BG_INCREASE_STEP;
loadMenuBg.b[1] += BG_INCREASE_STEP;
}
// Blue background
if (loadMenuBg.b[2] < BG_BLUE_TARGET_VALUE)
2017-02-04 14:49:08 +01:00
{
loadMenuBg.b[2] += BG_INCREASE_STEP;
loadMenuBg.b[3] += BG_INCREASE_STEP;
}
if (LoadMenuBarRect.r < LOADING_BAR_LUMINANCE_TARGET)
2017-02-04 14:49:08 +01:00
{
LoadMenuBarRect.r += LOADING_BAR_LUMINANCE_STEP;
LoadMenuBarRect.g += LOADING_BAR_LUMINANCE_STEP;
LoadMenuBarRect.b += LOADING_BAR_LUMINANCE_STEP;
}
else
{
startup_flag = false;
isr_started = true;
}
for (i = 0;i < LOADING_BAR_N_LINES ; i++)
2017-02-04 14:49:08 +01:00
{
if (LoadMenuBarLines[i].r < LOADING_BAR_LUMINANCE_TARGET)
2017-02-04 14:49:08 +01:00
{
LoadMenuBarLines[i].r += LOADING_BAR_LUMINANCE_STEP;
LoadMenuBarLines[i].g += LOADING_BAR_LUMINANCE_STEP;
LoadMenuBarLines[i].b += LOADING_BAR_LUMINANCE_STEP;
}
}
if (LoadMenuPlaneSpr.r < PLANE_LUMINANCE_TARGET_VALUE)
2017-02-04 14:49:08 +01:00
{
LoadMenuPlaneSpr.r += PLANE_LUMINANCE_STEP;
LoadMenuPlaneSpr.g += PLANE_LUMINANCE_STEP;
LoadMenuPlaneSpr.b += PLANE_LUMINANCE_STEP;
}
2017-02-04 14:49:08 +01:00
}
else if (end_flag != false)
2017-02-04 14:49:08 +01:00
{
LoadMenuTitleSpr.r -= LOADING_TITLE_LUMINANCE_STEP;
LoadMenuTitleSpr.g -= LOADING_TITLE_LUMINANCE_STEP;
LoadMenuTitleSpr.b -= LOADING_TITLE_LUMINANCE_STEP;
if (loadMenuBg.g[0] > 0)
2017-02-04 14:49:08 +01:00
{
loadMenuBg.r[0] -= BG_INCREASE_STEP;
loadMenuBg.r[1] -= BG_INCREASE_STEP;
2017-02-04 14:49:08 +01:00
loadMenuBg.g[0] -= BG_INCREASE_STEP;
loadMenuBg.g[1] -= BG_INCREASE_STEP;
2017-02-04 14:49:08 +01:00
loadMenuBg.b[0] -= BG_INCREASE_STEP;
loadMenuBg.b[1] -= BG_INCREASE_STEP;
}
if (loadMenuBg.b[2] > 0)
2017-02-04 14:49:08 +01:00
{
loadMenuBg.b[2] -= BG_INCREASE_STEP;
loadMenuBg.b[3] -= BG_INCREASE_STEP;
}
if (loadMenuBg.b[2] == 0)
2017-02-04 14:49:08 +01:00
{
end_flag = false;
isr_ended = true;
}
if (LoadMenuPlaneSpr.r > 0)
2017-02-04 14:49:08 +01:00
{
LoadMenuPlaneSpr.r -= PLANE_LUMINANCE_STEP;
LoadMenuPlaneSpr.g -= PLANE_LUMINANCE_STEP;
LoadMenuPlaneSpr.b -= PLANE_LUMINANCE_STEP;
}
2017-02-04 14:49:08 +01:00
LoadMenuPlaneSpr.x = (PLANE_START_X + LOADING_BAR_WIDTH);
LoadMenuPlaneSpr.y = PLANE_START_Y;
2017-02-04 14:49:08 +01:00
LoadMenuBarRect.w = LOADING_BAR_WIDTH;
if (LoadMenuBarRect.r > 0)
2017-02-04 14:49:08 +01:00
{
LoadMenuBarRect.r -= LOADING_BAR_LUMINANCE_STEP;
LoadMenuBarRect.g -= LOADING_BAR_LUMINANCE_STEP;
LoadMenuBarRect.b -= LOADING_BAR_LUMINANCE_STEP;
}
for (i = 0;i < LOADING_BAR_N_LINES ; i++)
2017-02-04 14:49:08 +01:00
{
if (LoadMenuBarLines[i].r > 0)
2017-02-04 14:49:08 +01:00
{
LoadMenuBarLines[i].r -= LOADING_BAR_LUMINANCE_STEP;
LoadMenuBarLines[i].g -= LOADING_BAR_LUMINANCE_STEP;
LoadMenuBarLines[i].b -= LOADING_BAR_LUMINANCE_STEP;
}
}
}
2017-02-04 14:49:08 +01:00
GsSortGPoly4(&loadMenuBg);
2017-02-04 14:49:08 +01:00
GsSortRectangle(&LoadMenuBarRect);
for (i = 0 ; i < LOADING_BAR_N_LINES ; i++)
2017-02-04 14:49:08 +01:00
{
GsSortLine(&LoadMenuBarLines[i]);
}
2017-02-04 14:49:08 +01:00
GsSortSprite(&LoadMenuTitleSpr);
2017-02-04 14:49:08 +01:00
LoadMenuPlaneSpr.w = PLANE_SIZE;
LoadMenuPlaneSpr.h = PLANE_SIZE;
2017-02-04 14:49:08 +01:00
GsSortSprite(&LoadMenuPlaneSpr);
2017-02-04 14:49:08 +01:00
FontSetFlags(&SmallFont, FONT_BLEND_EFFECT);
2017-02-04 14:49:08 +01:00
FontPrintText( &SmallFont,
LOADING_BAR_X,
2017-02-04 14:49:08 +01:00
LOADING_BAR_Y + LOADING_BAR_HEIGHT + 8,
strCurrentFile );
2017-02-04 14:49:08 +01:00
GfxDrawScene_Fast();
}
bool LoadMenuISRHasEnded(void)
{
return isr_ended;
}
bool LoadMenuISRHasStarted(void)
{
return isr_started;
}
void LoadMenu( const char* fileList[],
void* dest[],
2017-02-04 14:49:08 +01:00
uint8_t szFileList , uint8_t szDestList)
{
if (load_menu_running == false)
2017-02-04 14:49:08 +01:00
{
LoadMenuInit();
while (LoadMenuISRHasStarted() == false);
2017-02-04 14:49:08 +01:00
}
LoadMenuLoadFileList(fileList, dest, szFileList, szDestList);
2017-02-04 14:49:08 +01:00
}
void LoadMenuLoadFileList( const char* fileList[], void* dest[],
2017-02-04 14:49:08 +01:00
uint8_t szFileList, uint8_t szDestList)
{
char aux_file_name[100];
char* extension;
2017-02-04 14:49:08 +01:00
short x_increment;
uint8_t fileLoadedCount;
if (szFileList != szDestList)
2017-02-04 14:49:08 +01:00
{
Serial_printf("File list size different from dest list size! %d vs %d\n",
2017-02-04 14:49:08 +01:00
szFileList, szDestList);
return;
}
for (fileLoadedCount = 0; fileLoadedCount < szFileList ; fileLoadedCount++)
2017-02-04 14:49:08 +01:00
{
strCurrentFile = (char*)fileList[fileLoadedCount];
if (strCurrentFile == NULL)
2017-02-04 14:49:08 +01:00
{
continue;
}
2017-02-04 14:49:08 +01:00
x_increment = (short)(LOADING_BAR_WIDTH / szFileList);
2017-02-04 14:49:08 +01:00
// Calculate new X position for loading menu plane sprite.
// This is not calculated on ISR as to avoid longer ISR time.
LoadMenuPlaneSpr.x = (PLANE_START_X + (fileLoadedCount* x_increment) );
2017-02-04 14:49:08 +01:00
LoadMenuBarRect.w = fileLoadedCount* x_increment;
//Serial_printf("Files %d / %d loaded. New plane X = %d.\n",fileLoadedCount,szFileList,LoadMenuPlaneSpr.x);
2017-02-04 14:49:08 +01:00
// Backup original file path
strncpy(aux_file_name, strCurrentFile, 100);
2017-02-04 14:49:08 +01:00
//We want to get file extension, so split into tokens
strtok(strCurrentFile, ".;");
extension = strtok(NULL, ".;");
Serial_printf("File extension: .%s\n", extension);
2017-02-04 14:49:08 +01:00
//Restore original file path in order to load file
strncpy(strCurrentFile, aux_file_name, 100);
if (strncmp(extension, "TIM", 3) == 0)
2017-02-04 14:49:08 +01:00
{
if (GfxSpriteFromFile(strCurrentFile, dest[fileLoadedCount]) == false)
2017-02-04 14:49:08 +01:00
{
Serial_printf("Could not load image file \"%s\"!\n", strCurrentFile);
2017-02-04 14:49:08 +01:00
}
}
else if (strncmp(extension, "CLT", 3) == 0)
2017-02-04 14:49:08 +01:00
{
if (dest[fileLoadedCount] != NULL)
2017-02-04 14:49:08 +01:00
{
Serial_printf("WARNING: File %s linked to non-NULL destination pointer!\n", dest[fileLoadedCount]);
2017-02-04 14:49:08 +01:00
}
if (GfxCLUTFromFile(strCurrentFile) == false)
2017-02-04 14:49:08 +01:00
{
Serial_printf("Could not load CLUT file \"%s\"!\n", strCurrentFile);
2017-02-04 14:49:08 +01:00
}
}
else if (strncmp(extension, "VAG", 3) == 0)
2017-02-04 14:49:08 +01:00
{
if (SfxUploadSound(strCurrentFile, dest[fileLoadedCount]) == false)
2017-02-04 14:49:08 +01:00
{
Serial_printf("Could not load sound file \"%s\"!\n", strCurrentFile);
2017-02-04 14:49:08 +01:00
}
}
else if (strncmp(extension, "FNT", 3) == 0)
2017-02-04 14:49:08 +01:00
{
if (FontLoadImage(strCurrentFile, dest[fileLoadedCount]) == false)
2017-02-04 14:49:08 +01:00
{
Serial_printf("Could not load font file \"%s\"!\n", strCurrentFile);
2017-02-04 14:49:08 +01:00
}
}
else if (strncmp(extension, "PLT", 3) == 0)
2017-02-04 14:49:08 +01:00
{
if (PltParserLoadFile(strCurrentFile, dest[fileLoadedCount]) == false)
2017-02-04 14:49:08 +01:00
{
Serial_printf("Could not load pilots file \"%s\"!\n", strCurrentFile);
2017-02-04 14:49:08 +01:00
}
}
else
{
Serial_printf("LoadMenu does not recognize following extension: %s\n",extension);
2017-02-04 14:49:08 +01:00
}
}
}