/* SPDX-License-Identifier: MIT */ /* * konpicture.c * * Copyright (c) 2026 KonAki * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ #include "konpicture.h" #include #include #include #define FRAME (1.0f / 60.0f) /*** helpers ***/ static kon_picture_t *slot(kon_pictures_t *pictures, int id) { if (!pictures || id < 1 || id > KON_PICTURES) return NULL; return &pictures->slot[id - 1]; } static float clampf(float v, float low, float high) { if (!(v >= low)) return low; if (v > high) return high; return v; } static kon_ease_t easeFor(int easing) { switch (easing) { case 1: return KON_EASE_IN; case 2: return KON_EASE_OUT; case 3: return KON_EASE_INOUT; default: return KON_EASE_LINEAR; } } static void glide(kon_tween_t *tween, float to, int frames, int easing) { if (frames <= 0) kon_tweenSet(tween, to); else kon_tweenStart(tween, kon_tweenValue(tween), to, (float)frames * FRAME, easeFor(easing)); } static void dropToned(kon_picture_t *p) { if (p->toned) kon_freeImage(p->toned); p->toned = NULL; } /*** pictures ***/ void kon_picturesInit(kon_pictures_t *pictures, kon_pictureLoad load, void *user) { memset(pictures, 0, sizeof *pictures); pictures->load = load; pictures->user = user; } void kon_picturesFree(kon_pictures_t *pictures) { int i; if (!pictures) return; for (i = 0; i < KON_PICTURES; i++) dropToned(&pictures->slot[i]); memset(pictures->slot, 0, sizeof pictures->slot); } void kon_pictureShow(kon_pictures_t *pictures, int id, const char *name, int origin, int x, int y, int scaleX, int scaleY, int opacity, int blend) { kon_picture_t *p = slot(pictures, id); size_t n; if (!p) return; dropToned(p); memset(p, 0, sizeof *p); p->shown = 1; if (!name) name = ""; n = strlen(name); if (n >= KON_PICTURE_NAME) n = KON_PICTURE_NAME - 1; memcpy(p->name, name, n); p->name[n] = '\0'; p->image = pictures->load && p->name[0] ? pictures->load(p->name, pictures->user) : NULL; p->origin = origin == 1 ? 1 : 0; p->blend = blend >= 0 && blend <= 3 ? blend : 0; kon_tweenSet(&p->x, (float)x); kon_tweenSet(&p->y, (float)y); kon_tweenSet(&p->scaleX, (float)scaleX); kon_tweenSet(&p->scaleY, (float)scaleY); kon_tweenSet(&p->opacity, clampf((float)opacity, 0.0f, 255.0f)); } void kon_pictureMove(kon_pictures_t *pictures, int id, int origin, int x, int y, int scaleX, int scaleY, int opacity, int blend, int frames, int easing) { kon_picture_t *p = slot(pictures, id); if (!p || !p->shown) return; p->origin = origin == 1 ? 1 : 0; p->blend = blend >= 0 && blend <= 3 ? blend : 0; glide(&p->x, (float)x, frames, easing); glide(&p->y, (float)y, frames, easing); glide(&p->scaleX, (float)scaleX, frames, easing); glide(&p->scaleY, (float)scaleY, frames, easing); glide(&p->opacity, clampf((float)opacity, 0.0f, 255.0f), frames, easing); } void kon_pictureRotate(kon_pictures_t *pictures, int id, int speed) { kon_picture_t *p = slot(pictures, id); if (!p || !p->shown) return; p->turn = (float)speed; } void kon_pictureTint(kon_pictures_t *pictures, int id, int red, int green, int blue, int gray, int frames) { kon_picture_t *p = slot(pictures, id); if (!p || !p->shown) return; glide(&p->tone[0], clampf((float)red, -255.0f, 255.0f), frames, 0); glide(&p->tone[1], clampf((float)green, -255.0f, 255.0f), frames, 0); glide(&p->tone[2], clampf((float)blue, -255.0f, 255.0f), frames, 0); glide(&p->tone[3], clampf((float)gray, 0.0f, 255.0f), frames, 0); } void kon_pictureErase(kon_pictures_t *pictures, int id) { kon_picture_t *p = slot(pictures, id); if (!p) return; dropToned(p); memset(p, 0, sizeof *p); } /*** time ***/ void kon_picturesUpdate(kon_pictures_t *pictures, float dt) { int i, k; if (!pictures || !(dt > 0.0f)) return; if (dt > 0.25f) dt = 0.25f; for (i = 0; i < KON_PICTURES; i++) { kon_picture_t *p = &pictures->slot[i]; if (!p->shown) continue; kon_tweenStep(&p->x, dt); kon_tweenStep(&p->y, dt); kon_tweenStep(&p->scaleX, dt); kon_tweenStep(&p->scaleY, dt); kon_tweenStep(&p->opacity, dt); for (k = 0; k < 4; k++) kon_tweenStep(&p->tone[k], dt); if (p->turn != 0.0f) { p->angle += p->turn * 0.5f * (dt / FRAME) * (3.14159265f / 180.0f); if (p->angle > 6.2831853f || p->angle < -6.2831853f) p->angle = fmodf(p->angle, 6.2831853f); } } } int kon_picturesBusy(const kon_pictures_t *pictures) { int i, k; if (!pictures) return 0; for (i = 0; i < KON_PICTURES; i++) { const kon_picture_t *p = &pictures->slot[i]; if (!p->shown) continue; if (!kon_tweenDone(&p->x) || !kon_tweenDone(&p->y) || !kon_tweenDone(&p->scaleX) || !kon_tweenDone(&p->scaleY) || !kon_tweenDone(&p->opacity)) return 1; for (k = 0; k < 4; k++) { if (!kon_tweenDone(&p->tone[k])) return 1; } } return 0; } /*** drawing ***/ static int channel(int v) { return v < 0 ? 0 : v > 255 ? 255 : v; } /* the copy of the picture with its tone in it, made again only when the tone has changed */ static const kon_image *tonedImage(kon_picture_t *p) { int tone[4], k, i, count; for (k = 0; k < 4; k++) tone[k] = (int)floorf(kon_tweenValue(&p->tone[k]) + 0.5f); if (!tone[0] && !tone[1] && !tone[2] && !tone[3]) { dropToned(p); return p->image; } if (p->toned && !memcmp(tone, p->tonedFor, sizeof tone) && p->toned->width == p->image->width && p->toned->height == p->image->height) return p->toned; if (!p->toned || p->toned->width != p->image->width || p->toned->height != p->image->height) { dropToned(p); p->toned = kon_createFramebuffer(p->image->width, p->image->height); if (!p->toned) return p->image; } count = p->image->width * p->image->height; for (i = 0; i < count; i++) { uint32_t c = p->image->data[i]; int r = (int)((c >> 16) & 255), g = (int)((c >> 8) & 255), b = (int)(c & 255); if (tone[3]) { int gray = (r * 30 + g * 59 + b * 11) / 100; r += (gray - r) * tone[3] / 255; g += (gray - g) * tone[3] / 255; b += (gray - b) * tone[3] / 255; } p->toned->data[i] = (c & 0xFF000000u) | (uint32_t)channel(r + tone[0]) << 16 | (uint32_t)channel(g + tone[1]) << 8 | (uint32_t)channel(b + tone[2]); } memcpy(p->tonedFor, tone, sizeof tone); return p->toned; } void kon_picturesDraw(kon_pictures_t *pictures, kon_framebuffer_t *fb) { int i; if (!pictures || !fb) return; for (i = 0; i < KON_PICTURES; i++) { kon_picture_t *p = &pictures->slot[i]; const kon_image *image; float sx, sy, alpha; kon_rect_t whole; if (!p->shown || !p->image || p->image->width <= 0 || p->image->height <= 0) continue; alpha = clampf(kon_tweenValue(&p->opacity), 0.0f, 255.0f); if (alpha < 0.5f) continue; sx = kon_tweenValue(&p->scaleX) / 100.0f; sy = kon_tweenValue(&p->scaleY) / 100.0f; image = tonedImage(p); whole.x = whole.y = 0; whole.w = image->width; whole.h = image->height; kon_drawImageBlend(fb, image, whole, kon_tweenValue(&p->x), kon_tweenValue(&p->y), p->origin ? (float)image->width * 0.5f : 0.0f, p->origin ? (float)image->height * 0.5f : 0.0f, sx, sy, p->angle, sx == 1.0f && sy == 1.0f && p->angle == 0.0f ? KON_FILTER_NEAREST : KON_FILTER_LINEAR, ((uint32_t)alpha << 24) | 0x00FFFFFFu, (kon_blend_t)p->blend); } }