diff options
| author | Levent Kaya <42411502+lvntky@users.noreply.github.com> | 2024-11-24 21:17:01 +0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-11-24 21:17:01 +0300 |
| commit | 766962f9970cc3d8ee5b49c8bc987fe14c9eda7c (patch) | |
| tree | 7a6b8abc927b0ef832d677fd80aa255ca7dd0951 | |
| parent | 0898b3d26129f3880223d0d457fe05fd3f6be180 (diff) | |
| parent | f795c9d0bc73dca2cabb36c1889dc99422549def (diff) | |
Merge pull request #6 from dario-loi/master
[FIX] Move all implementation under include guard
| -rw-r--r-- | example/.gitignore | 3 | ||||
| -rw-r--r-- | example/CMakeLists.txt | 35 | ||||
| -rw-r--r-- | example/empty_example.c | 78 | ||||
| -rw-r--r-- | example/line.c | 7 | ||||
| -rw-r--r-- | example/raw_mode.c | 21 | ||||
| -rw-r--r-- | example/rectangle.c | 102 | ||||
| -rw-r--r-- | example/red.c | 13 | ||||
| -rw-r--r-- | fbgl.h | 61 |
8 files changed, 167 insertions, 153 deletions
diff --git a/example/.gitignore b/example/.gitignore index 07ac165..4a6e1b0 100644 --- a/example/.gitignore +++ b/example/.gitignore @@ -1,2 +1 @@ -build/ -core.* +build/core.* diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index 411945a..e7bf93e 100644 --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -2,23 +2,36 @@ cmake_minimum_required(VERSION 3.21) project(fbglExamples C) -include(../cmake/folders.cmake) +include(../ cmake / folders.cmake) if(PROJECT_IS_TOP_LEVEL) - find_package(fbgl REQUIRED) + find_package( + fbgl REQUIRED) endif() -add_custom_target(run-examples) +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}") + 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_example( + empty_example) -add_folders(Example) +add_folders( + Example) diff --git a/example/empty_example.c b/example/empty_example.c index aee9232..8c7d32d 100644 --- a/example/empty_example.c +++ b/example/empty_example.c @@ -1,53 +1,53 @@ #define FBGL_IMPLEMENTATION -//#define FBGL_HIDE_CURSOR +// #define FBGL_HIDE_CURSOR #define FBGL_USE_FREETYPE #include "../fbgl.h" -#include <stdio.h> #include <stddef.h> +#include <stdio.h> 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; - + printf("version %s\n", fbgl_version_info()); + printf("name %s\n", fbgl_name_info()); - FT_Library library = fbgl_freetype_init(); - if (!library) { - fbgl_destroy(&buffer); - return -1; - } + 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_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; - } + FT_Library library = fbgl_freetype_init(); + if (!library) { + fbgl_destroy(&buffer); + return -1; + } - // Render text to framebuffer - fbgl_render_freetype_text(&buffer, library, face, "Hello, World!", 50, 50); + 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; + } - // 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); + // 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; + fbgl_destroy(&buffer); + return 0; } diff --git a/example/line.c b/example/line.c index 9d937cd..f20ec7e 100644 --- a/example/line.c +++ b/example/line.c @@ -11,11 +11,12 @@ int main(int argc, char *argv[]) fbgl_set_bg(&buffer, 0xFF0000); fbgl_point_t start = { 0, 0 }; - fbgl_point_t end = { 1020, 1020}; - for(int i = 0; i < 1890; i++) { + 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++){} + for (int j = 0; j < 10000000; j++) { + } } fbgl_draw_line(start, end, 0x000000, &buffer); diff --git a/example/raw_mode.c b/example/raw_mode.c index 12127af..449a824 100644 --- a/example/raw_mode.c +++ b/example/raw_mode.c @@ -4,18 +4,17 @@ int main(int argc, char *argv[]) { - fbgl_t fb; - fbgl_init("/dev/fb0", &fb); + fbgl_t fb; + fbgl_init("/dev/fb0", &fb); - fbgl_set_bg(&fb, 0xFFFFFF); + fbgl_set_bg(&fb, 0xFFFFFF); - while(1) { - if(fbgl_check_esc_key()) { - printf("pressed"); - fbgl_set_bg(&fb, 0x000000); + while (1) { + if (fbgl_check_esc_key()) { + printf("pressed"); + fbgl_set_bg(&fb, 0x000000); + } } - } - - return 0; -} + return 0; +} diff --git a/example/rectangle.c b/example/rectangle.c index 73f9c4b..ecc1afd 100644 --- a/example/rectangle.c +++ b/example/rectangle.c @@ -3,57 +3,59 @@ 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; + fbgl_t buffer; + if (fbgl_init("/dev/fb0", &buffer) == -1) { + fprintf(stdout, "Error: could not open framebuffer device\n"); + return -1; } - usleep(50000); // Delay to make the animation visible (adjust as needed) - } + 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; + fbgl_destroy(&buffer); + return 0; } diff --git a/example/red.c b/example/red.c index 274bc31..7f64dbd 100644 --- a/example/red.c +++ b/example/red.c @@ -4,10 +4,11 @@ int main(int argc, char *argv[]) { - fbgl_t fb; - fbgl_init("/dev/fb0", &fb); - fbgl_set_bg(&fb, 0xFF0000); - while(1){} - - return 0; + fbgl_t fb; + fbgl_init("/dev/fb0", &fb); + fbgl_set_bg(&fb, 0xFF0000); + while (1) { + } + + return 0; } @@ -27,30 +27,30 @@ #define VERSION "0.1.0" #define NAME "FBGL" #define DEFAULT_FB "/dev/fb0" -#define #define FBGL_MAX_KEYS 256 // Maximum number of keys to track +#define FBGL_MAX_KEYS 256 // Maximum number of keys to track +#include <fcntl.h> +#include <linux/fb.h> +#include <signal.h> +#include <stdbool.h> +#include <stddef.h> #include <stdint.h> +#include <stdio.h> #include <stdlib.h> #include <string.h> -#include <fcntl.h> #include <sys/ioctl.h> #include <sys/mman.h> -#include <unistd.h> -#include <linux/fb.h> -#include <stddef.h> -#include <stdio.h> #include <termios.h> -#include <signal.h> -#include <stdbool.h> +#include <unistd.h> #ifdef FBGL_USE_FREETYPE #include <ft2build.h> #include FT_FREETYPE_H -#endif //FBGL_USE_FREETYPE +#endif // FBGL_USE_FREETYPE /** -* Structs -*/ + * Structs + */ typedef struct fbgl { int width; int height; @@ -93,18 +93,17 @@ typedef struct fbgl_tga_texture { } fbgl_tga_texture_t; typedef struct fbgl_keyboard { - bool keys[FBGL_MAX_KEYS]; // Current state of each key - bool prev_keys[FBGL_MAX_KEYS]; // Previous state of each key - bool is_initialized; // Track if keyboard is initialized + bool keys[FBGL_MAX_KEYS]; // Current state of each key + bool prev_keys[FBGL_MAX_KEYS]; // Previous state of each key + bool is_initialized; // Track if keyboard is initialized } fbgl_keyboard_t; - /** * Key state function and variables - * + * */ static struct termios orig_termios; -static fbgl_keyboard_t keyboard = {0}; +static fbgl_keyboard_t keyboard = { 0 }; #ifdef FBGL_HIDE_CURSOR #include <linux/kd.h> @@ -125,7 +124,7 @@ int fbgl_hide_cursor(int fd) close(tty_fd); return 0; } -#endif //FBGL_HIDE_CURSOR +#endif // FBGL_HIDE_CURSOR #ifdef FBGL_USE_FREETYPE FT_Library fbgl_freetype_init(); @@ -134,7 +133,7 @@ FT_Face fbgl_load_font(FT_Library library, const char *font_path, int font_size); void fbgl_render_freetype_text(fbgl_t *fb, FT_Library library, FT_Face face, const char *text, int x, int y); -#endif //FBGL_USE_FREETYPE +#endif // FBGL_USE_FREETYPE #ifdef __cplusplus extern "C" { @@ -156,28 +155,28 @@ int fbgl_init(const char *device, fbgl_t *fb); void fbgl_destroy(fbgl_t *fb); /** -* Drawing functions -*/ + * Drawing functions + */ void fbgl_clear(uint32_t color); void fbgl_put_pixel(int x, int y, uint32_t color, fbgl_t *fb); void fbgl_draw_line(fbgl_point_t x, fbgl_point_t y, uint32_t color, fbgl_t *fb); void fbgl_set_bg(); /** -* Display methods -*/ + * Display methods + */ void fbgl_display(); /** -* Access framebuffer data methods -*/ + * Access framebuffer data methods + */ uint32_t *fb_get_data(void); int fb_get_width(void); int fb_get_height(void); /** -* Shapes -*/ + * Shapes + */ void fbgl_draw_rectangle_outline(fbgl_point_t top_left, fbgl_point_t bottom_right, uint32_t color, fbgl_t *fb); @@ -186,14 +185,14 @@ void fbgl_draw_rectangle_filled(fbgl_point_t top_left, fbgl_t *fb); /** -* texture -*/ + * texture + */ fbgl_tga_texture_t *fbgl_load_tga_texture(const char *path); void fbgl_destroy_texture(fbgl_tga_texture_t *texture); void fbgl_draw_texture(fbgl_t *fb, fbgl_tga_texture_t *texture, int x, int y); /** - * Keyboard + * Keyboard */ int fbgl_keyboard_init(); void fbgl_keyboard_clean(); @@ -243,7 +242,7 @@ int fbgl_init(const char *device, fbgl_t *fb) fbgl_hide_cursor(fb->fd); fbgl_set_signal_handlers(); fbgl_enable_raw_mode(); -#endif //FBGL_HIDE_CURSOR +#endif // FBGL_HIDE_CURSOR fb->width = fb->vinfo.xres; fb->height = fb->vinfo.yres; |
