From d322babce46ee9041aabd3471be002323f3481c6 Mon Sep 17 00:00:00 2001 From: Levent Kaya Date: Mon, 3 Nov 2025 16:51:18 +0300 Subject: [feature] eliminate the dependency of mathlib linking --- emu/fbgl_viewer.c | 35 +++++++++++++++++++++++++++++++++++ fbgl.h | 19 ++++++++++++++++--- 2 files changed, 51 insertions(+), 3 deletions(-) create mode 100644 emu/fbgl_viewer.c diff --git a/emu/fbgl_viewer.c b/emu/fbgl_viewer.c new file mode 100644 index 0000000..c6e077d --- /dev/null +++ b/emu/fbgl_viewer.c @@ -0,0 +1,35 @@ +#include +#include + +#define SCREEN_WIDTH 800 +#define SCREEN_HEIGHT 600 +#define SCREEN_TITLE "FBGL Viewer" + +int main() +{ + if (SDL_Init(SDL_INIT_VIDEO) < 0) { + printf("Couldn't initialize SDL: %s\n", SDL_GetError()); + return EXIT_FAILURE; + } + + SDL_Window *window = SDL_CreateWindow(SCREEN_TITLE, + SDL_WINDOWPOS_UNDEFINED, + SDL_WINDOWPOS_UNDEFINED, + SCREEN_WIDTH, SCREEN_HEIGHT, 0); + if (!window) { + printf("Failed to open %d x %d window: %s\n", SCREEN_WIDTH, + SCREEN_HEIGHT, SDL_GetError()); + return EXIT_FAILURE; + } + + SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, 0); + SDL_SetRenderDrawColor(renderer, 255, 255, 255, 250); + SDL_RenderClear(renderer); + SDL_RenderPresent(renderer); + SDL_Delay(2000); + SDL_DestroyWindow(window); + SDL_DestroyRenderer(renderer); + SDL_Quit(); + + return 0; +} diff --git a/fbgl.h b/fbgl.h index 5bd95c7..6a0f33a 100644 --- a/fbgl.h +++ b/fbgl.h @@ -189,10 +189,23 @@ bool fbgl_is_key_pressed(fbgl_key_t key); (uint8_t)(b * 255))) #define FBGL_F32RGBA_TO_U32(r, g, b, a) ((uint32_t)(((uint8_t)(a * 255) << 24) | ((uint8_t)(r * 255) << 16) | ((uint8_t)(g * 255) << 8) | (uint8_t)(b * 255)) +#define FBGL_INLINE static inline + // Inside functions static void i_fbgl_die(const char *s); static void i_fbgl_disable_raw_mode(); static void i_fbgl_enable_raw_mode(); +FBGL_INLINE i_fbgl_abs_int(int x); + + FBGL_INLINE i_fbgl_sqrt_int(int x); + + FBGL_INLINE i_fbgl_abs_int(int x) { + return x < 0 ? -x : x; + } + + FBGL_INLINE i_fbgl_sqrt_int(int x) { + return x * x; + } static void i_fbgl_die(const char *s) { @@ -330,8 +343,8 @@ 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 *buffer) { - const int32_t dx = abs(y.x - x.x); - const int32_t dy = abs(y.y - x.y); + const int32_t dx = i_fbgl_abs_int(y.x - x.x); + const int32_t dy = i_fbgl_abs_int(y.y - x.y); const int32_t sx = (x.x < y.x) ? 1 : -1; const int32_t sy = (x.y < y.y) ? 1 : -1; @@ -435,7 +448,7 @@ void fbgl_draw_circle_filled(int x, int y, int radius, uint32_t color, fbgl_t *fb) { for (int yy = -radius; yy <= radius; ++yy) { - int half_width = (int)sqrt(radius * radius - yy * yy); + int half_width = (int)i_fbgl_sqrt_int(radius * radius - yy * yy); int row_start = x - half_width; int row_end = x + half_width; -- cgit v1.2.3