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 > graphics problem 2. (should be an answer...sorry)

#152312 - ben0bi - Thu Mar 13, 2008 6:36 pm

ok i fixed this issue. thanks for the help.
now i got another problem (im really new to DS as you might see. ;) ):

i want to setup an 8bit screen to save memory.

works with images but now i want to draw pixels. i set bg_palette 255 to my color and try this:

Code:

u8* mybuffer=(u8*) BG_BMP_RAM(0);
mybuffer[0]=255;
mybuffer[1]=255;


that does not work on the machine (emulator works..). WHY?

it works if i use:

Code:

u16* mybuffer=(u16*) BG_BMP_RAM(0);
mybuffer[0]=255 + 255<<8; // <<8 = second pixel
//mybuffer[1]=255;


but what is the difference? i don't get it....i mean, 8bit is half a 16bit. but why can i not assign the space directly with u8?

does anyone have a clue about this?

#152316 - Cearn - Thu Mar 13, 2008 7:20 pm

ben0bi wrote:
but what is the difference? i don't get it....i mean, 8bit is half a 16bit. but why can i not assign the space directly with u8?

It's just one of those little quirks of GBA/NDS hardware. VRAM (and palette RAM and OAM) can only be written to in 16- or 32-bit chunks, not bytes. If you try, the byte will be duplicated. If you want a 8-bit plotter, use something go here. Technically it's for GBA, but the principles are the same.

ben0bi wrote:
Code:
mybuffer[0]=255 + 255<<8; // <<8 = second pixel

Remember that addition has a higher operator precedence than shifts. This actually writes 510<<8 (which is then cut down to 254<<8 to keep it inside 16 bits)

#152353 - ben0bi - Fri Mar 14, 2008 1:11 am

thanks for the explanation.

i now use this formula:

Code:

buffer[pos] = rightColor | (leftColor<<8);


and to determine the position of the pixel i use this:

Code:

#define POSFROMXY(x,y) (x + y*SCREEN_WIDTH)

u16 pos=POSFROMXY(x,y);

int mod=pos%2;
if(pos!=0)
   pos=(pos-mod)/2;

if(mod==1)
   buffer[pos]=color |  (buffer[pos] & 0xff00);
else
   buffer[pos]=(color<<8) | (buffer[pos] & 0x00ff);

#152377 - nce - Fri Mar 14, 2008 4:22 pm

looks good :)

but you could have something a little faster with this ( but yes the compiler may optimise this for you anyway.... )

if SCREEN_WIDTH = 256 you can have :

#define POSFROMXY(x,y) (x + (y<<8))

and

int mod = pos & 1;
pos >>= 1; // no real need for a test with 0 here ...

and if you divide by 2 it will get rid of the first bit anyway so no need to do (pos-mod)/2. pos/2 gives you the same answer...
_________________
-jerome-

#152400 - ben0bi - Sat Mar 15, 2008 1:02 am

yes thanx.

soon i will post my very first ds game. it only uses one double-buffered background in 8bit on each screen because graphics setup is a little but to complicated for me at this time.