diff --git a/examples/graphics/backbuffer2/source/main.c b/examples/graphics/backbuffer2/source/main.c index 88fffff..c7595eb 100644 --- a/examples/graphics/backbuffer2/source/main.c +++ b/examples/graphics/backbuffer2/source/main.c @@ -85,9 +85,9 @@ int main(int argc, char **argv) square_y = 127; // Fill buffer - for (int y = square_y; y < (square_y + 64); y ++) + for (int y = square_y; y < square_y + 64; y++) { - for (int x = square_x; x < (square_x + 64); x ++) + for (int x = square_x; x < square_x + 64; x++) { // Get current color u32 rgb = NF_16BITS_BACKBUFFER[1][(y << 8) + x]; @@ -107,7 +107,7 @@ int main(int argc, char **argv) b = composite; // Pack the components as a RGB value - rgb = r | (g << 5)| (b << 10) | BIT(15); + rgb = r | (g << 5) | (b << 10) | BIT(15); // Write RGB value in the backbuffer NF_16BITS_BACKBUFFER[1][(y << 8) + x] = rgb; diff --git a/examples/graphics/backbuffer3/source/main.c b/examples/graphics/backbuffer3/source/main.c index e0795de..1d6d62a 100644 --- a/examples/graphics/backbuffer3/source/main.c +++ b/examples/graphics/backbuffer3/source/main.c @@ -1,175 +1,140 @@ // SPDX-License-Identifier: CC0-1.0 // // SPDX-FileContributor: NightFox & Co., 2009-2011 +// +// Basic example of using a 16-bit bitmap with a backbuffer and loading images. +// It shows two images with a window effect. +// http://www.nightfoxandco.com -/* -------------------------------------------------- - - NightFox's Lib Template - Ejemplo basico de BackBuffer en 16bits (modo BITMAP) - con carga de archivos de imagen. - Efecto de visualizar dos imagenes con efecto ventana. - - Requiere DevkitARM - Requiere NightFox's Lib - - Codigo por NightFox - http://www.nightfoxandco.com - Inicio 10 de Octubre del 2009 - -------------------------------------------------- -*/ - - - - - -/* -------------------------------------------------- - Includes -------------------------------------------------- -*/ - -// Includes C #include #include #include -// Includes propietarios NDS #include #include -// Includes librerias propias #include +int main(int argc, char **argv) +{ + // Prepare a NitroFS initialization screen + NF_Set2D(0, 0); + NF_Set2D(1, 0); + consoleDemoInit(); + printf("\n NitroFS init. Please wait.\n\n"); + printf(" Iniciando NitroFS,\n por favor, espere.\n\n"); + swiWaitForVBlank(); + // Initialize NitroFS and set it as the root folder of the filesystem + nitroFSInit(NULL); + NF_SetRootFolder("NITROFS"); + // Initialize 2D engine in both screens and use mode 5 + NF_Set2D(0, 5); + NF_Set2D(1, 5); + // Initialize bitmap backgrounds system + NF_InitBitmapBgSys(0, 1); + NF_InitBitmapBgSys(1, 1); -/* -------------------------------------------------- - Main() - Bloque general del programa -------------------------------------------------- -*/ + // Initialize storage buffers + NF_Init16bitsBgBuffers(); -int main(int argc, char **argv) { + // Initialize backbuffer + NF_Init16bitsBackBuffer(1); - // Pantalla de espera inicializando NitroFS - NF_Set2D(0, 0); - NF_Set2D(1, 0); - consoleDemoInit(); - printf("\n NitroFS init. Please wait.\n\n"); - printf(" Iniciando NitroFS,\n por favor, espere.\n\n"); - swiWaitForVBlank(); + // Enable backbuffer + NF_Enable16bitsBackBuffer(1); - // Define el ROOT e inicializa el sistema de archivos - nitroFSInit(NULL); - NF_SetRootFolder("NITROFS"); // Define la carpeta ROOT para usar NITROFS + // Load bitmap background files from NitroFS + NF_Load16bitsBg("bmp/bitmap16", 0); + NF_Load16bitsBg("bmp/img16_a", 1); + NF_Load16bitsBg("bmp/img16_b", 2); - // Inicializa el motor 2D en modo BITMAP - NF_Set2D(0, 5); // Modo 2D_5 en ambas pantallas - NF_Set2D(1, 5); + // Tranfer image to VRAM of both screens + NF_Copy16bitsBuffer(0, 0, 0); + NF_Copy16bitsBuffer(1, 1, 2); - // Inicializa los fondos en modo "BITMAP" - NF_InitBitmapBgSys(0, 1); - NF_InitBitmapBgSys(1, 1); + // It's not needed to use it any longer, so remove it from RAM + NF_Unload16bitsBg(0); - // Inicializa los buffers para guardar fondos en formato BITMAP - NF_Init16bitsBgBuffers(); + // Variables to control the window + s16 xa = 0; + s16 xb = 0; + s16 ya = 0; + s16 yb = 0; - // Inicializa el BackBuffer de 16 bits - // Usalo solo una vez antes de habilitarlo - NF_Init16bitsBackBuffer(1); + while (1) + { + // Copy original image to the backbuffer of the top screen + NF_Copy16bitsBuffer(1, 1, 2); - // Habilita el backbuffer en la pantalla superior - NF_Enable16bitsBackBuffer(1); + // Read keys and touchscreen + scanKeys(); + touchPosition touchscreen; + touchRead(&touchscreen); + u16 keys_held = keysHeld(); + u16 keys_down = keysDown(); - // Carga el archivo BITMAP de imagen en formato RAW a la RAM - NF_Load16bitsBg("bmp/bitmap16", 0); - NF_Load16bitsBg("bmp/img16_a", 1); - NF_Load16bitsBg("bmp/img16_b", 2); + if (keys_down & KEY_TOUCH) + { + // If this is the first time the user presses the screen, save that + // point as one corner of the window. + xa = touchscreen.px; + ya = touchscreen.py; + } + else if (keys_held & KEY_TOUCH) + { + // Save the other corner of the window + xb = touchscreen.px; + yb = touchscreen.py; - // Tranfiere la imagen a la VRAM de ambas pantallas - NF_Copy16bitsBuffer(0, 0, 0); - NF_Copy16bitsBuffer(1, 1, 2); + // If the two points aren't the same + if ((xa != xb) && (ya != yb)) + { + int sqr_xa, sqr_xb, sqr_ya, sqr_yb; - // Si no es necesario usarla mas, borrala de la RAM - NF_Unload16bitsBg(0); + // Calculate window bounds + if (xa < xb) + { + sqr_xa = xa; + sqr_xb = xb; + } + else + { + sqr_xa = xb; + sqr_xb = xa; + } - // Variables del control del touchpad - u16 keys = 0; - touchPosition touchscreen; + if (ya < yb) + { + sqr_ya = ya; + sqr_yb = yb; + } + else + { + sqr_ya = yb; + sqr_yb = ya; + } - // Variables del control de la ventana - u32 move = 0; - s16 y = 0; - s16 xa = 0; - s16 xb = 0; - s16 ya = 0; - s16 yb = 0; - s16 sqr_xa = 0; - s16 sqr_xb = 0; - s16 sqr_ya = 0; - s16 sqr_yb = 0; - bool in_touch = false; + // Draw the window + for (int y = sqr_ya; y < sqr_yb; y ++) + { + u32 offset = (y << 8) + sqr_xa; - // Repite para siempre - while (1) { + memcpy(NF_16BITS_BACKBUFFER[1] + offset, + NF_BG16B[1].buffer + offset, + (sqr_xb - sqr_xa) << 1); + } + } + } - // Copia al backbuffer la imagen de arriba - NF_Copy16bitsBuffer(1, 1, 2); + // Wait for the screen refresh + swiWaitForVBlank(); - // Lectura de posicion del stylus - scanKeys(); // Lee el touchpad via Libnds - touchRead(&touchscreen); - keys = keysHeld(); // Verifica el estado del touchscreen - if (keys & KEY_TOUCH) { // Si se toca el touchpad - if (!in_touch) { // Y es el primer toque - xa = touchscreen.px; // Guarda el punto - ya = touchscreen.py; - in_touch = true; // e indicalo - } - xb = touchscreen.px; // Guarda el segundo punto - yb = touchscreen.py; - } else { - in_touch = false; // Ya no se esta tocando el touchpad - } - - // Si se ha tocado el touchpad y la ventana es del tamaƱo suficiente... - if (in_touch && (xa != xb) && (ya != yb)) { - // Calcula los limites de la ventana - if (xa < xb) { - sqr_xa = xa; - sqr_xb = xb; - } else { - sqr_xa = xb; - sqr_xb = xa; - } - if (ya < yb) { - sqr_ya = ya; - sqr_yb = yb; - } else { - sqr_ya = yb; - sqr_yb = ya; - } - // Ahora dibuja la ventana linea a linea - for (y = sqr_ya; y < sqr_yb; y ++) { - // Calcula donde se escribira la linea - move = ((y << 8) + sqr_xa); - // Copia la linea de la Imagen A sobre la B - memcpy((NF_16BITS_BACKBUFFER[1] + move), (NF_BG16B[1].buffer + move), ((sqr_xb - sqr_xa) << 1)); - } - } - - // Sincronismo Vertical - swiWaitForVBlank(); - - // Manda el backbuffer a la pantalla - NF_Flip16bitsBackBuffer(1); - - } - - - return 0; + // Swap backbuffer and visible buffers + NF_Flip16bitsBackBuffer(1); + } + return 0; } diff --git a/examples/graphics/backbuffer4/source/main.c b/examples/graphics/backbuffer4/source/main.c index c21701b..a08a21e 100644 --- a/examples/graphics/backbuffer4/source/main.c +++ b/examples/graphics/backbuffer4/source/main.c @@ -1,184 +1,147 @@ // SPDX-License-Identifier: CC0-1.0 // // SPDX-FileContributor: NightFox & Co., 2009-2011 +// +// Basic example of using a 16-bit bitmap and zooming an image. +// http://www.nightfoxandco.com -/* -------------------------------------------------- - - NightFox's Lib Template - Ejemplo basico de BackBuffer en 16bits (modo BITMAP) - Zoom de imagen X2 - - Requiere DevkitARM - Requiere NightFox's Lib - - Codigo por NightFox - http://www.nightfoxandco.com - Inicio 10 de Octubre del 2009 - -------------------------------------------------- -*/ - - - - - -/* -------------------------------------------------- - Includes -------------------------------------------------- -*/ - -// Includes C #include #include #include -// Includes propietarios NDS #include #include -// Includes librerias propias #include +int main(int argc, char **argv) +{ + // Prepare a NitroFS initialization screen + NF_Set2D(0, 0); + NF_Set2D(1, 0); + consoleDemoInit(); + printf("\n NitroFS init. Please wait.\n\n"); + printf(" Iniciando NitroFS,\n por favor, espere.\n\n"); + swiWaitForVBlank(); + // Initialize NitroFS and set it as the root folder of the filesystem + nitroFSInit(NULL); + NF_SetRootFolder("NITROFS"); + // Initialize 2D engine in both screens and use mode 5 + NF_Set2D(0, 5); + NF_Set2D(1, 5); + // Initialize bitmap backgrounds system + NF_InitBitmapBgSys(0, 1); + NF_InitBitmapBgSys(1, 1); -/* -------------------------------------------------- - Main() - Bloque general del programa -------------------------------------------------- -*/ + // Initialize storage buffers + NF_Init16bitsBgBuffers(); -int main(int argc, char **argv) { + // Initialize backbuffers + NF_Init16bitsBackBuffer(0); + NF_Init16bitsBackBuffer(1); - // Pantalla de espera inicializando NitroFS - NF_Set2D(0, 0); - NF_Set2D(1, 0); - consoleDemoInit(); - printf("\n NitroFS init. Please wait.\n\n"); - printf(" Iniciando NitroFS,\n por favor, espere.\n\n"); - swiWaitForVBlank(); + // Enable backbuffers + NF_Enable16bitsBackBuffer(0); + NF_Enable16bitsBackBuffer(1); - // Define el ROOT e inicializa el sistema de archivos - nitroFSInit(NULL); - NF_SetRootFolder("NITROFS"); // Define la carpeta ROOT para usar NITROFS + // Load bitmap background files from NitroFS + NF_Load16bitsBg("bmp/bitmap16", 0); - // Inicializa el motor 2D en modo BITMAP - NF_Set2D(0, 5); // Modo 2D_5 en ambas pantallas - NF_Set2D(1, 5); + // Tranfer image to the backbuffer of the bottom screen + NF_Copy16bitsBuffer(1, 1, 0); - // Inicializa los fondos en modo "BITMAP" - NF_InitBitmapBgSys(0, 1); - NF_InitBitmapBgSys(1, 1); + // Window position + s16 square_x = 0; + s16 square_y = 0; - // Inicializa los buffers para guardar fondos en formato BITMAP - NF_Init16bitsBgBuffers(); + while (1) + { + // Copy original image + NF_Copy16bitsBuffer(1, 1, 0); - // Inicializa el BackBuffer de 16 bits - // Usalo solo una vez antes de habilitarlo - NF_Init16bitsBackBuffer(0); - NF_Init16bitsBackBuffer(1); + // Read keys and touchscreen + scanKeys(); + touchPosition touchscreen; + touchRead(&touchscreen); + u16 keys = keysHeld(); + if (keys & KEY_TOUCH) + { + square_x = touchscreen.px - 64; + square_y = touchscreen.py - 48; + } - // Habilita el backbuffer en la pantalla superior - NF_Enable16bitsBackBuffer(0); - NF_Enable16bitsBackBuffer(1); + // Restrict coordinates of the window + if (square_x < 0) + square_x = 0; + if (square_x > 127) + square_x = 127; - // Carga el archivo BITMAP de imagen en formato RAW a la RAM - NF_Load16bitsBg("bmp/bitmap16", 0); + if (square_y < 0) + square_y = 0; + if (square_y > 95) + square_y = 95; - // Tranfiere la imagen al BackBuffer de la pantalla inferior - NF_Copy16bitsBuffer(1, 1, 0); + // Reset source buffer read pointers + u32 zoom_x = 0; + u32 zoom_y = 0; - // Variables RGB - u8 r = 0; - u8 g = 0; - u8 b = 0; + // Fill the destination buffer + for (int y = square_y; y < square_y + 96; y++) + { + for (int x = square_x; x < square_x + 128; x++) + { + // Get current color + u32 rgb = NF_16BITS_BACKBUFFER[1][(y << 8) + x]; - // Calcula el valor RGB - u16 rgb = 0; + // Write value in 2x2 pixels in the bacbuffer of the bottom + // screen for the zoom effect. + NF_16BITS_BACKBUFFER[0][(zoom_y << 8) + zoom_x] = rgb; + NF_16BITS_BACKBUFFER[0][(zoom_y << 8) + (zoom_x + 1)] = rgb; + NF_16BITS_BACKBUFFER[0][((zoom_y + 1) << 8) + zoom_x] = rgb; + NF_16BITS_BACKBUFFER[0][((zoom_y + 1) << 8) + (zoom_x + 1)] = rgb; - // Ventana - s16 x = 0; - s16 y = 0; - s16 composite = 0; - s16 square_x = 0; - s16 square_y = 0; - s16 zoom_x = 0; - s16 zoom_y = 0; + // Get RGB components + u32 r = rgb & 0x1F; + u32 g = (rgb >> 5) & 0x1F; + u32 b = (rgb >> 10) & 0x1F; - // Variables del control del touchpad - u16 keys = 0; - touchPosition touchscreen; + // Generate a grayscale value based on the RGB components + u32 composite = ((r + g + b) / 3) + 3; + if (composite > 31) + composite = 31; - while (1) { + // Replace the RGB values by the grayscale value (this shows up + // as a rectangular window on the screen) + r = composite; + g = composite >> 2; + b = composite >> 1; - // Copia el original - NF_Copy16bitsBuffer(1, 1, 0); + // Pack the components as a RGB value + rgb = r | (g << 5) | (b << 10) | BIT(15); - // Lectura de posicion del stylus - scanKeys(); // Lee el touchpad via Libnds - touchRead(&touchscreen); - keys = keysHeld(); // Verifica el estado del touchscreen - if (keys & KEY_TOUCH) { - square_x = (touchscreen.px - 64); - square_y = (touchscreen.py - 48); - } + // Write RGB value in the backbuffer + NF_16BITS_BACKBUFFER[1][(y << 8) + x] = rgb; - // Calcula la ventana - if (square_x < 0) square_x = 0; - if (square_x > 127) square_x = 127; - if (square_y < 0) square_y = 0; - if (square_y > 95) square_y = 95; + // Increment X coordinate pointer + zoom_x += 2; + } - // Resetea el control de zoom - zoom_x = 0; - zoom_y = 0; + // Set X coordinate from the beginning + zoom_x = 0; + // Increment Y coordinate pointer + zoom_y += 2; + } - // Rellena el buffer - for (y = square_y; y < (square_y + 96); y ++) { - for (x = square_x; x < (square_x + 128); x ++) { - // Obten el color actual - rgb = NF_16BITS_BACKBUFFER[1][((y << 8) + x)]; - // Escribe los pixeles en el Backbuffer de la pantalla superior - // Esto genera el Zoom x2 en la pantalla superior - NF_16BITS_BACKBUFFER[0][((zoom_y << 8) + zoom_x)] = rgb; - NF_16BITS_BACKBUFFER[0][((zoom_y << 8) + (zoom_x + 1))] = rgb; - NF_16BITS_BACKBUFFER[0][(((zoom_y + 1) << 8) + zoom_x)] = rgb; - NF_16BITS_BACKBUFFER[0][(((zoom_y + 1) << 8) + (zoom_x + 1))] = rgb; - // Desglosa el RGB - r = (rgb & 0x1F); - g = ((rgb >> 5) & 0x1F); - b = ((rgb >> 10) & 0x1F); - // Genera el equivalente a blanco y negro y sube el brillo 3 puntos - composite = ((int)((r + g + b) / 3) + 3); - if (composite > 31) composite = 31; - // Reemplaza los valores RGB - r = (composite); - g = (composite >> 2); - b = (composite >> 1); - // Calcula el valor RGB (Alpha + RGB555) - rgb = ((r)|((g) << 5)|((b) << 10)|(BIT(15))); - // Escribelos en el buffer - NF_16BITS_BACKBUFFER[1][((y << 8) + x)] = rgb; - // Incrementa dos puntos la X del zoom - zoom_x += 2; - } - zoom_x = 0; // Resetea la X del zoom - zoom_y += 2; // Incrementa dos puntos la Y del zoom - } + // Wait for the screen refresh + swiWaitForVBlank(); - // Sincronismo Vertical - swiWaitForVBlank(); - - // Copia el contenido del Backbuffer a la VRAM - NF_Flip16bitsBackBuffer(0); - NF_Flip16bitsBackBuffer(1); - - } - - - return 0; + // Swap backbuffers and visible buffers + NF_Flip16bitsBackBuffer(0); + NF_Flip16bitsBackBuffer(1); + } + return 0; } diff --git a/examples/graphics/bg16bits/source/main.c b/examples/graphics/bg16bits/source/main.c index 8b3d043..9e07a2b 100644 --- a/examples/graphics/bg16bits/source/main.c +++ b/examples/graphics/bg16bits/source/main.c @@ -1,110 +1,70 @@ // SPDX-License-Identifier: CC0-1.0 // // SPDX-FileContributor: NightFox & Co., 2009-2011 +// +// Example of using 16-bit bitmap graphics. +// http://www.nightfoxandco.com -/* -------------------------------------------------- - - NightFox's Lib Template - Ejemplo graficos en modo BITMAP de 16bits - - Requiere DevkitARM - Requiere NightFox's Lib - - Codigo por NightFox - http://www.nightfoxandco.com - Inicio 10 de Octubre del 2009 - -------------------------------------------------- -*/ - - - - - -/* -------------------------------------------------- - Includes -------------------------------------------------- -*/ - -// Includes C -#include - -// Includes propietarios NDS #include -// Includes librerias propias #include +int main(int argc, char **argv) +{ + // Initialize 2D engine in both screens and use mode 5 + NF_Set2D(0, 5); + NF_Set2D(1, 5); + // Initialize bitmap backgrounds system + NF_InitBitmapBgSys(0, 1); + NF_InitBitmapBgSys(1, 1); + u32 r = 0; + u32 g = 0; + u32 b = 0; + // Rellena la pantalla + for (int y = 0; y < 256; y++) + { + for (int x = 0; x < 256; x++) + { + // Calculate new color + r++; + if (r > 31) + { + r = 0; + g++; + if (g > 31) + { + g = 0; + b++; + if (b > 31) + b = 0; + } + } -/* -------------------------------------------------- - Main() - Bloque general del programa -------------------------------------------------- -*/ + // Pack the components as a RGB value + u32 rgb = r | (g << 5) | (b << 10) | BIT(15); -int main(int argc, char **argv) { + // Calculate destination address for top screen (VRAM_A) + u32 address = 0x06000000 + (((y << 8) + x) << 1); - // Inicializa el motor 2D en modo BITMAP - NF_Set2D(0, 5); // Modo 2D_5 en ambas pantallas - NF_Set2D(1, 5); + // Write RGB value + *((u16*)address) = rgb; - // Inicializa los fondos en BITMAP - NF_InitBitmapBgSys(0, 1); - NF_InitBitmapBgSys(1, 1); + // Calculate destination address for bottom screen (VRAM_C) + address = 0x06200000 + (((x << 8) + y) << 1); + // Write RGB value + *((u16*)address) = rgb; + } + } - // Variables RGB - u8 r = 0; - u8 g = 0; - u8 b = 0; - // Calcula el valor RGB - u16 rgb = 0; - // Direccion en VRAM - u32 adress = 0; - // Otras - s16 x = 0; - s16 y = 0; - - // Rellena la pantalla - for (y = 0; y < 256; y ++) { - for (x = 0; x < 256; x ++) { - // Calcula el nuevo color - r ++; - if (r > 31) { - r = 0; - g ++; - if (g > 31) { - g = 0; - b ++; - if (b > 31) b = 0; - } - } - // Calcula el valor RGB (Alpha + RGB555) - rgb = ((r)|((g) << 5)|((b) << 10)|(BIT(15))); - // Calcula la posicion donde se escribira en la VRAM (Banco A, modo bitmap, pantalla superior) - adress = (0x06000000 + (((y << 8) + x) << 1)); - // Escribe el valor - *((u16*)adress) = rgb; - // Calcula la posicion donde se escribira en la VRAM (Banco C, modo bitmap, pantalla inferior) - adress = (0x06200000 + (((x << 8) + y) << 1)); - // Escribe el valor - *((u16*)adress) = rgb; - } - } - - - // Bucle (repite para siempre) - while(1) { - - swiWaitForVBlank(); // Espera al sincronismo vertical - - } - - return 0; + while (1) + { + // Wait for the screen refresh + swiWaitForVBlank(); + } + return 0; } diff --git a/examples/graphics/sprite/source/main.c b/examples/graphics/sprite/source/main.c index 9f03177..c171a66 100644 --- a/examples/graphics/sprite/source/main.c +++ b/examples/graphics/sprite/source/main.c @@ -124,9 +124,11 @@ int main(int argc, char **argv) bola_x[n] += bola_spx[n]; if ((bola_x[n] < 0) || (bola_x[n] > 223)) bola_spx[n] *= -1; + bola_y[n] += bola_spy[n]; if ((bola_y[n] < 0) || (bola_y[n] > 159)) bola_spy[n] *= -1; + NF_MoveSprite(0, n, bola_x[n], bola_y[n]); } diff --git a/examples/graphics/spritepal/source/main.c b/examples/graphics/spritepal/source/main.c index 1dac0e4..aa6ebfa 100644 --- a/examples/graphics/spritepal/source/main.c +++ b/examples/graphics/spritepal/source/main.c @@ -1,235 +1,206 @@ // SPDX-License-Identifier: CC0-1.0 // // SPDX-FileContributor: NightFox & Co., 2009-2011 +// +// Example of modifying sprite palettes. +// http://www.nightfoxandco.com -/* -------------------------------------------------- - - NightFox's Lib Template - Ejemplo de manipulacion de paletas de Sprites - - Requiere DevkitARM - Requiere NightFox's Lib - - Codigo por NightFox - http://www.nightfoxandco.com - Inicio 10 de Octubre del 2009 - -------------------------------------------------- -*/ - - - - - -/* -------------------------------------------------- - Includes -------------------------------------------------- -*/ - -// Includes C #include #include -// Includes propietarios NDS #include #include -// Includes librerias propias #include +int main(int argc, char **argv) +{ + // Set random seed based on the current time + srand(time(NULL)); + // Prepare a NitroFS initialization screen + NF_Set2D(0, 0); + NF_Set2D(1, 0); + consoleDemoInit(); + printf("\n NitroFS init. Please wait.\n\n"); + printf(" Iniciando NitroFS,\n por favor, espere.\n\n"); + swiWaitForVBlank(); + // Initialize NitroFS and set it as the root folder of the filesystem + nitroFSInit(NULL); + NF_SetRootFolder("NITROFS"); + // Initialize 2D engine in both screens and use mode 0 + NF_Set2D(0, 0); + NF_Set2D(1, 0); -/* -------------------------------------------------- - Main() - Bloque general del programa -------------------------------------------------- -*/ + // Initialize tiled backgrounds system + NF_InitTiledBgBuffers(); // Initialize storage buffers + NF_InitTiledBgSys(0); // Top screen + NF_InitTiledBgSys(1); // Bottom screen -int main(int argc, char **argv) { + // Initialize sprite system + NF_InitSpriteBuffers(); // Initialize storage buffers + NF_InitSpriteSys(0); // Top screen + NF_InitSpriteSys(1); // Bottom screen - // Pantalla de espera inicializando NitroFS - NF_Set2D(0, 0); - NF_Set2D(1, 0); - consoleDemoInit(); - printf("\n NitroFS init. Please wait.\n\n"); - printf(" Iniciando NitroFS,\n por favor, espere.\n\n"); - swiWaitForVBlank(); + // Load background files from NitroFS + NF_LoadTiledBg("bg/nfl", "nfl", 256, 256); + NF_LoadTiledBg("bg/bg3", "capa_3", 256, 256); + NF_LoadTiledBg("bg/bg2", "capa_2", 1024, 256); - // Define el ROOT e inicializa el sistema de archivos - nitroFSInit(NULL); - NF_SetRootFolder("NITROFS"); // Define la carpeta ROOT para usar NITROFS + // Load sprite files from NitroFS + NF_LoadSpriteGfx("sprite/personaje", 0, 64, 64); + NF_LoadSpritePal("sprite/personaje", 0); - // Inicializa el motor 2D - NF_Set2D(0, 0); // Modo 2D_0 en ambas pantallas - NF_Set2D(1, 0); + NF_LoadSpriteGfx("sprite/bola", 1, 32, 32); + NF_LoadSpritePal("sprite/bola", 1); - // Inicializa los fondos tileados - NF_InitTiledBgBuffers(); // Inicializa los buffers para almacenar fondos - NF_InitTiledBgSys(0); // Inicializa los fondos Tileados para la pantalla superior - NF_InitTiledBgSys(1); // Iniciliaza los fondos Tileados para la pantalla inferior + // Create top screen background + NF_CreateTiledBg(0, 3, "nfl"); - // Inicializa los Sprites - NF_InitSpriteBuffers(); // Inicializa los buffers para almacenar sprites y paletas - NF_InitSpriteSys(0); // Inicializa los sprites para la pantalla superior - NF_InitSpriteSys(1); // Inicializa los sprites para la pantalla inferior + // Create bottom screen backgrounds + NF_CreateTiledBg(1, 3, "capa_3"); + NF_CreateTiledBg(1, 2, "capa_2"); - // Carga los archivos de fondo desde la FAT / NitroFS a la RAM - NF_LoadTiledBg("bg/nfl", "nfl", 256, 256); // Carga el fondo para la pantalla superior - NF_LoadTiledBg("bg/bg3", "capa_3", 256, 256); // Carga el fondo para la capa 3, pantalla inferior - NF_LoadTiledBg("bg/bg2", "capa_2", 1024, 256); // Carga el fondo para la capa 2, pantalla inferior + // Transfer the required sprites to VRAM + NF_VramSpriteGfx(1, 0, 0, true); // Ball: Keep all frames in VRAM + NF_VramSpritePal(1, 0, 0); + NF_VramSpriteGfx(0, 1, 0, true); // Character: Keep all frames in VRAM + NF_VramSpritePal(0, 1, 0); - // Carga los archivos de sprites desde la FAT / NitroFS a la RAM - NF_LoadSpriteGfx("sprite/personaje", 0, 64, 64); // Personaje - NF_LoadSpritePal("sprite/personaje", 0); + // Setup character sprite + s16 pj_x = 0; + s16 pj_y = 127; + u8 pj_frame = 0; + u8 pj_anim = 0; + s8 pj_speed = 1; + NF_CreateSprite(1, 0, 0, 0, pj_x, pj_y); - NF_LoadSpriteGfx("sprite/bola", 1, 32, 32); // Bola azul - NF_LoadSpritePal("sprite/bola", 1); + // Setup ball sprites + s16 bola_x[32]; + s16 bola_y[32]; + s8 bola_spx[32]; + s8 bola_spy[32]; + for (int n = 0; n < 32; n++) + { + bola_x[n] = rand() % 223; + bola_y[n] = rand() % 159; + bola_spx[n] = (rand() % 3) + 1; + bola_spy[n] = (rand() % 3) + 1; + NF_CreateSprite(0, n, 0, 0, bola_x[n], bola_y[n]); + } - // Crea los fondos de la pantalla superior - NF_CreateTiledBg(0, 3, "nfl"); - // Crea los fondos de la pantalla inferior - NF_CreateTiledBg(1, 3, "capa_3"); - NF_CreateTiledBg(1, 2, "capa_2"); + // Palette color cycle speed + int speed = 0; + while (1) + { + // Color cycle handling + speed++; + if (speed > 2) + { + speed = 0; - // Transfiere a la VRAM los sprites necesarios - NF_VramSpriteGfx(1, 0, 0, true); // Bola (mantiene los frames en RAM) - NF_VramSpritePal(1, 0, 0); + // Balls + for (int n = 1; n < 256; n++) + { + // Get current color from the palette + u8 r, g, b; + NF_SpriteGetPalColor(0, 0, n, &r, &g, &b); - NF_VramSpriteGfx(0, 1, 0, true); // Personaje (mantiene los frames en RAM) Pantalla inferior - NF_VramSpritePal(0, 1, 0); + // Modify color + r--; + if (r > 31) + r = 31; + g++; + if (g > 31) + g = 0; - // Variables generales y inicializacion del random - u16 n = 0; - srand(time(NULL)); + b++; + if (b > 31) + b = 0; - // Crea el sprite del personaje en pantalla - s16 pj_x = 0; - s16 pj_y = 127; - u8 pj_frame = 0; - u8 pj_anim = 0; - s8 pj_speed = 1; - NF_CreateSprite(1, 0, 0, 0, pj_x, pj_y); + // Update color in the copy of the palette in RAM + NF_SpriteEditPalColor(0, 0, n, r, g, b); + } - // Crea las bolas en la pantalla superior - s16 bola_x[32]; - s16 bola_y[32]; - s8 bola_spx[32]; - s8 bola_spy[32]; - for (n = 0; n < 32; n ++) { - bola_x[n] = (rand() % 223); - bola_y[n] = (rand() % 159); - bola_spx[n] = (rand() % 3) + 1; - bola_spy[n] = (rand() % 3) + 1; - NF_CreateSprite(0, n, 0, 0, bola_x[n], bola_y[n]); - } + // Update palette in VRAM + NF_SpriteUpdatePalette(0, 0); - // Variables para el ciclo de colores - u8 r, g, b; - s8 red, green, blue; - s8 speed = 0; + // Character + for (int n = 1; n < 256; n++) + { + // Get current color from the palette + u8 r, g, b; + NF_SpriteGetPalColor(1, 0, n, &r, &g, &b); - // Bucle (repite para siempre) - while(1) { + // Modify color + r--; + if (r > 31) + r = 31; - // Ciclo de colores - speed ++; - if (speed > 2) { - speed = 0; + // Update color in the copy of the palette in RAM + NF_SpriteEditPalColor(1, 0, n, r, g, b); + } - // Bolas - for (n = 1; n < 256; n ++) { - // Obten el valor actual del color en la paleta - NF_SpriteGetPalColor(0, 0, n, &r, &g, &b); - // Pasa los valores del color a las variables de modificacion - red = r; - green = g; - blue = b; - // Modifica los valores - red --; - if (red < 0) red = 31; - green ++; - if (green > 31) green = 0; - blue ++; - if (blue > 31) blue = 0; - // Actualiza el color en la paleta en RAM - NF_SpriteEditPalColor(0, 0, n, red, green, blue); - } - // Actualiza la paleta en VRAM - NF_SpriteUpdatePalette(0, 0); + // Update palette in VRAM + NF_SpriteUpdatePalette(1, 0); + } - // Personaje - for (n = 1; n < 256; n ++) { - // Obten el valor actual del color en la paleta - NF_SpriteGetPalColor(1, 0, n, &r, &g, &b); - // Pasa los valores del color a las variables de modificacion - red = r; - green = g; - blue = b; - // Modifica los valores - red --; - if (red < 0) red = 31; - //green ++; - //if (green > 31) green = 0; - //blue ++; - //if (blue > 31) blue = 0; - // Actualiza el color en la paleta en RAM - NF_SpriteEditPalColor(1, 0, n, red, green, blue); - } - // Actualiza la paleta en VRAM - NF_SpriteUpdatePalette(1, 0); - } + // Move character + pj_x += pj_speed; + if ((pj_x < 0) || (pj_x > 191)) + { + pj_speed *= -1; + if (pj_speed > 0) + NF_HflipSprite(1, 0, false); + else + NF_HflipSprite(1, 0, true); + } + NF_MoveSprite(1, 0, pj_x, pj_y); - // Mueve el personaje - pj_x += pj_speed; - if ((pj_x < 0) || (pj_x > 191)) { - pj_speed *= -1; - if (pj_speed > 0) { - NF_HflipSprite(1, 0, false); - } else { - NF_HflipSprite(1, 0, true); - } - } - NF_MoveSprite(1, 0, pj_x, pj_y); + // Animate character + pj_anim++; + if (pj_anim > 5) + { + pj_anim = 0; + pj_frame++; + if (pj_frame > 11) + pj_frame = 0; - // Animacion del personaje - pj_anim ++; - if (pj_anim > 5) { - pj_anim = 0; - pj_frame ++; - if (pj_frame > 11) pj_frame = 0; - NF_SpriteFrame(1, 0, pj_frame); - } + NF_SpriteFrame(1, 0, pj_frame); + } - // Mueve las bolas - for (n = 0; n < 32; n ++) { - bola_x[n] += bola_spx[n]; - if ((bola_x[n] < 0) || (bola_x[n] > 223)) bola_spx[n] *= -1; - bola_y[n] += bola_spy[n]; - if ((bola_y[n] < 0) || (bola_y[n] > 159)) bola_spy[n] *= -1; - NF_MoveSprite(0, n, bola_x[n], bola_y[n]); - } + // Move balls + for (int n = 0; n < 32; n++) + { + bola_x[n] += bola_spx[n]; + if ((bola_x[n] < 0) || (bola_x[n] > 223)) + bola_spx[n] *= -1; - // Actualiza el array de OAM - NF_SpriteOamSet(0); - NF_SpriteOamSet(1); + bola_y[n] += bola_spy[n]; + if ((bola_y[n] < 0) || (bola_y[n] > 159)) + bola_spy[n] *= -1; - swiWaitForVBlank(); // Espera al sincronismo vertical + NF_MoveSprite(0, n, bola_x[n], bola_y[n]); + } - // Actualiza el OAM - oamUpdate(&oamMain); - oamUpdate(&oamSub); + // Update OAM array + NF_SpriteOamSet(0); + NF_SpriteOamSet(1); - } + // Wait for the screen refresh + swiWaitForVBlank(); - return 0; + // Update OAM + oamUpdate(&oamMain); + oamUpdate(&oamSub); + } + return 0; }