Some line drawing functions

This commit is contained in:
Thomas Lovén 2022-01-13 00:56:12 +01:00
parent a162680f5a
commit 54832f2636
4 changed files with 55 additions and 1 deletions

View File

@ -3,13 +3,19 @@
#include <mittos/graphics.h> #include <mittos/graphics.h>
extern gfx_context *term_fb; extern gfx_context *term_fb;
extern gfx_context kernel_fb;
void thread1() void thread1()
{ {
int a = 0; int a = 1;
gfx_context *ctx = framebuffer_make_subcontext(&kernel_fb, 700, 300, 100, 100);
while(1) { while(1) {
putCharacter(term_fb, 0, 0, RGB(255,255,0), RGB(0,0,0), '0'+(a++%10)); putCharacter(term_fb, 0, 0, RGB(255,255,0), RGB(0,0,0), '0'+(a++%10));
flip(term_fb); flip(term_fb);
draw_line(ctx, 0, 100, 50, ((a-1)%100), RGB(0,0,0));
draw_line(ctx, 0, 100, 50, (a%100), RGB(255,0,0));
flip(ctx);
sched_yield(); sched_yield();
} }
} }
@ -19,8 +25,10 @@ void thread2()
int a = 0; int a = 0;
while(1) { while(1) {
putCharacter(term_fb, 10, 10, RGB(0,255,255), RGB(0,0,0), 'A'+(a++%10)); putCharacter(term_fb, 10, 10, RGB(0,255,255), RGB(0,0,0), 'A'+(a++%10));
draw_rect(term_fb, 10, 10, 200, 300, RGB((uint8_t)(a%256), 0, 255-(uint8_t)(a%256)));
flip(term_fb); flip(term_fb);
sched_yield(); sched_yield();
} }
} }

View File

@ -60,4 +60,11 @@ void fbterm_init(struct fbinfo *fbinfo)
flip(term_fb); flip(term_fb);
term_fb = framebuffer_make_subcontext(&kernel_fb, 24, 24, 8*VGA_COLS, 16*VGA_ROWS); term_fb = framebuffer_make_subcontext(&kernel_fb, 24, 24, 8*VGA_COLS, 16*VGA_ROWS);
setup = 1; setup = 1;
draw_line(&kernel_fb, 750, 850, 120, 120, RGB(255,0,0));
draw_line(&kernel_fb, 750, 750, 120, 220, RGB(0,255,0));
draw_line(&kernel_fb, 750, 850, 120, 220, RGB(0,0,255));
draw_line(&kernel_fb, 750, 800, 120, 220, RGB(255,0,255));
draw_line(&kernel_fb, 750, 850, 120, 170, RGB(255,255,0));
flip(&kernel_fb);
} }

View File

@ -17,6 +17,43 @@ void putpixel(gfx_context *ctx, uint64_t x, uint64_t y, uint32_t clr)
*fb = clr; *fb = clr;
} }
void draw_line(gfx_context *ctx, uint64_t x0, uint64_t x1, uint64_t y0, uint64_t y1, uint32_t clr)
{
int64_t dx = x1 > x0 ? x1 - x0 : x0 - x1;
int64_t dy = y1 > y0 ? y1 - y0 : y0 - y1;
int sx = x1 > x0 ? 1 : -1;
int sy = y1 > y0 ? 1 : -1;
uint64_t x = x0, y = y0;
int64_t diff = dx - dy;
while(1)
{
putpixel(ctx, x, y, clr);
if(x == x1 && y == y1) break;
if((2*diff) > -dy) {
diff -= dy;
x += sx;
} else {
diff += dx;
y += sy;
}
}
}
void draw_rect(gfx_context *ctx, uint64_t x, uint64_t y, uint64_t width, uint64_t height, uint32_t clr)
{
for(uint64_t _x = x; _x <= x+width; _x++)
{
putpixel(ctx, _x, y, clr);
putpixel(ctx, _x, y+height, clr);
}
for(uint64_t _y = y; _y <= y+height; _y++)
{
putpixel(ctx, x, _y, clr);
putpixel(ctx, x+width, _y, clr);
}
}
void putCharacter(gfx_context *ctx, uint64_t x, uint64_t y, uint32_t clr_fg, uint32_t clr_bg, char c) void putCharacter(gfx_context *ctx, uint64_t x, uint64_t y, uint32_t clr_fg, uint32_t clr_bg, char c)
{ {

View File

@ -14,6 +14,8 @@ typedef struct {
#define RGB(r, g, b) (((uint32_t) (r<<16) + (g<<8) + (b))) #define RGB(r, g, b) (((uint32_t) (r<<16) + (g<<8) + (b)))
void putpixel(gfx_context *ctx, uint64_t x, uint64_t y, uint32_t clr); void putpixel(gfx_context *ctx, uint64_t x, uint64_t y, uint32_t clr);
void draw_line(gfx_context *ctx, uint64_t x0, uint64_t x1, uint64_t y0, uint64_t y1, uint32_t clr);
void draw_rect(gfx_context *ctx, uint64_t x, uint64_t y, uint64_t width, uint64_t height, uint32_t clr);
void putCharacter(gfx_context *ctx, uint64_t x, uint64_t y, uint32_t clr_fg, uint32_t clr_bg, char c); void putCharacter(gfx_context *ctx, uint64_t x, uint64_t y, uint32_t clr_fg, uint32_t clr_bg, char c);
void flip(gfx_context *ctx); void flip(gfx_context *ctx);
gfx_context *framebuffer_make_subcontext(gfx_context *ctx, uint64_t x, uint64_t y, uint64_t width, uint64_t height); gfx_context *framebuffer_make_subcontext(gfx_context *ctx, uint64_t x, uint64_t y, uint64_t width, uint64_t height);