diff --git a/lib/gfx/framebuffer.c b/lib/gfx/framebuffer.c index d577c619b6a7460ac5160f372d266f8d2afe67e3..672e956a3467999a9d8c232acb3510f4711786cb 100644 --- a/lib/gfx/framebuffer.c +++ b/lib/gfx/framebuffer.c @@ -1,23 +1,26 @@ #include "framebuffer.h" -void fb_clear_to_color(struct framebuffer *fb, Color c) { +void fb_clear_to_color(struct framebuffer *fb, Color c) +{ for (int y = 0; y < fb->height; y++) { for (int x = 0; x < fb->width; x++) fb_setpixel(fb, x, y, c); } } -void fb_clear(struct framebuffer *fb) { +void fb_clear(struct framebuffer *fb) +{ Color c = fb->encode_color_rgb(fb, 0, 0, 0); fb_clear_to_color(fb, c); } -Color fb_encode_color_rgb(struct framebuffer *fb, int r, int g, int b) { +Color fb_encode_color_rgb(struct framebuffer *fb, int r, int g, int b) +{ return fb->encode_color_rgb(fb, r, g, b); } -Color fb_encode_color_rgb_f(struct framebuffer *fb, float r, float g, - float b) { +Color fb_encode_color_rgb_f(struct framebuffer *fb, float r, float g, float b) +{ float r8 = r > 1.0f ? 1.0f : (uint8_t)(r * 255.0f); float g8 = g > 1.0f ? 1.0f : (uint8_t)(g * 255.0f); float b8 = b > 1.0f ? 1.0f : (uint8_t)(b * 255.0f); @@ -25,26 +28,29 @@ Color fb_encode_color_rgb_f(struct framebuffer *fb, float r, float g, r8 = r8 < .0f ? .0f : r8; g8 = g8 < .0f ? .0f : g8; b8 = b8 < .0f ? .0f : b8; - + return fb->encode_color_rgb(fb, r8, g8, b8); } -void fb_copy_raw(struct framebuffer *fb, const void *data, - size_t size) { +void fb_copy_raw(struct framebuffer *fb, const void *data, size_t size) +{ size_t to_copy = size < fb->stride ? size : fb->stride; memcpy(fb->data, data, to_copy); } -void fb_update(struct framebuffer *fb) { +void fb_update(struct framebuffer *fb) +{ fb->update(fb); } -size_t fb_bytes_per_pixel(const struct framebuffer *fb) { +size_t fb_bytes_per_pixel(const struct framebuffer *fb) +{ const int pixels = fb->height * fb->width; return fb->stride / pixels; } -void *fb_pixel(struct framebuffer *fb, int x, int y) { +void *fb_pixel(struct framebuffer *fb, int x, int y) +{ int xo; int yo; @@ -52,7 +58,7 @@ void *fb_pixel(struct framebuffer *fb, int x, int y) { case FB_O_0: xo = x; yo = y; - break; + break; case FB_O_90: xo = fb->width - y - 1; yo = x; @@ -78,13 +84,14 @@ void *fb_pixel(struct framebuffer *fb, int x, int y) { return (void *)(fb->data) + yo * fb->width * bpp + xo * bpp; } -void fb_setpixel(struct framebuffer *fb, int x, int y, Color c) { +void fb_setpixel(struct framebuffer *fb, int x, int y, Color c) +{ uint8_t *pixel = fb_pixel(fb, x, y); if (pixel == NULL) return; const uint8_t *color = (const uint8_t *)(&c); - const size_t bpp = fb_bytes_per_pixel(fb); + const size_t bpp = fb_bytes_per_pixel(fb); switch (bpp) { default: case 2: diff --git a/lib/gfx/gfx.c b/lib/gfx/gfx.c index b21aaa8613436473419bb5b925c070578ed8b277..4bc9f35da40eb1808df664ce7da0d7d41186d106 100644 --- a/lib/gfx/gfx.c +++ b/lib/gfx/gfx.c @@ -5,15 +5,16 @@ #include <math.h> const struct gfx_color_rgb gfx_colors_rgb[COLORS] = { - { 255, 255, 255 }, /* WHITE */ - { 0, 0, 0 }, /* BLACK */ - { 255, 0, 0 }, /* RED */ - { 0, 255, 0 }, /* GREEN */ - { 0, 0, 255 }, /* BLUE */ - { 255, 255, 0 } /* YELLOW */ + { 255, 255, 255 }, /* WHITE */ + { 0, 0, 0 }, /* BLACK */ + { 255, 0, 0 }, /* RED */ + { 0, 255, 0 }, /* GREEN */ + { 0, 0, 255 }, /* BLUE */ + { 255, 255, 0 } /* YELLOW */ }; -void gfx_setpixel(struct gfx_region *r, int x, int y, Color c) { +void gfx_setpixel(struct gfx_region *r, int x, int y, Color c) +{ if (x < 0 || y < 0) return; if (x >= r->width || y >= r->height) @@ -22,18 +23,18 @@ void gfx_setpixel(struct gfx_region *r, int x, int y, Color c) { fb_setpixel(r->fb, r->x + x, r->y + y, c); } -struct gfx_region gfx_screen(struct framebuffer *fb) { - struct gfx_region r = { - .fb = fb, - .x = 0, - .y = 0, - .width = fb->width, - .height = fb->height - }; +struct gfx_region gfx_screen(struct framebuffer *fb) +{ + struct gfx_region r = { .fb = fb, + .x = 0, + .y = 0, + .width = fb->width, + .height = fb->height }; return r; } -static inline int letter_bit(sFONT *font, char c, int x, int y) { +static inline int letter_bit(sFONT *font, char c, int x, int y) +{ if (x < 0 || y < 0) return 0; if (x >= font->Width || y >= font->Height) @@ -41,18 +42,25 @@ static inline int letter_bit(sFONT *font, char c, int x, int y) { if (c < ' ' || c > '~') return 0; - size_t bytes_per_row = font->Width / 8 + 1; - size_t bytes_per_letter = bytes_per_row * font->Height; - int letter = c - ' '; + size_t bytes_per_row = font->Width / 8 + 1; + size_t bytes_per_letter = bytes_per_row * font->Height; + int letter = c - ' '; const uint8_t *letter_ptr = font->table + bytes_per_letter * letter; - int horz_byte = x / 8; - int horz_bit = 7 - x % 8; + int horz_byte = x / 8; + int horz_bit = 7 - x % 8; return (*(letter_ptr + y * bytes_per_row + horz_byte) >> horz_bit) & 1; } -void gfx_putchar(sFONT *font, struct gfx_region *r, int x, int y, char ch, - Color fg, Color bg) { +void gfx_putchar( + sFONT *font, + struct gfx_region *r, + int x, + int y, + char ch, + Color fg, + Color bg +) { for (int yo = 0; yo < font->Height; yo++) { for (int xo = 0; xo < font->Width; xo++) { int lb = letter_bit(font, ch, xo, yo); @@ -69,8 +77,15 @@ void gfx_putchar(sFONT *font, struct gfx_region *r, int x, int y, char ch, } } -void gfx_puts(sFONT *font, struct gfx_region *r, int x, int y, const char *str, - Color fg, Color bg) { +void gfx_puts( + sFONT *font, + struct gfx_region *r, + int x, + int y, + const char *str, + Color fg, + Color bg +) { while (*str) { x += font->Width; if (x >= r->width) { @@ -84,31 +99,37 @@ void gfx_puts(sFONT *font, struct gfx_region *r, int x, int y, const char *str, } } -Color gfx_color_rgb_f(struct gfx_region *reg, float r, float g, float b) { - return fb_encode_color_rgb_f(reg->fb, r, g, b); +Color gfx_color_rgb_f(struct gfx_region *reg, float r, float g, float b) +{ + return fb_encode_color_rgb_f(reg->fb, r, g, b); } -Color gfx_color_rgb(struct gfx_region *reg, uint8_t r, uint8_t g, uint8_t b) { +Color gfx_color_rgb(struct gfx_region *reg, uint8_t r, uint8_t g, uint8_t b) +{ return fb_encode_color_rgb(reg->fb, r, g, b); } -void gfx_update(struct gfx_region *reg) { +void gfx_update(struct gfx_region *reg) +{ reg->fb->update(reg->fb); } -void gfx_clear_to_color(struct gfx_region *reg, Color c) { - fb_clear_to_color(reg->fb, c); +void gfx_clear_to_color(struct gfx_region *reg, Color c) +{ + fb_clear_to_color(reg->fb, c); } -void gfx_clear(struct gfx_region *reg) { +void gfx_clear(struct gfx_region *reg) +{ gfx_clear_to_color(reg, gfx_color(reg, BLACK)); } -void gfx_circle(struct gfx_region *reg, int x, int y, int r, int t, Color c) { +void gfx_circle(struct gfx_region *reg, int x, int y, int r, int t, Color c) +{ for (int y_ = y - r; y_ <= y + r; y_++) { for (int x_ = x - r; x_ <= x + r; x_++) { - int dx = (x_ - x) * (x_ - x); - int dy = (y_ - y) * (y_ - y); + int dx = (x_ - x) * (x_ - x); + int dy = (y_ - y) * (y_ - y); int outer = (r + t) * (r + t); int inner = r * r; int edge = ((dx + dy) >= inner) && ((dx + dy) <= outer); @@ -118,11 +139,12 @@ void gfx_circle(struct gfx_region *reg, int x, int y, int r, int t, Color c) { } } -void gfx_circle_fill(struct gfx_region *reg, int x, int y, int r, Color c) { +void gfx_circle_fill(struct gfx_region *reg, int x, int y, int r, Color c) +{ for (int y_ = y - r; y_ <= y + r; y_++) { for (int x_ = x - r; x_ <= x + r; x_++) { - int dx = (x_ - x) * (x_ - x); - int dy = (y_ - y) * (y_ - y); + int dx = (x_ - x) * (x_ - x); + int dy = (y_ - y) * (y_ - y); int edge = r * r; int fill = (dx + dy) <= edge; if (fill) @@ -131,8 +153,9 @@ void gfx_circle_fill(struct gfx_region *reg, int x, int y, int r, Color c) { } } -void gfx_rectangle(struct gfx_region *reg, int x, int y, int w, int h, - int t, Color c) { +void gfx_rectangle( + struct gfx_region *reg, int x, int y, int w, int h, int t, Color c +) { if (t > 1) { gfx_thick_line(reg, x, y, x + w, y, t, c); gfx_thick_line(reg, x, y + h, x + w, y + h, t, c); @@ -146,20 +169,22 @@ void gfx_rectangle(struct gfx_region *reg, int x, int y, int w, int h, } } -void gfx_rectangle_fill(struct gfx_region *reg, int x, int y, int w, int h, - Color c) { +void gfx_rectangle_fill( + struct gfx_region *reg, int x, int y, int w, int h, Color c +) { for (int y_ = y; y_ < y + h; y_++) { for (int x_ = x; x_ < x + w; x_++) gfx_setpixel(reg, x_, y_, c); } } -void gfx_line(struct gfx_region *reg, int x1, int y1, int x2, int y2, Color c) { +void gfx_line(struct gfx_region *reg, int x1, int y1, int x2, int y2, Color c) +{ float dx = x2 - x1; float dy = y2 - y1; float de = fabs(dy / dx); - float e = .0f; - int y = y1; + float e = .0f; + int y = y1; for (int x = x1; x < x2; x++) { gfx_setpixel(reg, x, y, c); e += de; @@ -170,13 +195,14 @@ void gfx_line(struct gfx_region *reg, int x1, int y1, int x2, int y2, Color c) { } } -void gfx_thick_line(struct gfx_region *reg, int x1, int y1, int x2, int y2, - int t, Color c) { +void gfx_thick_line( + struct gfx_region *reg, int x1, int y1, int x2, int y2, int t, Color c +) { float dx = x2 - x1; float dy = y2 - y1; float de = fabs(dy / dx); - float e = .0f; - int y = y1; + float e = .0f; + int y = y1; for (int x = x1; x < x2; x++) { gfx_circle_fill(reg, x, y, t, c); e += de; @@ -187,16 +213,24 @@ void gfx_thick_line(struct gfx_region *reg, int x1, int y1, int x2, int y2, } } -Color gfx_color(struct gfx_region *reg, enum gfx_color color) { +Color gfx_color(struct gfx_region *reg, enum gfx_color color) +{ if ((int)(color) >= COLORS) return 0; - + const struct gfx_color_rgb *c = &gfx_colors_rgb[color]; return gfx_color_rgb(reg, c->r, c->g, c->b); } -void gfx_copy_region_raw(struct gfx_region *reg, int x, int y, int w, int h, - size_t bpp, const void *p) { +void gfx_copy_region_raw( + struct gfx_region *reg, + int x, + int y, + int w, + int h, + size_t bpp, + const void *p +) { for (int y_ = 0; y_ < h; y_++) { for (int x_ = 0; x_ < w; x_++) { Color c; @@ -214,6 +248,7 @@ void gfx_copy_region_raw(struct gfx_region *reg, int x, int y, int w, int h, } } -void gfx_copy_raw(struct gfx_region *reg, const void *p, size_t size) { +void gfx_copy_raw(struct gfx_region *reg, const void *p, size_t size) +{ fb_copy_raw(reg->fb, p, size); } diff --git a/lib/gfx/textbuffer.c b/lib/gfx/textbuffer.c index 533b2316da44dc5e02bc65bced6b5c9e61868057..d2a8a39bcbc5dd1deb6d19ebad5a927e95fa31a3 100644 --- a/lib/gfx/textbuffer.c +++ b/lib/gfx/textbuffer.c @@ -2,58 +2,66 @@ #include "gfx.h" #include <string.h> -void txt_init(struct txt_buffer *txtb, struct gfx_region *reg, sFONT *f) { - txtb->reg = reg; - txtb->font = f; +void txt_init(struct txt_buffer *txtb, struct gfx_region *reg, sFONT *f) +{ + txtb->reg = reg; + txtb->font = f; txtb->cursor_column = 0; - txtb->cursor_row = 0; - txtb->fg_color = gfx_color_rgb_f(reg, 1.0f, 1.0f, 1.0f); - txtb->bg_color = gfx_color_rgb_f(reg, .0f, .0f, .0f); - txtb->draw_cursor = 1; - txtb->auto_update = 1; + txtb->cursor_row = 0; + txtb->fg_color = gfx_color_rgb_f(reg, 1.0f, 1.0f, 1.0f); + txtb->bg_color = gfx_color_rgb_f(reg, .0f, .0f, .0f); + txtb->draw_cursor = 1; + txtb->auto_update = 1; txt_clear(txtb); } -static inline size_t width_(struct txt_buffer *tm) { +static inline size_t width_(struct txt_buffer *tm) +{ return tm->reg->width / tm->font->Width; } -static inline size_t height_(struct txt_buffer *tm) { +static inline size_t height_(struct txt_buffer *tm) +{ return tm->reg->height / tm->font->Height; } -size_t txt_width(struct txt_buffer *tm) { +size_t txt_width(struct txt_buffer *tm) +{ return width_(tm); } -size_t txt_height(struct txt_buffer *tm) { +size_t txt_height(struct txt_buffer *tm) +{ return height_(tm); } -static void scrollup(struct txt_buffer *tm) { - const int last_row = height_(tm) - 1; +static void scrollup(struct txt_buffer *tm) +{ + const int last_row = height_(tm) - 1; const size_t line_size = width_(tm) * sizeof(struct txt_glyph); for (int row = 0; row < last_row; row++) memcpy(&tm->text[row][0], &tm->text[row + 1][0], line_size); for (int col = 0; col < width_(tm); col++) { struct txt_glyph *g = &tm->text[last_row][col]; - g->ch = ' '; - g->fg_color = tm->fg_color; - g->bg_color = tm->bg_color; + g->ch = ' '; + g->fg_color = tm->fg_color; + g->bg_color = tm->bg_color; } } -static void newline(struct txt_buffer *tm) { +static void newline(struct txt_buffer *tm) +{ const int last_row = height_(tm) - 1; scrollup(tm); - tm->cursor_row = last_row; + tm->cursor_row = last_row; tm->cursor_column = 0; } -static inline void advance_cursor(struct txt_buffer *tm) { +static inline void advance_cursor(struct txt_buffer *tm) +{ const int last_row = height_(tm) - 1; tm->cursor_column++; @@ -65,13 +73,14 @@ static inline void advance_cursor(struct txt_buffer *tm) { } } -static void tab(struct txt_buffer *tm) { - while(tm->cursor_column % 5) +static void tab(struct txt_buffer *tm) +{ + while (tm->cursor_column % 5) txt_putchar(tm, ' '); } -static inline struct txt_glyph *getchar_(struct txt_buffer *tm, int x, - int y) { +static inline struct txt_glyph *getchar_(struct txt_buffer *tm, int x, int y) +{ if (x < 0 || x >= TEXTBUFFER_MAX_WIDTH) return NULL; if (y < 0 || y >= TEXTBUFFER_MAX_HEIGHT) @@ -79,7 +88,8 @@ static inline struct txt_glyph *getchar_(struct txt_buffer *tm, int x, return &tm->text[y][x]; } -void txt_clear(struct txt_buffer *tm) { +void txt_clear(struct txt_buffer *tm) +{ int w = width_(tm); int h = height_(tm); for (int r = 0; r < h; r++) { @@ -88,20 +98,21 @@ void txt_clear(struct txt_buffer *tm) { if (g == NULL) continue; - g->ch = ' '; + g->ch = ' '; g->fg_color = tm->fg_color; g->bg_color = tm->bg_color; } } tm->cursor_column = 0; - tm->cursor_row = 0; + tm->cursor_row = 0; if (tm->auto_update) txt_update(tm); } -void txt_putchar(struct txt_buffer *tm, char ch) { +void txt_putchar(struct txt_buffer *tm, char ch) +{ struct txt_glyph *g = getchar_(tm, tm->cursor_column, tm->cursor_row); if (g == NULL) return; @@ -114,7 +125,7 @@ void txt_putchar(struct txt_buffer *tm, char ch) { tab(tm); break; default: - g->ch = ch; + g->ch = ch; g->fg_color = tm->fg_color; g->bg_color = tm->bg_color; advance_cursor(tm); @@ -124,22 +135,26 @@ void txt_putchar(struct txt_buffer *tm, char ch) { txt_update(tm); } -void txt_puts(struct txt_buffer *tm, const char *str) { +void txt_puts(struct txt_buffer *tm, const char *str) +{ while (*str) { txt_putchar(tm, *str); str++; } } -static inline int cursor_px_column(struct txt_buffer *tm) { +static inline int cursor_px_column(struct txt_buffer *tm) +{ return tm->font->Width * tm->cursor_column; } -static inline int cursor_px_row(struct txt_buffer *tm) { +static inline int cursor_px_row(struct txt_buffer *tm) +{ return tm->font->Height * tm->cursor_row; } -static void draw_cursor_(struct txt_buffer *tm) { +static void draw_cursor_(struct txt_buffer *tm) +{ const int x = cursor_px_column(tm); const int y = cursor_px_row(tm); const int w = tm->font->Width; @@ -147,11 +162,12 @@ static void draw_cursor_(struct txt_buffer *tm) { gfx_rectangle_fill(tm->reg, x, y, w, h, tm->fg_color); } -void txt_draw(struct txt_buffer *tm) { - const int w = width_(tm); - const int h = height_(tm); +void txt_draw(struct txt_buffer *tm) +{ + const int w = width_(tm); + const int h = height_(tm); int px_column = 0; - int px_row = 0; + int px_row = 0; for (int r = 0; r < h; r++) { px_column = 0; @@ -160,8 +176,15 @@ void txt_draw(struct txt_buffer *tm) { if (g == NULL) continue; - gfx_putchar(tm->font, tm->reg, px_column, px_row, - g->ch, g->fg_color, g->bg_color); + gfx_putchar( + tm->font, + tm->reg, + px_column, + px_row, + g->ch, + g->fg_color, + g->bg_color + ); px_column += tm->font->Width; } @@ -172,8 +195,9 @@ void txt_draw(struct txt_buffer *tm) { draw_cursor_(tm); } -void txt_set_color_f(struct txt_buffer *tm, enum txt_color sw, float r, float g, - float b) { +void txt_set_color_f( + struct txt_buffer *tm, enum txt_color sw, float r, float g, float b +) { Color c = gfx_color_rgb_f(tm->reg, r, g, b); switch (c) { @@ -186,8 +210,8 @@ void txt_set_color_f(struct txt_buffer *tm, enum txt_color sw, float r, float g, } } -void txt_set_color(struct txt_buffer *tm, enum txt_color sw, int r, int g, - int b) { +void txt_set_color(struct txt_buffer *tm, enum txt_color sw, int r, int g, int b) +{ Color c = gfx_color_rgb(tm->reg, r, g, b); switch (c) { @@ -200,7 +224,8 @@ void txt_set_color(struct txt_buffer *tm, enum txt_color sw, int r, int g, } } -void txt_set_cursor(struct txt_buffer *tm, int x, int y, int draw_cursor) { +void txt_set_cursor(struct txt_buffer *tm, int x, int y, int draw_cursor) +{ tm->draw_cursor = draw_cursor; if (x < 0 || x >= width_(tm)) @@ -209,16 +234,18 @@ void txt_set_cursor(struct txt_buffer *tm, int x, int y, int draw_cursor) { return; tm->cursor_column = x; - tm->cursor_row = y; + tm->cursor_row = y; if (tm->auto_update) txt_update(tm); } -void txt_set_transparent(struct txt_buffer *tm) { +void txt_set_transparent(struct txt_buffer *tm) +{ tm->bg_color = tm->fg_color; } -void txt_update(struct txt_buffer *tm) { +void txt_update(struct txt_buffer *tm) +{ gfx_update(tm->reg); }