#include #include #include #include #include #include #define WIDTH 720 #define HEIGHT 720 #define BPP 4 // BGRA = 4 bytes per pixel typedef struct { int fb_fd; uint8_t *framebuffer; } FramebufferDemo; // Initialize framebuffer int fb_init(FramebufferDemo *fb) { // Open framebuffer device fb->fb_fd = open("/dev/fb0", O_RDWR); if (fb->fb_fd < 0) { fprintf(stderr, "Error: Cannot open framebuffer device /dev/fb0\n"); fprintf(stderr, "Make sure you have permissions (try running with sudo)\n"); return -1; } // Allocate framebuffer memory fb->framebuffer = (uint8_t*)malloc(WIDTH * HEIGHT * BPP); if (!fb->framebuffer) { fprintf(stderr, "Error: Cannot allocate framebuffer memory\n"); close(fb->fb_fd); return -1; } printf("Framebuffer opened successfully\n"); printf("Resolution: %dx%d, Format: BGRA\n", WIDTH, HEIGHT); return 0; } // Close framebuffer void fb_close(FramebufferDemo *fb) { if (fb->framebuffer) { free(fb->framebuffer); fb->framebuffer = NULL; } if (fb->fb_fd >= 0) { close(fb->fb_fd); fb->fb_fd = -1; } } // Set a pixel at (x, y) with BGRA color void set_pixel(FramebufferDemo *fb, int x, int y, uint8_t b, uint8_t g, uint8_t r, uint8_t a) { if (x < 0 || x >= WIDTH || y < 0 || y >= HEIGHT) { return; } int offset = (y * WIDTH + x) * BPP; fb->framebuffer[offset + 0] = b; // Blue fb->framebuffer[offset + 1] = g; // Green fb->framebuffer[offset + 2] = r; // Red fb->framebuffer[offset + 3] = a; // Alpha } // Fill entire screen with a color void fill_screen(FramebufferDemo *fb, uint8_t b, uint8_t g, uint8_t r, uint8_t a) { for (int y = 0; y < HEIGHT; y++) { for (int x = 0; x < WIDTH; x++) { set_pixel(fb, x, y, b, g, r, a); } } } // Draw a rectangle void draw_rectangle(FramebufferDemo *fb, int x, int y, int w, int h, uint8_t b, uint8_t g, uint8_t r, uint8_t a) { for (int dy = 0; dy < h; dy++) { for (int dx = 0; dx < w; dx++) { set_pixel(fb, x + dx, y + dy, b, g, r, a); } } } // Draw a circle void draw_circle(FramebufferDemo *fb, int cx, int cy, int radius, uint8_t b, uint8_t g, uint8_t r, uint8_t a) { for (int y = -radius; y <= radius; y++) { for (int x = -radius; x <= radius; x++) { if (x * x + y * y <= radius * radius) { set_pixel(fb, cx + x, cy + y, b, g, r, a); } } } } // Update the framebuffer (write to /dev/fb0) void fb_update(FramebufferDemo *fb) { if (fb->fb_fd < 0) { fprintf(stderr, "Error: Framebuffer not opened\n"); return; } // Seek to beginning of framebuffer lseek(fb->fb_fd, 0, SEEK_SET); // Write framebuffer data ssize_t written = write(fb->fb_fd, fb->framebuffer, WIDTH * HEIGHT * BPP); if (written < 0) { fprintf(stderr, "Error writing to framebuffer\n"); } } int main(int argc, char *argv[]) { FramebufferDemo fb = {0}; if (fb_init(&fb) < 0) { return 1; } printf("Drawing demo graphics...\n"); // Fill screen with black fill_screen(&fb, 0, 0, 0, 255); // Draw a red rectangle draw_rectangle(&fb, 100, 100, 200, 150, 0, 0, 255, 255); // Draw a green circle draw_circle(&fb, 500, 200, 80, 0, 255, 0, 255); // Draw a blue rectangle draw_rectangle(&fb, 200, 400, 300, 100, 255, 0, 0, 255); // Draw a yellow circle draw_circle(&fb, 360, 360, 100, 0, 255, 255, 255); // Update the framebuffer fb_update(&fb); printf("Graphics drawn! Press Enter to clear and exit...\n"); getchar(); // Clear screen before exit fill_screen(&fb, 0, 0, 0, 255); fb_update(&fb); printf("Done!\n"); fb_close(&fb); return 0; }