newbie: one bg on bottom, one bg with text on top
Posted: Sat Dec 05, 2009 9:10 pm
I'm trying to write my first libnds program. I have converted a set of tiles and displayed them on the lower (main) screen. I have written on the top screen and now I want to also display a number of the same tiles on the top screen with the text at the same time. I can get the lower display to work, and I can get text on the upper display, but cannot display tiles as well. When I mess with the various values in the code I can get colored pixels on the screen, but they don't resemble any of my tiles. They do, however, move if I change the REG_BG1HOFS_SUB and REG_BG1VOFS_SUB registers.
P.S. much of the code here was taken from other tutorials. I've tried to piece something together.
P.S. much of the code here was taken from other tutorials. I've tried to piece something together.
Code: Select all
//-----------------------------------------------------------
#include <nds.h>
#include <nds/arm9/background.h> // needed for REG_BG0CNT
#include <stdlib.h>
#include <stdio.h>
#include "tilesMap_bin.h"
#include "tilesTiles_bin.h"
#include "tilesPal_bin.h"
static const int DMA_CHANNEL = 3;
static const int TILES_WIDTH = 28;
static const int TILES_HEIGHT = 16;
int main(void) {
int i;
int xoffset = 0;
int yoffset = 0;
int j, k;
int Hmove = 3;
int Vmove = 3;
int scrollY = 0;
int scrollX = 0;
int displayBg1Map = 15;
int displayBg1Tile = 2;
PrintConsole topScreen;
powerOn(POWER_ALL_2D);
lcdMainOnBottom(); // Place the main screen on the bottom physical screen
vramSetMainBanks(VRAM_A_MAIN_BG_0x06000000,
VRAM_B_MAIN_BG_0x06020000,
VRAM_C_SUB_BG_0x06200000,
VRAM_D_LCD);
//set video mode and map vram to the background
videoSetMode(MODE_5_2D | DISPLAY_BG0_ACTIVE | DISPLAY_BG2_ACTIVE);
videoSetModeSub(MODE_5_2D | DISPLAY_BG1_ACTIVE );
bgInit(0, BgType_Text8bpp, BgSize_T_512x512, 0, 1);
int bgs1 = bgInitSub(1, BgType_Text8bpp, BgSize_T_512x512, displayBg1Map, displayBg1Tile);
consoleInit(&topScreen, 0,BgType_Text4bpp, BgSize_T_256x256, 31, 0, false, true);
REG_BG0CNT = BG_64x64 | BG_COLOR_256 | BG_MAP_BASE(0) | BG_TILE_BASE(1);
REG_BG1CNT_SUB = BG_64x64 | BG_COLOR_256 | BG_MAP_BASE(displayBg1Map) | BG_TILE_BASE(displayBg1Tile);
u16* mapMemory = (u16*)SCREEN_BASE_BLOCK(0);
u16* mapMemorySub = bgGetGfxPtr(bgs1);
dmaCopyHalfWords(DMA_CHANNEL,
tilesTiles_bin,
(uint16 *)BG_TILE_RAM(1),
tilesTiles_bin_size);
dmaCopyHalfWords(DMA_CHANNEL,
tilesPal_bin,
(uint16 *)BG_PALETTE,
tilesPal_bin_size);
///////////////////////////////////////////////////////
dmaCopyHalfWords(DMA_CHANNEL,
tilesTiles_bin,
(uint16 *)bgGetGfxPtr(bgs1),
tilesTiles_bin_size);
if(1) {// turn off loop
for (k = 0; k < 4; k ++) { // one of each of 4 maps
for (i = 0; i < 32; i++) { //treat each row
for (j = 0; j < 32; j ++ ) { // treat each entry in each row (column)
xoffset = j & (TILES_WIDTH - 1);
yoffset = i & (TILES_HEIGHT - 1);
mapMemory[ (32 * i) + j + (k * 32 * 32)] = TILES_HEIGHT * TILES_WIDTH; //black tile
}
}
}
}
// lay out tiles on main screen (bottom)
for (i = 0; i < 32; i++) { //treat each row
for (j = 0; j < 32; j ++ ) { // treat each entry in each row (column)
if (j < TILES_WIDTH && i < TILES_HEIGHT)
mapMemory[ (32 * i) + j ] = j +( TILES_WIDTH * i);
else mapMemory[ (32 * i) + j ] = TILES_HEIGHT * TILES_WIDTH;
} // j block
}
// show first 10 tiles on top screen. -- Doesn't work.
for (i = 0; i < 10; i ++) {
mapMemorySub[i] = i;
}
consoleSelect(&topScreen);
consoleSetWindow(&topScreen, 10,10, 32,1);
while (1) {
scanKeys();
u16 keysPressed = keysHeld();
if(!(keysPressed & KEY_RIGHT))
{
scrollX -= Hmove;
}
if(!(keysPressed & KEY_LEFT))
{
scrollX += Hmove;
}
if(!(keysPressed & KEY_UP))
{
scrollY += Vmove;
}
if(!(keysPressed & KEY_DOWN))
{
scrollY -= Vmove;
}
REG_BG0HOFS = scrollX;
REG_BG0VOFS = scrollY;
REG_BG1HOFS_SUB = scrollX;
REG_BG1VOFS_SUB = scrollY;
swiWaitForVBlank();
consoleClear();
iprintf("hello");
}
return 0;
}