summaryrefslogtreecommitdiff
path: root/examples/text.c
diff options
context:
space:
mode:
authordario-loi <loi.1940849@studenti.uniroma1.it>2024-11-27 14:26:58 +0100
committerdario-loi <loi.1940849@studenti.uniroma1.it>2024-11-27 14:28:07 +0100
commit528209831fc8f188df895acae83174bb81996c96 (patch)
tree84f143235b5e1e6f1d9d06764ac35addd4b97f5e /examples/text.c
parenteff7f86c4b81e804b13c5606fcc73a6dbfa58677 (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/text.c')
-rw-r--r--examples/text.c47
1 files changed, 22 insertions, 25 deletions
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