diff options
| author | lvntky <klevent1903@gmail.com> | 2024-11-23 19:26:26 +0300 |
|---|---|---|
| committer | lvntky <klevent1903@gmail.com> | 2024-11-23 19:26:26 +0300 |
| commit | 90b972404f32159be5a877fad92d8ad3bc277774 (patch) | |
| tree | 15052c8032fb7cfaa00abf95a0446d7723b1829d | |
| parent | 24965e4b9180f8ced18088ee2a3b55fbf5cbd990 (diff) | |
[feature] basic shapes
| -rw-r--r-- | example/rectangle.c | 59 | ||||
| -rw-r--r-- | example/red.c | 13 | ||||
| -rw-r--r-- | fbgl.h | 45 |
3 files changed, 115 insertions, 2 deletions
diff --git a/example/rectangle.c b/example/rectangle.c new file mode 100644 index 0000000..73f9c4b --- /dev/null +++ b/example/rectangle.c @@ -0,0 +1,59 @@ +#define FBGL_IMPLEMENTATION +#include "../fbgl.h" + +int main(int argc, char *argv[]) +{ + fbgl_t buffer; + if (fbgl_init("/dev/fb0", &buffer) == -1) { + fprintf(stdout, "Error: could not open framebuffer device\n"); + return -1; + } + + fbgl_set_bg(&buffer, 0xFFFFFF); // Set the background to white + + fbgl_point_t start = {100, 100}; + fbgl_point_t end = {200, 200}; + fbgl_draw_rectangle_outline(start, end, 0xFF0000, &buffer); // Draw red rectangle outline + + fbgl_point_t start2 = {600, 400}; + fbgl_point_t end2 = {800, 800}; + uint32_t colors[] = {0xFFC00, 0x00FF00, 0x0000FF, 0xFF00FF}; // Yellow, Green, Blue, Magenta + size_t color_index = 0; + + // Initial position of the marquee rectangle + int dx = 15; // Horizontal speed + int dy = 8; // Vertical speed + + while (1) { + // Clear the framebuffer (set background) + fbgl_set_bg(&buffer, 0xFFFFFF); + + + // Draw the moving filled rectangle + fbgl_draw_rectangle_filled(start2, end2, colors[color_index], &buffer); + + // Move the filled rectangle by updating its position + start2.x += dx; + end2.x += dx; + start2.y += dy; + end2.y += dy; + + // Reverse direction if the rectangle hits the screen boundary + if (start2.x <= 0 || end2.x >= buffer.width) { + dx = -dx; + color_index++; + } + if (start2.y <= 0 || end2.y >= buffer.height) { + dy = -dy; + color_index++; + } + if (color_index >= 4) { + color_index = 0; + } + + usleep(50000); // Delay to make the animation visible (adjust as needed) + } + + fbgl_destroy(&buffer); + return 0; +} diff --git a/example/red.c b/example/red.c new file mode 100644 index 0000000..274bc31 --- /dev/null +++ b/example/red.c @@ -0,0 +1,13 @@ +#define FBGL_IMPLEMENTATION +#include "../fbgl.h" +#include <stdio.h> + +int main(int argc, char *argv[]) +{ + fbgl_t fb; + fbgl_init("/dev/fb0", &fb); + fbgl_set_bg(&fb, 0xFF0000); + while(1){} + + return 0; +} @@ -117,7 +117,7 @@ void fbgl_destroy(fbgl_t *fb); */ void fbgl_clear(uint32_t color); void fbgl_put_pixel(int x, int y, uint32_t color, fbgl_t *fb); -void fbgl_draw_line(fbgl_point_t x, fbgl_point_t y, uint32_t color, fbgl_t* fb); +void fbgl_draw_line(fbgl_point_t x, fbgl_point_t y, uint32_t color, fbgl_t *fb); void fbgl_set_bg(); /** @@ -132,6 +132,16 @@ uint32_t *fb_get_data(void); int fb_get_width(void); int fb_get_height(void); +/** +* Shapes +*/ +void fbgl_draw_rectangle_outline(fbgl_point_t top_left, + fbgl_point_t bottom_right, uint32_t color, + fbgl_t *fb); +void fbgl_draw_rectangle_filled(fbgl_point_t top_left, + fbgl_point_t bottom_right, uint32_t color, + fbgl_t *fb); + #ifdef FBGL_IMPLEMENTATION char const *fbgl_name_info(void) @@ -364,7 +374,8 @@ void fbgl_render_freetype_text(fbgl_t *fb, FT_Library library, FT_Face face, } #endif // FBGL_USE_FREETYPE -void fbgl_draw_line(fbgl_point_t x, fbgl_point_t y, uint32_t color, fbgl_t *buffer) +void fbgl_draw_line(fbgl_point_t x, fbgl_point_t y, uint32_t color, + fbgl_t *buffer) { int dx = abs(y.x - x.x); int dy = abs(y.y - x.y); @@ -395,6 +406,36 @@ void fbgl_draw_line(fbgl_point_t x, fbgl_point_t y, uint32_t color, fbgl_t *buff } } } +void fbgl_draw_rectangle_outline(fbgl_point_t top_left, fbgl_point_t bottom_right, uint32_t color, fbgl_t *fb) { + // Top horizontal line + for (int x = top_left.x; x < bottom_right.x; x++) { + fbgl_put_pixel(x, top_left.y, color, fb); + } + + // Bottom horizontal line + for (int x = top_left.x; x < bottom_right.x; x++) { + fbgl_put_pixel(x, bottom_right.y - 1, color, fb); + } + + // Left vertical line + for (int y = top_left.y; y < bottom_right.y; y++) { + fbgl_put_pixel(top_left.x, y, color, fb); + } + + // Right vertical line + for (int y = top_left.y; y < bottom_right.y; y++) { + fbgl_put_pixel(bottom_right.x - 1, y, color, fb); + } +} + +void fbgl_draw_rectangle_filled(fbgl_point_t top_left, fbgl_point_t bottom_right, uint32_t color, fbgl_t *fb) { + for (int y = top_left.y; y < bottom_right.y; y++) { + // Manually set each pixel in the row + for (int x = top_left.x; x < bottom_right.x; x++) { + fbgl_put_pixel(x, y, color, fb); + } + } +} #ifdef __cplusplus } // extern "C" |
