examples: Cleanup and translate more examples to English

This commit is contained in:
Antonio Niño Díaz 2023-05-20 18:42:39 +01:00
parent c6d6883caf
commit 11b2717515
5 changed files with 463 additions and 625 deletions

View File

@ -1,214 +1,192 @@
// SPDX-License-Identifier: CC0-1.0 // SPDX-License-Identifier: CC0-1.0
// //
// SPDX-FileContributor: NightFox & Co., 2009-2011 // SPDX-FileContributor: NightFox & Co., 2009-2011
//
// Alpha blending example (real alpha blending and fake blending by blinking).
// http://www.nightfoxandco.com
/*
-------------------------------------------------
NightFox's Lib Template
Ejemplo de Alpha Blending
Importante: El efecto Fake alpha solo funciona bien
en la consola, no en los emuladores.
Requiere DevkitARM
Requiere NightFox's Lib
Codigo por NightFox
http://www.nightfoxandco.com
Inicio 10 de Octubre del 2009
-------------------------------------------------
*/
/*
-------------------------------------------------
Includes
-------------------------------------------------
*/
// Includes C
#include <stdio.h> #include <stdio.h>
#include <time.h> #include <time.h>
// Includes propietarios NDS
#include <nds.h> #include <nds.h>
#include <filesystem.h> #include <filesystem.h>
// Includes librerias propias
#include <nf_lib.h> #include <nf_lib.h>
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);
/* // Initialize tiled backgrounds system
------------------------------------------------- NF_InitTiledBgBuffers(); // Initialize storage buffers
Main() - Bloque general del programa 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 // Load background files from NitroFS
NF_Set2D(0, 0); NF_LoadTiledBg("bg/nfl", "nfl", 256, 256);
NF_Set2D(1, 0); NF_LoadTiledBg("bg/bg3", "capa_3", 256, 256);
consoleDemoInit(); NF_LoadTiledBg("bg/bg2", "capa_2", 1024, 256);
printf("\n NitroFS init. Please wait.\n\n");
printf(" Iniciando NitroFS,\n por favor, espere.\n\n");
swiWaitForVBlank();
// Define el ROOT e inicializa el sistema de archivos // Load sprite files from NitroFS
nitroFSInit(NULL); NF_LoadSpriteGfx("sprite/personaje", 0, 64, 64);
NF_SetRootFolder("NITROFS"); // Define la carpeta ROOT para usar NITROFS NF_LoadSpritePal("sprite/personaje", 0);
// Inicializa el motor 2D NF_LoadSpriteGfx("sprite/bola", 1, 32, 32);
NF_Set2D(0, 0); // Modo 2D_0 en ambas pantallas NF_LoadSpritePal("sprite/bola", 1);
NF_Set2D(1, 0);
// Inicializa los fondos tileados // Create top screen background
NF_InitTiledBgBuffers(); // Inicializa los buffers para almacenar fondos NF_CreateTiledBg(0, 3, "nfl");
NF_InitTiledBgSys(0); // Inicializa los fondos Tileados para la pantalla superior
NF_InitTiledBgSys(1); // Iniciliaza los fondos Tileados para la pantalla inferior
// Inicializa los Sprites // Create bottom screen backgrounds
NF_InitSpriteBuffers(); // Inicializa los buffers para almacenar sprites y paletas NF_CreateTiledBg(1, 3, "capa_3");
NF_InitSpriteSys(0); // Inicializa los sprites para la pantalla superior NF_CreateTiledBg(1, 2, "capa_2");
NF_InitSpriteSys(1); // Inicializa los sprites para la pantalla inferior
// Carga los archivos de fondo desde la FAT / NitroFS a la RAM // Transfer the required sprites to VRAM
NF_LoadTiledBg("bg/nfl", "nfl", 256, 256); // Carga el fondo para la pantalla superior NF_VramSpriteGfx(1, 0, 0, true); // Ball: Keep all frames in VRAM
NF_LoadTiledBg("bg/bg3", "capa_3", 256, 256); // Carga el fondo para la capa 3, pantalla inferior NF_VramSpritePal(1, 0, 0);
NF_LoadTiledBg("bg/bg2", "capa_2", 1024, 256); // Carga el fondo para la capa 2, pantalla inferior
NF_VramSpriteGfx(0, 1, 0, false); // Character: Keep unused frames in RAM
NF_VramSpritePal(0, 1, 0);
// Carga los archivos de sprites desde la FAT / NitroFS a la RAM // Enable alpha
NF_LoadSpriteGfx("sprite/personaje", 0, 64, 64); // Personaje //REG_BLDCNT = BLEND_ALPHA // Layer 2 over layer 3 and sprites (top screen)
NF_LoadSpritePal("sprite/personaje", 0); // | BLEND_SRC_BG2
// | BLEND_DST_BG3 | BLEND_DST_SPRITE;
REG_BLDCNT_SUB = BLEND_ALPHA // Layer 2 over layer 3 (bottom screen)
| BLEND_SRC_BG2
| BLEND_DST_BG3;
NF_LoadSpriteGfx("sprite/bola", 1, 32, 32); // Bola azul // Alpha value
NF_LoadSpritePal("sprite/bola", 1); //REG_BLDALPHA = 0x05 | (0x0F << 8); // Top screen (5 / 15)
REG_BLDALPHA_SUB = 0x00 | (0x0F << 8); // Bottom screen (0 / 15)
// Variables
s8 blink = 1;
u8 alpha = 0;
u8 alpha_timer = 0;
s8 alpha_inc = 1;
// Crea los fondos de la pantalla superior // Setup character sprite
NF_CreateTiledBg(0, 3, "nfl"); s16 pj_x = 0;
// Crea los fondos de la pantalla inferior s16 pj_y = 127;
NF_CreateTiledBg(1, 3, "capa_3"); u8 pj_frame = 0;
NF_CreateTiledBg(1, 2, "capa_2"); u8 pj_anim = 0;
s8 pj_speed = 1;
NF_CreateSprite(1, 0, 0, 0, pj_x, pj_y);
// Setup ball sprites
s16 bola_x[32];
s16 bola_y[32];
s8 bola_spx[32];
s8 bola_spy[32];
// Transfiere a la VRAM los sprites necesarios for (int n = 0; n < 32; n++)
NF_VramSpriteGfx(1, 0, 0, true); // Personaje (mantiene los frames en RAM) Pantalla inferior {
NF_VramSpritePal(1, 0, 0); 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]);
}
NF_VramSpriteGfx(0, 1, 0, false); // Personaje (mantiene los frames en RAM) Pantalla inferior while (1)
NF_VramSpritePal(0, 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);
// Habilita el canal Alpha // Animate character
//REG_BLDCNT = BLEND_ALPHA | BLEND_SRC_BG2 | (BLEND_DST_BG3 + BLEND_DST_SPRITE); // Alpha del fondo 2 sobre el fondo 3 y los sprites (Main Screen) pj_anim++;
REG_BLDCNT_SUB = BLEND_ALPHA | BLEND_SRC_BG2 | BLEND_DST_BG3; // Alpha del fondo 2 sobre el 3 (Sub Screen) if (pj_anim > 5)
// Nivel de Alpha {
//REG_BLDALPHA = 0x05 | (0x0F << 8); // Nivel de alpha (5 sobre 15) en Main Screen pj_anim = 0;
REG_BLDALPHA_SUB = 0x00 | (0x0F << 8); // Nivel de alpha (0 sobre 15) en Sub Screen pj_frame++;
if (pj_frame > 11)
pj_frame = 0;
NF_SpriteFrame(1, 0, pj_frame);
}
// 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;
bola_y[n] += bola_spy[n];
if ((bola_y[n] < 0) || (bola_y[n] > 159))
bola_spy[n] *= -1;
// Variables generales y inicializacion del random NF_MoveSprite(0, n, bola_x[n], bola_y[n]);
u8 n = 0;
s8 blink = 1;
u8 alpha = 0;
u8 alpha_timer = 0;
s8 alpha_inc = 1;
srand(time(NULL));
// Crea el sprite del personaje en pantalla // Fake alpha effect in the last 15 balls. This is done by showing
s16 pj_x = 0; // the balls every other frame. This only looks fine in old LCD
s16 pj_y = 127; // screens with long refresh times. In old DS models it looks better
u8 pj_frame = 0; // than 2DS/3DS and emulators for PC.
u8 pj_anim = 0; if (n < 15)
s8 pj_speed = 1; {
NF_CreateSprite(1, 0, 0, 0, pj_x, pj_y); if (blink)
NF_ShowSprite(0, n, true);
else
NF_ShowSprite(0, n, false);
}
}
blink ^= 1;
// Crea las bolas en la pantalla superior // "Dissolve" background effect
s16 bola_x[32]; alpha_timer ++;
s16 bola_y[32]; if (alpha_timer > 5)
s8 bola_spx[32]; {
s8 bola_spy[32]; alpha_timer = 0;
for (n = 0; n < 32; n ++) { alpha += alpha_inc;
bola_x[n] = (rand() % 223); if ((alpha == 0) || (alpha == 14))
bola_y[n] = (rand() % 159); alpha_inc *= -1;
bola_spx[n] = (rand() % 3) + 1;
bola_spy[n] = (rand() % 3) + 1;
NF_CreateSprite(0, n, 0, 0, bola_x[n], bola_y[n]);
}
// Alpha level in bottom screen (the max is 15)
REG_BLDALPHA_SUB = alpha | (0x0F << 8);
}
// Bucle (repite para siempre) // Update OAM array
while(1) { NF_SpriteOamSet(0);
NF_SpriteOamSet(1);
// Mueve el personaje // Wait for the screen refresh
pj_x += pj_speed; swiWaitForVBlank();
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);
// Animacion del personaje // Update OAM
pj_anim ++; oamUpdate(&oamMain);
if (pj_anim > 5) { oamUpdate(&oamSub);
pj_anim = 0; }
pj_frame ++;
if (pj_frame > 11) pj_frame = 0;
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]);
// Efecto "Fake alpha" en las ultimas 15 bolas
if (n < 15) {
if ((blink > 0)) {
NF_ShowSprite(0, n, true);
} else {
NF_ShowSprite(0, n, false);
}
}
}
blink *= -1;
// Efecto Ghost del fondo (Alpha channel)
alpha_timer ++;
if (alpha_timer > 5) {
alpha_timer = 0;
alpha += alpha_inc;
if ((alpha == 0) || (alpha == 14)) alpha_inc *= -1;
REG_BLDALPHA_SUB = alpha | (0x0F << 8); // Nivel de alpha (sobre 15) en Sub Screen
}
// Actualiza el array de OAM
NF_SpriteOamSet(0);
NF_SpriteOamSet(1);
swiWaitForVBlank(); // Espera al sincronismo vertical
// Actualiza el OAM
oamUpdate(&oamMain);
oamUpdate(&oamSub);
}
return 0;
return 0;
} }

View File

@ -1,267 +1,209 @@
// SPDX-License-Identifier: CC0-1.0 // SPDX-License-Identifier: CC0-1.0
// //
// SPDX-FileContributor: NightFox & Co., 2009-2011 // SPDX-FileContributor: NightFox & Co., 2009-2011
//
// Example of animating tiled backgrounds.
// http://www.nightfoxandco.com
/*
-------------------------------------------------
NightFox's Lib Template
Ejemplo de fondos animados (Tile animation)
Requiere DevkitARM
Requiere NightFox's Lib
Codigo por NightFox
http://www.nightfoxandco.com
Inicio 10 de Octubre del 2009
-------------------------------------------------
*/
/*
-------------------------------------------------
Includes
-------------------------------------------------
*/
// Includes C
#include <stdio.h> #include <stdio.h>
#include <time.h> #include <time.h>
// Includes propietarios NDS
#include <nds.h> #include <nds.h>
#include <filesystem.h> #include <filesystem.h>
// Includes librerias propias
#include <nf_lib.h> #include <nf_lib.h>
#define SPEED 2 // Scroll speed
#define MAP_X 16 // Map width (animated blocks are 4x4, 64 / 4 = 16)
#define MAP_Y 16 // Map height (animated blocks are 4x4, 64 / 4 = 16)
#define WATER_FRAMES 50 // Number of frames of the animation
#define WATER_DELAY 10 // Animation speed (number of frames to wait to change)
// Algunos defines interesantes // Global variables
#define SPEED 2 // Velocidad del scroll
#define MAP_X 16 // Ancho del mapa (bloques de animacion de 4x4 tiles)
#define MAP_Y 16 // Alto del mapa (bloques de animacion de 4x4 tiles)
#define WATER_FRAMES 50 // Frames totales de la animacion
#define WATER_DELAY 10 // Velocidad de la animacion (espera entre frames)
// Declara funciones
void InitAnimatedBg(void); // Inicializa la animacion
void AnimateWater(void); // Animacion del fondo
// Declara las variables globales
u8 WATER_SPEED; u8 WATER_SPEED;
u8 WATER_FRAME[MAP_X][MAP_Y]; // 512 pixeles, 64 tiles, cada frame son 4x4 tiles u8 WATER_FRAME[MAP_X][MAP_Y]; // 512 pixels, 64 tiles, every frame is 4x4 tiles
// Initialize animated background
void InitAnimatedBg(void)
{
// Initialize water animations
for (int y = 0; y < MAP_Y; y++)
{
for (int x = 0; x < MAP_X; x++)
WATER_FRAME[x][y] = rand() % (WATER_FRAMES + 1);
}
// Generate map of water background
for (int y = 0; y < MAP_Y; y ++)
{
for (int x = 0; x < MAP_X; x ++)
{
// Calculate the map 4x4 region to fill
int start_x = x << 2;
int end_x = start_x + 4;
int start_y = y << 2;
int end_y = start_y + 4;
// Displacement
int n = 0;
// Fill the 4x4 region
for (int tile_y = start_y; tile_y < end_y; tile_y++)
{
for (int tile_x = start_x; tile_x < end_x; tile_x++)
{
// The animation has 11 frames. Any number over the limit
// will display the "rest" frame.
int frame = WATER_FRAME[x][y];
if (frame > 10)
frame = 10;
NF_SetTileOfMap(1, 3, tile_x, tile_y, (frame << 4) + n);
n++;
}
}
}
}
/* // Update map in VRAM
------------------------------------------------- NF_UpdateVramMap(1, 3);
Main() - Bloque general del programa
-------------------------------------------------
*/
int main(int argc, char **argv) {
// 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();
// Define el ROOT e inicializa el sistema de archivos
nitroFSInit(NULL);
NF_SetRootFolder("NITROFS"); // Define la carpeta ROOT para usar NITROFS
// Inicializa el motor 2D
NF_Set2D(0, 0); // Modo 2D_0 en ambas pantallas
NF_Set2D(1, 0);
// 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
// Carga los archivos de fondo desde la FAT / NitroFS
NF_LoadTiledBg("bg/nfl", "nfl", 256, 256); // Carga el fondo para la pantalla superior
NF_LoadTilesForBg("bg/water", "water", 512, 512, 0, 175); // Carga el tileset para el agua
// Crea los fondos de la pantalla superior
NF_CreateTiledBg(0, 3, "nfl");
// Crea los fondos de la pantalla inferior
NF_CreateTiledBg(1, 3, "water");
// Inicializa la animacion del fondo
InitAnimatedBg();
// Variables
s16 bg_x = 0;
s16 bg_y = 0;
u16 keys = 0;
// Bucle (repite para siempre)
while(1) {
// Lee el keypad
scanKeys();
keys = keysHeld(); // Teclas presionadas
// Si pulsas izquierda
if (keys & KEY_LEFT) {
bg_x -= SPEED;
if (bg_x < 0) bg_x = 0;
}
// Si pulsas derecha
if (keys & KEY_RIGHT) {
bg_x += SPEED;
if (bg_x > 255) bg_x = 255; // Limite fisico de la capa
}
// Si pulsas arriba
if (keys & KEY_UP) {
bg_y -= SPEED;
if (bg_y < 0) bg_y = 0;
}
// Si pulsas abajo
if (keys & KEY_DOWN) {
bg_y += SPEED;
if (bg_y > 255) bg_y = 255; // Limite fisico de la capa
}
// Anima el fondo
AnimateWater();
swiWaitForVBlank(); // Espera al sincronismo vertical
// Actualiza la posicion de los fondos
NF_ScrollBg(1, 3, bg_x, bg_y);
}
return 0;
} }
// Handle tiled background animation
void AnimateWater(void)
{
// Check if we need to do an animation step
WATER_SPEED++;
if (WATER_SPEED < WATER_DELAY)
return;
WATER_SPEED = 0;
// Funcion para inicializar el fondo animado // Change the required tiles for the animation
void InitAnimatedBg(void) { for (int y = 0; y < MAP_Y; y ++)
{
for (int x = 0; x < MAP_X; x ++)
{
// Calculate the map 4x4 region to fill
int start_x = x << 2;
int end_x = start_x + 4;
int start_y = y << 2;
int end_y = start_y + 4;
// Variables locales // CAlculate the frame to draw
u8 x = 0; WATER_FRAME[x][y]++;
u8 y = 0;
u8 tile_x = 0;
u8 tile_y = 0;
u8 start_x = 0;
u8 end_x = 0;
u8 start_y = 0;
u8 end_y = 0;
u8 n = 0;
u8 frame = 0;
// Inicializa la animacion del agua if (WATER_FRAME[x][y] > WATER_FRAMES)
srand(time(NULL)); WATER_FRAME[x][y] = 0;
for (y = 0; y < MAP_Y; y ++) {
for (x = 0; x < MAP_X; x ++) {
WATER_FRAME[x][y] = (rand() % (WATER_FRAMES + 1));
}
}
// Genera el mapa de el fondo marino if (WATER_FRAME[x][y] <= 10)
for (y = 0; y < MAP_Y; y ++) { {
for (x = 0; x < MAP_X; x ++) { // Displacement
// Calcula el area a rellenar int n = 0;
start_x = (x << 2);
end_x = start_x + 4;
start_y = (y << 2);
end_y = start_y +4;
// Desplazamiento
n = 0;
// Rellena la cuadricula de 4x4 tiles
for (tile_y = start_y; tile_y < end_y; tile_y ++) {
for (tile_x = start_x; tile_x < end_x; tile_x ++) {
frame = WATER_FRAME[x][y];
if (frame > 10) frame = 10; // La animacion tiene 11 frames, por encima de estos, muestra siempre el mismo (sensacion de "reposo")
NF_SetTileOfMap(1, 3, tile_x, tile_y, ((frame << 4) + n));
n ++;
}
}
}
}
// Actualiza el mapa en VRAM // Fill the 4x4 region
NF_UpdateVramMap(1, 3); for (int tile_y = start_y; tile_y < end_y; tile_y++)
{
for (int tile_x = start_x; tile_x < end_x; tile_x++)
{
NF_SetTileOfMap(1, 3, tile_x, tile_y, (WATER_FRAME[x][y] << 4) + n);
n++;
}
}
}
}
}
// Update map in VRAM
NF_UpdateVramMap(1, 3);
} }
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();
// Funcion para animar el fondo mediante tiles // Initialize NitroFS and set it as the root folder of the filesystem
void AnimateWater(void) { nitroFSInit(NULL);
NF_SetRootFolder("NITROFS");
// Calcula el movimiento // Initialize 2D engine in both screens and use mode 0
WATER_SPEED ++; NF_Set2D(0, 0);
NF_Set2D(1, 0);
// Si debes cambiar de frame... // Initialize tiled backgrounds system
if (WATER_SPEED >= WATER_DELAY) { NF_InitTiledBgBuffers(); // Initialize storage buffers
NF_InitTiledBgSys(0); // Top screen
NF_InitTiledBgSys(1); // Bottom screen
// Reset del contador de espera // Load background files from NitroFS
WATER_SPEED = 0; NF_LoadTiledBg("bg/nfl", "nfl", 256, 256);
NF_LoadTilesForBg("bg/water", "water", 512, 512, 0, 175);
// Variables temporales // Create top screen background
u8 x = 0; NF_CreateTiledBg(0, 3, "nfl");
u8 y = 0; // Create bottom screen backgrounds
u8 tile_x = 0; NF_CreateTiledBg(1, 3, "water");
u8 tile_y = 0;
u8 start_x = 0;
u8 end_x = 0;
u8 start_y = 0;
u8 end_y = 0;
u8 n = 0;
// Cambia los tiles necesarios para realizar la animacion // Initialize background animation
for (y = 0; y < MAP_Y; y ++) { InitAnimatedBg();
for (x = 0; x < MAP_X; x ++) {
// Calcula el area a rellenar en este ciclo
start_x = (x << 2);
end_x = start_x + 4;
start_y = (y << 2);
end_y = start_y + 4;
// Y el frame a ejecutar
WATER_FRAME[x][y] ++;
if (WATER_FRAME[x][y] > WATER_FRAMES) WATER_FRAME[x][y] = 0;
if (WATER_FRAME[x][y] <= 10) {
// Desplazamiento
n = 0;
// Rellena la cuadricula de 4x4 tiles
for (tile_y = start_y; tile_y < end_y; tile_y ++) {
for (tile_x = start_x; tile_x < end_x; tile_x ++) {
NF_SetTileOfMap(1, 3, tile_x, tile_y, ((WATER_FRAME[x][y] << 4) + n));
n ++;
}
}
}
}
}
// Actualiza el mapa en la VRAM // Variables
NF_UpdateVramMap(1, 3); s16 bg_x = 0;
s16 bg_y = 0;
} while (1)
{
scanKeys(); // Read keypad
u16 keys = keysHeld();
// Scroll background
if (keys & KEY_LEFT)
{
bg_x -= SPEED;
if (bg_x < 0)
bg_x = 0;
}
if (keys & KEY_RIGHT)
{
bg_x += SPEED;
if (bg_x > 255)
bg_x = 255;
}
if (keys & KEY_UP)
{
bg_y -= SPEED;
if (bg_y < 0)
bg_y = 0;
}
if (keys & KEY_DOWN)
{
bg_y += SPEED;
if (bg_y > 255)
bg_y = 255;
}
// Animate background
AnimateWater();
// Wait for the screen refresh
swiWaitForVBlank();
// Scroll background
NF_ScrollBg(1, 3, bg_x, bg_y);
}
return 0;
} }

