// SPDX-License-Identifier: CC0-1.0
//
// SPDX-FileContributor: Antonio Niño Díaz, 2024-2026

#include <nds.h>

// This is autogenerated from statue.png and statue.grit
#include "statue.h"
// This is autogenerated from forest_town.png and forest_town.grit
#include "forest_town.h"

int main(int argc, char *argv[])
{
    videoSetMode(MODE_0_2D);

    vramSetPrimaryBanks(VRAM_A_MAIN_BG, VRAM_B_MAIN_SPRITE, VRAM_C_LCD, VRAM_D_LCD);

    int bg = bgInitHidden(0, BgType_Text8bpp, BgSize_T_256x256, 0,1);

    memcpy(bgGetGfxPtr(bg), forest_townTiles, forest_townTilesLen);
    memcpy(bgGetMapPtr(bg), forest_townMap, forest_townMapLen);
    memcpy(BG_PALETTE, forest_townPal, forest_townPalLen);

    bgShow(bg);

    // Make the backgdrop color black. This is the color that gets drawn on the
    // screen if no background or sprite is in that part of the screen. This
    // value is stored in BG_PALETTE[0], so it must be set after copying the
    // background palette.
    setBackdropColor(RGB15(0, 0, 0));

    oamInit(&oamMain, SpriteMapping_1D_32, false);

    // Allocate space for the tiles and copy them there
    u16 *gfxMain = oamAllocateGfx(&oamMain, SpriteSize_64x64, SpriteColorFormat_256Color);
    memcpy(gfxMain, statueTiles, statueTilesLen);

    // No need to copy the palette, this sprite won't be displayed normally
    //memcpy(SPRITE_PALETTE, statuePal, statuePalLen);

    oamSet(&oamMain, 0,
           100, 50, // X, Y
           0, // Priority
           0, // Palette index (unused for window sprites)
           SpriteSize_64x64, SpriteColorFormat_256Color, // Size, format
           gfxMain,  // Graphics offset
           -1, // Affine index
           false, // Double size
           false, // Hide
           false, false, // H flip, V flip
           false); // Mosaic

    // Mark this sprite to be used as window mask
    oamSetBlendMode(&oamMain, 0, SpriteMode_Windowed);

    // Disable display of the background everywhere but in the parts highlighted
    // by the sprite.
    bgWindowEnable(bg, WINDOW_OBJ);
    bgWindowDisable(bg, WINDOW_OUT);
    windowEnable(WINDOW_OBJ);

    consoleDemoInit();

    printf("PAD:   Move sprite\n");
    printf("START: Exit to loader\n");

    int x = 50, y = 70;

    while (1)
    {
        swiWaitForVBlank();

        oamSetXY(&oamMain, 0, x, y);

        oamUpdate(&oamMain);

        scanKeys();

        u16 keys_held = keysHeld();

        if (keys_held & KEY_UP)
            y--;
        else if (keys_held & KEY_DOWN)
            y++;

        if (keys_held & KEY_LEFT)
            x--;
        else if (keys_held & KEY_RIGHT)
            x++;

        if (keys_held & KEY_START)
            break;
    }

    oamFreeGfx(&oamMain, gfxMain);

    return 0;
}
