Draw Rectangles

Define Color buffer

uint32_t *color_buffer = (uint32_t*) malloc(sizeof(uint32_t) * window_width * window_height);

In my case, window size set as 800 x 600. Define an uint32t represent as 4 bytes * 800 * 600 size buffer array.

Function:

void draw_rect(int x, int y, int width, int height, uint32_t color) {
  for(int row = y; row < y + height; row++) {
    for (int col = x; col < x + width; col++){
      color_buffer[(window_width * row) + col] = color;
    }
  }
}

Result:

draw_rect( 300 , 300, 200, 100, 0xFFFF00FF)

More Example:

for(int j = 0; j < window_height; j+= 60) {
    for(int i = 0; i < window_width; i += 100) {
     draw_rect( i - 10, j - 10, 80, 40, 0xFF00FFFF);
    }
}
for(int j = 0; j < window_height; j+= 60) {
    for(int i = 0; i < window_width; i += 100) {
      draw_rect( i - 10, j - 10, 80, 40, 0xFF00FFFF);
      draw_rect( i + 20, j + 20, 80, 40, 0xFFFF00FF);
    }
}

Resources: pikuma course

Date: 2023-12-24 Sun 00:00

Author: Terry Fung

Created: 2024-11-10 Sun 14:09

Emacs 29.4 (Org mode 9.6.15)

Validate