library: Refactor library initialization state handling

Now that there are 4 different possibilities (not initialized, single
screen 3D mode, dual 3D mode, safe dual 3D mode) it is better to have an
enum instead of multiple bools.
This commit is contained in:
Antonio Niño Díaz 2023-10-09 00:37:47 +01:00
parent 16258d711f
commit e1662b7357
5 changed files with 94 additions and 62 deletions

View File

@ -44,6 +44,19 @@ typedef struct {
/// scanKeys() each frame for this to work.
void NE_UpdateInput(void);
///< List of all the possible initialization states of Nitro Engine.
typedef enum {
NE_ModeUninitialized = 0, ///< Nitro Engine hasn't been initialized.
NE_ModeSingle3D = 1, ///< Nitro Engine initialized in single 3D mode.
NE_ModeDual3D = 2, ///< Nitro Engine initialized in regular dual 3D mode.
NE_ModeSafeDual3D = 3 ///< Nitro Engine initialized in safe dual 3D mode.
} NE_ExecutionModes;
/// Returns the current execution mode.
///
/// @return Returns the execution mode.
NE_ExecutionModes NE_CurrentExecutionMode(void);
/// Ends Nitro Engine and frees all memory used by it.
void NE_End(void);

View File

@ -70,8 +70,6 @@ static void NE_write32(u32 *address, u32 value)
*fourth = (value >> 24) & 0xff;
}
extern bool NE_Dual;
int NE_ScreenshotBMP(const char *filename)
{
FILE *f = fopen(filename, "wb");
@ -86,8 +84,10 @@ int NE_ScreenshotBMP(const char *filename)
// In normal 3D mode we need to capture the composited (3D+2D output)
// and save it to VRAM. In dual 3D mode it already is in VRAM.
if (!NE_Dual)
if (NE_CurrentExecutionMode() == NE_ModeSingle3D)
{
// TODO: VRAM_D needs to be saved somewhere and then restored!
vramSetBankD(VRAM_D_LCD);
REG_DISPCAPCNT = DCAP_BANK(DCAP_BANK_VRAM_D)
@ -96,19 +96,19 @@ int NE_ScreenshotBMP(const char *filename)
| DCAP_SRC_A(DCAP_SRC_A_COMPOSITED)
| DCAP_ENABLE;
while (REG_DISPCAPCNT & DCAP_ENABLE) ;
while (REG_DISPCAPCNT & DCAP_ENABLE);
}
int ysize = 0;
if (NE_Dual)
ysize = 384;
else
if (NE_CurrentExecutionMode() == NE_ModeSingle3D)
ysize = 192;
else
ysize = 384;
u8 *temp = (u8 *)malloc(256 * ysize * 3
+ sizeof(NE_BMPInfoHeader)
+ sizeof(NE_BMPHeader));
u8 *temp = malloc(256 * ysize * 3
+ sizeof(NE_BMPInfoHeader)
+ sizeof(NE_BMPHeader));
NE_BMPHeader *header = (NE_BMPHeader *) temp;
NE_BMPInfoHeader *infoheader =
@ -136,7 +136,7 @@ int NE_ScreenshotBMP(const char *filename)
// Allow CPU to access VRAM
uint32 vramTemp = 0;
if (NE_Dual)
if (NE_CurrentExecutionMode() != NE_ModeSingle3D)
{
vramTemp = vramSetPrimaryBanks(VRAM_A_LCD, VRAM_B_LCD,
VRAM_C_LCD, VRAM_D_LCD);
@ -148,17 +148,17 @@ int NE_ScreenshotBMP(const char *filename)
{
u16 color = 0;
if (NE_Dual)
if (NE_CurrentExecutionMode() == NE_ModeSingle3D)
{
color = VRAM_D[256 * 192 - (y + 1) * 256 + x];
}
else
{
if (y > 191)
color = VRAM_C[256 * 192 - (y - 192 + 1) * 256 + x];
else
color = VRAM_D[256 * 192 - (y + 1) * 256 + x];
}
else
{
color = VRAM_D[256 * 192 - (y + 1) * 256 + x];
}
u8 b = (color & 31) << 3;
u8 g = ((color >> 5) & 31) << 3;
@ -174,7 +174,7 @@ int NE_ScreenshotBMP(const char *filename)
}
}
if (NE_Dual)
if (NE_CurrentExecutionMode() != NE_ModeSingle3D)
vramRestorePrimaryBanks(vramTemp);
DC_FlushAll();

View File

@ -21,12 +21,11 @@ bool NE_TestTouch;
static int NE_screenratio;
static uint32_t NE_viewport;
static u8 NE_Screen; // 1 = main screen, 0 = sub screen
bool NE_Dual;
static NE_ExecutionModes ne_execution_mode = NE_ModeUninitialized;
NE_Input ne_input;
static bool ne_inited = false;
static SpriteEntry *NE_Sprites; // 2D sprites used for Dual 3D mode
static int ne_znear, ne_zfar;
@ -34,9 +33,14 @@ static int fov;
static int ne_main_screen = 1; // 1 = top, 0 = bottom
NE_ExecutionModes NE_CurrentExecutionMode(void)
{
return ne_execution_mode;
}
void NE_End(void)
{
if (!ne_inited)
if (ne_execution_mode == NE_ModeUninitialized)
return;
// Hide BG0
@ -44,24 +48,49 @@ void NE_End(void)
vramSetBankA(VRAM_A_LCD);
vramSetBankB(VRAM_B_LCD);
if (NE_Dual)
{
vramSetBankC(VRAM_C_LCD);
vramSetBankD(VRAM_D_LCD);
free(NE_Sprites);
}
else if (GFX_CONTROL & GL_CLEAR_BMP)
switch (ne_execution_mode)
{
NE_ClearBMPEnable(false);
case NE_ModeSingle3D:
{
if (GFX_CONTROL & GL_CLEAR_BMP)
NE_ClearBMPEnable(false);
if (NE_UsingConsole)
vramSetBankF(VRAM_F_LCD);
break;
}
case NE_ModeDual3D:
{
vramSetBankC(VRAM_C_LCD);
vramSetBankD(VRAM_D_LCD);
free(NE_Sprites);
if (NE_UsingConsole)
vramSetBankF(VRAM_F_LCD);
break;
}
case NE_ModeSafeDual3D:
{
vramSetBankC(VRAM_C_LCD);
vramSetBankD(VRAM_D_LCD);
vramSetBankI(VRAM_I_LCD); // The console goes here
break;
}
default:
break;
}
vramSetBankE(VRAM_E_LCD);
if (NE_UsingConsole)
{
vramSetBankF(VRAM_F_LCD);
NE_UsingConsole = false;
}
NE_UsingConsole = false;
vramSetBankE(VRAM_E_LCD); // Palettes
NE_GUISystemEnd();
NE_SpriteSystemEnd();
@ -78,7 +107,7 @@ void NE_End(void)
NE_DebugPrint("Nitro Engine disabled");
ne_inited = false;
ne_execution_mode = NE_ModeUninitialized;
}
void NE_Viewport(int x1, int y1, int x2, int y2)
@ -246,8 +275,7 @@ void NE_UpdateInput(void)
int NE_Init3D(void)
{
if (ne_inited)
NE_End();
NE_End();
if (ne_systems_reset_all(NE_VRAM_ABCD) != 0)
return -1;
@ -258,8 +286,7 @@ int NE_Init3D(void)
ne_init_registers();
ne_inited = true;
NE_Dual = false;
ne_execution_mode = NE_ModeSingle3D;
NE_DebugPrint("Nitro Engine initialized in normal 3D mode");
@ -268,8 +295,7 @@ int NE_Init3D(void)
int NE_InitDual3D(void)
{
if (ne_inited)
NE_End();
NE_End();
NE_Sprites = calloc(128, sizeof(SpriteEntry));
if (NE_Sprites == NULL)
@ -323,8 +349,7 @@ int NE_InitDual3D(void)
videoSetModeSub(MODE_5_2D | DISPLAY_BG2_ACTIVE | DISPLAY_SPR_ACTIVE |
DISPLAY_SPR_2D_BMP_256);
ne_inited = true;
NE_Dual = true;
ne_execution_mode = NE_ModeDual3D;
NE_Screen = 0;
@ -335,8 +360,7 @@ int NE_InitDual3D(void)
int NE_InitSafeDual3D(void)
{
if (ne_inited)
NE_End();
NE_End();
if (ne_systems_reset_all(NE_VRAM_AB) != 0)
return -1;
@ -350,8 +374,7 @@ int NE_InitSafeDual3D(void)
videoSetMode(0);
videoSetModeSub(0);
ne_inited = true;
NE_Dual = true;
ne_execution_mode = NE_ModeSafeDual3D;
NE_Screen = 0;
@ -362,7 +385,7 @@ int NE_InitSafeDual3D(void)
void NE_InitConsole(void)
{
if (!ne_inited)
if (ne_execution_mode == NE_ModeUninitialized)
return;
NE_UsingConsole = true;
@ -384,7 +407,7 @@ void NE_InitConsole(void)
void NE_InitConsoleSafeDual3D(void)
{
if (!ne_inited)
if (ne_execution_mode == NE_ModeUninitialized)
return;
NE_UsingConsole = true;
@ -693,7 +716,7 @@ static int ne_sine_mult = 10, ne_sine_shift = 9;
void NE_VBLFunc(void)
{
if (!ne_inited)
if (ne_execution_mode == NE_ModeUninitialized)
return;
if (ne_dma_enabled)
@ -749,12 +772,12 @@ void NE_SpecialEffectPause(bool pause)
void NE_HBLFunc(void)
{
if (ne_execution_mode == NE_ModeUninitialized)
return;
s16 angle;
int val;
if (!ne_inited)
return;
// This counter is used to estimate CPU usage
ne_cpucount++;
@ -852,8 +875,9 @@ void __ne_debugoutputtoconsole(const char *text)
void __NE_debugprint(const char *text)
{
if (!ne_inited)
if (ne_execution_mode == NE_ModeUninitialized)
return;
if (ne_userdebugfn)
ne_userdebugfn(text);
}

View File

@ -220,12 +220,9 @@ void NE_ClearColorSet(u32 color, u32 alpha, u32 id)
GFX_CLEAR_COLOR = ne_clearcolor;
}
// From NE_General.c
extern bool NE_Dual;
void NE_ClearBMPEnable(bool value)
{
if (NE_Dual)
if (NE_CurrentExecutionMode() != NE_ModeSingle3D)
{
// It needs two banks that are used for the display capture
NE_DebugPrint("Not available in dual 3D mode");

View File

@ -508,8 +508,6 @@ void NE_MaterialUse(const NE_Material *tex)
GFX_TEX_FORMAT = NE_Texture[tex->texindex].param;
}
extern bool NE_Dual;
int NE_TextureSystemReset(int max_textures, int max_palettes,
NE_VRAMBankFlags bank_flags)
{
@ -539,7 +537,7 @@ int NE_TextureSystemReset(int max_textures, int max_palettes,
bank_flags = NE_VRAM_ABCD;
// VRAM_C and VRAM_D can't be used in dual 3D mode
if (NE_Dual)
if (NE_CurrentExecutionMode() != NE_ModeSingle3D)
bank_flags &= ~NE_VRAM_CD;
// Now, configure allocation system. The buffer size always sees the