2

I am programming a game in GBA for fun and want to use mode 4. I have recently created a game in mode 3 and the dma for it was quite simple. Would the dma structure be same if I wanted to draw an image onto the screen? Here is what I have:

/* 
 * A function that will draw an arbitrary sized image * onto the screen (with DMA).
 * @param r row to draw the image
 * @param c column to draw the image
 * @param width width of the image
 * @param height height of the image
 * @param image Pointer to the first element of the image. */
 void drawImageInMode4(int r, int c, int width, int height, const u16* image)
 {
   for(int i = 0; i < height; i++){
     DMA[3].src = image + OFFSET(i,0,width);
     //offset calculates the offset of pixel to screen
     DMA[3].dst = VIDEOBUFFER + OFFSET(r+i,c,240);
     DMA[3].cnt = DMA_ON | width;
 }

I feel like this is not using mode 4 but using mode 3. I have looked up on how to modify my code so it could work in mode 4. Thank you in advance!

Raptor
  • 53,206
  • 45
  • 230
  • 366
James Carter
  • 387
  • 2
  • 6
  • 18

1 Answers1

1

On GBA, the main difference between mode 3 and mode 4 is that mode 3 uses 2 bytes for every pixel, and mode 4 uses one byte for every pixel with a palette table. You still copy the data to the same place, but the video hardware interprets it differently. (Tonc includes a good demo of this.)

The main changes you'll need to make to your code are:

  • Halving the number of bytes you're copying per line
  • Adjusting your OFFSET macro to return the correct values for mode 4
  • Selecting which of the two pages you'd like to write pixel data to

(You'll also need to convert your bitmaps to 8bpp format and provide palette data for them.)

Mode 4 introduces one other problem: the fact that your images can be an odd number of bytes wide. Since VRAM doesn't support single-byte writes, you'll either need to write some special code to handle this case, or just never draw an image with an odd-numbered width or x position.

Carter Sande
  • 1,789
  • 13
  • 26