9. Drawing a full background

Ok, we learned earlier that you can only draw to the screen… 1. During V-blank or 2. While the screen is off. With option 1, you can only draw 2 rows per frame, and with 30 rows to draw it will take 15-16 frames to do. With option 2 you get 1-2 frames of black screen (or whatever the default BG color is) and the new background is fully shown.

I prefer the second option, as the code is easier. I’ve used NES Screen Tool to create the backgrounds quickly, and used the Save Nametable as .RLE to compress it. I will then be using the UNRLE asm code to decompress it. (another thing that you don’t need to know how it works, it works, forget about it and move on).

I’ve set it to load a new background every time Start is pressed. I have it also checking against previous frame presses, so that we don’t get multiples in a row.

This code…

  1. Gets input every frame
  2. If Current press = Start, and last frame press != Start…
  3. Set flag to turn off screen
  4. Next V-blank, turn off screen
  5. Draw a new screen
  6. Wait till V-blank, turn on screen

 

Draw the new background…

void Draw_Background(void) {
 All_Off();
 PPU_ADDRESS = 0x20; //address $2000 = start of nametable #0
 PPU_ADDRESS = 0x00;
 UnRLE(All_Backgrounds[which_BGD]); //uncompresses our data
 Wait_Vblank(); //don't turn on screen until in v-blank
 All_On();
 ++which_BGD;
 if (which_BGD == 4) //shuffles between 0-3
   which_BGD = 0;
}

const unsigned char * const All_Backgrounds[]={n1,n2,n3,n4};
// pointers to the addresses of each background

 

lesson7

Here’s the link to the source code. Now ‘Start’ will change the background…

http://dl.dropboxusercontent.com/s/e7mktrwqblyb6zr/lesson7.zip

 

Advertisements

4 thoughts on “9. Drawing a full background

  1. Hi Doug,

    I’m really enjoying these tutorials. I’m going through them a bit. And I actually WAS wondering about RLE.
    Do you know what type of RLE is used to encode / decode the background sheets? I’m trying to reproduce it, but come up empty. Any pointers? Or perhaps an explanation of what the UNRLE function does? I’m curious to find out.

    Like

  2. Hi doug,

    Have you got a tool to edit the positions of the sprites background’s in the .h files?
    I don’t understand all the data in the .h files. Is there a pattern in the array for each range of sprites?
    Thank you for this great tuto 😉

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s