View File

@ -1,115 +1,74 @@
// SPDX-License-Identifier: CC0-1.0 // SPDX-License-Identifier: CC0-1.0
// //
// SPDX-FileContributor: NightFox & Co., 2009-2011 // SPDX-FileContributor: NightFox & Co., 2009-2011
//
// 16-bit bitmap backbuffer example.
// http://www.nightfoxandco.com
/*
-------------------------------------------------
NightFox's Lib Template
Ejemplo basico de BackBuffer en 16bits (modo BITMAP)
Requiere DevkitARM
Requiere NightFox's Lib
Codigo por NightFox
http://www.nightfoxandco.com
Inicio 10 de Octubre del 2009
-------------------------------------------------
*/
/*
-------------------------------------------------
Includes
-------------------------------------------------
*/
// Includes C
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
// Includes propietarios NDS
#include <nds.h> #include <nds.h>
// Includes librerias propias
#include <nf_lib.h> #include <nf_lib.h>
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);
// Initialize backbuffers
NF_Init16bitsBackBuffer(0);
NF_Init16bitsBackBuffer(1);
// Enable backbuffers
NF_Enable16bitsBackBuffer(0);
NF_Enable16bitsBackBuffer(1);
/* u32 r = 0;
------------------------------------------------- u32 g = 0;
Main() - Bloque general del programa u32 b = 0;
-------------------------------------------------
*/
int main(int argc, char **argv) { while (1)
{
// Fill buffer
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 > 16)
g = 0;
}
b = (y << 5) >> 8;
// Inicializa el motor 2D en modo BITMAP // Compose RGB value
NF_Set2D(0, 5); // Modo 2D_5 en ambas pantallas u32 rgb = r | (g << 5) | (b << 10) | BIT(15);
NF_Set2D(1, 5);
// Inicializa los fondos en modo "BITMAP" // Write color to the backbuffer
NF_InitBitmapBgSys(0, 1); NF_16BITS_BACKBUFFER[0][(y << 8) + x] = rgb;
NF_InitBitmapBgSys(1, 1); NF_16BITS_BACKBUFFER[1][(x << 8) + y] = rgb;
}
}
// Inicializa el BackBuffer de 16 bits en ambas pantallas // Send backbuffers to the screen
// Usalo solo una vez antes de habilitarlo NF_Flip16bitsBackBuffer(0);
NF_Init16bitsBackBuffer(0); NF_Flip16bitsBackBuffer(1);
NF_Init16bitsBackBuffer(1);
// Habilita el backbuffer de ambas pantallas // Wait for the screen refresh
NF_Enable16bitsBackBuffer(0); swiWaitForVBlank();
NF_Enable16bitsBackBuffer(1); }
// Variables RGB
u8 r = 0;
u8 g = 0;
u8 b = 0;
// Calcula el valor RGB
u16 rgb = 0;
// Otras
s16 x = 0;
s16 y = 0;
while (1) {
// Rellena el buffer
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 > 16) {
g = 0;
}
}
b = ((y << 5) >> 8);
// Calcula el valor RGB (Alpha + RGB555)
rgb = ((r)|((g) << 5)|((b) << 10)|(BIT(15)));
// Escribelos en el buffer
NF_16BITS_BACKBUFFER[0][((y << 8) + x)] = rgb;
NF_16BITS_BACKBUFFER[1][((x << 8) + y)] = rgb;
}
}
// Copia el contenido del Backbuffer a la VRAM
NF_Flip16bitsBackBuffer(0);
NF_Flip16bitsBackBuffer(1);
swiWaitForVBlank();
}
return 0;
return 0;
} }

