Homebrew Understanding libdns oamAllocateGfx

BluFox

New Member
OP
Newbie
Joined
Jan 18, 2023
Messages
2
Trophies
0
Age
44
XP
42
Country
United States
Hey all, I'm getting started with libdns and the sprites system. Trying to get a good understanding on what `oamAllocateGfx` does. My understanding is that the DS has memory banks which the sprite system / oam is able to reference for drawing sprites. And that you're options are to either dma copy a whole sprite sheet in to the bank, or you can use `oamAllocateGfx` which acts like a kind of memory manager like malloc to load and unload sprites. From what I can tell, the data returned from this function is a pointer to the vram memory we can copy in to. I put together a little test program but I'm getting unexpected results.

C++:
#include <nds.h>
#include <stdio.h>

int main()
{

    videoSetModeSub(MODE_0_2D);

    consoleDemoInit();

    // initialize the sub sprite engine with 1D mapping 128 byte boundary
    // and no external palette support
    oamInit(&oamSub, SpriteMapping_Bmp_1D_128, false);

    vramSetBankD(VRAM_D_SUB_SPRITE);

    u16 *gfx = oamAllocateGfx(&oamSub, SpriteSize_32x32, SpriteColorFormat_Bmp);
    printf("pointer1 %hu\n", &gfx);
    u16 *gfx2 = oamAllocateGfx(&oamSub, SpriteSize_32x32, SpriteColorFormat_Bmp);
    printf("pointer2 %hu\n", &gfx2);

    u16 *gfx3 = oamAllocateGfx(&oamSub, SpriteSize_32x32, SpriteColorFormat_Bmp);
    printf("pointer3 %hu\n", &gfx3);

    u16 *gfx4 = oamAllocateGfx(&oamSub, SpriteSize_32x32, SpriteColorFormat_Bmp);
    printf("pointer4 %hu\n", &gfx4);

    while (1)
    {
    }

    swiWaitForVBlank();
}

This allocates a few times and prints the addresses. What confuses me is the addresses are only 4 apart , the color format and size don't change the addresses printed which I'd expect since they would need more space. Is my understanding of this correct? Should I expect the printed addresses to be spaced far enough apart that the sprite fits in the addresses between them? Also since there are multiple vram banks, how does the allocate function know which one we want to allocate to?
 

Pk11

A catgirl with a DSi
Member
Joined
Jun 26, 2019
Messages
1,285
Trophies
1
Age
22
Location
米国
Website
xn--rck9c.xn--tckwe
XP
3,912
Country
United States
You're printing the addresses of your pointers, not their content. Using the fixed code below they're starting at 0x06600000 (the start of sub engine OAM memory) and increasing in increments of 0x800 with this size. You decide which bank is used by which bank you map at the start of OBJ VRAM, that's what mapping does. If you map multiple to the same place it'll be in all of them. (they get OR'd when mapping)

Code:
#include <nds.h>
#include <stdio.h>

int main()
{

    videoSetModeSub(MODE_0_2D);

    consoleDemoInit();

    // initialize the sub sprite engine with 1D mapping 128 byte boundary
    // and no external palette support
    oamInit(&oamSub, SpriteMapping_Bmp_1D_128, false);

    vramSetBankD(VRAM_D_SUB_SPRITE);

    u16 *gfx = oamAllocateGfx(&oamSub, SpriteSize_32x32, SpriteColorFormat_Bmp);
    printf("pointer1 %p\n", gfx); // Alternatively 0x%lx\n" (u32)gfx -- same thing
    u16 *gfx2 = oamAllocateGfx(&oamSub, SpriteSize_32x32, SpriteColorFormat_Bmp);
    printf("pointer2 %p\n", gfx2);

    u16 *gfx3 = oamAllocateGfx(&oamSub, SpriteSize_32x32, SpriteColorFormat_Bmp);
    printf("pointer3 %p\n", gfx3);

    u16 *gfx4 = oamAllocateGfx(&oamSub, SpriteSize_32x32, SpriteColorFormat_Bmp);
    printf("pointer4 %p\n", gfx4);

    while (1)
    {
    }

    swiWaitForVBlank();
}
[CODE]
[/SPOILER]
 

Site & Scene News

Popular threads in this forum

General chit-chat
Help Users
  • BakerMan
    I rather enjoy a life of taking it easy. I haven't reached that life yet though.
  • Xdqwerty @ Xdqwerty:
    updated ship of harkinian, gonna install some hd texture pack
  • Xdqwerty @ Xdqwerty:
    I might download rayman revolution for my ps3
  • BigOnYa @ BigOnYa:
    I may try the new ram site, and download more RAM to my Switch. Not sure if ddr3 is the right ram
    for it tho. Edit- no it uses floppy Ram, just like @AncientBoi
    +1
  • Xdqwerty @ Xdqwerty:
    aeiou
  • BigOnYa @ BigOnYa:
    And sometimes Z
  • SylverReZ @ SylverReZ:
    @K3Nv2, MAGA supporters be wearing tin foil hats lol.
    +1
  • Xdqwerty @ Xdqwerty:
    @SylverReZ, whats maga?
  • BigOnYa @ BigOnYa:
    It stands for Maniacs Against General Acceptance
    +1
  • Xdqwerty @ Xdqwerty:
    @BigOnYa, people rejecting general consensus about stuff?
    +1
  • BigOnYa @ BigOnYa:
    Yup, nuh its really just Trump followers
  • Xdqwerty @ Xdqwerty:
    @BigOnYa, im not american so i dont care about trump
    +1
  • Xdqwerty @ Xdqwerty:
    or us elections
  • BigOnYa @ BigOnYa:
    Me niether, us north Koreans don't care
  • Xdqwerty @ Xdqwerty:
    good night
  • BakerMan @ BakerMan:
    i don't care either, even if i'm american
  • BakerMan @ BakerMan:
    truth be told, i agree with psi, i dislike both candidates, but i'd probably vote trump simply because the economy was better during his presidency
    +1
  • AngryCinnabon @ AngryCinnabon:
    Just be careful, if trump ends up winning and using project 2025 America might really change...for the worse.
  • AngryCinnabon @ AngryCinnabon:
    I'm not american and even that sends shivers down my spine.
  • AngryCinnabon @ AngryCinnabon:
    anything that offers trump an opportunity to become an actual dictator
    is bad in my book, i could care less if it wasn't for that...
  • K3Nv2 @ K3Nv2:
    Canada: America's Russia
  • NinStar @ NinStar:
    people are so dramatic that I can't even tell if they are being serious
  • Psionic Roshambo @ Psionic Roshambo:
    Why so serious!
  • Psionic Roshambo @ Psionic Roshambo:
    @BakerMan, yeah that's about the only reason I would vote for Trump over Biden.
    +1
  • Psionic Roshambo @ Psionic Roshambo:
    In my opinion on all other factors they are pretty much the same.
    +1
    Psionic Roshambo @ Psionic Roshambo: In my opinion on all other factors they are pretty much the same. +1