From 298e4972a9e72842c67c06eb9a23f517b16f411a Mon Sep 17 00:00:00 2001 From: lvntky Date: Tue, 26 Nov 2024 11:28:44 +0300 Subject: [test] fps and text rendering example --- CMakeLists.txt | 1 + examples/texture_show_fps.c | 96 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 examples/texture_show_fps.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 10cb5e7..e47cc2c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -30,3 +30,4 @@ add_example(texture) add_example(raw_mode) add_example(framebuf_info) add_example(text) +add_example(texture_show_fps) diff --git a/examples/texture_show_fps.c b/examples/texture_show_fps.c new file mode 100644 index 0000000..0b079c7 --- /dev/null +++ b/examples/texture_show_fps.c @@ -0,0 +1,96 @@ +#define FBGL_IMPLEMENTATION +#include "fbgl.h" +#include +#include +#include // for usleep + +char *float_to_string(float value) +{ + // Allocate a buffer large enough for the string representation + char *buffer = + (char *)malloc(32); // Ensure sufficient size for most floats + if (buffer == NULL) { + return NULL; // Handle allocation failure + } + + // Format the float into the buffer + snprintf(buffer, 32, "%.6f", value); // Adjust precision as needed + return buffer; +} + +int main(int argc, char **argv) +{ + // Initialize framebuffer + fbgl_t framebuffer; + if (fbgl_init(NULL, &framebuffer) != 0) { + fprintf(stderr, "Failed to initialize framebuffer.\n"); + 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; + } + + // Set a background color (e.g., black) + fbgl_set_bg(&framebuffer, 0x000000); // Clear the framebuffer to black + + // Texture movement parameters + int texture_x = 0; // Initial horizontal position of the texture + int texture_y = 100; // Initial vertical position of the texture + int dx = 5; // Horizontal speed (adjust for desired marquee speed) + int dy = 3; // Vertical speed (adjust for desired marquee speed) + + // Main rendering loop + int framesize = 30 * 30; + + fbgl_psf1_font_t *font = fbgl_load_psf1_font(argv[2]); + while (framesize) { + + // Clear the framebuffer (set background) + fbgl_set_bg(&framebuffer, 0x000000); + + // Draw the texture at the current position + fbgl_draw_texture(&framebuffer, texture, texture_x, texture_y); + + // Move the texture by updating its position + texture_x += dx; + texture_y += dy; + + // Reverse direction if the texture hits the screen boundary (X-axis) + if (texture_x <= 0 || + texture_x + texture->width >= framebuffer.width) { + dx = -dx; // Reverse horizontal direction when hitting the left or right edge + } + + // Reverse direction if the texture hits the screen boundary (Y-axis) + if (texture_y <= 0 || + texture_y + texture->height >= framebuffer.height) { + dy = -dy; // Reverse vertical direction when hitting the top or bottom edge + } + + + fbgl_render_psf1_text(&framebuffer, font, "FPS: ", 5, 0, 0xFF0000); + char *fps = float_to_string(fbgl_get_fps()); + fbgl_render_psf1_text(&framebuffer, font, fps, 100, 0, 0xFF0000); + + usleep(50000); // Delay to make the marquee effect visible (adjust as needed) + framesize--; + } + + // Wait for the user to press the escape key before exiting + printf("Press ESC to exit...\n"); + while (!fbgl_check_esc_key()) { + // You can add additional rendering logic here if needed + } + + // Clean up + fbgl_destroy_texture(texture); + fbgl_destroy(&framebuffer); + + return EXIT_SUCCESS; +} -- cgit v1.2.3