View File

@ -1,166 +1,125 @@
// SPDX-License-Identifier: CC0-1.0 // SPDX-License-Identifier: CC0-1.0
// //
// SPDX-FileContributor: NightFox & Co., 2009-2011 // SPDX-FileContributor: NightFox & Co., 2009-2011
//
// Basic example of using a 16-bit bitmap with a backbuffer and loading images.
// http://www.nightfoxandco.com
/*
-------------------------------------------------
NightFox's Lib Template
Ejemplo basico de BackBuffer en 16bits (modo BITMAP)
con carga de archivos de imagen
Requiere DevkitARM
Requiere NightFox's Lib
Codigo por NightFox
http://www.nightfoxandco.com
Inicio 10 de Octubre del 2009
-------------------------------------------------
*/
/*
-------------------------------------------------
Includes
-------------------------------------------------
*/
// Includes C
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
// Includes propietarios NDS
#include <nds.h> #include <nds.h>
#include <filesystem.h> #include <filesystem.h>
// Includes librerias propias
#include <nf_lib.h> #include <nf_lib.h>
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);
/* // Initialize storage buffers
------------------------------------------------- NF_Init16bitsBgBuffers();
Main() - Bloque general del programa
-------------------------------------------------
*/
int main(int argc, char **argv) { // Initialize backbuffer
NF_Init16bitsBackBuffer(1);
// Pantalla de espera inicializando NitroFS // Enable backbuffer
NF_Set2D(0, 0); NF_Enable16bitsBackBuffer(1);
NF_Set2D(1, 0);
consoleDemoInit();
printf("\n NitroFS init. Please wait.\n\n");
printf(" Iniciando NitroFS,\n por favor, espere.\n\n");
swiWaitForVBlank();
// Define el ROOT e inicializa el sistema de archivos // Load bitmap background files from NitroFS
nitroFSInit(NULL); NF_Load16bitsBg("bmp/bitmap16", 0);
NF_SetRootFolder("NITROFS"); // Define la carpeta ROOT para usar NITROFS
// Inicializa el motor 2D en modo BITMAP // Tranfer image to VRAM of the top screen
NF_Set2D(0, 5); // Modo 2D_5 en ambas pantallas NF_Copy16bitsBuffer(0, 0, 0);
NF_Set2D(1, 5); // Tranfer image to the backbuffer of the bottom screen
NF_Copy16bitsBuffer(1, 1, 0);
// Inicializa los fondos en modo "BITMAP" // Window location
NF_InitBitmapBgSys(0, 1); s16 square_x = 0;
NF_InitBitmapBgSys(1, 1); s16 square_y = 0;
// Inicializa los buffers para guardar fondos en formato BITMAP while (1)
NF_Init16bitsBgBuffers(); {
// Copy original image
NF_Copy16bitsBuffer(1, 1, 0);
// Inicializa el BackBuffer de 16 bits // Read keys and touchscreen
// Usalo solo una vez antes de habilitarlo scanKeys();
NF_Init16bitsBackBuffer(1); touchPosition touchscreen;
touchRead(&touchscreen);
u16 keys = keysHeld();
if (keys & KEY_TOUCH)
{
square_x = touchscreen.px - 32;
square_y = touchscreen.py - 32;
}
// Habilita el backbuffer en la pantalla superior // Restrict coordinates of the window
NF_Enable16bitsBackBuffer(1); if (square_x < 0)
square_x = 0;
if (square_x > 191)
square_x = 191;
// Carga el archivo BITMAP de imagen en formato RAW a la RAM if (square_y < 0)
NF_Load16bitsBg("bmp/bitmap16", 0); square_y = 0;
if (square_y > 127)
square_y = 127;
// Tranfiere la imagen a la VRAM de la pantalla superior // Fill buffer
NF_Copy16bitsBuffer(0, 0, 0); for (int y = square_y; y < (square_y + 64); y ++)
// Tranfiere la imagen al BackBuffer de la pantalla inferior {
NF_Copy16bitsBuffer(1, 1, 0); for (int x = square_x; x < (square_x + 64); x ++)
{
// Get current color
u32 rgb = NF_16BITS_BACKBUFFER[1][(y << 8) + x];
u32 r = rgb & 0x1F;
u32 g = (rgb >> 5) & 0x1F;
u32 b = (rgb >> 10) & 0x1F;
// Variables RGB // Generate a grayscale value based on the RGB components
u8 r = 0; u32 composite = ((r + g + b) / 3) + 3;
u8 g = 0; if (composite > 31)
u8 b = 0; composite = 31;
// Calcula el valor RGB // Replace the RGB values by the grayscale value (this shows up
u16 rgb = 0; // as a rectangular window on the screen)
r = composite;
g = composite;
b = composite;
// Ventana // Pack the components as a RGB value
s16 x = 0; rgb = r | (g << 5)| (b << 10) | BIT(15);
s16 y = 0;
s16 composite = 0;
s16 square_x = 0;
s16 square_y = 0;
// Variables del control del touchpad // Write RGB value in the backbuffer
u16 keys = 0; NF_16BITS_BACKBUFFER[1][(y << 8) + x] = rgb;
touchPosition touchscreen; }
}
while (1) { // Swap backbuffer and visible buffers
NF_Flip16bitsBackBuffer(1);
// Copia el original // Wait for the screen refresh
NF_Copy16bitsBuffer(1, 1, 0); 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) {
square_x = (touchscreen.px - 32);
square_y = (touchscreen.py - 32);
}
// Calcula la ventana
if (square_x < 0) square_x = 0;
if (square_x > 191) square_x = 191;
if (square_y < 0) square_y = 0;
if (square_y > 127) square_y = 127;
// Rellena el buffer
for (y = square_y; y < (square_y + 64); y ++) {
for (x = square_x; x < (square_x + 64); x ++) {
// Obten el color actual
rgb = NF_16BITS_BACKBUFFER[1][((y << 8) + x)];
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;
b = composite;
// 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;
}
}
// Copia el contenido del Backbuffer a la VRAM
NF_Flip16bitsBackBuffer(1);
// Sincronismo Vertical
swiWaitForVBlank();
}
return 0;
return 0;
} }

