library: Translate and cleanup more source files

This commit is contained in:
Antonio Niño Díaz 2023-05-22 01:02:17 +01:00
parent 08798c9260
commit 5150862e16
4 changed files with 783 additions and 749 deletions

View File

@ -53,7 +53,7 @@ extern char NF_ROOTFOLDER[64];
/// @param code Error code.
/// @param text Description.
/// @param value Additional info.
__attribute__((noreturn)) void NF_Error(u16 code, const char *text, u32 value);
__attribute__((noreturn)) void NF_Error(u16 code, const char *text, unsigned int value);
/// Defines the root folder of your project (FAT or NitroFS).
///

View File

@ -10,298 +10,313 @@
#endif
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <nds.h>
#include "nf_basic.h"
// Define la variable global NF_ROOTFOLDER
// Folder used as root by NFLib
char NF_ROOTFOLDER[64];
__attribute__((noreturn)) void NF_Error(u16 code, const char *text, u32 value) {
__attribute__((noreturn)) void NF_Error(u16 code, const char *text, unsigned int value)
{
consoleDemoInit(); // Initialize demo text console
setBrightness(3, 0); // Set the brightness to the top
consoleDemoInit(); // Inicializa la consola de texto
consoleClear(); // Borra la pantalla
setBrightness(3, 0); // Restaura el brillo
switch (code)
{
case 101: // File not found
iprintf("File %s not found.\n", text);
break;
u32 n = 0; // Variables de uso general
case 102: // Not enough memory
iprintf("Out of memory.\n");
iprintf("%u bytes\n", value);
iprintf("can't be allocated.\n");
break;
// Captura el codigo de error
switch (code) {
case 103: // Not enough free slots
iprintf("Out of %s slots.\n", text);
iprintf("All %u slots are in use.\n", value);
break;
case 101: // Fichero no encontrado
iprintf("File %s not found.\n", text);
break;
case 104: // Tiled background not found
iprintf("Tiled Bg %s\n", text);
iprintf("not found.\n");
break;
case 102: // Memoria insuficiente
iprintf("Out of memory.\n");
iprintf("%d bytes\n", (int)value);
iprintf("can't be allocated.\n");
break;
case 105: // Background hasn't been created
iprintf("Background number %u\n", value);
iprintf("on screen %s is\n", text);
iprintf("not created.\n");
break;
case 103: // No quedan Slots libres
iprintf("Out of %s slots.\n", text);
iprintf("All %d slots are in use.\n", (int)value);
break;
case 106: // Value out of range
iprintf("%s Id out\n", text);
iprintf("of range (%u max).\n", value);
break;
case 104: // Fondo no encontrado
iprintf("Tiled Bg %s\n", text);
iprintf("not found.\n");
break;
case 107: // Not enough contiguous VRAM blocks (tiles)
iprintf("Can't allocate %d\n", (int)value);
iprintf("blocks of tiles for\n");
iprintf("%s background\n", text);
iprintf("Free %d KB of VRAM or try to\n", (value * 16384) / 1024);
iprintf("reload all BGs again\n");
break;
case 105: // Fondo no creado
iprintf("Background number %d\n", (int)value);
iprintf("on screen %s is\n", text);
iprintf("not created.\n");
break;
case 108: // Not enough contiguous VRAM blocks (maps)
iprintf("Can't allocate %d\n", (int)value);
iprintf("blocks of maps for\n");
iprintf("%s background\n", text);
iprintf("Free %d KB of VRAM or try to\n", (value * 2048) / 1024);
iprintf("reload all BGs again\n");
break;
case 106: // Fuera de rango
iprintf("%s Id out\n", text);
iprintf("of range (%d max).\n", (int)value);
break;
case 109: // ID in use
iprintf("%s ID %u\n", text, value);
iprintf("is already in use.\n");
break;
case 107: // Insuficientes bloques contiguos en VRAM (Tiles)
n = (int)((value * 16384) / 1024);
iprintf("Can't allocate %d\n", (int)value);
iprintf("blocks of tiles for\n");
iprintf("%s background\n", text);
iprintf("Free %dkb of VRAM or try to\n", (int)n);
iprintf("reload all Bg's again\n");
break;
case 110: // ID not loaded
iprintf("%s\n", text);
iprintf("%u not loaded.\n", value);
break;
case 108: // Insuficientes bloques contiguos en VRAM (Maps)
n = (int)((value * 2048) / 1024);
iprintf("Can't allocate %d\n", (int)value);
iprintf("blocks of maps for\n");
iprintf("%s background\n", text);
iprintf("Free %dkb of VRAM or try to\n", (int)n);
iprintf("reload all Bg's again\n");
break;
case 111: // ID not in VRAM
iprintf("%s\n", text);
iprintf("%u not in VRAM.\n", value);
break;
case 109: // Id ocupada
iprintf("%s Id.%d\n", text, (int)value);
iprintf("is already in use.\n");
break;
case 112: // Sprite not created
iprintf("Sprite number %u\n", value);
iprintf("on screen %s is\n", text);
iprintf("not created.\n");
break;
case 110: // Id no cargada
iprintf("%s\n", text);
iprintf("%d not loaded.\n", (int)value);
break;
case 113: // Not enough VRAM
iprintf("Out of VRAM.\n");
iprintf("%u bytes for %s\n", value, text);
iprintf("can't be allocated.\n");
break;
case 111: // Id no en VRAM
iprintf("%s\n", text);
iprintf("%d not in VRAM.\n", (int)value);
break;
case 114: // Text layer doesn't exist
iprintf("Text layer on screen\n");
iprintf("%u layer don't exist.\n", value);
break;
case 112: // Sprite no creado
iprintf("Sprite number %d\n", (int)value);
iprintf("on screen %s is\n", text);
iprintf("not created.\n");
break;
case 115: // Invalid tiled background size (not a multiple of 256)
iprintf("Tiled BG %s\n", text);
iprintf("has wrong size.\n");
iprintf("Your bg sizes must be\n");
iprintf("a multiple of 256 pixels.\n");
break;
case 113: // Memoria VRAM insuficiente
iprintf("Out of VRAM.\n");
iprintf("%d bytes for %s\n", (int)value, text);
iprintf("can't be allocated.\n");
break;
case 116: // File is too big
iprintf("File %s\n", text);
iprintf("is too big.\n");
iprintf("Max size for\n");
iprintf("file is %u KB.\n", value >> 10);
break;
case 114: // La capa de Texto no existe
iprintf("Text layer on screen\n");
iprintf("nº %d don't exist.\n", (int)value);
break;
case 117: // Invalid affine background size (only 256x256 and 512x512 allowed)
iprintf("Affine BG %s\n", text);
iprintf("has wrong size.\n");
iprintf("Your bg sizes must be\n");
iprintf("256x256 or 512x512 and\n");
iprintf("with 256 tiles or less.\n");
break;
case 115: // Medidas del fondo no compatibles (no son multiplos de 256)
iprintf("Tiled Bg %s\n", text);
iprintf("has wrong size.\n");
iprintf("Your bg sizes must be\n");
iprintf("dividable by 256 pixels.\n");
break;
case 118: // Invalid affine background layer
iprintf("Affine Bg %s\n", text);
iprintf("only can be created\n");
iprintf("on layers 2 or 3.\n");
break;
case 116: // Archivo demasiado grande
iprintf("File %s\n", text);
iprintf("is too big.\n");
iprintf("Max size for\n");
iprintf("file is %dkb.\n", (int)(value >> 10));
break;
case 119: // Invalid texture size
iprintf("Texture ID %u illegal size.\n", value);
iprintf("Only power of 2 sizes can\n");
iprintf("be used (8 to 1024).\n");
break;
case 117: // Medidas del fondo affine no compatibles (Solo se admiten 256x256 y 512x512)
iprintf("Affine Bg %s\n", text);
iprintf("has wrong size.\n");
iprintf("Your bg sizes must be\n");
iprintf("256x256 or 512x512 and\n");
iprintf("with 256 tiles or less.\n");
break;
case 120: // Invalid sprite size
iprintf("Sprite ID %u illegal size.\n", value);
iprintf("8x8 Sprites can't be used\n");
iprintf("in 1D_128 mode.\n");
break;
}
case 118: // Medidas del fondo affine no compatibles (Solo se admiten 256x256 y 512x512)
iprintf("Affine Bg %s\n", text);
iprintf("only can be created\n");
iprintf("on layers 2 or 3.\n");
break;
case 119: // Tamaño de la textura ilegal.
iprintf("Texture id.%d illegal size.\n", (int)value);
iprintf("Only power of 2 sizes can\n");
iprintf("be used (8 to 1024).\n");
break;
case 120: // Tamaño de la Sprite ilegal.
iprintf("Sprite id.%d illegal size.\n", (int)value);
iprintf("8x8 Sprites can't be used\n");
iprintf("in 1D_128 mode.\n");
break;
}
iprintf("Error code %d.\n", (int)code); // Imprime el codigo de error
// Deten la ejecucion del programa
while (1) {
swiWaitForVBlank();
}
// Print error code
iprintf("Error code %d.\n", (int)code);
// Stop program
while (1)
swiWaitForVBlank();
}
void NF_SetRootFolder(const char* folder) {
void NF_SetRootFolder(const char *folder)
{
if (strcmp(folder, "NITROFS") == 0)
{
// Initialize both NitroFS and FAT
if (strcmp(folder, "NITROFS") == 0) { // Si se debe iniciar el modo NitroFS y FAT
// Define NitroFS as the root folder
snprintf(NF_ROOTFOLDER, sizeof(NF_ROOTFOLDER), "%s", "");
// Define NitroFS como la carpeta inicial
snprintf(NF_ROOTFOLDER, sizeof(NF_ROOTFOLDER), "%s", "");
// Check if NitroFS exists.
// NitroFS must be mounted beforehand for this to work.
if(access("nitro:/", F_OK) == 0) {
// NitroFS ok
// Si es correcto, cambia al ROOT del NitroFS
chdir("nitro:/");
} else {
// Fallo. Deten el programa
consoleDemoInit(); // Inicializa la consola de texto
if (NF_GetLanguage() == 5) {
iprintf("Error iniciando NitroFS.\n");
iprintf("Programa detenido.\n\n");
iprintf("Verifica que tu flashcard\n");
iprintf("es compatible con Argv.\n");
iprintf("Si no lo es, intenta usar el\n");
iprintf("Homebrew Menu para ejecutarla.\n\n");
} else {
iprintf("NitroFS Init Error.\n");
iprintf("Abnormal termination.\n\n");
iprintf("Check if your flashcard is\n");
iprintf("Argv compatible.\n");
iprintf("If not, try to launch the ROM\n");
iprintf("using the Homebrew Menu.\n\n");
}
iprintf("http://sourceforge.net/projects/devkitpro/files/hbmenu/");
// Bucle infinito. Fin del programa
while(1) {
swiWaitForVBlank();
}
}
// Check if NitroFS exists. NitroFS must be mounted beforehand for this
// to work.
if (access("nitro:/", F_OK) == 0)
{
// NitroFS started ok. Set it as the NFLib root folder
chdir("nitro:/");
}
else
{
// NitroFS start error
} else { // Si se debe iniciar solo la FAT
// Initialize text console
consoleDemoInit();
// Define la carpeta inicial de la FAT
snprintf(NF_ROOTFOLDER, sizeof(NF_ROOTFOLDER), "%s", folder);
if (NF_GetLanguage() == 5)
{
iprintf("Error iniciando NitroFS.\n");
iprintf("Programa detenido.\n\n");
iprintf("Verifica que tu flashcard\n");
iprintf("es compatible con Argv.\n");
iprintf("Si no lo es, intenta usar el\n");
iprintf("Homebrew Menu para ejecutarla.\n\n");
}
else
{
iprintf("NitroFS Init Error.\n");
iprintf("Abnormal termination.\n\n");
iprintf("Check if your flashcard is\n");
iprintf("Argv compatible.\n");
iprintf("If not, try to launch the ROM\n");
iprintf("using the Homebrew Menu.\n\n");
}
iprintf("http://sourceforge.net/projects/devkitpro/files/hbmenu/");
// Check where the NDS is running from
bool init_ok = false;
// Stop program
while (1)
swiWaitForVBlank();
}
}
else
{
// Initialize FAT only
// First, try to detect the drive where the NDS ROM is. If argv has been
// provided by the loader, it will contain the drive and the path of the
// ROM.
if (__system_argv->argvMagic == ARGV_MAGIC && __system_argv->argc >= 1) {
if (strncmp(__system_argv->argv[0], "fat:", 4) == 0) {
// If argv starts by "fat:", try to setup "fat:" as root
if (access("fat:/", F_OK) == 0) {
chdir("fat:/");
init_ok = true;
}
} else if (strncmp(__system_argv->argv[0], "sd:", 3) == 0) {
// If argv starts by "sd:", try to setup "sd:" as root
if (access("sd:/", F_OK) == 0) {
chdir("sd:/");
init_ok = true;
}
}
}
// Define the root folder of NFLib
snprintf(NF_ROOTFOLDER, sizeof(NF_ROOTFOLDER), "%s", folder);
// Second, try to bruteforce the detection. Check if there is access to
// the SD card of the DSi first. If not, try with DLDI.
if (!init_ok) {
if (access("sd:/", F_OK) == 0) {
chdir("sd:/");
init_ok = true;
} else if (access("fat:/", F_OK) == 0) {
chdir("fat:/");
init_ok = true;
}
}
// Check where the NDS is running from
bool init_ok = false;
// If that didn't work, give up.
if (!init_ok) {
// Fallo. Deten el programa
consoleDemoInit(); // Inicializa la consola de texto
if (NF_GetLanguage() == 5) {
iprintf("Error iniciando FAT.\n");
iprintf("Programa detenido.\n\n");
iprintf("Verifica que tu flashcard es\n");
iprintf("compatible con DLDI y la ROM\n");
iprintf("este parcheada correctamente.\n");
} else {
iprintf("FAT Init Error.\n");
iprintf("Abnormal termination.\n\n");
iprintf("Check if your flashcard is\n");
iprintf("DLDI compatible and the ROM\n");
iprintf("is correctly patched.\n");
}
// Bucle infinito. Fin del programa
while(1) {
swiWaitForVBlank();
}
}
}
// First, try to detect the drive where the NDS ROM is. If argv has been
// provided by the loader, it will contain the drive and the path of the
// ROM.
if (__system_argv->argvMagic == ARGV_MAGIC && __system_argv->argc >= 1)
{
if (strncmp(__system_argv->argv[0], "fat:", 4) == 0)
{
// If argv starts by "fat:", try to setup "fat:" as root
if (access("fat:/", F_OK) == 0)
{
chdir("fat:/");
init_ok = true;
}
}
else if (strncmp(__system_argv->argv[0], "sd:", 3) == 0)
{
// If argv starts by "sd:", try to setup "sd:" as root
if (access("sd:/", F_OK) == 0)
{
chdir("sd:/");
init_ok = true;
}
}
}
// Second, try to bruteforce the detection. Check if there is access to
// the SD card of the DSi first. If not, try with DLDI.
if (!init_ok)
{
if (access("sd:/", F_OK) == 0)
{
chdir("sd:/");
init_ok = true;
}
else if (access("fat:/", F_OK) == 0)
{
chdir("fat:/");
init_ok = true;
}
}
// If that didn't work, give up.
if (!init_ok)
{
// NitroFS start error
// Initialize text console
consoleDemoInit();
if (NF_GetLanguage() == 5)
{
iprintf("Error iniciando FAT.\n");
iprintf("Programa detenido.\n\n");
iprintf("Verifica que tu flashcard es\n");
iprintf("compatible con DLDI y la ROM\n");
iprintf("este parcheada correctamente.\n");
}
else
{
iprintf("FAT Init Error.\n");
iprintf("Abnormal termination.\n\n");
iprintf("Check if your flashcard is\n");
iprintf("DLDI compatible and the ROM\n");
iprintf("is correctly patched.\n");
}
// Stop program
while (1)
swiWaitForVBlank();
}
}
}
void NF_DmaMemCopy(void* destination, const void* source, u32 size) {
void NF_DmaMemCopy(void *destination, const void *source, u32 size)
{
// Based on Coranac's function:
// http://www.coranac.com/2009/05/dma-vs-arm9-fight/
// Funcion basada en la documentacion de Coranac
// http://www.coranac.com/2009/05/dma-vs-arm9-fight/
// Source and destination addresses
u32 src = (u32)source;
u32 dst = (u32)destination;
// Datos de origen y destino
u32 src = (u32)source;
u32 dst = (u32)destination;
// Check if addresses are aligned to at least 16 bits
if ((src | dst) & 1)
{
// Not aligned. DMA can't be used, use memcpy()
memcpy(destination, source, size);
}
else
{
// Addresses are aligned, use DMA
// Verifica si los datos estan correctamente alineados
if ((src | dst) & 1) {
// Wait until channel 3 is available
while (dmaBusy(3));
// No estan alineados para un DMA copy
// Se realiza un copia con el memcpy();
memcpy(destination, source, size);
// Make sure that the data in the cache is sent to the main RAM
DC_FlushRange(source, size);
} else {
// Estan alineados correctamente
// Espera a que el canal 3 de DMA este libre
while (dmaBusy(3));
// Manda el cache a la memoria
DC_FlushRange(source, size);
// Dependiendo de la alineacion de datos, selecciona el metodo de copia
if ((src | dst | size) & 3) {
// Copia de 16 bits
dmaCopyHalfWords(3, source, destination, size);
} else {
// Copia de 32 bits
dmaCopyWords(3, source, destination, size);
}
// Evita que el destino sea almacenado en cache
DC_InvalidateRange(destination, size);
}
// Depending on the alignment use 32-bit or 16-bit copy modes
if ((src | dst | size) & 3)
dmaCopyHalfWords(3, source, destination, size);
else
dmaCopyWords(3, source, destination, size);
// Prevent the destination from being in cache, it would corrupt the
// data when it is flushed
DC_InvalidateRange(destination, size);
}
}

View File

@ -7,7 +7,6 @@
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <nds.h>
@ -15,506 +14,526 @@
#include "nf_basic.h"
#include "nf_bitmapbg.h"
// Define los Buffers para almacenar datos de 16 bits
NF_TYPE_BG16B_INFO NF_BG16B[NF_SLOTS_BG16B]; // Fondos RAW de 16 bits
// Structures that hold information about 16 bit bitmap backgrounds
NF_TYPE_BG16B_INFO NF_BG16B[NF_SLOTS_BG16B];
// Backbuffer de 16 bits de cada pantalla
u16* NF_16BITS_BACKBUFFER[2];
// Backbuffer of 16 bit bitmaps for each screen
u16 *NF_16BITS_BACKBUFFER[2];
// Define los Buffers para almacenar datos de 8 bits
NF_TYPE_BG8B_INFO NF_BG8B[NF_SLOTS_BG8B]; // Fondos indexados de 8 bits
// Structures that hold information about 8 bit bitmap backgrounds
NF_TYPE_BG8B_INFO NF_BG8B[NF_SLOTS_BG8B];
// Backbuffer de 8 bits de cada pantalla
// Backbuffer of 8 bit bitmaps for each screen
NF_TYPE_BB8B_INFO NF_8BITS_BACKBUFFER[2];
void NF_Init16bitsBgBuffers(void) {
for (int n = 0; n < NF_SLOTS_BG16B; n ++) {
NF_BG16B[n].buffer = NULL;
NF_BG16B[n].size = 0;
NF_BG16B[n].inuse = false;
NF_BG16B[n].width = 0;
NF_BG16B[n].height = 0;
}
void NF_Init16bitsBgBuffers(void)
{
for (int n = 0; n < NF_SLOTS_BG16B; n++)
{
NF_BG16B[n].buffer = NULL;
NF_BG16B[n].size = 0;
NF_BG16B[n].inuse = false;
NF_BG16B[n].width = 0;
NF_BG16B[n].height = 0;
}
}
void NF_Reset16bitsBgBuffers(void) {
// Libera la RAM
for (int n = 0; n < NF_SLOTS_BG16B; n ++) {
free(NF_BG16B[n].buffer);
}
// Reinicia los datos
NF_Init16bitsBgBuffers();
void NF_Reset16bitsBgBuffers(void)
{
// Free buffers
for (int n = 0; n < NF_SLOTS_BG16B; n++)
free(NF_BG16B[n].buffer);
// Reset background information
NF_Init16bitsBgBuffers();
}
void NF_Init16bitsBackBuffer(u8 screen) {
u8 scr = screen;
if (scr > 1) scr = 1;
NF_16BITS_BACKBUFFER[scr] = NULL;
void NF_Init16bitsBackBuffer(u8 screen)
{
if (screen > 1)
screen = 1;
NF_16BITS_BACKBUFFER[screen] = NULL;
}
void NF_Enable16bitsBackBuffer(u8 screen) {
u8 scr = screen;
if (scr > 1) scr = 1;
// Resetea el buffer
free(NF_16BITS_BACKBUFFER[scr]);
NF_16BITS_BACKBUFFER[scr] = NULL;
// Asignale 128kb de memoria
NF_16BITS_BACKBUFFER[scr] = (u16*) calloc(65536, sizeof(u16));
// Devuelve error si no hay suficiente memoria
if (NF_16BITS_BACKBUFFER[scr] == NULL) NF_Error(102, NULL, 131072);
void NF_Enable16bitsBackBuffer(u8 screen)
{
if (screen > 1)
screen = 1;
// Free buffer if it was already allocated
free(NF_16BITS_BACKBUFFER[screen]);
NF_16BITS_BACKBUFFER[screen] = NULL;
// Try to allocate 128 KB
NF_16BITS_BACKBUFFER[screen] = calloc(65536, sizeof(u16));
// Fail if there isn't enough free memory
if (NF_16BITS_BACKBUFFER[screen] == NULL)
NF_Error(102, NULL, 131072);
}
void NF_Disble16bitsBackBuffer(u8 screen) {
u8 scr = screen;
if (scr > 1) scr = 1;
// Resetea el buffer
free(NF_16BITS_BACKBUFFER[scr]);
NF_16BITS_BACKBUFFER[scr] = NULL;
void NF_Disble16bitsBackBuffer(u8 screen)
{
if (screen > 1)
screen = 1;
// Free buffer if it was already allocated
free(NF_16BITS_BACKBUFFER[screen]);
NF_16BITS_BACKBUFFER[screen] = NULL;
}
void NF_Flip16bitsBackBuffer(u8 screen) {
// Copia el contenido del Backbuffer a la VRAM
// de la pantalla solicitada
if (screen == 0) {
NF_DmaMemCopy((void*)0x06000000, NF_16BITS_BACKBUFFER[0], 131072);
} else {
NF_DmaMemCopy((void*)0x06200000, NF_16BITS_BACKBUFFER[1], 131072);
}
void NF_Flip16bitsBackBuffer(u8 screen)
{
// Copy contents of the backuffer to VRAM
if (screen == 0)
NF_DmaMemCopy((void *)0x06000000, NF_16BITS_BACKBUFFER[0], 131072);
else
NF_DmaMemCopy((void *)0x06200000, NF_16BITS_BACKBUFFER[1], 131072);
}
void NF_InitBitmapBgSys(u8 screen, u8 mode) {
void NF_InitBitmapBgSys(u8 screen, u8 mode)
{
// Setup layer 3 (and optionally layer 2) of the selected screen as a bitmap
// Habilita la capa 3 de la pantalla indicada en modo BITMAP
// Initialize VRAM
if (screen == 0)
{
vramSetBankA(VRAM_A_MAIN_BG); // VRAM_A: Main engine backgrounds (128 KB)
memset((void *)0x06000000, 0, 131072); // Clear VRAM_A
for (int n = 0; n < 4; n++) // Hide all 4 layers
NF_HideBg(0, n);
}
else
{
vramSetBankC(VRAM_C_SUB_BG); // VRAM_C: Sub engine backgrounds (128 KB)
memset((void*)0x06200000, 0, 131072); // Clear VRAM_C
for (int n = 0; n < 4; n++) // Hide all 4 layers
NF_HideBg(1, n);
}
u8 n = 0;
// Initialize drawing layer
if (screen == 0)
{
if (mode == 0)
{
// Setup layers 2 and 3 in 8 bit mode
REG_BG3CNT = BG_PRIORITY_3 | BG_BMP_BASE(4) | BG_BMP8_256x256;
REG_BG2CNT = BG_PRIORITY_2 | BG_BMP_BASE(0) | BG_BMP8_256x256;
}
else
{
// Setup layer 3 in 16 bit mode
REG_BG3CNT = BG_PRIORITY_3 | BG_BMP_BASE(0) | BG_BMP16_256x256;
}
// Inicializa la VRAM
if (screen == 0) {
vramSetBankA(VRAM_A_MAIN_BG); // Banco A de la VRAM para fondos (128kb)
memset((void*)0x06000000, 0, 131072); // Borra el contenido del banco A
// Oculta todas las capas
for (n = 0; n < 4; n ++) { // Oculta todas las 4 capas
NF_HideBg(0, n);
}
} else {
vramSetBankC(VRAM_C_SUB_BG); // Banco C de la VRAM para fondos (128kb)
memset((void*)0x06200000, 0, 131072); // Borra el contenido del banco C
// Oculta todas las capas
for (n = 0; n < 4; n ++) { // Oculta todas las 4 capas
NF_HideBg(1, n);
}
}
// Reset rotation/scaling of layer 3
REG_BG3PA = 1 << 8;
REG_BG3PB = 0;
REG_BG3PC = 0;
REG_BG3PD = 1 << 8;
NF_ScrollBg(0, 3, 0, 0); // Reset scroll
NF_ShowBg(0, 3); // Show layer 3
// Inicializa la capa de dibujado
if (screen == 0) {
if (mode == 0) { // Modo 8 bits (Capas 1 y 3)
REG_BG3CNT = BG_PRIORITY_3 | BG_BMP_BASE(4) | BG_BMP8_256x256;
REG_BG2CNT = BG_PRIORITY_2 | BG_BMP_BASE(0) | BG_BMP8_256x256;
} else { // Modo 16 bits
REG_BG3CNT = BG_PRIORITY_3 | BG_BMP_BASE(0) | BG_BMP16_256x256;
}
// Resetea los registros de RotScale (Capa 3)
REG_BG3PA = (1 << 8);
REG_BG3PB = 0;
REG_BG3PC = 0;
REG_BG3PD = (1 << 8);
NF_ScrollBg(0, 3, 0, 0); // Posicionala en 0, 0
NF_ShowBg(0, 3); // Muestra la capa 3
// Resetea los registros de RotScale (Capa 2)
if (mode == 0) {
REG_BG2PA = (1 << 8);
REG_BG2PB = 0;
REG_BG2PC = 0;
REG_BG2PD = (1 << 8);
NF_ScrollBg(0, 2, 0, 0); // Posicionala en 0, 0
NF_ShowBg(0, 2); // Muestra la capa 2
}
} else {
if (mode == 0) { // Modo 8 bits (Capas 2 y 3)
REG_BG3CNT_SUB = BG_PRIORITY_3 | BG_BMP_BASE(4) | BG_BMP8_256x256;
REG_BG2CNT_SUB = BG_PRIORITY_2 | BG_BMP_BASE(0) | BG_BMP8_256x256;
} else { // Modo 16 bits
REG_BG3CNT_SUB = BG_PRIORITY_3 | BG_BMP_BASE(0) | BG_BMP16_256x256;
}
// Resetea los registros de RotScale (Capa 3)
REG_BG3PA_SUB = (1 << 8);
REG_BG3PB_SUB = 0;
REG_BG3PC_SUB = 0;
REG_BG3PD_SUB = (1 << 8);
NF_ScrollBg(1, 3, 0, 0); // Posicionala en 0, 0
NF_ShowBg(1, 3); // Muestra la capa 3
// Resetea los registros de RotScale (Capa 2)
if (mode == 0) {
REG_BG2PA_SUB = (1 << 8);
REG_BG2PB_SUB = 0;
REG_BG2PC_SUB = 0;
REG_BG2PD_SUB = (1 << 8);
NF_ScrollBg(1, 2, 0, 0); // Posicionala en 0, 0
NF_ShowBg(1, 2); // Muestra la capa 2
}
}
if (mode == 0)
{
// Reset rotation/scaling of layer 2
REG_BG2PA = 1 << 8;
REG_BG2PB = 0;
REG_BG2PC = 0;
REG_BG2PD = 1 << 8;
NF_ScrollBg(0, 2, 0, 0); // Reset scroll
NF_ShowBg(0, 2); // Show layer 2
}
}
else
{
if (mode == 0)
{
// Setup layers 2 and 3 in 8 bit mode
REG_BG3CNT_SUB = BG_PRIORITY_3 | BG_BMP_BASE(4) | BG_BMP8_256x256;
REG_BG2CNT_SUB = BG_PRIORITY_2 | BG_BMP_BASE(0) | BG_BMP8_256x256;
}
else
{
// Setup layer 3 in 16 bit mode
REG_BG3CNT_SUB = BG_PRIORITY_3 | BG_BMP_BASE(0) | BG_BMP16_256x256;
}
// Reset rotation/scaling of layer 3
REG_BG3PA_SUB = 1 << 8;
REG_BG3PB_SUB = 0;
REG_BG3PC_SUB = 0;
REG_BG3PD_SUB = 1 << 8;
NF_ScrollBg(1, 3, 0, 0); // Reset scroll
NF_ShowBg(1, 3); // Show layer 3
if (mode == 0)
{
// Reset rotation/scaling of layer 2
REG_BG2PA_SUB = 1 << 8;
REG_BG2PB_SUB = 0;
REG_BG2PC_SUB = 0;
REG_BG2PD_SUB = 1 << 8;
NF_ScrollBg(1, 2, 0, 0); // Reset scroll
NF_ShowBg(1, 2); // Show layer 2
}
}
}
void NF_Load16bitsBg(const char* file, u8 slot) {
// Llama a la funcion de carga de datos de imagen de 16bits
NF_Load16bImgData(file, slot, 256, 256, 0);
void NF_Load16bitsBg(const char *file, u8 slot)
{
NF_Load16bImgData(file, slot, 256, 256, 0);
}
void NF_Load16bitsImage(const char* file, u8 slot, u16 size_x, u16 size_y) {
// Llama a la funcion de carga de datos de imagen de 16bits
NF_Load16bImgData(file, slot, size_x, size_y, 1);
void NF_Load16bitsImage(const char *file, u8 slot, u16 size_x, u16 size_y)
{
NF_Load16bImgData(file, slot, size_x, size_y, 1);
}
void NF_Load16bImgData(const char* file, u8 slot, u16 x, u16 y, u8 type) {
void NF_Load16bImgData(const char *file, u8 slot, u16 x, u16 y, u8 type)
{
// Verify that the slot is in the valid range
if (slot >= NF_SLOTS_BG16B)
{
if (type == 0)
NF_Error(106, "16 bit BG", NF_SLOTS_BG16B);
else
NF_Error(106, "16 bit image", NF_SLOTS_BG16B);
}
// Verifica el rango de Id's
if (slot >= NF_SLOTS_BG16B) {
if (type == 0) {
NF_Error(106, "16 Bits Bg's", NF_SLOTS_BG16B);
} else {
NF_Error(106, "16 Bits Image", NF_SLOTS_BG16B);
}
}
// Free buffer if it is already in use
free(NF_BG16B[slot].buffer);
NF_BG16B[slot].buffer = NULL;
// Vacia los buffers que se usaran
free(NF_BG16B[slot].buffer);
NF_BG16B[slot].buffer = NULL;
// Load .IMG file
char filename[256];
snprintf(filename, sizeof(filename), "%s/%s.img", NF_ROOTFOLDER, file);
FILE *file_id = fopen(filename, "rb");
if (file_id == NULL)
{
// If the file can't be found
NF_Error(101, filename, 0);
}
// Declara los punteros a los ficheros
FILE* file_id;
// Get file size
fseek(file_id, 0, SEEK_END);
u32 size = ftell(file_id);
rewind(file_id);
// Variable para almacenar el path al archivo
char filename[256];
// If the size is too big (over 128kb), fail
if (size > 131072)
NF_Error(116, filename, 131072);
// Variable para el tamaño de archivo
u32 size = 0;
// Allocate memory in RAM
NF_BG16B[slot].buffer = calloc(size >> 1, sizeof(u16));
if (NF_BG16B[slot].buffer == NULL)
NF_Error(102, NULL, size); // Not enough free RAM
// Carga el archivo .IMG
snprintf(filename, sizeof(filename), "%s/%s.img", NF_ROOTFOLDER, file);
file_id = fopen(filename, "rb");
if (file_id) { // Si el archivo existe...
// Obten el tamaño del archivo
fseek(file_id, 0, SEEK_END);
size = ftell(file_id);
rewind(file_id);
// Si excede del tamaño maximo (128kb), error
if (size > 131072) NF_Error(116, filename, 131072);
// Reserva el espacio en RAM
NF_BG16B[slot].buffer = (u16*) calloc ((size >> 1), sizeof(u16));
if (NF_BG16B[slot].buffer == NULL) { // Si no hay suficiente RAM libre
NF_Error(102, NULL, size);
}
// Lee el archivo y ponlo en la RAM
fread(NF_BG16B[slot].buffer, 1, size, file_id);
} else { // Si el archivo no existe...
NF_Error(101, filename, 0);
}
fclose(file_id); // Cierra el archivo
// Read file to the RAM
fread(NF_BG16B[slot].buffer, 1, size, file_id);
fclose(file_id);
// Asegurate que el alpha bit (BIT 15) esta marcado
u32 n = 0;
for (n = 0; n < (size >> 1); n ++) {
NF_BG16B[slot].buffer[n] |= BIT(15);
}
// Guarda los parametros del fondo
NF_BG16B[slot].size = size; // Guarda el tamaño
NF_BG16B[slot].width = x; // Ancho del fondo
NF_BG16B[slot].height = y; // Altura del fondo
NF_BG16B[slot].inuse = true; // Marca que esta en uso
// Ensure that the alpha bit is set to 1
for (u32 n = 0; n < (size >> 1); n++)
NF_BG16B[slot].buffer[n] |= BIT(15);
// Save background parameters
NF_BG16B[slot].size = size;
NF_BG16B[slot].width = x;
NF_BG16B[slot].height = y;
NF_BG16B[slot].inuse = true; // Set slot as being used
}
void NF_Unload16bitsBg(u8 slot) {
void NF_Unload16bitsBg(u8 slot)
{
// Verify that the slot contains data
if (!NF_BG16B[slot].inuse)
NF_Error(110, "16 bit BG", slot);
// Verifica si el buffer contiene datos
if (!NF_BG16B[slot].inuse) NF_Error(110, "16 Bits Bg", slot);
// Vacia los buffers que se usaran
free(NF_BG16B[slot].buffer);
NF_BG16B[slot].buffer = NULL;
NF_BG16B[slot].size = 0; // Tamaño a 0
NF_BG16B[slot].inuse = false; // Marca que esta libre
// Free the buffer
free(NF_BG16B[slot].buffer);
NF_BG16B[slot].buffer = NULL;
NF_BG16B[slot].size = 0;
NF_BG16B[slot].inuse = false; // Mark slot as being free
}
void NF_Copy16bitsBuffer(u8 screen, u8 destination, u8 slot) {
// Verifica si el buffer contiene datos
if (!NF_BG16B[slot].inuse) NF_Error(110, "16 Bits Bg", slot);
if (destination == 0) { // Si el destino es la VRAM
// Dependiendo de la pantalla
if (screen == 0) {
NF_DmaMemCopy((void*)0x06000000, NF_BG16B[slot].buffer, NF_BG16B[slot].size);
} else {
NF_DmaMemCopy((void*)0x06200000, NF_BG16B[slot].buffer, NF_BG16B[slot].size);
}
} else { // Si el destino es el BackBuffer
// Dependiendo de la pantalla
if (screen == 0) {
memcpy(NF_16BITS_BACKBUFFER[0], NF_BG16B[slot].buffer, NF_BG16B[slot].size);
} else {
memcpy(NF_16BITS_BACKBUFFER[1], NF_BG16B[slot].buffer, NF_BG16B[slot].size);
}
}
void NF_Copy16bitsBuffer(u8 screen, u8 destination, u8 slot)
{
// Verify that the slot contains data
if (!NF_BG16B[slot].inuse)
NF_Error(110, "16 bit BG", slot);
if (destination == 0) // Destination is VRAM
{
if (screen == 0)
NF_DmaMemCopy((void *)0x06000000, NF_BG16B[slot].buffer, NF_BG16B[slot].size);
else
NF_DmaMemCopy((void *)0x06200000, NF_BG16B[slot].buffer, NF_BG16B[slot].size);
}
else // Destination is backbuffer
{
if (screen == 0)
memcpy(NF_16BITS_BACKBUFFER[0], NF_BG16B[slot].buffer, NF_BG16B[slot].size);
else
memcpy(NF_16BITS_BACKBUFFER[1], NF_BG16B[slot].buffer, NF_BG16B[slot].size);
}
}
void NF_Draw16bitsImage(u8 screen, u8 slot, s16 x, s16 y, bool alpha) {
void NF_Draw16bitsImage(u8 screen, u8 slot, s16 x, s16 y, bool alpha)
{
// Verify that the slot contains data
if (!NF_BG16B[slot].inuse)
NF_Error(110, "16 Bits Image", slot);
// Verifica si el buffer contiene datos
if (!NF_BG16B[slot].inuse) NF_Error(110, "16 Bits Image", slot);
u8 scr = screen;
if (scr > 1)
scr = 1;
// Variables locales
u16 img_x = 0;
u16 img_y = 0;
s16 buff_x = 0;
s16 buff_y = 0;
u32 buff_idx = 0;
u16 data = 0;
// The destination is the backbuffer
for (int img_y = 0; img_y < NF_BG16B[slot].height; img_y++)
{
for (int img_x = 0; img_x < NF_BG16B[slot].width; img_x++)
{
// Location of the destination pixel
int buff_x = img_x + x;
int buff_y = img_y + y;
// Filtro de pantalla
u8 scr = screen;
if (scr > 1) scr = 1;
// Si el destino es el BackBuffer
for (img_y = 0; img_y < NF_BG16B[slot].height; img_y ++) {
for (img_x = 0; img_x < NF_BG16B[slot].width; img_x ++ ) {
// Calcula donde se escribira el pixel
buff_x = (img_x + x);
buff_y = (img_y + y);
// Si esta dentro de la pantalla, dibujalo
if (
(buff_x >= 0)
&&
(buff_x <= 255)
&&
(buff_y >= 0)
&&
(buff_y <= 255)
) {
// Calcula el offset dentro del buffer
buff_idx = ((buff_y << 8) + buff_x);
// Valor del Pixel
data = NF_BG16B[slot].buffer[((img_y * NF_BG16B[slot].width) + img_x)];
// Si el pixel NO es magenta !(RGB15(31, 0, 31) | BIT(15))
if ((data != 0xFC1F) || (!alpha)) {
// Escribe el pixel en el BackBuffer
*(NF_16BITS_BACKBUFFER[scr] + buff_idx) = data;
}
}
}
}
// Draw it if it's inside the background
if ((buff_x >= 0) && (buff_x <= 255) && (buff_y >= 0) && (buff_y <= 255))
{
u32 buff_idx = (buff_y << 8) + buff_x;
u32 data = NF_BG16B[slot].buffer[(img_y * NF_BG16B[slot].width) + img_x];
// Write pixel if it isn't magenta (RGB15(31, 0, 31) | BIT(15))
if ((data != 0xFC1F) || (!alpha))
*(NF_16BITS_BACKBUFFER[scr] + buff_idx) = data;
}
}
}
}
void NF_Init8bitsBgBuffers(void) {
for (int n = 0; n < NF_SLOTS_BG8B; n ++) {
NF_BG8B[n].data = NULL;
NF_BG8B[n].pal = NULL;
NF_BG8B[n].data_size = 0;
NF_BG8B[n].pal_size = 0;
NF_BG8B[n].inuse = false;
}
void NF_Init8bitsBgBuffers(void)
{
for (int n = 0; n < NF_SLOTS_BG8B; n++)
{
NF_BG8B[n].data = NULL;
NF_BG8B[n].pal = NULL;
NF_BG8B[n].data_size = 0;
NF_BG8B[n].pal_size = 0;
NF_BG8B[n].inuse = false;
}
}
void NF_Reset8bitsBgBuffers(void) {
// Libera la RAM usada
for (int n = 0; n < NF_SLOTS_BG8B; n ++) {
free(NF_BG8B[n].data);
free(NF_BG8B[n].pal);
}
// Reinicia los datos
NF_Init8bitsBgBuffers();
void NF_Reset8bitsBgBuffers(void)
{
// Free the RAM buffers
for (int n = 0; n < NF_SLOTS_BG8B; n++)
{
free(NF_BG8B[n].data);
free(NF_BG8B[n].pal);
}
// Reset data structures
NF_Init8bitsBgBuffers();
}
void NF_Load8bitsBg(const char* file, u8 slot) {
void NF_Load8bitsBg(const char *file, u8 slot)
{
// Verify that the slot is in the valid range
if (slot >= NF_SLOTS_BG8B)
NF_Error(106, "8 Bits Bg's", NF_SLOTS_BG8B);
// Verifica el rango de Id's
if (slot >= NF_SLOTS_BG8B) {
NF_Error(106, "8 Bits Bg's", NF_SLOTS_BG8B);
}
// Free buffers if they are already in use
free(NF_BG8B[slot].data);
NF_BG8B[slot].data = NULL;
free(NF_BG8B[slot].pal);
NF_BG8B[slot].pal = NULL;
// Vacia los buffers que se usaran
free(NF_BG8B[slot].data);
NF_BG8B[slot].data = NULL;
free(NF_BG8B[slot].pal);
NF_BG8B[slot].pal = NULL;
// File path
char filename[256];
// Declara los punteros a los ficheros
FILE* file_id;
// Load .IMG file
snprintf(filename, sizeof(filename), "%s/%s.img", NF_ROOTFOLDER, file);
FILE *file_id = fopen(filename, "rb");
if (file_id == NULL) // If the file doesn't exist
NF_Error(101, filename, 0);
// Variable para almacenar el path al archivo
char filename[256];
// Get file size
fseek(file_id, 0, SEEK_END);
u32 size = ftell(file_id);
rewind(file_id);
// Variable para el tamaño de archivo
u32 size = 0;
// If it's too big (more than 64 KB), fail
if (size > 65536)
NF_Error(116, filename, 65536);
// Carga el archivo .IMG
snprintf(filename, sizeof(filename), "%s/%s.img", NF_ROOTFOLDER, file);
file_id = fopen(filename, "rb");
if (file_id) { // Si el archivo existe...
// Obten el tamaño del archivo
fseek(file_id, 0, SEEK_END);
size = ftell(file_id);
rewind(file_id);
// Si excede del tamaño maximo (64kb), error
if (size > 65536) NF_Error(116, filename, 65536);
// Reserva el espacio en RAM
NF_BG8B[slot].data = (u8*) calloc (size, sizeof(u8));
if (NF_BG8B[slot].data == NULL) { // Si no hay suficiente RAM libre
NF_Error(102, NULL, size);
}
// Lee el archivo y ponlo en la RAM
fread(NF_BG8B[slot].data, 1, size, file_id);
} else { // Si el archivo no existe...
NF_Error(101, filename, 0);
}
fclose(file_id); // Cierra el archivo
NF_BG8B[slot].data_size = size; // Guarda el tamaño del buffer
// Allocate space in RAM
NF_BG8B[slot].data = calloc(size, sizeof(u8));
if (NF_BG8B[slot].data == NULL) // Not enough memory
NF_Error(102, NULL, size);
// Carga el archivo .PAL
snprintf(filename, sizeof(filename), "%s/%s.pal", NF_ROOTFOLDER, file);
file_id = fopen(filename, "rb");
if (file_id) { // Si el archivo existe...
// Obten el tamaño del archivo
fseek(file_id, 0, SEEK_END);
size = ftell(file_id);
rewind(file_id);
// Si la paleta tiene un tamaño inferior a 512, ajusta el tamaño
if (size < 512) size = 512;
// Reserva el espacio en RAM
NF_BG8B[slot].pal = (u16*) calloc ((size >> 1), sizeof(u16));
if (NF_BG8B[slot].pal == NULL) { // Si no hay suficiente RAM libre
NF_Error(102, NULL, size);
}
// Lee el archivo y ponlo en la RAM
fread(NF_BG8B[slot].pal, 1, size, file_id);
} else { // Si el archivo no existe...
NF_Error(101, filename, 0);
}
fclose(file_id); // Cierra el archivo
NF_BG8B[slot].pal_size = size; // Guarda el tamaño del buffer
// Read file and save it to RAM
fread(NF_BG8B[slot].data, 1, size, file_id);
fclose(file_id);
// Marca el slot como que esta en uso
NF_BG8B[slot].inuse = true;
NF_BG8B[slot].data_size = size; // Save file size
// Load .PAL file
snprintf(filename, sizeof(filename), "%s/%s.pal", NF_ROOTFOLDER, file);
file_id = fopen(filename, "rb");
if (file_id == NULL) // If the file doesn't exist
NF_Error(101, filename, 0);
// Get file size
fseek(file_id, 0, SEEK_END);
size = ftell(file_id);
rewind(file_id);
// If the size is smaller than the maximum size, adjust the size
if (size < 512)
size = 512;
// Allocate space in RAM
NF_BG8B[slot].pal = calloc(size >> 1, sizeof(u16));
if (NF_BG8B[slot].pal == NULL) // Not enough free RAM
NF_Error(102, NULL, size);
// Read file and save it to RAM
fread(NF_BG8B[slot].pal, 1, size, file_id);
fclose(file_id);
NF_BG8B[slot].pal_size = size; // Save file size
// Mark this slot as being in use
NF_BG8B[slot].inuse = true;
}
void NF_Unload8bitsBg(u8 slot) {
void NF_Unload8bitsBg(u8 slot)
{
// Verify that the slot contains data
if (!NF_BG8B[slot].inuse)
NF_Error(110, "8 Bits Bg", slot);
// Verifica si el buffer contiene datos
if (!NF_BG8B[slot].inuse) NF_Error(110, "8 Bits Bg", slot);
// Vacia los buffers que se usaran
free(NF_BG8B[slot].data);
NF_BG8B[slot].data = NULL;
NF_BG8B[slot].data_size = 0;
free(NF_BG8B[slot].pal);
NF_BG8B[slot].pal = NULL;
NF_BG8B[slot].pal_size = 0;
NF_BG8B[slot].inuse = false; // Marca que esta libre
// Free buffers
free(NF_BG8B[slot].data);
NF_BG8B[slot].data = NULL;
NF_BG8B[slot].data_size = 0;
free(NF_BG8B[slot].pal);
NF_BG8B[slot].pal = NULL;
NF_BG8B[slot].pal_size = 0;
NF_BG8B[slot].inuse = false; // Mark slot as free
}
void NF_Copy8bitsBuffer(u8 screen, u8 destination, u8 slot) {
void NF_Copy8bitsBuffer(u8 screen, u8 destination, u8 slot)
{
// Verify that the slot contains data
if (!NF_BG8B[slot].inuse) NF_Error(110, "8 Bits Bg", slot);
// Verifica si el buffer contiene datos
if (!NF_BG8B[slot].inuse) NF_Error(110, "8 Bits Bg", slot);
if (screen > 1)
screen = 1;
u8 scr = screen;
if (scr > 1) scr = 1;
// Si el destino es la VRAM
if (destination < 2)
{
u32 data, pal; // Addresses in VRAM for data and palette
// Si el destino es la VRAM
if (destination < 2) {
if (screen == 0)
{
data = 0x06000000;
pal = 0x05000000;
}
else
{
data = 0x06200000;
pal = 0x05000400;
}
// Segun la pantalla...
u32 data = 0;
u32 pal = 0;
if (screen == 0) {
data = (0x06000000); // Direccion en VRAM para los datos
pal = (0x05000000); // Direccion en VRAM para la paleta
} else {
data = (0x06200000); // Direccion en VRAM para los datos
pal = (0x05000400); // Direccion en VRAM para la paleta
}
// Segun la capa
if (destination == 1) data += 65536;
// Copia los datos a la VRAM
NF_DmaMemCopy((void*)data, NF_BG8B[slot].data, NF_BG8B[slot].data_size);
NF_DmaMemCopy((void*)pal, NF_BG8B[slot].pal, NF_BG8B[slot].pal_size);
} else {
// Copia los datos al BackBuffer
memcpy(NF_8BITS_BACKBUFFER[scr].data, NF_BG8B[slot].data, NF_BG8B[slot].data_size);
memcpy(NF_8BITS_BACKBUFFER[scr].pal, NF_BG8B[slot].pal, NF_BG8B[slot].pal_size);
}
if (destination == 1)
data += 65536;
// Copy data to VRAM
NF_DmaMemCopy((void *)data, NF_BG8B[slot].data, NF_BG8B[slot].data_size);
NF_DmaMemCopy((void *)pal, NF_BG8B[slot].pal, NF_BG8B[slot].pal_size);
}
else
{
// Copy data to backbuffer
memcpy(NF_8BITS_BACKBUFFER[screen].data, NF_BG8B[slot].data,
NF_BG8B[slot].data_size);
memcpy(NF_8BITS_BACKBUFFER[screen].pal, NF_BG8B[slot].pal,
NF_BG8B[slot].pal_size);
}
}
void NF_Init8bitsBackBuffer(u8 screen) {
u8 scr = screen;
if (scr > 1) scr = 1;
NF_8BITS_BACKBUFFER[scr].data = NULL;
NF_8BITS_BACKBUFFER[scr].pal = NULL;
void NF_Init8bitsBackBuffer(u8 screen)
{
if (screen > 1)
screen = 1;
NF_8BITS_BACKBUFFER[screen].data = NULL;
NF_8BITS_BACKBUFFER[screen].pal = NULL;
}
void NF_Enable8bitsBackBuffer(u8 screen) {
u8 scr = screen;
if (scr > 1) scr = 1;
// Resetea el buffer
free(NF_8BITS_BACKBUFFER[scr].data);
free(NF_8BITS_BACKBUFFER[scr].pal);
NF_8BITS_BACKBUFFER[scr].data = NULL;
NF_8BITS_BACKBUFFER[scr].pal = NULL;
// Asignale 64kb de memoria para datos
NF_8BITS_BACKBUFFER[scr].data = (u8*) calloc(65536, sizeof(u8));
if (NF_8BITS_BACKBUFFER[scr].data == NULL) NF_Error(102, NULL, 65536);
// Asignale 512 bytes de memoria para paletas
NF_8BITS_BACKBUFFER[scr].pal = (u16*) calloc(256, sizeof(u16));
if (NF_8BITS_BACKBUFFER[scr].pal == NULL) NF_Error(102, NULL, 512);
void NF_Enable8bitsBackBuffer(u8 screen)
{
if (screen > 1)
screen = 1;
// Free buffers
free(NF_8BITS_BACKBUFFER[screen].data);
free(NF_8BITS_BACKBUFFER[screen].pal);
NF_8BITS_BACKBUFFER[screen].data = NULL;
NF_8BITS_BACKBUFFER[screen].pal = NULL;
// Allocate 64 KB for bitmap data
NF_8BITS_BACKBUFFER[screen].data = calloc(65536, sizeof(u8));
if (NF_8BITS_BACKBUFFER[screen].data == NULL)
NF_Error(102, NULL, 65536);
// Allocate 512 bytes for bitmap palette
NF_8BITS_BACKBUFFER[screen].pal = calloc(256, sizeof(u16));
if (NF_8BITS_BACKBUFFER[screen].pal == NULL)
NF_Error(102, NULL, 512);
}
void NF_Disble8bitsBackBuffer(u8 screen) {
u8 scr = screen;
if (scr > 1) scr = 1;
// Resetea el buffer
free(NF_8BITS_BACKBUFFER[scr].data);
free(NF_8BITS_BACKBUFFER[scr].pal);
NF_8BITS_BACKBUFFER[scr].data = NULL;
NF_8BITS_BACKBUFFER[scr].pal = NULL;
void NF_Disble8bitsBackBuffer(u8 screen)
{
if (screen > 1)
screen = 1;
// Free buffers
free(NF_8BITS_BACKBUFFER[screen].data);
free(NF_8BITS_BACKBUFFER[screen].pal);
NF_8BITS_BACKBUFFER[screen].data = NULL;
NF_8BITS_BACKBUFFER[screen].pal = NULL;
}
void NF_Flip8bitsBackBuffer(u8 screen, u8 destination) {
void NF_Flip8bitsBackBuffer(u8 screen, u8 destination)
{
// Copy contents of the backuffer to VRAM
// Copia el contenido del Backbuffer a la VRAM
// de la pantalla solicitada
u8 scr = screen;
if (scr > 1) scr = 1;
if (screen > 1)
screen = 1;
// Segun la pantalla...
u32 data = 0;
u32 pal = 0;
if (scr == 0) {
data = (0x06000000); // Direccion en VRAM para los datos
pal = (0x05000000); // Direccion en VRAM para la paleta
} else {
data = (0x06200000); // Direccion en VRAM para los datos
pal = (0x05000400); // Direccion en VRAM para la paleta
}
u32 data, pal; // Addresses in VRAM for data and palette
// Segun la capa
if (destination == 1) data += 65536;
if (screen == 0)
{
data = 0x06000000;
pal = 0x05000000;
}
else
{
data = 0x06200000;
pal = 0x05000400;
}
// Copia los datos a la VRAM
NF_DmaMemCopy((void*)data, NF_8BITS_BACKBUFFER[scr].data, 65536);
NF_DmaMemCopy((void*)pal, NF_8BITS_BACKBUFFER[scr].pal, 512);
if (destination == 1)
data += 65536;
NF_DmaMemCopy((void *)data, NF_8BITS_BACKBUFFER[screen].data, 65536);
NF_DmaMemCopy((void *)pal, NF_8BITS_BACKBUFFER[screen].pal, 512);
}

View File

@ -5,9 +5,7 @@
// NightFox LIB - Include de Fondos mixtos (Tiled / Bitmap 8 bits)
// http://www.nightfoxandco.com/
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <nds.h>
@ -17,95 +15,97 @@
#include "nf_mixedbg.h"
#include "nf_tiledbg.h"
void NF_InitMixedBgSys(u8 screen) {
void NF_InitMixedBgSys(u8 screen)
{
// Define the number of banks of maps and tiles.
//
// Tile banks are 16 KB in size. Map banks are 2 KB in size. You can fit 8
// map banks in a tile bank. For that reason, the number of map banks must
// be a multiple of 8 banks.
//
// By default, use 4 tile banks and 8 map banks.
NF_BANKS_TILES[screen] = 4;
NF_BANKS_MAPS[screen] = 8;
u8 n = 0;
// Set all tile and map blocks as free
for (int n = 0; n < NF_BANKS_TILES[screen]; n++)
NF_TILEBLOCKS[screen][n] = 0;
// Define el numero de bancos de Mapas y Tiles
NF_BANKS_TILES[screen] = 4; // (1 banks = 16kb) Cada banco de tiles puede alvergar 8 bancos de Mapas
NF_BANKS_MAPS[screen] = 8; // (1 bank = 2kb) Usar multiplos de 8. Cada set de 8 bancos consume 1 banco de tiles
// Por defecto Tiles = 4, Mapas = 8
// Esto nos deja 3 bancos de 16kb para tiles
// y 8 bancos de 2kb para mapas
for (int n = 0; n < NF_BANKS_MAPS[screen]; n++)
NF_MAPBLOCKS[screen][n] = 0;
// Inicializa el array de bloques libres de Tiles
for (n = 0; n < NF_BANKS_TILES[screen]; n ++) {
NF_TILEBLOCKS[screen][n] = 0;
}
// Reset information of backgrounds
for (int n = 0; n < 4; n ++)
{
NF_TILEDBG_LAYERS[screen][n].tilebase = 0; // Tileset base
NF_TILEDBG_LAYERS[screen][n].tileblocks = 0; // Blocks used by the tileset
NF_TILEDBG_LAYERS[screen][n].mapbase = 0; // Map base
NF_TILEDBG_LAYERS[screen][n].mapblocks = 0; // Blocks used by the map
NF_TILEDBG_LAYERS[screen][n].bgwidth = 0; // Background width
NF_TILEDBG_LAYERS[screen][n].bgheight = 0; // Background height
NF_TILEDBG_LAYERS[screen][n].mapwidth = 0; // Map width
NF_TILEDBG_LAYERS[screen][n].mapheight = 0; // Map height
NF_TILEDBG_LAYERS[screen][n].bgtype = 0; // Map type
NF_TILEDBG_LAYERS[screen][n].bgslot = 0; // Graphics slot
NF_TILEDBG_LAYERS[screen][n].blockx = 0; // Current horizontal map block
NF_TILEDBG_LAYERS[screen][n].blocky = 0; // Current vertical map block
NF_TILEDBG_LAYERS[screen][n].created = false; // Is the background created?
}
// Inicializa el array de bloques libres de Mapas
for (n = 0; n < NF_BANKS_MAPS[screen]; n ++) {
NF_MAPBLOCKS[screen][n] = 0;
}
// Now reserve as many VRAM banks as needed for maps. Each tile map is as
// big as 8 map banks.
// Inicializa el array de informacion de fondos en pantalla
for (n = 0; n < 4; n ++) {
NF_TILEDBG_LAYERS[screen][n].tilebase = 0; // Base del Tileset
NF_TILEDBG_LAYERS[screen][n].tileblocks = 0; // Bloques usados por el Tileset
NF_TILEDBG_LAYERS[screen][n].mapbase = 0; // Base del Map
NF_TILEDBG_LAYERS[screen][n].mapblocks = 0; // Bloques usados por el Map
NF_TILEDBG_LAYERS[screen][n].bgwidth = 0; // Ancho del fondo
NF_TILEDBG_LAYERS[screen][n].bgheight = 0; // Altura del fondo
NF_TILEDBG_LAYERS[screen][n].mapwidth = 0; // Ancho del mapa
NF_TILEDBG_LAYERS[screen][n].mapheight = 0; // Altura del mapa
NF_TILEDBG_LAYERS[screen][n].bgtype = 0; // Tipo de mapa
NF_TILEDBG_LAYERS[screen][n].bgslot = 0; // Buffer de graficos usado
NF_TILEDBG_LAYERS[screen][n].blockx = 0; // Bloque de mapa actual (horizontal)
NF_TILEDBG_LAYERS[screen][n].blocky = 0; // Bloque de mapa actual (vertical)
NF_TILEDBG_LAYERS[screen][n].created = false; // Esta creado ?
}
// Number of required tile maps to reserve for maps
u8 r_banks = ((NF_BANKS_MAPS[screen] - 1) >> 3) + 1;
for (int n = 0; n < r_banks; n ++)
NF_TILEBLOCKS[screen][n] = 128; // Flag them as "map" banks
// Ahora reserva los bancos necesarios de VRAM para mapas
// Cada bloque de 16kb (1 banco de tiles) permite 8 bancos de mapas (de 2kb cada uno)
u8 r_banks;
r_banks = ((NF_BANKS_MAPS[screen] - 1) >> 3) + 1; // Calcula los bancos de Tiles a reservar para Maps
for (n = 0; n < r_banks; n ++) {
NF_TILEBLOCKS[screen][n] = 128; // Marca que bancos de VRAM son para MAPS
}
if (screen == 0) {
// Si es la pantalla 0 (Superior, Main engine)
REG_DISPCNT |= (DISPLAY_BG_EXT_PALETTE); // Activa las paletas extendidas
vramSetBankA(VRAM_A_MAIN_BG); // Banco A de la VRAM para fondos (128kb)
memset((void*)0x06000000, 0, 131072); // Borra el contenido del banco A
vramSetBankE(VRAM_E_LCD); // Reserva el banco E de la VRAM para Paletas Extendidas (0-3) (32kb de 64kb)
memset((void*)0x06880000, 0, 32768); // Borra el contenido del banco E
for (n = 0; n < 4; n ++) { // Oculta todas las 4 capas
NF_HideBg(0, n);
}
} else {
// Si es la pantalla 1 (Inferior, Sub engine)
REG_DISPCNT_SUB |= (DISPLAY_BG_EXT_PALETTE); // Activa las paletas extendidas
vramSetBankC(VRAM_C_SUB_BG); // Banco C de la VRAM para fondos (128kb)
memset((void*)0x06200000, 0, 131072); // Borra el contenido del banco C
vramSetBankH(VRAM_H_LCD); // Reserva el banco H de la VRAM para Paletas Extendidas (0-3) (32kb)
memset((void*)0x06898000, 0, 32768); // Borra el contenido del banco H
for (n = 0; n < 4; n ++) { // Oculta todas las 4 capas
NF_HideBg(1, n);
}
}
// Inicializa la capa de dibujado para bitmaps (capa 3 unicamente)
if (screen == 0) {
// Modo 8 bits (Capas 3)
REG_BG3CNT = BG_PRIORITY_3 | BG_BMP_BASE(4) | BG_BMP8_256x256;
// Resetea los registros de RotScale (Capa 3)
REG_BG3PA = (1 << 8);
REG_BG3PB = 0;
REG_BG3PC = 0;
REG_BG3PD = (1 << 8);
NF_ScrollBg(0, 3, 0, 0); // Posicionala en 0, 0
NF_ShowBg(0, 3); // Muestra la capa 3
} else {
// Modo 8 bits (Capas 2 y 3)
REG_BG3CNT_SUB = BG_PRIORITY_3 | BG_BMP_BASE(4) | BG_BMP8_256x256;
// Resetea los registros de RotScale (Capa 3)
REG_BG3PA_SUB = (1 << 8);
REG_BG3PB_SUB = 0;
REG_BG3PC_SUB = 0;
REG_BG3PD_SUB = (1 << 8);
NF_ScrollBg(1, 3, 0, 0); // Posicionala en 0, 0
NF_ShowBg(1, 3); // Muestra la capa 3
}
if (screen == 0)
{
// Top screen, main engine
REG_DISPCNT |= DISPLAY_BG_EXT_PALETTE; // Enable extended palettes
vramSetBankA(VRAM_A_MAIN_BG); // VRAM_A: Main engine backgrounds (128 KB)
memset((void *)0x06000000, 0, 131072); // Clear VRAM_A
vramSetBankE(VRAM_E_LCD); // VRAM_E: Extended palettes (32 / 64 KB)
memset((void *)0x06880000, 0, 32768); // Clear VRAM_E
for (int n = 0; n < 4; n++) // Hide all 4 layers
NF_HideBg(0, n);
}
else
{
// Bottom screen, sub engine
REG_DISPCNT_SUB |= DISPLAY_BG_EXT_PALETTE; // Enable extended palettes
vramSetBankC(VRAM_C_SUB_BG); // VRAM_C: Sub engine backgrounds (128 KB)
memset((void *)0x06200000, 0, 131072); // Clear VRAM_C
vramSetBankH(VRAM_H_LCD); // VRAM_H: Extended palettes (32 / 64 KB)
memset((void *)0x06898000, 0, 32768); // Clear VRAM_H
for (int n = 0; n < 4; n++) // Hide all 4 layers
NF_HideBg(1, n);
}
// Initialize the drawing layer for bitmaps (layer 3)
if (screen == 0)
{
// Set layer 3 to 8 bits mode
REG_BG3CNT = BG_PRIORITY_3 | BG_BMP_BASE(4) | BG_BMP8_256x256;
// Reset rotation/scaling
REG_BG3PA = 1 << 8;
REG_BG3PB = 0;
REG_BG3PC = 0;
REG_BG3PD = 1 << 8;
NF_ScrollBg(0, 3, 0, 0); // Reset scroll
NF_ShowBg(0, 3); // Show layer 3
}
else
{
// Set layer 3 to 8 bits mode
REG_BG3CNT_SUB = BG_PRIORITY_3 | BG_BMP_BASE(4) | BG_BMP8_256x256;
// Reset rotation/scaling
REG_BG3PA_SUB = 1 << 8;
REG_BG3PB_SUB = 0;
REG_BG3PC_SUB = 0;
REG_BG3PD_SUB = 1 << 8;
NF_ScrollBg(1, 3, 0, 0); // Reset scroll
NF_ShowBg(1, 3); // Show layer 3
}
}