From 528209831fc8f188df895acae83174bb81996c96 Mon Sep 17 00:00:00 2001 From: dario-loi Date: Wed, 27 Nov 2024 14:26:58 +0100 Subject: 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). --- examples/text.c | 47 ++++++++++++++++++++++------------------------- 1 file changed, 22 insertions(+), 25 deletions(-) (limited to 'examples/text.c') 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 -- cgit v1.2.3