summaryrefslogtreecommitdiff
path: root/examples/text.c
blob: 7cbf3ff51c27d9e38fe886b54ada44602090f393 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#define FBGL_IMPLEMENTATION
#include "fbgl.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h> // for usleep
#include <stdint.h>
#include <string.h>

// Function to save framebuffer as PPM image
int save_framebuffer_as_ppm(fbgl_t *fb, const char *filename)
{
	FILE *fp = fopen(filename, "wb");
	if (!fp) {
		perror("Error opening file");
		return -1;
	}

	// Write PPM header (P6 format - binary RGB)
	fprintf(fp, "P6\n%zu %zu\n255\n", fb->width, fb->height);

	// 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 (size_t y = 0; y < fb->height; y++) {
		for (size_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);

	// Cleanup
	free(pixel_buffer);
	fclose(fp);

	return 0;
}

int main(int argc, char *argv[])
{
	if (argc < 2) {
		fprintf(stderr, "Usage: %s <font_file> [output_image]\n",
			argv[0]);
		return 1;
	}

	fbgl_t fb;
	fbgl_init("/dev/fb0", &fb);
	fbgl_set_bg(&fb, 0xFFFFFF);

	// Load font
	fbgl_psf1_font_t *font = fbgl_load_psf1_font(argv[1]);
	if (!font) {
		fprintf(stderr, "Failed to load font\n");
		fbgl_destroy(&fb);
		return 1;
	}

	// 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;

	// Render centered text
	fbgl_render_psf1_text(&fb, font, text, x, y, 0xFF0000);

	// Save screenshot
	const char *output_filename = (argc > 2) ? argv[2] :
						   "fbgl_screenshot.ppm";
	if (save_framebuffer_as_ppm(&fb, output_filename) == 0) {
		//        printf("Screenshot saved as %s\n", output_filename);
	}

	// Wait for a bit to show the image
	size_t framerate = 30 * 30;
	for (size_t i = 0; i < framerate; i++) {
		usleep(50000);
	}

	// Cleanup
	fbgl_destroy_psf1_font(font);
	fbgl_destroy(&fb);
	return 0;
}