From 8a347f9515a0021361549016e86fcca159c948f5 Mon Sep 17 00:00:00 2001 From: dario-loi Date: Sun, 24 Nov 2024 17:33:45 +0100 Subject: Working CMakeLists.txt and many bugfixes Provided a working CMakeLists.txt file to build all of the examples, also fixed a bunch of bugs (some fixes also depend on personal preference and should be reviewed by maintainers): 1. Switched fbgl_t types from size_t to uint32_t as there is no way a screen resolution will be more than 2^32, this way we save 4 bytes and we have faster comparisons 2. Used int32_t instead of int for better clarity 3. fbgl_point now uses int32_t instead of uint32_t, no more sign comparison warnings (for loops that worked with fbgl_point were already working with int32_t in practice) 4. line.c now uses usleep(1000) to wait inbetween individual line draws instead of an empty for loop, as that was optimized away in release builds and led to all the animation being played instantly 5. removed the void fbgl_set_bg() forward declaration as it was incorrect w.r.t the actual function definition 6. fbgl_draw_texture specifies a const pointer to the texture resource, possibly allowing for compiler optimizations 7. fbgl_set_bg and fbgl_put_pixel now use #ifdef DEBUG to compile away checks in release builds, for faster performance 8. various const-correctness improvements 9. all required UNIX headers have been imported so that fbgl_check_esc_key does not give any more warnings All examples have been tested (on my local machine, so take this with a grain of salt) --- examples/texture.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 examples/texture.c (limited to 'examples/texture.c') diff --git a/examples/texture.c b/examples/texture.c new file mode 100644 index 0000000..da614d4 --- /dev/null +++ b/examples/texture.c @@ -0,0 +1,72 @@ +#define FBGL_IMPLEMENTATION +#include "../fbgl.h" +#include +#include +#include // for usleep + +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 + while (1) { + // 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 + } + + usleep(50000); // Delay to make the marquee effect visible (adjust as needed) + } + + // 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