diff options
| author | dario-loi <loi.1940849@studenti.uniroma1.it> | 2024-11-27 14:26:58 +0100 |
|---|---|---|
| committer | dario-loi <loi.1940849@studenti.uniroma1.it> | 2024-11-27 14:28:07 +0100 |
| commit | 528209831fc8f188df895acae83174bb81996c96 (patch) | |
| tree | 84f143235b5e1e6f1d9d06764ac35addd4b97f5e /examples | |
| parent | eff7f86c4b81e804b13c5606fcc73a6dbfa58677 (diff) | |
Code quality and warning removal
Solved all code quality issues, removed dead code.
Correctly freed fps buffer in texture_show_fps.c
Correctly checked for argc size before accessing argv (did not show up
in warnings but it's still dangerous)
Reordered some resource acquisition operations in examples in order to
avoid framebuffer creation if wrong inputs are specified (we fail
first).
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/circle.c | 2 | ||||
| -rw-r--r-- | examples/framebuf_info.c | 2 | ||||
| -rw-r--r-- | examples/line.c | 4 | ||||
| -rw-r--r-- | examples/rectangle.c | 4 | ||||
| -rw-r--r-- | examples/red.c | 2 | ||||
| -rw-r--r-- | examples/text.c | 47 | ||||
| -rw-r--r-- | examples/texture.c | 20 | ||||
| -rw-r--r-- | examples/texture_show_fps.c | 12 |
8 files changed, 54 insertions, 39 deletions
diff --git a/examples/circle.c b/examples/circle.c index 40ad800..8a69f78 100644 --- a/examples/circle.c +++ b/examples/circle.c @@ -22,7 +22,7 @@ int main(void) &buffer); i = (i + 1) % 360; fbgl_draw_circle_filled(480, 540, 40, 0xFFFFFF, &buffer); - usleep(10000); + nanosleep((struct timespec[]){ { 0, 10000000 } }, NULL); } while (1) { diff --git a/examples/framebuf_info.c b/examples/framebuf_info.c index 9d149fd..58c9ca7 100644 --- a/examples/framebuf_info.c +++ b/examples/framebuf_info.c @@ -1,7 +1,7 @@ #define FBGL_IMPLEMENTATION #include "fbgl.h" -int main(int argc, char *argv[]) +int main(void) { fbgl_t buffer; if (fbgl_init("/dev/fb0", &buffer) == -1) { diff --git a/examples/line.c b/examples/line.c index e22a113..c57fe1f 100644 --- a/examples/line.c +++ b/examples/line.c @@ -1,7 +1,7 @@ #define FBGL_IMPLEMENTATION #include "fbgl.h" -int main(int argc, char *argv[]) +int main() { fbgl_t buffer; if (fbgl_init("/dev/fb0", &buffer) == -1) { @@ -15,7 +15,7 @@ int main(int argc, char *argv[]) for (int i = 0; i < 1890; i++) { start.x = i; fbgl_draw_line(start, end, 0xFFFFFF, &buffer); - usleep(1000); // sleep for 1 millisecond + nanosleep((struct timespec[]){ { 0, 10000000 } }, NULL); } fbgl_draw_line(start, end, 0x000000, &buffer); diff --git a/examples/rectangle.c b/examples/rectangle.c index 1b3a6f7..38def2e 100644 --- a/examples/rectangle.c +++ b/examples/rectangle.c @@ -1,7 +1,7 @@ #define FBGL_IMPLEMENTATION #include "fbgl.h" -int main(int argc, char *argv[]) +int main(void) { fbgl_t buffer; if (fbgl_init("/dev/fb0", &buffer) == -1) { @@ -53,7 +53,7 @@ int main(int argc, char *argv[]) color_index = 0; } - usleep(50000); // Delay to make the animation visible (adjust as needed) + nanosleep((struct timespec[]){ { 0, (int)5e7 } }, NULL); } fbgl_destroy(&buffer); diff --git a/examples/red.c b/examples/red.c index c074143..8240cf4 100644 --- a/examples/red.c +++ b/examples/red.c @@ -2,7 +2,7 @@ #include "fbgl.h" #include <stdio.h> -int main(int argc, char *argv[]) +int main(void) { fbgl_t fb; fbgl_init("/dev/fb0", &fb); diff --git a/examples/text.c b/examples/text.c index 9003e3c..ee1efc9 100644 --- a/examples/text.c +++ b/examples/text.c @@ -16,35 +16,35 @@ int save_framebuffer_as_ppm(fbgl_t *fb, const char *filename) } // Write PPM header (P6 format - binary RGB) - fprintf(fp, "P6\n%u %u\n255\n", fb->width, fb->height); + fprintf(fp, "P6\n%u %u\n255\n", fb->width, fb->height); - // Allocate buffer for pixel data - uint8_t* pixel_buffer = malloc(fb->width * fb->height * 3); - if (!pixel_buffer) { + // Allocate buffer for pixel data + uint8_t *pixel_buffer = malloc(fb->width * fb->height * 3); + if (!pixel_buffer) { perror("Memory allocation error"); fclose(fp); return -1; } // Convert framebuffer to RGB - for (int32_t y = 0; y < fb->height; y++) { - for (int32_t x = 0; x < fb->width; x++) { - uint32_t pixel = fb->pixels[y * fb->width + x]; - - // Extract RGB components (assuming 32-bit ARGB or RGB) - uint8_t r = (pixel >> 16) & 0xFF; - uint8_t g = (pixel >> 8) & 0xFF; - uint8_t b = pixel & 0xFF; - - // Store in buffer for PPM - pixel_buffer[(y * fb->width + x) * 3] = r; - pixel_buffer[(y * fb->width + x) * 3 + 1] = g; - pixel_buffer[(y * fb->width + x) * 3 + 2] = b; - } - } + for (int32_t y = 0; y < fb->height; y++) { + for (int32_t x = 0; x < fb->width; x++) { + uint32_t pixel = fb->pixels[y * fb->width + x]; + + // Extract RGB components (assuming 32-bit ARGB or RGB) + uint8_t r = (pixel >> 16) & 0xFF; + uint8_t g = (pixel >> 8) & 0xFF; + uint8_t b = pixel & 0xFF; + + // Store in buffer for PPM + pixel_buffer[(y * fb->width + x) * 3] = r; + pixel_buffer[(y * fb->width + x) * 3 + 1] = g; + pixel_buffer[(y * fb->width + x) * 3 + 2] = b; + } + } - // Write pixel data - fwrite(pixel_buffer, 1, fb->width * fb->height * 3, fp); + // Write pixel data + fwrite(pixel_buffer, 1, fb->width * fb->height * 3, fp); // Cleanup free(pixel_buffer); @@ -76,9 +76,6 @@ int main(int argc, char *argv[]) // Text to render const char *text = "Hello, fbgl!"; - // Calculate text width - size_t text_width = strlen(text) * 8; - // Calculate centered position int x = (fb.width - 8) / 2; int y = (fb.height - 16) / 2; @@ -96,7 +93,7 @@ int main(int argc, char *argv[]) // Wait for a bit to show the image size_t framerate = 30 * 30; for (size_t i = 0; i < framerate; i++) { - usleep(50000); + nanosleep((struct timespec[]){ { 0, (int)5e7 } }, NULL); } // Cleanup diff --git a/examples/texture.c b/examples/texture.c index afa0985..f3e0f0c 100644 --- a/examples/texture.c +++ b/examples/texture.c @@ -6,19 +6,25 @@ int main(int argc, char **argv) { - // Initialize framebuffer - fbgl_t framebuffer; - if (fbgl_init(NULL, &framebuffer) != 0) { - fprintf(stderr, "Failed to initialize framebuffer.\n"); + if (argc < 2) { + fprintf(stderr, "Usage: %s <texture_path>\n", argv[0]); return EXIT_FAILURE; } // Load a TGA texture const char *texture_path = argv[1]; + fbgl_tga_texture_t *texture = fbgl_load_tga_texture(texture_path); if (!texture) { fprintf(stderr, "Failed to load texture.\n"); - fbgl_destroy(&framebuffer); + + return EXIT_FAILURE; + } + + // Initialize framebuffer + fbgl_t framebuffer; + if (fbgl_init(NULL, &framebuffer) != 0) { + fprintf(stderr, "Failed to initialize framebuffer.\n"); return EXIT_FAILURE; } @@ -55,8 +61,10 @@ int main(int argc, char **argv) texture_y + texture->height >= framebuffer.height) { dy = -dy; // Reverse vertical direction when hitting the top or bottom edge } + nanosleep( + (struct timespec[]){ { 0, (int)5e7 } }, + NULL); // Delay to make the marquee effect visible (adjust as needed) - usleep(50000); // Delay to make the marquee effect visible (adjust as needed) framesize--; } diff --git a/examples/texture_show_fps.c b/examples/texture_show_fps.c index 90ddc4f..df54115 100644 --- a/examples/texture_show_fps.c +++ b/examples/texture_show_fps.c @@ -20,6 +20,12 @@ char *float_to_string(float value) int main(int argc, char **argv) { + if (argc < 3) { + fprintf(stderr, "Usage: %s <texture_path> <font_path>\n", + argv[0]); + return EXIT_FAILURE; + } + // Initialize framebuffer fbgl_t framebuffer; if (fbgl_init(NULL, &framebuffer) != 0) { @@ -78,7 +84,11 @@ int main(int argc, char **argv) fbgl_render_psf1_text(&framebuffer, font, fps, 100, 0, 0xFF0000); - usleep(50000); // Delay to make the marquee effect visible (adjust as needed) + free(fps); + + nanosleep( + (struct timespec[]){ { 0, (int)5e7 } }, + NULL); // Delay to make the marquee effect visible (adjust as needed) framesize--; } |
