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) --- example/.gitignore | 2 -- example/CMakeLists.txt | 24 ----------------- example/empty_example.c | 53 ------------------------------------ example/line.c | 26 ------------------ example/raw_mode.c | 21 --------------- example/rectangle.c | 59 ---------------------------------------- example/red.c | 13 --------- example/texture.c | 72 ------------------------------------------------- 8 files changed, 270 deletions(-) delete mode 100644 example/.gitignore delete mode 100644 example/CMakeLists.txt delete mode 100644 example/empty_example.c delete mode 100644 example/line.c delete mode 100644 example/raw_mode.c delete mode 100644 example/rectangle.c delete mode 100644 example/red.c delete mode 100644 example/texture.c (limited to 'example') diff --git a/example/.gitignore b/example/.gitignore deleted file mode 100644 index 07ac165..0000000 --- a/example/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -build/ -core.* diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt deleted file mode 100644 index 411945a..0000000 --- a/example/CMakeLists.txt +++ /dev/null @@ -1,24 +0,0 @@ -cmake_minimum_required(VERSION 3.21) - -project(fbglExamples C) - -include(../cmake/folders.cmake) - -if(PROJECT_IS_TOP_LEVEL) - find_package(fbgl REQUIRED) -endif() - -add_custom_target(run-examples) - -function(add_example NAME) - add_executable("${NAME}" "${NAME}.c") - target_link_libraries("${NAME}" PRIVATE fbgl::fbgl) - target_compile_features("${NAME}" PRIVATE c_std_99) - add_custom_target("run_${NAME}" COMMAND "${NAME}" VERBATIM) - add_dependencies("run_${NAME}" "${NAME}") - add_dependencies(run-examples "run_${NAME}") -endfunction() - -add_example(empty_example) - -add_folders(Example) diff --git a/example/empty_example.c b/example/empty_example.c deleted file mode 100644 index aee9232..0000000 --- a/example/empty_example.c +++ /dev/null @@ -1,53 +0,0 @@ -#define FBGL_IMPLEMENTATION -//#define FBGL_HIDE_CURSOR -#define FBGL_USE_FREETYPE -#include "../fbgl.h" - -#include -#include - -int main() -{ - printf("version %s\n", fbgl_version_info()); - printf("name %s\n", fbgl_name_info()); - - fbgl_t buffer; - if (fbgl_init("/dev/fb0", &buffer) == -1) { - fprintf(stdout, "Error: could not open framebuffer device\n"); - return -1; - } - int color = 0x00000000; - - - FT_Library library = fbgl_freetype_init(); - if (!library) { - fbgl_destroy(&buffer); - return -1; - } - - FT_Face face = fbgl_load_font(library, "../asset/font_2.ttf", 24); // Adjust path and size - if (!face) { - fbgl_freetype_cleanup(library); - fbgl_destroy(&buffer); - return -1; - } - - // Render text to framebuffer - fbgl_render_freetype_text(&buffer, library, face, "Hello, World!", 50, 50); - - // Main loop checking for ESC key - int l = 0; - while (1) { - - if (fbgl_check_esc_key()) { - fprintf(stdout, "ESC pressed\n"); - break; - } - //fbgl_set_bg(&buffer, i++); // Set background color to - for(int i = 0x000000; i <= 0xFFFFFF; i++) { - fbgl_set_bg(&buffer, i); - } - } - fbgl_destroy(&buffer); - return 0; -} diff --git a/example/line.c b/example/line.c deleted file mode 100644 index 9d937cd..0000000 --- a/example/line.c +++ /dev/null @@ -1,26 +0,0 @@ -#define FBGL_IMPLEMENTATION -#include "../fbgl.h" - -int main(int argc, char *argv[]) -{ - fbgl_t buffer; - if (fbgl_init("/dev/fb0", &buffer) == -1) { - fprintf(stdout, "Error: could not open framebuffer device\n"); - return -1; - } - - fbgl_set_bg(&buffer, 0xFF0000); - fbgl_point_t start = { 0, 0 }; - fbgl_point_t end = { 1020, 1020}; - for(int i = 0; i < 1890; i++) { - start.x = i; - fbgl_draw_line(start, end, 0xFFFFFF, &buffer); - for(int j = 0; j < 10000000; j++){} - } - fbgl_draw_line(start, end, 0x000000, &buffer); - - while (1) { - } - - return 0; -} diff --git a/example/raw_mode.c b/example/raw_mode.c deleted file mode 100644 index 12127af..0000000 --- a/example/raw_mode.c +++ /dev/null @@ -1,21 +0,0 @@ -#define FBGL_IMPLEMENTATION -#include "../fbgl.h" -#include - -int main(int argc, char *argv[]) -{ - fbgl_t fb; - fbgl_init("/dev/fb0", &fb); - - fbgl_set_bg(&fb, 0xFFFFFF); - - while(1) { - if(fbgl_check_esc_key()) { - printf("pressed"); - fbgl_set_bg(&fb, 0x000000); - } - } - - return 0; -} - diff --git a/example/rectangle.c b/example/rectangle.c deleted file mode 100644 index 73f9c4b..0000000 --- a/example/rectangle.c +++ /dev/null @@ -1,59 +0,0 @@ -#define FBGL_IMPLEMENTATION -#include "../fbgl.h" - -int main(int argc, char *argv[]) -{ - fbgl_t buffer; - if (fbgl_init("/dev/fb0", &buffer) == -1) { - fprintf(stdout, "Error: could not open framebuffer device\n"); - return -1; - } - - fbgl_set_bg(&buffer, 0xFFFFFF); // Set the background to white - - fbgl_point_t start = {100, 100}; - fbgl_point_t end = {200, 200}; - fbgl_draw_rectangle_outline(start, end, 0xFF0000, &buffer); // Draw red rectangle outline - - fbgl_point_t start2 = {600, 400}; - fbgl_point_t end2 = {800, 800}; - uint32_t colors[] = {0xFFC00, 0x00FF00, 0x0000FF, 0xFF00FF}; // Yellow, Green, Blue, Magenta - size_t color_index = 0; - - // Initial position of the marquee rectangle - int dx = 15; // Horizontal speed - int dy = 8; // Vertical speed - - while (1) { - // Clear the framebuffer (set background) - fbgl_set_bg(&buffer, 0xFFFFFF); - - - // Draw the moving filled rectangle - fbgl_draw_rectangle_filled(start2, end2, colors[color_index], &buffer); - - // Move the filled rectangle by updating its position - start2.x += dx; - end2.x += dx; - start2.y += dy; - end2.y += dy; - - // Reverse direction if the rectangle hits the screen boundary - if (start2.x <= 0 || end2.x >= buffer.width) { - dx = -dx; - color_index++; - } - if (start2.y <= 0 || end2.y >= buffer.height) { - dy = -dy; - color_index++; - } - if (color_index >= 4) { - color_index = 0; - } - - usleep(50000); // Delay to make the animation visible (adjust as needed) - } - - fbgl_destroy(&buffer); - return 0; -} diff --git a/example/red.c b/example/red.c deleted file mode 100644 index 274bc31..0000000 --- a/example/red.c +++ /dev/null @@ -1,13 +0,0 @@ -#define FBGL_IMPLEMENTATION -#include "../fbgl.h" -#include - -int main(int argc, char *argv[]) -{ - fbgl_t fb; - fbgl_init("/dev/fb0", &fb); - fbgl_set_bg(&fb, 0xFF0000); - while(1){} - - return 0; -} diff --git a/example/texture.c b/example/texture.c deleted file mode 100644 index da614d4..0000000 --- a/example/texture.c +++ /dev/null @@ -1,72 +0,0 @@ -#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