Allocating Graphics from OAM

Post Reply
Altik_0
Posts: 1
Joined: Mon Jun 25, 2012 5:22 am

Allocating Graphics from OAM

Post by Altik_0 » Mon Jun 25, 2012 5:35 am

I'm having a bit of difficulty getting sprites to be allocated to me by the oamAllocateGfx function - or, rather, getting pointers into VRAM offsets. Currently I have a function that is supposed to setup the sprites in VRAM so I can use them later, but not necessarily put them into sprites - that would be the idea, anyway:

Code: Select all

void setupToolSprites()
{
	for(int i = 0; i < NUM_TOOLTYPES; ++i)
	{
		toolGfxLoc[i] = oamAllocateGfx(&oamSub, SpriteSize_32x32, SpriteColorFormat_16Color);
	}

	// Copy sprites into VRAM
	dmaCopyHalfWords(3, Cursor_ToolTiles,	 toolGfxLoc[0], Cursor_ToolTilesLen);
	dmaCopyHalfWords(3, Marquee_ToolTiles,	 toolGfxLoc[1], Marquee_ToolTilesLen);
	dmaCopyHalfWords(3, Brush_ToolTiles,	 toolGfxLoc[2], Brush_ToolTilesLen);
	dmaCopyHalfWords(3, Bucket_ToolTiles,	 toolGfxLoc[3], Bucket_ToolTilesLen);
	dmaCopyHalfWords(3, Gradient_ToolTiles,	 toolGfxLoc[4], Gradient_ToolTilesLen);
	dmaCopyHalfWords(3, Rectangle_ToolTiles, toolGfxLoc[5], Rectangle_ToolTilesLen);
}
toolGfxLoc is a global array of pointers designed to keep track of the graphics' addresses for use in the oamSet() function later. From what I can tell, though, oamAllocateGfx is returning the same address every time. I've checked VRAM in DeSmuME's tile viewer, and only one sprite's tiles is displaying - comment out the last line, and it is the one previous, and so on.

I've been able to get sprites to display by calling oamAllocateGfx, dmaCopyHalfWords, and oamSet in that order, so my guess is that somewhere in the oamSet function, the OamState gets updated to know that it ought to proceed to the next offset. If this is the case, how should I go about setting up my sprites into memory if I don't want them assigned to a sprite in OAM immediately? Or is that simply not an option with libnds's current implementation?

mtheall
Posts: 210
Joined: Thu Feb 03, 2011 10:47 pm

Re: Allocating Graphics from OAM

Post by mtheall » Mon Jun 25, 2012 3:33 pm

You need to provide more info in order to solve your issue. Are you using oamInit()? If so, can you provide that line of code? Can you provide the list of addresses in toolGfxLoc after the for loop is completed?

My code would look something like this for debugging:

Code: Select all

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

u16 *toolGfxLoc[NUM_TOOLTYPES];

void setupToolSprites()
{
   for(int i = 0; i < NUM_TOOLTYPES; ++i)
   {
      toolGfxLoc[i] = oamAllocateGfx(&oamSub, SpriteSize_32x32, SpriteColorFormat_16Color);
      fprintf(stderr, "toolGfxLoc[%d] = %p\n", i, toolGfxLoc[i]); // print to desmume console window
   }

   // Copy sprites into VRAM
   dmaCopyHalfWords(3, Cursor_ToolTiles,    toolGfxLoc[0], Cursor_ToolTilesLen);
   dmaCopyHalfWords(3, Marquee_ToolTiles,   toolGfxLoc[1], Marquee_ToolTilesLen);
   dmaCopyHalfWords(3, Brush_ToolTiles,     toolGfxLoc[2], Brush_ToolTilesLen);
   dmaCopyHalfWords(3, Bucket_ToolTiles,    toolGfxLoc[3], Bucket_ToolTilesLen);
   dmaCopyHalfWords(3, Gradient_ToolTiles,  toolGfxLoc[4], Gradient_ToolTilesLen);
   dmaCopyHalfWords(3, Rectangle_ToolTiles, toolGfxLoc[5], Rectangle_ToolTilesLen);
}

int main(int argc, char *argv[]) {
  // initialize video modes
  videoSetMode(MODE_0_2D);
  videoSetModeSub(MODE_0_2D);

  // initialize the primary vram banks
  vramSetPrimaryBanks(VRAM_A_MAIN_BG, VRAM_B_MAIN_SPRITE, VRAM_C_SUB_BG, VRAM_D_SUB_SPRITE);

  // initialize the debug console (goes to desmume console window)
  consoleDebugInit(DebugDevice_NOCASH);

  // initialize the OAM
  oamInit(&oamMain, SpriteMapping_1D_128, false);

  // copy the sprite palette
  PUT YOUR SPRITE PALETTE FILLING CODE HERE

  // initialize the tool sprites
  setupToolSprites();

  // place the sprites somewhere
  for(int i = 0; i < NUM_TOOLTYPES; ++i)
    oamSet(&oamMain, // OAM
           i,        // index
           (i%8)*32, // x position
           (i/8)*32, // y position
           0,        // priority
           i,        // palette number
           SpriteSize_32x32,          // sprite size
           SpriteColorFormat_16Color, // sprite color format
           toolGfxLoc[i],             // gfx pointer
           -1,       // affine index
           false,    // double size
           false,    // hide
           false,    // horizontal flip
           false,    // vertical flip
           false);   // mosaic effect

  fprintf(stderr, "Complete\n"); // print to desmume console window

  while(1)
    swiWaitForVBlank();

  return 0;
}

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 7 guests