diff options
| author | Levent Kaya <42411502+lvntky@users.noreply.github.com> | 2024-11-25 00:44:07 +0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-11-25 00:44:07 +0300 |
| commit | 8aed83d2e77c006eebf21776487f788ef12216de (patch) | |
| tree | 14d7dfd2b4c86dcb3b27912513439b4648145733 /fbgl.h | |
| parent | ff43c66c491b443b7522a3b3d716905ee9732b60 (diff) | |
| parent | 8650f5e1d7bb72625ae5d6cda2974f6fafa6362d (diff) | |
Merge pull request #8 from dario-loi/bugfixes
Diffstat (limited to 'fbgl.h')
| -rw-r--r-- | fbgl.h | 116 |
1 files changed, 66 insertions, 50 deletions
@@ -52,20 +52,20 @@ * Structs */ typedef struct fbgl { - int width; - int height; - int fd; - size_t screen_size; + int32_t width; + int32_t height; + int32_t fd; + uint32_t screen_size; uint32_t *pixels; struct fb_var_screeninfo vinfo; // Variable screen information struct fb_fix_screeninfo finfo; // Fixed screen information } fbgl_t; typedef struct fbgl_window { - int x; // Top-left x-coordinate of the window - int y; // Top-left y-coordinate of the window - int width; // Width of the window - int height; // Height of the window + int32_t x; // Top-left x-coordinate of the window + int32_t y; // Top-left y-coordinate of the window + uint32_t width; // Width of the window + uint32_t height; // Height of the window fbgl_t *fb; // Pointer to the framebuffer context } fbgl_window_t; @@ -82,8 +82,8 @@ typedef struct fbgl_psf2_header { } fbgl_psf2_header_t; typedef struct fbgl_point { - size_t x; - size_t y; + int32_t x; + int32_t y; } fbgl_point_t; typedef struct fbgl_tga_texture { @@ -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); @@ -160,19 +160,13 @@ void fbgl_destroy(fbgl_t *fb); 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 - */ -void fbgl_display(); /** * Access framebuffer data methods */ -uint32_t *fb_get_data(void); -int fb_get_width(void); -int fb_get_height(void); +uint32_t *fb_get_data(fbgl_t const *fb); +uint32_t fb_get_width(fbgl_t const *fb); +uint32_t fb_get_height(fbgl_t const *fb); /** * Shapes @@ -189,14 +183,15 @@ void fbgl_draw_rectangle_filled(fbgl_point_t top_left, */ 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); +void fbgl_draw_texture(fbgl_t *fb, fbgl_tga_texture_t const *texture, int32_t x, + int32_t y); /** * 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); @@ -283,13 +278,15 @@ void fbgl_destroy(fbgl_t *fb) void fbgl_set_bg(fbgl_t *fb, uint32_t color) { +#ifdef DEBUG if (!fb || fb->fd == -1) { fprintf(stderr, "Error: framebuffer not initialized.\n"); return; } +#endif // DEBUG // Fill the entire framebuffer with the specified color - for (size_t i = 0; i < (fb->width * fb->height); ++i) { + for (int32_t i = 0; i < fb->width * fb->height; i++) { fb->pixels[i] = color; } } @@ -306,11 +303,12 @@ void fbgl_put_pixel(int x, int y, uint32_t color, fbgl_t *fb) return; // Ignore out-of-bound coordinates } #endif // FBGL_VALIDATE_PUT_PIXEL - size_t index = y * fb->width + x; + + const size_t index = y * fb->width + x; fb->pixels[index] = color; } -void fbgl_enable_raw_mode() +void fbgl_enable_raw_mode(void) { struct termios raw; @@ -326,7 +324,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"); @@ -340,7 +338,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 @@ -365,7 +363,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; @@ -379,10 +377,8 @@ void fbgl_set_signal_handlers() } } -#endif - #ifdef FBGL_USE_FREETYPE -FT_Library fbgl_freetype_init() +FT_Library fbgl_freetype_init(void) { FT_Library library; if (FT_Init_FreeType(&library)) { @@ -411,15 +407,15 @@ 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) + const char *text, int32_t x, int32_t y) { while (*text) { FT_Load_Char(face, *text, FT_LOAD_RENDER); FT_Bitmap bitmap = face->glyph->bitmap; // Draw the bitmap to framebuffer - for (int j = 0; j < bitmap.rows; j++) { - for (int 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, @@ -434,26 +430,27 @@ void fbgl_render_freetype_text(fbgl_t *fb, FT_Library library, FT_Face face, } #endif // FBGL_USE_FREETYPE + void fbgl_draw_line(fbgl_point_t x, fbgl_point_t y, uint32_t color, fbgl_t *buffer) { - int dx = abs(y.x - x.x); - int dy = abs(y.y - x.y); + const int32_t dx = abs(y.x - x.x); + const int32_t dy = abs(y.y - x.y); - int sx = (x.x < y.x) ? 1 : -1; - int sy = (x.y < y.y) ? 1 : -1; + const int32_t sx = (x.x < y.x) ? 1 : -1; + const int32_t sy = (x.y < y.y) ? 1 : -1; - int err = dx - dy; + int32_t err = dx - dy; while (1) { // Set the pixel at the current position fbgl_put_pixel(x.x, x.y, color, buffer); // If we've reached the end point, break - if (x.x == y.x && x.y == y.y) + if (x.x >= y.x && x.y >= y.y) break; - int e2 = 2 * err; + const int32_t e2 = 2 * err; if (e2 > -dy) { err -= dy; @@ -495,9 +492,9 @@ void fbgl_draw_rectangle_filled(fbgl_point_t top_left, fbgl_point_t bottom_right, uint32_t color, fbgl_t *fb) { - for (int y = top_left.y; y < bottom_right.y; y++) { + for (int32_t y = top_left.y; y < bottom_right.y; y++) { // Manually set each pixel in the row - for (int x = top_left.x; x < bottom_right.x; x++) { + for (int32_t x = top_left.x; x < bottom_right.x; x++) { fbgl_put_pixel(x, y, color, fb); } } @@ -615,7 +612,8 @@ 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) +void fbgl_draw_texture(fbgl_t *fb, fbgl_tga_texture_t const *texture, int32_t x, + int32_t y) { if (!fb || !texture || !texture->data) { return; @@ -641,6 +639,24 @@ void fbgl_draw_texture(fbgl_t *fb, fbgl_tga_texture_t *texture, int x, int y) } } } + +uint32_t fb_get_width(fbgl_t const *fb) +{ + return fb->width; +} + +uint32_t fb_get_height(fbgl_t const *fb) +{ + return fb->height; +} + +uint32_t *fb_get_data(fbgl_t const *fb) +{ + return fb->pixels; +} + +#endif // FBGL_IMPLEMENTATION + #ifdef __cplusplus } // extern "C" #endif |
