Page 1 of 1

tiled bg and console problem SOLVED!!!

Posted: Fri Nov 12, 2010 6:35 pm
by ex-peluo
Hi all:
I'm trying to put two tiled bg and print over them in the main screen. I have a my own library that loads two 8bpp bitmap files. Their size is 256x256. With my library, I take the map and the tiles of each image, and compress tileset. After load and compress images I have these arrays:

maps:
u16 back_map[1024];
u16 front_map[1024];

tileset
u8 back_tiles[1600];
u8 front_tiles[1600];

As you can see, tiles array sizes are 1600 instead 65536, because with my library i take out repeated tiles. I have the palettes too. To put all of this in the screen, i do this:

Code: Select all

	videoSetMode(MODE_0_2D);
	videoSetModeSub(MODE_0_2D);

	vramSetBankA(VRAM_A_MAIN_BG);
	vramSetBankB(VRAM_B_MAIN_BG);

	int bg = bgInit(0, BgType_Text8bpp, BgSize_T_256x256, 1,0);
	int bg2 = bgInit(1, BgType_Text8bpp, BgSize_T_256x256, 2,1);
	
	copy_memory(back_tiles, bgGetGfxPtr(bg), 1600); //tiles for back bg
	copy_memory(front_tiles, bgGetGfxPtr(bg2), 1600);//tiles for front bg
	copy_memory(back_map,bgGetMapPtr(bg), 2048); //map for back bg
	copy_memory(front_map, bgGetMapPtr(bg2), 2048);//map for back bg
	copy_memory(palette, BG_PALETTE, 512); //palette
*copy_memory(void *o, void *d, int size) is a function made by me that copy "size" blocks of bytes from "o" to "d".
As you can see, i assign to bg the first tile base, but it not be completely filled, only the first 1.5kb, so i have map base 1,2,3...7 free. I use them to allocate my two maps and save memory. The second tileset is allocated in the second tile base, but i only need the first 1.5kb, rest is free.
When i update bg all is shown in the screen fine. Now i want to print text over these bg, and I use the consoleInit function. This function needs a map index and tile index. My problem is what indexes must i use?. I've tried a lot of combinations. I guess that I need 1 map base for the 256x256, and 1 tile base for the characters. But i have it not clear. I found a lot of information about that but i don't understand all well. When I finish this, I want to put a custom font.

Thanks and sorry for my english

Re: tiled bg and consope problem

Posted: Fri Nov 12, 2010 7:05 pm
by zeromus
don't guess. keep trying to understand this.

Code: Select all

here is what you've got:

     tiles:
     00000000 11111111 22222222

     maps:             11111111 
     01234567 89ABCDEF 01234567
bg : TM...... ........ ........ (map at 1 and tile at 0)
bg2: ..M..... T....... ........ (map at 2 and tile at 1)

so this should work for the console:
con: ...M.... ........ T???.... (map at 3 and tile at 2.. how big are the console tiles? dunno)

Re: tiled bg and console problem

Posted: Fri Nov 12, 2010 7:17 pm
by ex-peluo
That was that i tried first, but no text is shown. May be the problem is in another part of the code. I use mode 02D and put the bg in layers 0 and 1. Text is in layer 3. In mode 0 there are 4 layers of tiled bg, so that's not the problem. Before print text, I use the consoleSelect(console); function to select the layer. After that i print "hello word", update bg and swiWaitForVBlank();. I tried to change bgs priorities too. But tiled bg are shown and text not. I'm a bit lost.

Re: tiled bg and console problem

Posted: Fri Nov 12, 2010 11:32 pm
by zeromus
I presume you made the text layer visible?

Your question is too detailed to answer with generalities. post your project and im 100% sure the problem can be solved.

Re: tiled bg and console problem

Posted: Sat Nov 13, 2010 12:48 am
by elhobbs
running your project in the emulator desmume can help. It has tools to view map, tiles, and layers indepently. you can then see if the problem is visibilty, layering, or vram layout.

Re: tiled bg and console problem

Posted: Sat Nov 13, 2010 1:20 am
by ex-peluo
I cannot use an emulator because I load the .bmp files. I've read about emulate fat, but I use linux, and the document I read was for windows. The tiled backgrouds appear nice in the screen. May be I don't made text layer visible as zeromus said. Here I post my code. I don't post the .bmp loader code because is a class and there is a lot of code.

