Page 1 of 1

Need Help Understanding 2d Example

Posted: Sun Jan 18, 2009 6:03 am
by olwildcory723
Hi all. I'm going through all the examples in attempt to comment everything so its understandable to a newbie like me. I'm starting in the graphics, 2d examples. In the 256_color_bmp example, I need some clarification. (BTW I do try to find these answers before I post and I could not).

Ok. So here my newbieness shows. Here is the code in question:

Code: Select all

dmaCopy(drunkenlogoBitmap, 	//Source - from grit header
        bgGetGfxPtr(bg3),     //Destination (function points to memory allocation of bg3
        256*256);             //size -- still don't understand what 256*256 is

dmaCopy(drunkenlogoPal,       //Source - from grit header
        BG_PALETTE,           //Destination, BG_PALETTE refers to something, can't find exactly
        256*2);               //size -- still don't understand what 256*2 is
I get that the dmaCopy is loading the bitmap image into the right memory place, and that the palette is also loaded into the memory via the DMA (for speed) rather than some loop or something. I just don't understand the details here. So you can see in my notes where I'm confused. I couldn't find anything about BG_PALETTE, except I'm assuming its a constant that points to the memory location where the NDS looks for the pallete for the background. Also, I'm still confused about the 256*256 and 256*2. Is that because its the size of the image? If so, I get that, but why is the palette 256*2 and not just 256?

Thanks in advance for your help :)

Re: Need Help Understanding 2d Example

Posted: Sun Jan 18, 2009 6:07 pm
by Sylus101
You're right about BG_PALETTE. It is defined in video.h in this way (along with the bg palette for the sub engine):

Code: Select all

#define BG_PALETTE       ((u16*)0x05000000)/** \brief background palette memory*/
#define BG_PALETTE_SUB   ((u16*)0x05000400)/** \brief background palette memory (sub engine)*/
The size is in bytes. So in the case of the drunkenlogoBitmap, it's a 256 by 256 pixel image * 1 byte per pixel. Each byte is basically a reference to a location in the palette. 256 color = 8 bit = 1 byte, so that's the size of that background and how many bytes dmaCopy needs to copy.

The palette is a 256 color palette, but it's entries are each 16 bit color values = 2 bytes each, hence 256 * 2.

Honestly, what you might see more often with something like this is the use of sizeof(u16) instead of 2, but no biggie.

(sorry if I mentioned things you already knew, just tried to give a complete answer)

Re: Need Help Understanding 2d Example

Posted: Mon Jan 19, 2009 12:45 am
by olwildcory723
Thanks again Sylus. I'm a php/javascript/html/flash web design guy who's trying to understand C for the sole purpose of coding on the DS. Not the best background for it, I know, but oh well. That makes much more sense now. The whole memory allocation stuff is all new to me and I'm still not sure if I'm supposed to learn it all more in-depth, or if I'll just be able to rely on the abstraction put in place by the developers like in this BG_PALETTE situation.

Re: Need Help Understanding 2d Example

Posted: Mon Jan 19, 2009 3:36 am
by Sylus101
No problem. I started off about a year ago without knowing any C and being pretty impatient in wanting to get something up and running right away. I went with a higher abstraction level library called PAlib because learning C and libnds at the same time was pretty daunting. PALib, I should mention, relies on libnds to work.

I'm not sure what to recommend to be honest. Most tried and true libnds'ers would tell you to stay away from PALib as it's not particularly kept up, some community members are working on it, and is currently incompatible with the most recent of devkitARM and libnds builds. It is, however, very easy to jump into if you absolutely have to have something on screen right away. One thing in particular that it handles for you is sprite animations and that can be a pain to write yourself, especially if you're just learning C. I'm in the midst of writing some functions to handle animations for a current project, and it's proving to be tricky.

I would say, give learning libnds a fair shot. The flexibility is great, and hell, I'm happy to share what I know as I'm learning as well and will answer what I can. I check in quite often as I'm hoping to get some feedback for some of my older questions but since they have to do with recent additions by Dovoto, I have a feeling he's the only one that can answer them. He posted recently about moving, so I'm sure he's up to other things. Lives... I guess we all have other things to do :)

Re: Need Help Understanding 2d Example

Posted: Wed Jan 21, 2009 4:56 am
by WinterMute
Until such times as the PAlib maintainers create a library which works without destroying the toolchain on which PAlib depends we cannot recommend its use under any circumstances.

http://wiki.devkitpro.org/index.php/PAlib

Personally I would prefer if PAlib was not mentioned around here at all.

Re: Need Help Understanding 2d Example

Posted: Wed Jan 21, 2009 7:35 pm
by Sylus101
:shock:

By your command...