summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xexample/empty_examplebin15720 -> 16128 bytes
-rw-r--r--example/empty_example.c9
-rw-r--r--fbgl.h63
3 files changed, 69 insertions, 3 deletions
diff --git a/example/empty_example b/example/empty_example
index e19c325..9f9c717 100755
--- a/example/empty_example
+++ b/example/empty_example
Binary files differ
diff --git a/example/empty_example.c b/example/empty_example.c
index c275526..81f9c04 100644
--- a/example/empty_example.c
+++ b/example/empty_example.c
@@ -1,5 +1,5 @@
#define FBGL_IMPLEMENTATION
-#include "../include/fbgl/fbgl.h"
+#include "../fbgl.h"
#include <stdio.h>
@@ -8,7 +8,12 @@ int main()
printf("version %s\n", fbgl_version_info());
printf("name %s\n", fbgl_name_info());
fbgl_t buffer;
- fbgl_init("/dev/fb0", &buffer);
+ if( fbgl_init("/dev/fb0", &buffer) == -1) {
+ fprintf(stdout, "error could not oppen fb device");
+ return -1;
+ }
+ fbgl_set_bg(&buffer, 0xFF0000);
+ while(1){}
return 0;
}
diff --git a/fbgl.h b/fbgl.h
index 7dbcfb4..5d0d8ff 100644
--- a/fbgl.h
+++ b/fbgl.h
@@ -41,7 +41,7 @@ char const *fbgl_version_info(void);
/*Create and destroy methods*/
int fbgl_init(const char *device, fbgl_t *fb);
-void fbgl_destroy(void);
+void fbgl_destroy(fbgl_t *fb);
/**
* Drawing functions
@@ -49,6 +49,7 @@ void fbgl_destroy(void);
void fbgl_clear(uint32_t color);
void fbgl_put_pixel(int x, int y, uint32_t color);
void fbgl_draw_line(int x0, int y0, int x1, int y1, uint32_t color);
+void fbgl_set_bg();
/**
* Display methods
@@ -76,12 +77,72 @@ char const *fbgl_version_info(void)
int fbgl_init(const char *device, fbgl_t *fb)
{
+ if (!fb) {
+ fprintf(stderr, "Error: fbgl_t pointer is NULL.");
+ return -1;
+ }
+
fb->fd = device == NULL ? open(DEFAULT_FB, O_RDWR) :
open(device, O_RDWR);
if (fb->fd == -1) {
perror("Error openning framebuffer device");
return -1;
}
+
+ if (ioctl(fb->fd, FBIOGET_FSCREENINFO, &fb->finfo) == -1) {
+ perror("Error: Reading fixed information."); close(fb->fd);
+ return -1;
+ }
+ if (ioctl(fb->fd, FBIOGET_VSCREENINFO, &fb->vinfo) == -1) {
+ perror("Error reading variable information");
+ close(fb->fd);
+ return -1;
+ }
+
+ fb->width = fb->vinfo.xres;
+ fb->height = fb->vinfo.yres;
+ fb->screen_size = fb->finfo.smem_len;
+
+ // Map framebuffer to memory
+ fb->pixels = (uint32_t *)mmap(NULL, fb->screen_size,
+ PROT_READ | PROT_WRITE, MAP_SHARED,
+ fb->fd, 0);
+ if (fb->pixels == MAP_FAILED) {
+ perror("Error mapping framebuffer device to memory");
+ close(fb->fd);
+ return -1;
+ }
+
+ return 0;
+}
+
+void fbgl_destroy(fbgl_t *fb)
+{
+ if (!fb || fb->fd == -1) {
+ fprintf(stderr,
+ "Error: framebuffer not initialized or already destroyed.\n");
+ return;
+ }
+
+ if (fb->pixels && fb->pixels != MAP_FAILED) {
+ munmap(fb->pixels, fb->screen_size);
+ }
+
+ close(fb->fd);
+ fb->fd = -1;
+}
+
+void fbgl_set_bg(fbgl_t *fb, uint32_t color)
+{
+ if (!fb || fb->fd == -1) {
+ fprintf(stderr, "Error: framebuffer not initialized.\n");
+ return;
+ }
+
+ // Fill the entire framebuffer with the specified color
+ for (size_t i = 0; i < (fb->width * fb->height); ++i) {
+ fb->pixels[i] = color;
+ }
}
#endif