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.

Beginners > Mode 4 graphics

#29656 - ymalik - Tue Nov 23, 2004 2:40 pm

Can someone explain to me how mode 4 works? I don't understand this thing about packing two pixels into one pixel.

Thanks,
Yasir

#29657 - poslundc - Tue Nov 23, 2004 2:59 pm

It's not packing two pixels into one pixel; it's two pixels into 16 bits. This is because the graphics hardware only writes 16 bits at a time, so if you attempt to access the video buffer with a u8 pointer (which is fine for other areas of memory) you may get the wrong result. Since a pixel in Mode 4 is 8 bits, it's necessary to write two pixels at a time (with a u16 pointer) in order to conform with this restriction.

Dan.

#29658 - ymalik - Tue Nov 23, 2004 3:08 pm

Each pixel in mode 4 is 8 bits, but it takes the same amount of space on the screen as a mode 3 pixel, right? And where does the palette fit in?

#29660 - Wriggler - Tue Nov 23, 2004 3:34 pm

Here's the rundown (as it confused me first of all too!)

Mode 3 has 16 bits representing one pixel. This is because you specify the colour out of a possible 65000, so you need lots of room to store it.

Got that? Mode 3=16 bits per pixel.

Mode 4 is paletted, so you only need to store numbers from 0-256 for each pixel. Therefore you only need 8 bits per pixel.

Mode 4=8 bits per pixel.

However, the processor inside the GBA can only write at 16bits at a time. This is fine for mode3, as you can just write one pixel each loop and everyone is happy.

However, for Mode 4, you can't just write 8 bits (or one pixel) at a time. You've gotta write 16. So therefore, you have to effectively write 2 pixels at a time. Get it?

Hope this helps,

Ben

#29662 - poslundc - Tue Nov 23, 2004 3:47 pm

ymalik wrote:
Each pixel in mode 4 is 8 bits, but it takes the same amount of space on the screen as a mode 3 pixel, right?


Right. Both modes operate in 240 pixels horizontal by 160 pixels vertical.

Quote:
And where does the palette fit in?


The palette matches the pixel value up to what colour to display. 8 bits = 256 possible values; for each of those values you can tell the GBA what 15-bit colour to display when it encounters that value. This information must be stored in the palette in order for the GBA to know what the pixel values "5, 43, 200, 87" etc. represent.

Dan.

#29663 - ymalik - Tue Nov 23, 2004 3:48 pm

Ok, that helped a lot. Thanks. But why isn't there a palettle in mode 3?

#29664 - ymalik - Tue Nov 23, 2004 3:58 pm

Thanks Dan and Ben for your help.

poslundc wrote:

The palette matches the pixel value up to what colour to display. 8 bits = 256 possible values; for each of those values you can tell the GBA what 15-bit colour to display when it encounters that value. This information must be stored in the palette in order for the GBA to know what the pixel values "5, 43, 200, 87" etc. represent.

Dan.


So each value in the 256 array can hold a 16 bit color value?

#29667 - poslundc - Tue Nov 23, 2004 4:26 pm

ymalik wrote:
Ok, that helped a lot. Thanks. But why isn't there a palettle in mode 3?


Because each pixel takes 16 bits instead of 8, which is enough to store the red, green and blue colour components. Unless you think it's sensible to set up and keep track of a palette of over 30,000 colours.

(The Mode 3 technique is known as direct colour, because the pixel values directly contain the description of the colour to be displayed. Mode 4 is indexed colour, because the pixel values contain an index into a table (aka palette) that must be used to determine what colour is displayed.)

Quote:
So each value in the 256 array can hold a 16 bit color value?


Yes. (It's technically 15-bit, with an extra wasted bit to keep things even.)

Dan.

#29669 - ymalik - Tue Nov 23, 2004 4:45 pm

poslundc wrote:
Yes. (It's technically 15-bit, with an extra wasted bit to keep things even.)


Thanks for all your help, but I'm still kind of confused. Since each pixel in mode 3 is 8 bits, why would the palette store 16 bit values?

Thanks,
Yasir

#29670 - poslundc - Tue Nov 23, 2004 4:51 pm

Colours are 16 bits.

In order to know what colour to display in a pixel on the screen, the GBA needs a 16-bit number telling it how much red, green and blue to use.

In Mode 3 these numbers are stored, one 16-bit number per pixel, in a 240x160 array that covers the entire screen.

In Mode 4 these 16-bit numbers are stored in a palette that is 256 entries long, and the screen is then represented by a 240x160 array of numbers (ranging from 0 to 255) that index into this palette.

I don't know how to make it any simpler than that.

Dan.

#29671 - ymalik - Tue Nov 23, 2004 4:57 pm

poslundc wrote:
In Mode 4 these 16-bit numbers are stored in a palette that is 256 entries long, and the screen is then represented by a 240x160 array of numbers (ranging from 0 to 255) that index into this palette.


Ohhhh. Now that cleared everything up. Thank you very much.

Yasir