Add imperfect, but sufficient, rounded rectangle routine

This commit is contained in:
K. Lange 2018-07-20 21:22:34 +09:00
parent 3bafe770a1
commit 7cd46127a9
3 changed files with 56 additions and 0 deletions

View File

@ -63,6 +63,9 @@ static void draw_background(int width, int height) {
}
draw_rounded_rectangle(wallpaper_ctx, 300, 300, wallpaper_ctx->width - 600, wallpaper_ctx->height - 600, 30, rgba(0,0,0,127));
{
char * s = "It is now safe to shut down your virtual machine.";

View File

@ -91,3 +91,4 @@ extern void gfx_clear_clip(gfx_context_t * ctx);
extern uint32_t getBilinearFilteredPixelColor(sprite_t * tex, double u, double v);
extern uint32_t interp_colors(uint32_t bottom, uint32_t top, uint8_t interp);
extern void draw_rounded_rectangle(gfx_context_t * ctx, int32_t x, int32_t y, uint16_t width, uint16_t height, int radius, uint32_t color);

View File

@ -730,3 +730,55 @@ uint32_t interp_colors(uint32_t bottom, uint32_t top, uint8_t interp) {
uint8_t alp = (_ALP(bottom) * (255 - interp) + _ALP(top) * interp) / 255;
return rgba(red,gre,blu, alp);
}
void draw_rounded_rectangle(gfx_context_t * ctx, int32_t x, int32_t y, uint16_t width, uint16_t height, int radius, uint32_t color) {
/* Draw a rounded rectangle */
if (radius > width / 2) {
radius = width / 2;
}
if (radius > height / 2) {
radius = height / 2;
}
for (int row = y; row < y + height; row++){
for (int col = x; col < x + width; col++) {
if ((col < x + radius || col > x + width - radius - 1) &&
(row < y + radius || row > y + height - radius - 1)) {
continue;
}
GFX(ctx, col, row) = alpha_blend_rgba(GFX(ctx, col, row), color);
}
}
/* draw the actual rounding */
for (int i = 0; i < radius; ++i) {
long r2 = radius * radius;
long i2 = i * i;
long j2 = r2 - i2;
double j_max = sqrt((double)j2);
for (int j = 0; j <= (int)j_max; ++j) {
int _x = x + width - radius + i;
int _y = y + height - radius + j;
int _z = y + radius - j - 1;
uint32_t c = color;
if (j == (int)j_max) {
c = rgba(_RED(c),_BLU(c),_GRE(c),(int)((double)_ALP(c) * (j_max - (double)j)));
}
GFX(ctx, _x, _y) = alpha_blend_rgba(GFX(ctx, _x, _y), c);
GFX(ctx, _x, _z) = alpha_blend_rgba(GFX(ctx, _x, _z), c);
_x = x + radius - i - 1;
GFX(ctx, _x, _y) = alpha_blend_rgba(GFX(ctx, _x, _y), c);
GFX(ctx, _x, _z) = alpha_blend_rgba(GFX(ctx, _x, _z), c);
}
}
}