Padding calculation in I10 Sprite Drawing / Bitmap Loading

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
HavocVulture
Posts: 16
Joined: October 30th, 2017, 11:50 pm

Padding calculation in I10 Sprite Drawing / Bitmap Loading

Post by HavocVulture » November 3rd, 2017, 5:50 pm

I just completed the homework for I10, but while working on it I was confused by the padding formula. In the video the calculation used is:

Code: Select all

const int padding = (4 - (width * 3) % 4) % 4;
Seemed a bit overwrought to me so I replaced it with:

Code: Select all

const int padding = (width * 3) % 4;
This worked with all the supplied 24-bit images. Is there something I'm missing? Is there a case where the simpler formula fails?

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Padding calculation in I10 Sprite Drawing / Bitmap Loadi

Post by albinopapa » November 3rd, 2017, 7:22 pm

Let's look at the possible values of width in regards to multiples of 4.

width = 12
(12 * 3) % 4 = 36 % 4 = 0 // Good

width = 13
(13 * 3) % 4 = 39 % 4 = 3 // Not good, we only need a padding of 1 here to make it 40

width = 14
(14 * 3) % 4 = 42 % 4 = 2 // Good

width = 15
(15 * 3) % 4 = 45 % 4 = 1 // Not good, we need a padding of 3 here to make it 48
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

HavocVulture
Posts: 16
Joined: October 30th, 2017, 11:50 pm

Re: Padding calculation in I10 Sprite Drawing / Bitmap Loadi

Post by HavocVulture » November 3rd, 2017, 10:08 pm

Thanks for clearing that up!

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Padding calculation in I10 Sprite Drawing / Bitmap Loadi

Post by albinopapa » November 4th, 2017, 3:42 am

That only explains part of it, the 4- part, the reason for the last %4 is in cases where padding = 0, 4 - 0 = 4, but you don't need 4 padding bytes so %4 would make it 0.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

Post Reply