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

// The main engine of the DS has a special mode that lets you display the
// contents of any of the main VRAM banks (A, B, C or D) as a 16-bit bitmap on
// the screen without any other setup required.
//
// This can be useful if you're using video capture (the destination of the
// video capture hardware needs to be in LCD mode).
//
// However, if you aren't using video capture, this mode is worse than regular
// video modes because you can only display a single 16-bit background that
// can't even be scrolled. You can't display any other background layer or
// sprites.

#include <nds.h>

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

int main(int argc, char *argv[])
{
    // MODE_VRAM_B tells the main engine to display the contents of VRAM_B as a
    // 16-bit background (and VRAM_B must be in LCD mode).
    vramSetBankB(VRAM_B_LCD);
    videoSetMode(MODE_VRAM_B);

    // Show the demo console in the bottom screen
    consoleDemoInit();

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

    int x = 20, y = 30;

    while (1)
    {
        swiWaitForVBlank();

        // Draw scene. Copy background image and draw a square on top of it.
        uint16_t *buffer = VRAM_B;

        memcpy(buffer, photoBitmap, photoBitmapLen);

        for (int j = y; j < 50 + y; j++)
        {
            for (int i = x; i < 50 + x; i++)
            {
                // Invert color, but ignore the top bit.
                buffer[256 * j + i] ^= 0x7FFF;
            }
        }

        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;
    }

    return 0;
}
