..
#gamedev

Fill Whole Bitmap Array in Game

To fill a whole bitmap array correctly, I need to interate the X and Y axes. Being that pixel has 4 bytes (32 bits = unsigned int), which is equivalent o the AA RR GG BB channels.

Create a function signature:

static void
render_gradient(Back_Buffer *buffer, int xo, int yo)
{
}

Now we need to convert the block of memory (void *) to the unsigned char, because 1 byte can be represented by unsigned char (8 bits = 0 - 255) and I'm going to use this value to advance the memory block byte per byte. I call it row.

unsigned char *row (unsigned char *) buffer->data; // data is a void*

You'll see the process of advance the bytes consists in jump to the next row by pitch.

pitch = bytes_per_pixel * width;

buffer_size = height * pitch;

With the block converted, we need interate through X and Y axes and fill each piece of 32 bits (4 bytes) that contains the values for RGBA.

for (int y = 0; y < back_buffer->height; ++y) {
        // convert the row (8 bits) to single pixel start pointer (32 bits)
        u32 *pixel = (u32 *) row;

        for (int x = 0; x < back_buffer->width; ++x) {
                u8 xx = (x + x_offset);
                u8 yy = (y + y_offset);
                // fill each of channel with some value
                *pixel = (xx << 8) | yy;
                pixel++;
        }

        // add the pitch to the row. So, it's same as jump to the next
        // block of memory of current 8 bits + width * bytes_per_pixel
        row += back_buffer->pitch;
 }

The key point is to convert from u8 to u32 every Y step and add the pitch to this pointer. This process jump to the next row every Y steps.

While the X, we need to convert that specific row pointer (u8) to the u32 for fill the whole RGBA chunk.