gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

DS development > Displaying sprites above a bgd

#108977 - smoggie83 - Mon Nov 13, 2006 7:00 pm

Hi

I am a newbie to ds development and have been trying to display a sprite above a bgd. What is necessary in the code below to do this as i cant find any tutorials. I basically want my graphic backArrow displayed above the menu graphic.

Code:

#include <nds.h>

#include <nds/arm9/math.h>
#include <nds/arm9/console.h>
#include <nds/arm9/trig_lut.h>
#include <stdio.h>

//make file automaticaly makes a header file for access
//to the binary data in any file ending in .bin that is in
//the data folder.  It also links in that data to your project

SpriteEntry OAMCopy[128];

#include "drunkenlogo_bin.h"
#include "palette_bin.h"

#include "backArrow_bin.h"
#include "backArrowpalette_bin.h"

#include "gameBoard_bin.h"
#include "gameBoardPalette_bin.h"

#include "menuTopScreen_bin.h"
#include "menuTopScreenPalette_bin.h"

#include "options_bin.h"
#include "optionsPalette_bin.h"

#include "diceRoll_bin.h"
#include "diceRollPalette_bin.h"

#include "players_bin.h"
#include "playersPalette_bin.h"

enum { CONTINUOUS, SINGLE } TouchType = SINGLE;
enum { MAINMENU, OPTIONS, HOWTO, GAME } GameState = MAINMENU;

volatile int frame = 0;

//----------------------------------------------------------------------------------
typedef struct selectionBoundaries{
//----------------------------------------------------------------------------------
   int minX,minY,maxX,maxY;
} tSelectionBoundaries;

//---------------------------------------------------------------------------------
void Vblank() {
//---------------------------------------------------------------------------------
   frame++;
}


void initVideo() {
   //enable vram and map it to the right places
    vramSetMainBanks(   VRAM_A_MAIN_BG_0x6000000,      //map A and B to main background memory
                        VRAM_B_MAIN_BG_0x6020000,
                        VRAM_C_SUB_BG_0x6200000,      //map C to sub background memory
                        VRAM_D_LCD                  //map D to LCD free space
                  //allows adjacent banks to overflow into D if a bug like that was ever to occur
                        );
   
   //map a bank for use with sprites
   vramSetBankE(VRAM_E_MAIN_SPRITE); //mapping E to main sprites gives us 64k for sprites
   //(64k is the max space that 1024 tiles take up in 256 color mode)
   
   vramSetBankE(VRAM_D_SUB_SPRITE);
   
   //set the video mode
    videoSetMode(  MODE_5_2D | DISPLAY_SPR_ACTIVE | DISPLAY_BG0_ACTIVE |   
                   DISPLAY_BG3_ACTIVE |    //turn on background 3
               DISPLAY_SPR_1D
               );
   
   videoSetModeSub(MODE_5_2D | DISPLAY_BG3_ACTIVE | DISPLAY_SPR_1D  );
}


void initBackgrounds() {

   //setup exrot bg 3 on main as a 16bit color background
   BG3_CR = BG_BMP8_256x256 | BG_BMP_BASE(8) | BG_PRIORITY(1);
   //attributes of the affine translation matrix
   BG3_XDX = 1 << 8; //scale x
   BG3_XDY = 0; //rotation x
   BG3_YDX = 0; //rotation y
   BG3_YDY = 1 << 8; //scale y
   BG3_CX = 0; //translation x
   BG3_CY = 0; //translation y
   
   
   BG0_CR = BG_MAP_BASE(31) | BG_PRIORITY(0); //use bg0 for the text 
   
   //setup exrot bg 3 on sub
   SUB_BG3_CR = BG_BMP8_256x256 | BG_BMP_BASE(0) | BG_PRIORITY(0) ;
   //attributes of the affine translation matrix
   SUB_BG3_XDX = 1 << 8; //scale x
   SUB_BG3_XDY = 0; //rotation x
   SUB_BG3_YDX = 0; //scale y
   SUB_BG3_YDY = 1 << 8; //scale y
   SUB_BG3_CX = 0; //translation x
   SUB_BG3_CY = 0; //translation y
}

void showMainMenu(void){
   //Top Screen
   dmaCopy(menuTopScreen_bin, (uint16 *)BG_BMP_RAM_SUB(0), 256*192);
   dmaCopy(menuTopScreenPalette_bin, BG_PALETTE_SUB, 256*2);
   
   //Lower Screen Menu
   dmaCopy(drunkenlogo_bin, (void*)BG_BMP_RAM(8), 256*192);
   dmaCopy(palette_bin, BG_PALETTE, 256*2);
}

void showOptionsMenu(void){
   dmaCopy(options_bin, (void*)BG_BMP_RAM(8), 256*192);
   dmaCopy(optionsPalette_bin, BG_PALETTE, 256*2);
}