Code: Select all

	//video mode
	videoSetMode(MODE_0_2D);
	videoSetModeSub(MODE_0_2D);

	vramSetBankA(VRAM_A_MAIN_BG);
	vramSetBankB(VRAM_B_MAIN_BG);

	int bg = bgInit(0, BgType_Text8bpp, BgSize_T_256x256, 1,0);
	int bg2 = bgInit(1, BgType_Text8bpp, BgSize_T_256x256, 2,1);
	
	copy_memory(back_tiles, bgGetGfxPtr(bg), 1600);
	copy_memory(back_map,bgGetMapPtr(bg), 2048); 
	copy_memory(palette, BG_PALETTE, 512);

	copy_memory(front_tiles, bgGetGfxPtr(bg2), 1600);
	copy_memory(front_map, bgGetMapPtr(bg2), 2048); 

	PrintConsole console;
	consoleInit(&console, 2, BgType_Text4bpp, BgSize_T_256x256, 3, 2, true, false);
	int	bg3 = console.bgId;

	//Priorities
	bgSetPriority(bg, 1);
	bgSetPriority(bg2, 0);
	bgSetPriority(bg3, 2);

	consoleSelect(&console);

	iprintf("Hello Word\n");
	u8 count = 0;
	u8 a,b,c;
	a = b = c = 0;
	while(1){
		if ( (count/30) == 0){
			a = 0;
			b = 1;
			c = 2;
		}else if ( (count/30) == 1){
			a = 2;
			b = 0;
			c = 1;
		}else if ( (count/30) == 2){
			a = 1;
			b = 2;
			c = 0;
		}else if ( (count/30) > 3){
			count = 0;
		}
		bgSetPriority(bg, a);
		bgSetPriority(bg2, b);
		bgSetPriority(bg3, c);
		swiWaitForVBlank();
		bgUpdate();
		count++;
	}
First time I try it i didn't see the text, so I use the while loop to alternate the bgs priorities because i thought that the text was under the other bgs.

If I load a custom font and after "consoleInit" I use:

Code: Select all

	ConsoleFont font;
	font.gfx = (u16*)ascii_tiles;
	font.pal = (u16*)back_palette;
	font.numChars = 95;
	font.numColors =  256;
	font.bpp = 8;
	font.asciiOffset = 32;
	font.convertSingleColor = false;
	
	consoleSetFont(&console, &font);
Of course, in consoleInit, y change Text4bpp for Text8bpp, my font has 256 colors
Nds show the three bg correctly. So, why it did'nt work without custom font?.

Re: tiled bg and console problem

Posted: Sat Nov 13, 2010 2:13 am
by zeromus
perhaps loading the font loads the palette that is necessary for the font, and until then, the console is going to be using the font which you loaded for your BGs which may be inappropriate for the font and text consoles.

it is a shame you can't use an emulator, even in wine. that really impairs your debugging capability.

if you get the latest desmume svn and build it i assure you that issuing --cflash-path=/path/to/directory/with/files then desmume will be able to get to the bmps.

Re: tiled bg and console problem

Posted: Sun Nov 14, 2010 10:18 pm
by ex-peluo
In the console examples, the function used is "consoleDemoInit". I read console.c from libraries and it uses the consoleInit like me. But there is no place where palette is used. I'm confused about it. May be my palette shows white color as black.

Re: tiled bg and console problem

Posted: Sun Nov 14, 2010 10:34 pm
by zeromus
look harder. consoleInit uses consoleLoadFont touches the palette

Re: tiled bg and console problem

Posted: Mon Nov 15, 2010 12:26 am
by ex-peluo
Thanks man, you solved it!!!. Looking harder in this file, y understand something basic:
when i use consoleInit, I must send a null pointer as *console, so libnds uses the default console. And the las parameter must be true, to tell libds i don't use custom font and I want to use system font. the result call to this function is like this:

Code: Select all

	PrintConsole *console;
	console = consoleInit(NULL, 2, BgType_Text4bpp, BgSize_T_256x256, 3, 2, true, true);
	int	bg3 = console->bgId;
NULL=>Null pointer indicates libnds that I want to use default console
2=>Layer to use
BgType_Text4bpp => 16 colors
BgSize_T_256x256 => I only write in a 256X256 layer
3,2=>place to put map and tiles from font in the Vram
true=>main screen
true=>Use system graphics for the font

that's all. Now all is well shown in the screen. Thanks a lot!!!