diff --git a/examples/graphics/spriteaffine/Makefile b/examples/graphics/spriteaffine/Makefile new file mode 100644 index 0000000..ce63e7c --- /dev/null +++ b/examples/graphics/spriteaffine/Makefile @@ -0,0 +1 @@ +include ../../Makefile.example diff --git a/examples/graphics/spriteaffine/assets/ball.png b/examples/graphics/spriteaffine/assets/ball.png new file mode 100644 index 0000000..ae92aeb Binary files /dev/null and b/examples/graphics/spriteaffine/assets/ball.png differ diff --git a/examples/graphics/spriteaffine/assets/convert.sh b/examples/graphics/spriteaffine/assets/convert.sh new file mode 100644 index 0000000..f18f5eb --- /dev/null +++ b/examples/graphics/spriteaffine/assets/convert.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +BLOCKSDS="${BLOCKSDS:-/opt/blocksds/core/}" +GRIT=$BLOCKSDS/tools/grit/grit + +$GRIT ball.png -ftB -fh! -gTFF00FF -gt -gB8 -m! + +mv *.pal *.img ../nitrofiles/sprite diff --git a/examples/graphics/spriteaffine/nitrofiles/sprite/ball.img b/examples/graphics/spriteaffine/nitrofiles/sprite/ball.img new file mode 100644 index 0000000..4e18a50 Binary files /dev/null and b/examples/graphics/spriteaffine/nitrofiles/sprite/ball.img differ diff --git a/examples/graphics/spriteaffine/nitrofiles/sprite/ball.pal b/examples/graphics/spriteaffine/nitrofiles/sprite/ball.pal new file mode 100644 index 0000000..fc9cf6d Binary files /dev/null and b/examples/graphics/spriteaffine/nitrofiles/sprite/ball.pal differ diff --git a/examples/graphics/spriteaffine/source/main.c b/examples/graphics/spriteaffine/source/main.c new file mode 100644 index 0000000..15273ab --- /dev/null +++ b/examples/graphics/spriteaffine/source/main.c @@ -0,0 +1,109 @@ +// SPDX-License-Identifier: CC0-1.0 +// +// SPDX-FileContributor: NightFox & Co., 2009-2011 +// SPDX-FileContributor: Antonio Niño Díaz, 2025 +// +// Affine sprite loading example +// http://www.nightfoxandco.com + +#include +#include + +#include +#include + +#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); + + // Initialize sprite system + NF_InitSpriteBuffers(); // Initialize storage buffers + NF_InitSpriteSys(0); // Top screen + NF_InitSpriteSys(1); // Bottom screen + + NF_LoadSpriteGfx("sprite/ball", 0, 32, 32); + NF_LoadSpritePal("sprite/ball", 0); + + // Transfer the required sprites to VRAM + NF_VramSpriteGfx(1, 0, 0, true); + NF_VramSpritePal(1, 0, 0); + + // Setup ball sprites + NF_CreateSprite(1, 10, 0, 0, 16, 16); + NF_EnableSpriteRotScale(1, 10, 4, false); // rotation ID 4, no double size + + NF_CreateSprite(1, 20, 0, 0, 128, 16); + NF_EnableSpriteRotScale(1, 20, 4, true); // rotation ID 4, double size + + NF_CreateSprite(1, 30, 0, 0, 16, 96); + NF_EnableSpriteRotScale(1, 30, 5, false); // rotation ID 5, no double size + + NF_CreateSprite(1, 40, 0, 0, 128, 96); + NF_EnableSpriteRotScale(1, 40, 5, true); // rotation ID 5, double size + + int angle = 512; + int scale = 350; + + while (1) + { + scanKeys(); // Read keypad + u16 keys = keysHeld(); // Keys currently pressed + + // Rotate and scale sprites + + if (keys & KEY_LEFT) + angle += 16; + if (keys & KEY_RIGHT) + angle -= 16; + + angle &= 511; + + if (keys & KEY_A) + { + scale += 2; + if (scale > 512) + scale = 512; + } + if (keys & KEY_B) + { + scale -= 2; + if (scale < 0) + scale = 0; + } + + NF_SpriteRotScale(1, 4, angle, scale, scale); + NF_SpriteRotScale(1, 5, -angle, scale, scale); + + // Update OAM array + NF_SpriteOamSet(0); + NF_SpriteOamSet(1); + + // Wait for the screen refresh + swiWaitForVBlank(); + + // Update OAM + oamUpdate(&oamMain); + oamUpdate(&oamSub); + } + + return 0; +}