View File

@ -15,6 +15,9 @@
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
// Set random seed based on the current time
srand(time(NULL));
// Prepare a NitroFS initialization screen // Prepare a NitroFS initialization screen
NF_Set2D(0, 0); NF_Set2D(0, 0);
NF_Set2D(1, 0); NF_Set2D(1, 0);
@ -27,7 +30,7 @@ int main(int argc, char **argv)
nitroFSInit(NULL); nitroFSInit(NULL);
NF_SetRootFolder("NITROFS"); NF_SetRootFolder("NITROFS");
// Inicialize 2D engine in both screens and use mode 0 // Initialize 2D engine in both screens and use mode 0
NF_Set2D(0, 0); NF_Set2D(0, 0);
NF_Set2D(1, 0); NF_Set2D(1, 0);
@ -61,15 +64,12 @@ int main(int argc, char **argv)
NF_CreateTiledBg(1, 2, "capa_2"); NF_CreateTiledBg(1, 2, "capa_2");
// Transfer the required sprites to VRAM // Transfer the required sprites to VRAM
NF_VramSpriteGfx(1, 0, 0, true); // Ball: Keep unused frames in RAM NF_VramSpriteGfx(1, 0, 0, true); // Ball: Keep all frames in VRAM
NF_VramSpritePal(1, 0, 0); NF_VramSpritePal(1, 0, 0);
NF_VramSpriteGfx(0, 1, 0, false); // Character: Keep unused frames in VRAM NF_VramSpriteGfx(0, 1, 0, false); // Character: Keep unused frames in RAM
NF_VramSpritePal(0, 1, 0); NF_VramSpritePal(0, 1, 0);
// Set random seed based on the current time
srand(time(NULL));
// Setup character sprite // Setup character sprite
s16 pj_x = 0; s16 pj_x = 0;
s16 pj_y = 127; s16 pj_y = 127;
@ -108,7 +108,7 @@ int main(int argc, char **argv)
NF_MoveSprite(1, 0, pj_x, pj_y); NF_MoveSprite(1, 0, pj_x, pj_y);
// Animate character // Animate character
pj_anim ++; pj_anim++;
if (pj_anim > 5) if (pj_anim > 5)
{ {
pj_anim = 0; pj_anim = 0;