summaryrefslogtreecommitdiff
path: root/kon
diff options
context:
space:
mode:
Diffstat (limited to 'kon')
-rw-r--r--kon/konfont.h7
-rw-r--r--kon/konsofren.h21
2 files changed, 26 insertions, 2 deletions
diff --git a/kon/konfont.h b/kon/konfont.h
index 1e01b01..aa6f863 100644
--- a/kon/konfont.h
+++ b/kon/konfont.h
@@ -55,6 +55,7 @@ int kon_fontResize(kon_font_t *font, float pixelHeight);
/*** text draw declarations ***/
+/* x, y is the left end of the baseline. color is 0xAARRGGBB, the alpha fades the text. ascii only for now, anything else is skipped */
void kon_drawText(kon_framebuffer_t *fb, kon_font_t *font, int x, int y, const char *text, uint32_t color);
void kon_measureText(kon_font_t *font, const char *text, int *outWidth, int *outHeight);
@@ -183,7 +184,7 @@ static void kon_fontDrawBuiltin_(kon_framebuffer_t *fb, const kon_font_t *font,
int s = font->scale;
int top = y - KON_BUILTIN_BASELINE * s;
- color = (color & 0x00FFFFFFu) | 0xFF000000u;
+ if (color >> 24 == 0) return;
for (const char *p = text; *p; p++) {
unsigned char c = (unsigned char)*p;
@@ -331,6 +332,9 @@ void kon_drawText(kon_framebuffer_t *fb, kon_font_t *font, int x, int y, const c
}
uint32_t baseColor = color & 0x00FFFFFF;
+ uint32_t colorAlpha = color >> 24;
+
+ if (colorAlpha == 0) return;
float cursorX = (float)x;
float cursorY = (float)y;
@@ -364,6 +368,7 @@ void kon_drawText(kon_framebuffer_t *fb, kon_font_t *font, int x, int y, const c
for (int gx = gx0; gx < gx1; gx++) {
uint32_t alpha = in[gx];
+ if (colorAlpha != 0xFF) alpha = (alpha * colorAlpha + 127) / 255;
if (alpha == 0) continue;
if (alpha == 0xFF) {
diff --git a/kon/konsofren.h b/kon/konsofren.h
index 6cca535..ce81774 100644
--- a/kon/konsofren.h
+++ b/kon/konsofren.h
@@ -184,6 +184,10 @@ kon_rect_t kon_frameRect(const kon_image *image, int frameWidth, int frameHeight
angle is in radians, positive turns clockwise on screen. a negative scale flips */
void kon_drawImageEx(kon_framebuffer_t *fb, const kon_image *image, kon_rect_t src, float x, float y, float originX, float originY, float scaleX, float scaleY, float angle, kon_filter_t filter);
+/* the same, with every pixel multiplied by tint (0xAARRGGBB, 0xFFFFFFFF changes nothing). the alpha fades the image out,
+ the colour darkens or colours it */
+void kon_drawImageTint(kon_framebuffer_t *fb, const kon_image *image, kon_rect_t src, float x, float y, float originX, float originY, float scaleX, float scaleY, float angle, kon_filter_t filter, uint32_t tint);
+
/*** draw function declarations ***/
void kon_drawRectangle(kon_framebuffer_t *fb, int x, int y, int width, int height, uint32_t color);
@@ -778,12 +782,26 @@ kon_rect_t kon_frameRect(const kon_image *image, int frameWidth, int frameHeight
return r;
}
+static uint32_t kon_tintColor_(uint32_t color, uint32_t tint) {
+ uint32_t a = ((color >> 24) * (tint >> 24) + 127) / 255;
+ uint32_t r = (((color >> 16) & 0xFF) * ((tint >> 16) & 0xFF) + 127) / 255;
+ uint32_t g = (((color >> 8) & 0xFF) * ((tint >> 8) & 0xFF) + 127) / 255;
+ uint32_t b = ((color & 0xFF) * (tint & 0xFF) + 127) / 255;
+
+ return (a << 24) | (r << 16) | (g << 8) | b;
+}
+
void kon_drawImageEx(kon_framebuffer_t *fb, const kon_image *image, kon_rect_t src, float x, float y, float originX, float originY, float scaleX, float scaleY, float angle, kon_filter_t filter) {
+ kon_drawImageTint(fb, image, src, x, y, originX, originY, scaleX, scaleY, angle, filter, 0xFFFFFFFFu);
+}
+
+void kon_drawImageTint(kon_framebuffer_t *fb, const kon_image *image, kon_rect_t src, float x, float y, float originX, float originY, float scaleX, float scaleY, float angle, kon_filter_t filter, uint32_t tint) {
if (!fb || !image) return;
if (!kon_clipRegion_(image, &src)) return;
if (scaleX == 0.0f || scaleY == 0.0f) return;
- if (angle == 0.0f && scaleX == 1.0f && scaleY == 1.0f && filter == KON_FILTER_NEAREST) {
+ if (tint >> 24 == 0) return;
+ if (angle == 0.0f && scaleX == 1.0f && scaleY == 1.0f && filter == KON_FILTER_NEAREST && tint == 0xFFFFFFFFu) {
kon_drawImageRegion(fb, (int)floorf(x - originX + 0.5f), (int)floorf(y - originY + 0.5f), image, src);
return;
}
@@ -831,6 +849,7 @@ void kon_drawImageEx(kon_framebuffer_t *fb, const kon_image *image, kon_rect_t s
color = image->data[(size_t)(src.y + (int)sy) * (size_t)image->width + (size_t)(src.x + (int)sx)];
}
+ if (tint != 0xFFFFFFFFu) color = kon_tintColor_(color, tint);
uint32_t alpha = color >> 24;
if (alpha == 0xFF) out[px] = color;
else if (alpha != 0) out[px] = kon_blendColor(out[px], color);