bool touchPosBetween(int posMinX,int posMaxX,int posMinY, int posMaxY, touchPosition touch){
   if ( touch.px > posMinX && touch.px < posMaxX){      
      if ( touch.py > posMinY && touch.py < posMaxY){
         return true;
      }
   }

   return false;
}

void checkSelectedOptionsMainMenu(touchPosition touch){
   tSelectionBoundaries option1Bounds;
   tSelectionBoundaries option2Bounds;
   tSelectionBoundaries option3Bounds;
   
   option1Bounds.minX = 32;
   option1Bounds.maxX = 232;
   option1Bounds.minY = 80;
   option1Bounds.maxY = 110;
   
   option2Bounds.minX = 32;
   option2Bounds.maxX = 232;
   option2Bounds.minY = 117;
   option2Bounds.maxY = 147;
   
   option3Bounds.minX = 32;
   option3Bounds.maxX = 232;
   option3Bounds.minY = 156;
   option3Bounds.maxY = 186;

   if ( touchPosBetween(option1Bounds.minX,option1Bounds.maxX,option1Bounds.minY,option1Bounds.maxY,touch) )
   {
      GameState = GAME;
   }else
   if ( touchPosBetween(option2Bounds.minX,option2Bounds.maxX,option2Bounds.minY,option2Bounds.maxY,touch) )
   {
      GameState = HOWTO;
   }else
   if ( touchPosBetween(option3Bounds.minX,option3Bounds.maxX,option3Bounds.minY,option3Bounds.maxY,touch) )
   {
      GameState = OPTIONS;
      showOptionsMenu();
   }

}

int main(void) {
//---------------------------------------------------------------------------------
   int min_x  = 4096 , min_y  = 4096, max_x  = 0, max_y   = 0;
   int min_px = 4096 , min_py = 4096, max_px = 0 , max_py = 0;
   touchPosition touch;

   powerON(POWER_ALL);
   
   //initialise the interrupt system
   irqInit();
   //install our simple vblank handler
   irqSet(IRQ_VBLANK, Vblank);
   //enable the interrupt
   irqEnable(IRQ_VBLANK);
   
   //set the mode for 2 text layers and two extended background layers
   initVideo();

   ///////////////set up our bitmap background///////////////////////
   initBackgrounds();
      
   //consoleInit() is a lot more flexible but this gets you up and running quick
   consoleInitDefault((u16*)SCREEN_BASE_BLOCK(31), (u16*)CHAR_BASE_BLOCK(0), 16);

   BG_PALETTE[255] = RGB15(31,31,31);//by default font rendered with color 255
   
   showMainMenu();
         
   lcdMainOnBottom();
   while(1) {
      swiWaitForVBlank();
      
      // read the button states
      scanKeys();

      // read the touchscreen coordinates
      touch=touchReadXY();
      
      int pressed = keysDown();   // buttons pressed this loop
      int held = keysHeld();      // buttons currently held

      // Right Shoulder button toggles the mode
      if ( pressed & KEY_R) TouchType ^= SINGLE;
      
         iprintf("\x1b[14;4HTouch mode: %s",TouchType==CONTINUOUS?"CONTINUOUS ":"SINGLE SHOT");
         
         if ( TouchType == SINGLE && !(pressed & KEY_TOUCH) ) continue;
   
         if ( !(held & KEY_TOUCH) || touch.x == 0 || touch.y == 0) continue;
         
         iprintf("\x1b[10;10H(%d,%d)      ",touch.px,touch.py);
         
         if ( touch.x > max_x)      max_x = touch.x;
         if ( touch.y > max_y)      max_y = touch.y;
         if ( touch.px > max_px)   max_px = touch.px;
         if ( touch.py > max_py)   max_py = touch.py;
   
         if ( touch.x < min_x)      min_x = touch.x;
         if ( touch.y < min_y)      min_y = touch.y;
         if ( touch.px < min_px)   min_px = touch.px;
         if ( touch.py < min_py)   min_py = touch.py;
         
         if (GameState == MAINMENU){
            checkSelectedOptionsMainMenu(touch);
         }
         iprintf("\x1b[9;10HGameState %d\n", GameState);
         
         OAMCopy[0].attribute[2] = 0;
         OAMCopy[0].attribute[1] = ATTR1_SIZE_32 |((touch.px - 16) & 0x01FF);
         OAMCopy[0].attribute[0] = ATTR0_COLOR_256 | ATTR0_SQUARE | ((touch.py -16) & 0x00FF);
   }

   return 0;
}




Cheers

Dave

#108985 - Dark Knight ez - Mon Nov 13, 2006 9:25 pm

You still need to load in the sprite graphics.
Loading it should be done to SPRITE_GFX, in about the same way as you did with loading in background graphics.
_________________
AmplituDS website

#108989 - Lick - Mon Nov 13, 2006 9:54 pm

You don't copy the OAMCopy back to the actual OAM?
_________________
http://licklick.wordpress.com