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) --- CMakeLists.txt | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 CMakeLists.txt (limited to 'CMakeLists.txt') diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..03453ef --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,31 @@ +cmake_minimum_required(VERSION 3.11) + +project(fbglExamples C) + +add_custom_target(run-examples) + +set(FBGL_HEADER "fbgl.h") +set(EXAMPLES_DIR "${CMAKE_CURRENT_SOURCE_DIR}/examples") + +# find freetype2 +find_package(PkgConfig REQUIRED) +pkg_check_modules(FREETYPE2 REQUIRED freetype2) +include_directories(${FREETYPE2_INCLUDE_DIRS}) + +function(add_example NAME) + add_executable("${NAME}" "${EXAMPLES_DIR}/${NAME}.c" "${FBGL_HEADER}") + target_compile_features("${NAME}" PRIVATE c_std_99) + target_include_directories("${NAME}" PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}") + target_link_libraries("${NAME}" PRIVATE ${FREETYPE2_LIBRARIES}) + 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(line) +add_example(rectangle) +add_example(red) +add_example(texture) +add_example(raw_mode) +add_example(framebuf_info) \ No newline at end of file -- cgit v1.2.3 From 274cacc5fadb3c3a220b833f027eee47cbda824c Mon Sep 17 00:00:00 2001 From: dario-loi Date: Sun, 24 Nov 2024 20:09:43 +0100 Subject: Worked on minor changes for warnings --- CMakeLists.txt | 1 + examples/empty_example.c | 2 +- fbgl.h | 36 ++++++++++++++++++------------------ 3 files changed, 20 insertions(+), 19 deletions(-) (limited to 'CMakeLists.txt') diff --git a/CMakeLists.txt b/CMakeLists.txt index 03453ef..02aa36d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,6 +15,7 @@ include_directories(${FREETYPE2_INCLUDE_DIRS}) function(add_example NAME) add_executable("${NAME}" "${EXAMPLES_DIR}/${NAME}.c" "${FBGL_HEADER}") target_compile_features("${NAME}" PRIVATE c_std_99) + target_compile_options("${NAME}" PRIVATE -Wall -Wextra -Wpedantic) target_include_directories("${NAME}" PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}") target_link_libraries("${NAME}" PRIVATE ${FREETYPE2_LIBRARIES}) add_custom_target("run_${NAME}" COMMAND "${NAME}" VERBATIM) diff --git a/examples/empty_example.c b/examples/empty_example.c index 09f7d88..352824a 100644 --- a/examples/empty_example.c +++ b/examples/empty_example.c @@ -43,7 +43,7 @@ int main() fprintf(stdout, "ESC pressed\n"); break; } - //fbgl_set_bg(&buffer, i++); // Set background color to + //fbgl_set_bg(&buffer, i++); // Set background color to for (int i = 0x000000; i <= 0xFFFFFF; i++) { fbgl_set_bg(&buffer, i); } diff --git a/fbgl.h b/fbgl.h index d92bfb7..75abb6f 100644 --- a/fbgl.h +++ b/fbgl.h @@ -127,7 +127,7 @@ int fbgl_hide_cursor(int fd) #endif // FBGL_HIDE_CURSOR #ifdef FBGL_USE_FREETYPE -FT_Library fbgl_freetype_init(); +FT_Library fbgl_freetype_init(void); void fbgl_freetype_cleanup(FT_Library library); FT_Face fbgl_load_font(FT_Library library, const char *font_path, int font_size); @@ -144,11 +144,11 @@ extern "C" { */ char const *fbgl_name_info(void); char const *fbgl_version_info(void); -void fbgl_enable_raw_mode(); -void fbgl_disable_raw_mode(); +void fbgl_enable_raw_mode(void); +void fbgl_disable_raw_mode(void); void fbgl_cleanup(int sig); -int fbgl_check_esc_key(); -void fbgl_set_signal_handlers(); +int fbgl_check_esc_key(void); +void fbgl_set_signal_handlers(void); /*Create and destroy methods*/ int fbgl_init(const char *device, fbgl_t *fb); @@ -164,7 +164,7 @@ void fbgl_draw_line(fbgl_point_t x, fbgl_point_t y, uint32_t color, fbgl_t *fb); /** * Display methods */ -void fbgl_display(); +void fbgl_display(void); /** * Access framebuffer data methods @@ -194,9 +194,9 @@ void fbgl_draw_texture(fbgl_t *fb, fbgl_tga_texture_t const *texture, int32_t x, /** * Keyboard */ -int fbgl_keyboard_init(); -void fbgl_keyboard_clean(); -void fbgl_keyboard_update(); +int fbgl_keyboard_init(void); +void fbgl_keyboard_clean(void); +void fbgl_keyboard_update(void); bool fbgl_key_pressed(unsigned char key); bool fbgl_key_released(unsigned char key); bool fbgl_key_down(unsigned char key); @@ -291,7 +291,7 @@ void fbgl_set_bg(fbgl_t *fb, uint32_t color) #endif // DEBUG // Fill the entire framebuffer with the specified color - for (int i = 0; i < fb->screen_size; i++) { + for (uint32_t i = 0; i < fb->width * fb->height; i++) { fb->pixels[i] = color; } } @@ -313,7 +313,7 @@ void fbgl_put_pixel(int x, int y, uint32_t color, fbgl_t *fb) fb->pixels[index] = color; } -void fbgl_enable_raw_mode() +void fbgl_enable_raw_mode(void) { struct termios raw; @@ -329,7 +329,7 @@ void fbgl_enable_raw_mode() } } -void fbgl_disable_raw_mode() +void fbgl_disable_raw_mode(void) { if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &orig_termios) == -1) { perror("tcsetattr"); @@ -343,7 +343,7 @@ void fbgl_cleanup(int sig) exit(sig); } -int fbgl_check_esc_key() +int fbgl_check_esc_key(void) { char c; struct timeval tv = { 0, 0 }; // Timeout of 0, to poll immediately @@ -368,7 +368,7 @@ int fbgl_check_esc_key() return 0; } -void fbgl_set_signal_handlers() +void fbgl_set_signal_handlers(void) { struct sigaction sa; sa.sa_handler = fbgl_cleanup; @@ -383,7 +383,7 @@ void fbgl_set_signal_handlers() } #ifdef FBGL_USE_FREETYPE -FT_Library fbgl_freetype_init() +FT_Library fbgl_freetype_init(void) { FT_Library library; if (FT_Init_FreeType(&library)) { @@ -419,8 +419,8 @@ void fbgl_render_freetype_text(fbgl_t *fb, FT_Library library, FT_Face face, FT_Bitmap bitmap = face->glyph->bitmap; // Draw the bitmap to framebuffer - for (int32_t j = 0; j < bitmap.rows; j++) { - for (int32_t i = 0; i < bitmap.width; i++) { + for (uint32_t j = 0; j < bitmap.rows; j++) { + for (uint32_t i = 0; i < bitmap.width; i++) { if (bitmap.buffer[j * bitmap.width + i]) { // Check pixel is not empty fbgl_put_pixel(x + i, y + j, 0xFF0000, @@ -707,4 +707,4 @@ AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ------------------------------------------------------------------------------ -*/ \ No newline at end of file +*/ -- cgit v1.2.3