Skip to content
Snippets Groups Projects
Select Git revision
  • 497a094634ae9c4b4779545e2f2019d6e3c25a25
  • master default protected
2 results

gfx.c

Blame
  • gfx.c 5.77 KiB
    #include "gfx.h"
    #include "framebuffer.h"
    #include <math.h>
    #include <stddef.h>
    #include <stdlib.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 */
    };
    
    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)
    		return;
    
    	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 };
    	return r;
    }
    
    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)
    		return 0;
    	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 - ' ';
    	const uint8_t *letter_ptr = font->table + bytes_per_letter * letter;
    	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
    ) {
    	for (int yo = 0; yo < font->Height; yo++) {
    		for (int xo = 0; xo < font->Width; xo++) {
    			int lb = letter_bit(font, ch, xo, yo);
    
    			if (fg != bg) {
    				Color c = lb ? fg : bg;
    				gfx_setpixel(r, x + xo, y + yo, c);